Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -1,36 +1,31 @@
|
|
1 |
import gradio as gr
|
2 |
-
from transformers import pipeline
|
3 |
-
import torch
|
4 |
-
import warnings
|
5 |
-
warnings.filterwarnings("ignore")
|
6 |
-
|
7 |
-
# Load models
|
8 |
-
summarizer = pipeline("summarization", model="facebook/bart-large-cnn")
|
9 |
-
sentiment_analyzer = pipeline("sentiment-analysis", model="distilbert-base-uncased-finetuned-sst-2-english")
|
10 |
-
translator = pipeline("translation", model="Helsinki-NLP/opus-mt-en-mul")
|
11 |
-
speech_to_text = pipeline("automatic-speech-recognition", model="openai/whisper-small")
|
12 |
-
question_generator_model = pipeline("text2text-generation", model="iarfmoose/t5-base-question-generator")
|
13 |
-
|
14 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
15 |
|
16 |
-
#
|
17 |
|
18 |
-
def
|
19 |
-
|
20 |
-
return summary[0]['summary_text']
|
21 |
|
22 |
def analyze_sentiment(text):
|
23 |
result = sentiment_analyzer(text)[0]
|
24 |
-
|
25 |
-
|
26 |
-
|
27 |
-
|
28 |
-
|
29 |
-
|
30 |
-
|
31 |
-
translator_fr = pipeline("translation", model="Helsinki-NLP/opus-mt-en-fr")
|
32 |
-
translator_de = pipeline("translation", model="Helsinki-NLP/opus-mt-en-de")
|
33 |
-
translator_es = pipeline("translation", model="Helsinki-NLP/opus-mt-en-es")
|
34 |
|
35 |
def translate(text, lang):
|
36 |
if lang == "Tamil":
|
@@ -44,54 +39,85 @@ def translate(text, lang):
|
|
44 |
elif lang == "Spanish":
|
45 |
return translator_es(text)[0]["translation_text"]
|
46 |
else:
|
47 |
-
return "Unsupported
|
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 |
-
|
73 |
-
|
74 |
-
|
75 |
-
|
76 |
-
|
77 |
-
|
78 |
-
|
79 |
-
|
80 |
-
|
81 |
-
|
82 |
-
|
83 |
-
|
84 |
-
with gr.
|
85 |
-
|
86 |
-
|
87 |
-
|
88 |
-
|
89 |
-
|
90 |
-
|
91 |
-
|
92 |
-
|
93 |
-
|
94 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
95 |
|
96 |
-
# Launch app
|
97 |
demo.launch()
|
|
|
1 |
import gradio as gr
|
2 |
+
from transformers import pipeline
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
3 |
|
4 |
+
# Preload models
|
5 |
+
summarizer = pipeline("summarization")
|
6 |
+
sentiment_analyzer = pipeline("sentiment-analysis", model="cardiffnlp/twitter-roberta-base-sentiment")
|
7 |
+
translator_hi = pipeline("translation", model="Helsinki-NLP/opus-mt-en-hi")
|
8 |
+
translator_fr = pipeline("Helsinki-NLP/opus-mt-en-fr")
|
9 |
+
translator_de = pipeline("Helsinki-NLP/opus-mt-en-de")
|
10 |
+
translator_es = pipeline("Helsinki-NLP/opus-mt-en-es")
|
11 |
+
translator_ta = pipeline("translation", model="facebook/nllb-200-distilled-600M", src_lang="eng_Latn", tgt_lang="tam_Taml")
|
12 |
+
speech_to_text = pipeline("automatic-speech-recognition", model="openai/whisper-small")
|
13 |
+
question_generator = pipeline("e2e-qg")
|
14 |
|
15 |
+
# --- Functional Modules ---
|
16 |
|
17 |
+
def summarize(text):
|
18 |
+
return summarizer(text, max_length=60, min_length=20, do_sample=False)[0]['summary_text']
|
|
|
19 |
|
20 |
def analyze_sentiment(text):
|
21 |
result = sentiment_analyzer(text)[0]
|
22 |
+
label = result["label"]
|
23 |
+
if label == "LABEL_1":
|
24 |
+
return "Neutral"
|
25 |
+
elif label == "LABEL_2":
|
26 |
+
return "Positive"
|
27 |
+
else:
|
28 |
+
return "Negative"
|
|
|
|
|
|
|
29 |
|
30 |
def translate(text, lang):
|
31 |
if lang == "Tamil":
|
|
|
39 |
elif lang == "Spanish":
|
40 |
return translator_es(text)[0]["translation_text"]
|
41 |
else:
|
42 |
+
return "Unsupported Language"
|
43 |
+
|
44 |
+
def transcribe(audio):
|
45 |
+
return speech_to_text(audio)["text"]
|
46 |
+
|
47 |
+
def generate_questions(text):
|
48 |
+
output = question_generator(text)
|
49 |
+
return "\n".join(f"- {item['question']}" for item in output[:10])
|
50 |
+
|
51 |
+
# --- UI Sections for each task ---
|
52 |
+
|
53 |
+
def summarization_ui():
|
54 |
+
with gr.Column():
|
55 |
+
input_text = gr.Textbox(label="Enter a long paragraph", lines=8, placeholder="Paste your paragraph here...")
|
56 |
+
output_text = gr.Textbox(label="Summarized text", lines=4)
|
57 |
+
gr.Button("Summarize").click(summarize, inputs=input_text, outputs=output_text)
|
58 |
+
|
59 |
+
def sentiment_ui():
|
60 |
+
with gr.Column():
|
61 |
+
input_text = gr.Textbox(label="Enter a sentence", lines=3)
|
62 |
+
output_text = gr.Textbox(label="Sentiment")
|
63 |
+
gr.Button("Analyze Sentiment").click(analyze_sentiment, inputs=input_text, outputs=output_text)
|
64 |
+
|
65 |
+
def translation_ui():
|
66 |
+
with gr.Column():
|
67 |
+
input_text = gr.Textbox(label="Enter English text", lines=3)
|
68 |
+
lang_dropdown = gr.Dropdown(["Tamil", "Hindi", "French", "German", "Spanish"], value="Tamil", label="Target Language")
|
69 |
+
output_text = gr.Textbox(label="Translated text", lines=3)
|
70 |
+
gr.Button("Translate").click(translate, inputs=[input_text, lang_dropdown], outputs=output_text)
|
71 |
+
|
72 |
+
def speech_ui():
|
73 |
+
with gr.Column():
|
74 |
+
audio = gr.Audio(source="microphone", type="filepath", label="Record or Upload")
|
75 |
+
output_text = gr.Textbox(label="Recognized Text")
|
76 |
+
gr.Button("Convert Speech to Text").click(transcribe, inputs=audio, outputs=output_text)
|
77 |
+
|
78 |
+
def question_ui():
|
79 |
+
with gr.Column():
|
80 |
+
input_text = gr.Textbox(label="Enter a paragraph", lines=8)
|
81 |
+
output_text = gr.Textbox(label="Generated Questions", lines=10)
|
82 |
+
gr.Button("Generate Questions").click(generate_questions, inputs=input_text, outputs=output_text)
|
83 |
+
|
84 |
+
# --- Homepage Navigation ---
|
85 |
+
|
86 |
+
with gr.Blocks(theme=gr.themes.Soft()) as demo:
|
87 |
+
gr.Markdown("<h1 style='text-align:center;'>🌐 Multi-Task AI App (Hugging Face Space)</h1>")
|
88 |
+
gr.Markdown("<h4 style='text-align:center;'>By Joel — Powered by Transformers</h4>")
|
89 |
+
|
90 |
+
with gr.Row():
|
91 |
+
btn1 = gr.Button("Text Summarization")
|
92 |
+
btn2 = gr.Button("Sentiment Analysis")
|
93 |
+
btn3 = gr.Button("Translation")
|
94 |
+
btn4 = gr.Button("Speech-to-Text")
|
95 |
+
btn5 = gr.Button("Question Generation")
|
96 |
+
|
97 |
+
main_content = gr.Column()
|
98 |
+
|
99 |
+
with main_content:
|
100 |
+
output_area = gr.Column(visible=False)
|
101 |
+
|
102 |
+
def load_tab(tab_name):
|
103 |
+
with output_area:
|
104 |
+
output_area.children.clear()
|
105 |
+
if tab_name == "summarization":
|
106 |
+
summarization_ui()
|
107 |
+
elif tab_name == "sentiment":
|
108 |
+
sentiment_ui()
|
109 |
+
elif tab_name == "translation":
|
110 |
+
translation_ui()
|
111 |
+
elif tab_name == "speech":
|
112 |
+
speech_ui()
|
113 |
+
elif tab_name == "question":
|
114 |
+
question_ui()
|
115 |
+
output_area.visible = True
|
116 |
+
|
117 |
+
btn1.click(fn=load_tab, inputs=[], outputs=[], _js="() => 'summarization'")
|
118 |
+
btn2.click(fn=load_tab, inputs=[], outputs=[], _js="() => 'sentiment'")
|
119 |
+
btn3.click(fn=load_tab, inputs=[], outputs=[], _js="() => 'translation'")
|
120 |
+
btn4.click(fn=load_tab, inputs=[], outputs=[], _js="() => 'speech'")
|
121 |
+
btn5.click(fn=load_tab, inputs=[], outputs=[], _js="() => 'question'")
|
122 |
|
|
|
123 |
demo.launch()
|