Spaces:
Runtime error
Runtime error
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,137 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
# prompt: conbine all the models above in one gradio interface , and well customized
|
2 |
+
|
3 |
+
import gradio as gr
|
4 |
+
from transformers import pipeline
|
5 |
+
import csv
|
6 |
+
import heapq as hq
|
7 |
+
from google.colab import files
|
8 |
+
|
9 |
+
# Install necessary libraries
|
10 |
+
!pip install transformers gradio librosa
|
11 |
+
|
12 |
+
|
13 |
+
# Sentiment Analysis
|
14 |
+
classifier_sentiment = pipeline("sentiment-analysis", model="distilbert-base-uncased-finetuned-sst-2-english")
|
15 |
+
def analyze_sentiment(text):
|
16 |
+
result = classifier_sentiment(text)[0]
|
17 |
+
label = result['label']
|
18 |
+
score = result['score']
|
19 |
+
return f"Label: {label},"
|
20 |
+
|
21 |
+
# Translation
|
22 |
+
translator = pipeline("translation", model="Helsinki-NLP/opus-mt-en-fr")
|
23 |
+
def translate_text(text):
|
24 |
+
result = translator(text)[0]
|
25 |
+
translated_text = result["translation_text"]
|
26 |
+
return translated_text
|
27 |
+
|
28 |
+
# Image Classification
|
29 |
+
classifier_image = pipeline("image-classification", model="google/mobilenet_v2_1.0_224")
|
30 |
+
def classify_image(image):
|
31 |
+
results = classifier_image(image)
|
32 |
+
output = ""
|
33 |
+
for result in results:
|
34 |
+
output += f"{result['label']}: {result['score']:.2f}\n"
|
35 |
+
return output
|
36 |
+
|
37 |
+
# Speech to Text
|
38 |
+
speech_to_text = pipeline("automatic-speech-recognition", model="facebook/wav2vec2-base-960h")
|
39 |
+
def transcribe_audio(audio):
|
40 |
+
text = speech_to_text(audio)["text"]
|
41 |
+
return text
|
42 |
+
|
43 |
+
# Text Summarization
|
44 |
+
summarizer = pipeline("summarization", model="facebook/bart-large-cnn")
|
45 |
+
def summarize_text(text):
|
46 |
+
summary = summarizer(text, max_length=130, min_length=30, do_sample=False)[0]["summary_text"]
|
47 |
+
return summary
|
48 |
+
|
49 |
+
# Woodall Number
|
50 |
+
def is_woodall(x):
|
51 |
+
if (x % 2 == 0):
|
52 |
+
return False
|
53 |
+
if (x == 1):
|
54 |
+
return True
|
55 |
+
x = x + 1
|
56 |
+
p = 0
|
57 |
+
while (x % 2 == 0):
|
58 |
+
x = x/2
|
59 |
+
p = p + 1
|
60 |
+
if (p == x):
|
61 |
+
return True
|
62 |
+
return False
|
63 |
+
|
64 |
+
|
65 |
+
# Largest Numbers from a List (Heap Queue)
|
66 |
+
def heap_queue_largest(nums,n):
|
67 |
+
try:
|
68 |
+
largest_nums = hq.nlargest(n, nums)
|
69 |
+
return largest_nums
|
70 |
+
except Exception as e:
|
71 |
+
return f"Error: {e}"
|
72 |
+
|
73 |
+
|
74 |
+
# CSV Reader
|
75 |
+
def csv_reader(filepath):
|
76 |
+
try:
|
77 |
+
with open(filepath, 'r') as f:
|
78 |
+
reader = csv.reader(f)
|
79 |
+
data = [row for row in reader]
|
80 |
+
return data
|
81 |
+
except Exception as e:
|
82 |
+
return f"Error: {e}"
|
83 |
+
|
84 |
+
|
85 |
+
with gr.Blocks() as demo:
|
86 |
+
gr.Markdown("<h1>Multi-functional AI Demo</h1>")
|
87 |
+
|
88 |
+
with gr.Tab("Sentiment Analysis"):
|
89 |
+
text_input = gr.Textbox(placeholder="Enter text here...")
|
90 |
+
text_output = gr.Textbox()
|
91 |
+
sentiment_button = gr.Button("Analyze")
|
92 |
+
sentiment_button.click(analyze_sentiment, inputs=text_input, outputs=text_output)
|
93 |
+
|
94 |
+
with gr.Tab("Translation"):
|
95 |
+
text_input_trans = gr.Textbox(placeholder="Enter English text here...")
|
96 |
+
text_output_trans = gr.Textbox()
|
97 |
+
trans_button = gr.Button("Translate")
|
98 |
+
trans_button.click(translate_text, inputs=text_input_trans, outputs=text_output_trans)
|
99 |
+
|
100 |
+
with gr.Tab("Image Classification"):
|
101 |
+
image_input = gr.Image(type="pil")
|
102 |
+
image_output = gr.Textbox()
|
103 |
+
image_button = gr.Button("Classify")
|
104 |
+
image_button.click(classify_image, inputs=image_input, outputs=image_output)
|
105 |
+
|
106 |
+
with gr.Tab("Speech to Text"):
|
107 |
+
audio_input = gr.Audio(sources=["microphone"], type="filepath")
|
108 |
+
audio_output = gr.Textbox()
|
109 |
+
audio_button = gr.Button("Transcribe")
|
110 |
+
audio_button.click(transcribe_audio, inputs=audio_input, outputs=audio_output)
|
111 |
+
|
112 |
+
with gr.Tab("Text Summarization"):
|
113 |
+
text_input_summ = gr.Textbox(placeholder="Enter text here...")
|
114 |
+
text_output_summ = gr.Textbox()
|
115 |
+
summ_button = gr.Button("Summarize")
|
116 |
+
summ_button.click(summarize_text, inputs=text_input_summ, outputs=text_output_summ)
|
117 |
+
|
118 |
+
with gr.Tab("Woodall Number"):
|
119 |
+
woodall_input = gr.Number(label="Enter a number:")
|
120 |
+
woodall_output = gr.Textbox()
|
121 |
+
woodall_button = gr.Button("Check")
|
122 |
+
woodall_button.click(is_woodall, inputs=woodall_input, outputs=woodall_output)
|
123 |
+
|
124 |
+
with gr.Tab("Largest Numbers (Heap Queue)"):
|
125 |
+
nums_input = gr.Textbox(label="Enter numbers separated by spaces:")
|
126 |
+
n_input = gr.Number(label="Number of largest elements:")
|
127 |
+
nums_output = gr.Textbox()
|
128 |
+
nums_button = gr.Button("Find Largest")
|
129 |
+
nums_button.click(heap_queue_largest, inputs=[nums_input, n_input], outputs=nums_output)
|
130 |
+
|
131 |
+
with gr.Tab("CSV Reader"):
|
132 |
+
csv_input = gr.File(label="Upload CSV File")
|
133 |
+
csv_output = gr.Textbox()
|
134 |
+
csv_button = gr.Button("Read CSV")
|
135 |
+
csv_button.click(csv_reader, inputs=csv_input, outputs=csv_output)
|
136 |
+
|
137 |
+
demo.launch()
|