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