Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -1,7 +1,7 @@
|
|
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")
|
@@ -10,10 +10,9 @@ translator_de = pipeline("translation", model="Helsinki-NLP/opus-mt-en-de")
|
|
10 |
translator_es = pipeline("translation", model="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("
|
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 |
|
@@ -45,87 +44,70 @@ def transcribe(audio):
|
|
45 |
return speech_to_text(audio)["text"]
|
46 |
|
47 |
def generate_questions(text):
|
48 |
-
output = question_generator(
|
49 |
-
return
|
50 |
-
|
51 |
-
#
|
52 |
-
|
53 |
-
|
54 |
-
|
55 |
-
|
56 |
-
|
57 |
-
|
58 |
-
|
59 |
-
|
60 |
-
|
61 |
-
|
62 |
-
|
63 |
-
|
64 |
-
|
65 |
-
|
66 |
-
|
67 |
-
|
68 |
-
with gr.Column():
|
69 |
-
|
70 |
-
|
71 |
-
|
72 |
-
|
73 |
-
|
74 |
-
|
75 |
-
|
76 |
-
|
77 |
-
|
78 |
-
|
79 |
-
|
80 |
-
|
81 |
-
|
82 |
-
|
83 |
-
|
84 |
-
|
85 |
-
|
86 |
-
|
87 |
-
|
88 |
-
|
89 |
-
|
90 |
-
|
91 |
-
|
92 |
-
|
93 |
-
|
94 |
-
|
95 |
-
|
96 |
-
""
|
97 |
-
|
98 |
-
|
99 |
-
|
100 |
-
|
101 |
-
|
102 |
-
|
103 |
-
|
104 |
-
|
105 |
-
|
106 |
-
|
107 |
-
|
108 |
-
|
109 |
-
|
110 |
-
|
111 |
-
|
112 |
-
|
113 |
-
sentiment_ui()
|
114 |
-
elif tab_name == "translation":
|
115 |
-
translation_ui()
|
116 |
-
elif tab_name == "speech":
|
117 |
-
speech_ui()
|
118 |
-
elif tab_name == "question":
|
119 |
-
question_ui()
|
120 |
-
|
121 |
-
btn1.click(lambda: update_view("summarization"), outputs=[])
|
122 |
-
btn2.click(lambda: update_view("sentiment"), outputs=[])
|
123 |
-
btn3.click(lambda: update_view("translation"), outputs=[])
|
124 |
-
btn4.click(lambda: update_view("speech"), outputs=[])
|
125 |
-
btn5.click(lambda: update_view("question"), outputs=[])
|
126 |
-
|
127 |
-
return demo
|
128 |
-
|
129 |
-
demo = build_interface()
|
130 |
-
demo.launch()
|
131 |
|
|
|
|
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")
|
|
|
10 |
translator_es = pipeline("translation", model="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 logic
|
16 |
def summarize(text):
|
17 |
return summarizer(text, max_length=60, min_length=20, do_sample=False)[0]['summary_text']
|
18 |
|
|
|
44 |
return speech_to_text(audio)["text"]
|
45 |
|
46 |
def generate_questions(text):
|
47 |
+
output = question_generator(text)
|
48 |
+
return "\n".join(f"- {item['question']}" for item in output[:10])
|
49 |
+
|
50 |
+
# UI App
|
51 |
+
with gr.Blocks(theme=gr.themes.Soft()) as demo:
|
52 |
+
gr.Markdown("## 🌐 AI Buddy — Multi-task App by Joel")
|
53 |
+
gr.Markdown("Choose a task below:")
|
54 |
+
|
55 |
+
# Navigation buttons
|
56 |
+
with gr.Row():
|
57 |
+
task = gr.State(value="") # to keep track of current screen
|
58 |
+
btn_summ = gr.Button("Text Summarization")
|
59 |
+
btn_sent = gr.Button("Sentiment Analysis")
|
60 |
+
btn_trans = gr.Button("Translation")
|
61 |
+
btn_speech = gr.Button("Speech-to-Text")
|
62 |
+
btn_qgen = gr.Button("Question Generation")
|
63 |
+
|
64 |
+
output_container = gr.Column(visible=False)
|
65 |
+
|
66 |
+
# Summarization
|
67 |
+
with gr.Column(visible=False) as summarize_tab:
|
68 |
+
text = gr.Textbox(label="Enter paragraph", lines=10)
|
69 |
+
out = gr.Textbox(label="Summary", lines=4)
|
70 |
+
gr.Button("Summarize").click(summarize, inputs=text, outputs=out)
|
71 |
+
|
72 |
+
# Sentiment
|
73 |
+
with gr.Column(visible=False) as sentiment_tab:
|
74 |
+
sent_in = gr.Textbox(label="Enter sentence", lines=3)
|
75 |
+
sent_out = gr.Textbox(label="Sentiment")
|
76 |
+
gr.Button("Analyze Sentiment").click(analyze_sentiment, inputs=sent_in, outputs=sent_out)
|
77 |
+
|
78 |
+
# Translation
|
79 |
+
with gr.Column(visible=False) as translate_tab:
|
80 |
+
tran_in = gr.Textbox(label="Enter English text", lines=3)
|
81 |
+
lang = gr.Dropdown(["Tamil", "Hindi", "French", "German", "Spanish"], value="Tamil", label="Language")
|
82 |
+
tran_out = gr.Textbox(label="Translated text", lines=3)
|
83 |
+
gr.Button("Translate").click(translate, inputs=[tran_in, lang], outputs=tran_out)
|
84 |
+
|
85 |
+
# Speech-to-Text
|
86 |
+
with gr.Column(visible=False) as speech_tab:
|
87 |
+
audio = gr.Audio(source="microphone", type="filepath", label="Record or upload")
|
88 |
+
speech_out = gr.Textbox(label="Recognized Text")
|
89 |
+
gr.Button("Convert Speech to Text").click(transcribe, inputs=audio, outputs=speech_out)
|
90 |
+
|
91 |
+
# Question Generation
|
92 |
+
with gr.Column(visible=False) as question_tab:
|
93 |
+
ques_in = gr.Textbox(label="Enter a paragraph", lines=10)
|
94 |
+
ques_out = gr.Textbox(label="Generated Questions", lines=10)
|
95 |
+
gr.Button("Generate Questions").click(generate_questions, inputs=ques_in, outputs=ques_out)
|
96 |
+
|
97 |
+
# Logic to show/hide tabs
|
98 |
+
def show_tab(tab_name):
|
99 |
+
return [
|
100 |
+
gr.update(visible=(tab_name == "summarize")),
|
101 |
+
gr.update(visible=(tab_name == "sentiment")),
|
102 |
+
gr.update(visible=(tab_name == "translate")),
|
103 |
+
gr.update(visible=(tab_name == "speech")),
|
104 |
+
gr.update(visible=(tab_name == "question")),
|
105 |
+
]
|
106 |
+
|
107 |
+
btn_summ.click(show_tab, outputs=[summarize_tab, sentiment_tab, translate_tab, speech_tab, question_tab], inputs=[], _js="() => 'summarize'")
|
108 |
+
btn_sent.click(show_tab, outputs=[summarize_tab, sentiment_tab, translate_tab, speech_tab, question_tab], inputs=[], _js="() => 'sentiment'")
|
109 |
+
btn_trans.click(show_tab, outputs=[summarize_tab, sentiment_tab, translate_tab, speech_tab, question_tab], inputs=[], _js="() => 'translate'")
|
110 |
+
btn_speech.click(show_tab, outputs=[summarize_tab, sentiment_tab, translate_tab, speech_tab, question_tab], inputs=[], _js="() => 'speech'")
|
111 |
+
btn_qgen.click(show_tab, outputs=[summarize_tab, sentiment_tab, translate_tab, speech_tab, question_tab], inputs=[], _js="() => 'question'")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
112 |
|
113 |
+
demo.launch()
|