JoelJebaraj93 commited on
Commit
e4081a2
·
verified ·
1 Parent(s): 42db93c

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +101 -75
app.py CHANGED
@@ -1,36 +1,31 @@
1
  import gradio as gr
2
- from transformers import pipeline, AutoTokenizer, AutoModelForSeq2SeqLM
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
- # Functions
17
 
18
- def summarize_text(text):
19
- summary = summarizer(text, max_length=130, min_length=30, do_sample=False)
20
- return summary[0]['summary_text']
21
 
22
  def analyze_sentiment(text):
23
  result = sentiment_analyzer(text)[0]
24
- return f"{result['label']} (score: {result['score']:.2f})"
25
-
26
- from transformers import pipeline
27
-
28
- # Updated Translation Pipelines
29
- translator_ta = pipeline("translation", model="facebook/nllb-200-distilled-600M", src_lang="eng_Latn", tgt_lang="tam_Taml")
30
- translator_hi = pipeline("translation", model="Helsinki-NLP/opus-mt-en-hi")
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 language."
48
-
49
-
50
-
51
- def transcribe_audio(audio):
52
- result = speech_to_text(audio)
53
- return result['text']
54
-
55
- def generate_questions(paragraph):
56
- qg_input = "generate questions: " + paragraph
57
- questions = question_generator_model(qg_input, max_length=256, do_sample=False)[0]['generated_text']
58
- return questions
59
-
60
- # Gradio UI
61
- with gr.Blocks() as demo:
62
- gr.Markdown("# 🌐 Multi-Task AI App (Hugging Face Space)\n**By Joel** — Powered by Transformers")
63
-
64
- with gr.Tab("Text Summarization"):
65
- input_text = gr.Textbox(label="Enter a long paragraph")
66
- summary_output = gr.Textbox(label="Summarized text")
67
- summarize_btn = gr.Button("Summarize")
68
- summarize_btn.click(summarize_text, input_text, summary_output)
69
-
70
- with gr.Tab("Sentiment Analysis"):
71
- sentiment_input = gr.Textbox(label="Enter a sentence")
72
- sentiment_output = gr.Textbox(label="Sentiment Result")
73
- sentiment_btn = gr.Button("Analyze Sentiment")
74
- sentiment_btn.click(analyze_sentiment, sentiment_input, sentiment_output)
75
-
76
- with gr.Tab("Translation"):
77
- translation_input = gr.Textbox(label="Enter English text")
78
- target_lang = gr.Dropdown(choices=["Tamil", "Hindi", "French", "German", "Spanish"], label="Choose target language", value="Tamil")
79
- translation_output = gr.Textbox(label="Translated text")
80
- translate_btn = gr.Button("Translate")
81
- translate_btn.click(fn=translate, inputs=[translation_input, target_lang], outputs=translation_output)
82
-
83
-
84
- with gr.Tab("Speech-to-Text"):
85
- audio_input = gr.Audio(label="Upload or record audio", type="filepath")
86
- transcript_output = gr.Textbox(label="Transcribed Text")
87
- transcribe_btn = gr.Button("Transcribe")
88
- transcribe_btn.click(transcribe_audio, audio_input, transcript_output)
89
-
90
- with gr.Tab("Question Generation"):
91
- paragraph_input = gr.Textbox(label="Enter a paragraph to generate questions", lines=5)
92
- question_output = gr.Textbox(label="Generated Questions")
93
- question_btn = gr.Button("Generate Questions")
94
- question_btn.click(generate_questions, paragraph_input, question_output)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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()