File size: 4,926 Bytes
e715f76
e4081a2
533c1f5
e4081a2
 
 
 
 
 
 
 
 
 
e715f76
e4081a2
e715f76
e4081a2
 
e715f76
 
 
e4081a2
 
 
 
 
 
 
533c1f5
 
 
 
 
 
 
 
 
 
 
 
 
e4081a2
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
e715f76
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
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
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
114
115
116
117
118
119
120
121
122
123
124
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()