Spaces:
Sleeping
Sleeping
import gradio as gr | |
from transformers import pipeline | |
# Preload models | |
summarizer = pipeline("summarization") | |
sentiment_analyzer = pipeline("sentiment-analysis", model="cardiffnlp/twitter-roberta-base-sentiment") | |
translator_hi = pipeline("translation", model="Helsinki-NLP/opus-mt-en-hi") | |
translator_fr = pipeline("Helsinki-NLP/opus-mt-en-fr") | |
translator_de = pipeline("Helsinki-NLP/opus-mt-en-de") | |
translator_es = pipeline("Helsinki-NLP/opus-mt-en-es") | |
translator_ta = pipeline("translation", model="facebook/nllb-200-distilled-600M", src_lang="eng_Latn", tgt_lang="tam_Taml") | |
speech_to_text = pipeline("automatic-speech-recognition", model="openai/whisper-small") | |
question_generator = pipeline("e2e-qg") | |
# --- Functional Modules --- | |
def summarize(text): | |
return summarizer(text, max_length=60, min_length=20, do_sample=False)[0]['summary_text'] | |
def analyze_sentiment(text): | |
result = sentiment_analyzer(text)[0] | |
label = result["label"] | |
if label == "LABEL_1": | |
return "Neutral" | |
elif label == "LABEL_2": | |
return "Positive" | |
else: | |
return "Negative" | |
def translate(text, lang): | |
if lang == "Tamil": | |
return translator_ta(text)[0]["translation_text"] | |
elif lang == "Hindi": | |
return translator_hi(text)[0]["translation_text"] | |
elif lang == "French": | |
return translator_fr(text)[0]["translation_text"] | |
elif lang == "German": | |
return translator_de(text)[0]["translation_text"] | |
elif lang == "Spanish": | |
return translator_es(text)[0]["translation_text"] | |
else: | |
return "Unsupported Language" | |
def transcribe(audio): | |
return speech_to_text(audio)["text"] | |
def generate_questions(text): | |
output = question_generator(text) | |
return "\n".join(f"- {item['question']}" for item in output[:10]) | |
# --- UI Sections for each task --- | |
def summarization_ui(): | |
with gr.Column(): | |
input_text = gr.Textbox(label="Enter a long paragraph", lines=8, placeholder="Paste your paragraph here...") | |
output_text = gr.Textbox(label="Summarized text", lines=4) | |
gr.Button("Summarize").click(summarize, inputs=input_text, outputs=output_text) | |
def sentiment_ui(): | |
with gr.Column(): | |
input_text = gr.Textbox(label="Enter a sentence", lines=3) | |
output_text = gr.Textbox(label="Sentiment") | |
gr.Button("Analyze Sentiment").click(analyze_sentiment, inputs=input_text, outputs=output_text) | |
def translation_ui(): | |
with gr.Column(): | |
input_text = gr.Textbox(label="Enter English text", lines=3) | |
lang_dropdown = gr.Dropdown(["Tamil", "Hindi", "French", "German", "Spanish"], value="Tamil", label="Target Language") | |
output_text = gr.Textbox(label="Translated text", lines=3) | |
gr.Button("Translate").click(translate, inputs=[input_text, lang_dropdown], outputs=output_text) | |
def speech_ui(): | |
with gr.Column(): | |
audio = gr.Audio(source="microphone", type="filepath", label="Record or Upload") | |
output_text = gr.Textbox(label="Recognized Text") | |
gr.Button("Convert Speech to Text").click(transcribe, inputs=audio, outputs=output_text) | |
def question_ui(): | |
with gr.Column(): | |
input_text = gr.Textbox(label="Enter a paragraph", lines=8) | |
output_text = gr.Textbox(label="Generated Questions", lines=10) | |
gr.Button("Generate Questions").click(generate_questions, inputs=input_text, outputs=output_text) | |
# --- Homepage Navigation --- | |
with gr.Blocks(theme=gr.themes.Soft()) as demo: | |
gr.Markdown("<h1 style='text-align:center;'>🌐 Multi-Task AI App (Hugging Face Space)</h1>") | |
gr.Markdown("<h4 style='text-align:center;'>By Joel — Powered by Transformers</h4>") | |
with gr.Row(): | |
btn1 = gr.Button("Text Summarization") | |
btn2 = gr.Button("Sentiment Analysis") | |
btn3 = gr.Button("Translation") | |
btn4 = gr.Button("Speech-to-Text") | |
btn5 = gr.Button("Question Generation") | |
main_content = gr.Column() | |
with main_content: | |
output_area = gr.Column(visible=False) | |
def load_tab(tab_name): | |
with output_area: | |
output_area.children.clear() | |
if tab_name == "summarization": | |
summarization_ui() | |
elif tab_name == "sentiment": | |
sentiment_ui() | |
elif tab_name == "translation": | |
translation_ui() | |
elif tab_name == "speech": | |
speech_ui() | |
elif tab_name == "question": | |
question_ui() | |
output_area.visible = True | |
btn1.click(fn=load_tab, inputs=[], outputs=[], _js="() => 'summarization'") | |
btn2.click(fn=load_tab, inputs=[], outputs=[], _js="() => 'sentiment'") | |
btn3.click(fn=load_tab, inputs=[], outputs=[], _js="() => 'translation'") | |
btn4.click(fn=load_tab, inputs=[], outputs=[], _js="() => 'speech'") | |
btn5.click(fn=load_tab, inputs=[], outputs=[], _js="() => 'question'") | |
demo.launch() | |