JoelJebaraj93 commited on
Commit
acfbdf3
·
verified ·
1 Parent(s): 8e747f4

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +69 -87
app.py CHANGED
@@ -1,7 +1,7 @@
1
  import gradio as gr
2
  from transformers import pipeline
3
 
4
- # Preload models (optimized and corrected)
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("text2text-generation", model="valhalla/t5-base-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
 
@@ -45,87 +44,70 @@ def transcribe(audio):
45
  return speech_to_text(audio)["text"]
46
 
47
  def generate_questions(text):
48
- output = question_generator("generate questions: " + text)
49
- return output[0]['generated_text']
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=12, placeholder="Paste your paragraph here...")
56
- output_text = gr.Textbox(label="Summarized text", lines=4)
57
- summarize_btn = gr.Button("Summarize")
58
- summarize_btn.click(summarize, inputs=input_text, outputs=output_text)
59
-
60
- def sentiment_ui():
61
- with gr.Column():
62
- input_text = gr.Textbox(label="Enter a sentence", lines=3)
63
- output_text = gr.Textbox(label="Sentiment")
64
- analyze_btn = gr.Button("Analyze Sentiment")
65
- analyze_btn.click(analyze_sentiment, inputs=input_text, outputs=output_text)
66
-
67
- def translation_ui():
68
- with gr.Column():
69
- input_text = gr.Textbox(label="Enter English text", lines=4)
70
- lang_dropdown = gr.Dropdown(["Tamil", "Hindi", "French", "German", "Spanish"], value="Tamil", label="Target Language")
71
- output_text = gr.Textbox(label="Translated text", lines=4)
72
- translate_btn = gr.Button("Translate")
73
- translate_btn.click(translate, inputs=[input_text, lang_dropdown], outputs=output_text)
74
-
75
- def speech_ui():
76
- with gr.Column():
77
- audio = gr.Audio(source="microphone", type="filepath", label="Record or Upload")
78
- output_text = gr.Textbox(label="Recognized Text")
79
- speech_btn = gr.Button("Convert Speech to Text")
80
- speech_btn.click(transcribe, inputs=audio, outputs=output_text)
81
-
82
- def question_ui():
83
- with gr.Column():
84
- input_text = gr.Textbox(label="Enter a paragraph", lines=10)
85
- output_text = gr.Textbox(label="Generated Questions", lines=10)
86
- question_btn = gr.Button("Generate Questions")
87
- question_btn.click(generate_questions, inputs=input_text, outputs=output_text)
88
-
89
- # --- App Layout ---
90
-
91
- def build_interface():
92
- with gr.Blocks(theme=gr.themes.Soft()) as demo:
93
- gr.Markdown("""
94
- <h1 style='text-align:center;'>🌐 Multi-Task AI App (Hugging Face Space)</h1>
95
- <h4 style='text-align:center;'>By Joel Powered by Transformers</h4>
96
- """)
97
-
98
- with gr.Row():
99
- btn1 = gr.Button("Text Summarization")
100
- btn2 = gr.Button("Sentiment Analysis")
101
- btn3 = gr.Button("Translation")
102
- btn4 = gr.Button("Speech-to-Text")
103
- btn5 = gr.Button("Question Generation")
104
-
105
- container = gr.Column()
106
-
107
- def update_view(tab_name):
108
- container.children.clear()
109
- with container:
110
- if tab_name == "summarization":
111
- summarization_ui()
112
- elif tab_name == "sentiment":
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()