File size: 5,379 Bytes
e715f76
e4081a2
533c1f5
acfbdf3
e4081a2
 
 
d04a04a
 
 
e4081a2
 
71eb037
2cb8531
acfbdf3
e4081a2
 
e715f76
 
 
e4081a2
 
 
 
 
 
 
533c1f5
 
 
 
 
 
 
 
 
 
 
 
 
e4081a2
 
 
 
 
 
71eb037
 
 
 
acfbdf3
 
431c37b
acfbdf3
 
 
 
2cb8531
acfbdf3
 
 
 
 
 
66b1fe1
acfbdf3
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2cb8531
acfbdf3
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
66b1fe1
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
d04a04a
acfbdf3
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
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("translation", model="Helsinki-NLP/opus-mt-en-fr")
translator_de = pipeline("translation", model="Helsinki-NLP/opus-mt-en-de")
translator_es = pipeline("translation", model="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("text2text-generation", model="valhalla/t5-base-e2e-qg")

# Functional logic
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):
    prompt = "generate questions: " + text
    result = question_generator(prompt, max_length=256, do_sample=False)
    return result[0]["generated_text"]

# UI App
with gr.Blocks(theme=gr.themes.Soft()) as demo:
    gr.Markdown("## 🌐 AI Buddy — Multi-task App l")
    gr.Markdown("Choose a task below:")

    # Navigation buttons
    with gr.Row():
        task = gr.State(value="")
        btn_summ = gr.Button("Text Summarization")
        btn_sent = gr.Button("Sentiment Analysis")
        btn_trans = gr.Button("Translation")
        btn_speech = gr.Button("Speech-to-Text")
        btn_qgen = gr.Button("Question Generation")

    # Tab sections
    with gr.Column(visible=False) as summarize_tab:
        text = gr.Textbox(label="Enter paragraph", lines=10)
        out = gr.Textbox(label="Summary", lines=4)
        gr.Button("Summarize").click(summarize, inputs=text, outputs=out)

    with gr.Column(visible=False) as sentiment_tab:
        sent_in = gr.Textbox(label="Enter sentence", lines=3)
        sent_out = gr.Textbox(label="Sentiment")
        gr.Button("Analyze Sentiment").click(analyze_sentiment, inputs=sent_in, outputs=sent_out)

    with gr.Column(visible=False) as translate_tab:
        tran_in = gr.Textbox(label="Enter English text", lines=3)
        lang = gr.Dropdown(["Tamil", "Hindi", "French", "German", "Spanish"], value="Tamil", label="Language")
        tran_out = gr.Textbox(label="Translated text", lines=3)
        gr.Button("Translate").click(translate, inputs=[tran_in, lang], outputs=tran_out)

    with gr.Column(visible=False) as speech_tab:
        audio = gr.Audio(type="filepath", label="Record or upload")
        speech_out = gr.Textbox(label="Recognized Text")
        gr.Button("Convert Speech to Text").click(transcribe, inputs=audio, outputs=speech_out)

    with gr.Column(visible=False) as question_tab:
        ques_in = gr.Textbox(label="Enter a paragraph", lines=10)
        ques_out = gr.Textbox(label="Generated Questions", lines=10)
        gr.Button("Generate Questions").click(generate_questions, inputs=ques_in, outputs=ques_out)

    # Logic to show/hide tabs
    def show_tab(tab_name):
        return [
            gr.update(visible=(tab_name == "summarize")),
            gr.update(visible=(tab_name == "sentiment")),
            gr.update(visible=(tab_name == "translate")),
            gr.update(visible=(tab_name == "speech")),
            gr.update(visible=(tab_name == "question")),
        ]

    # Shared hidden input to control tab switching
    hidden_tab_name = gr.Textbox(value="", visible=False)

    btn_summ.click(lambda: "summarize", outputs=hidden_tab_name).then(
        show_tab, inputs=hidden_tab_name, outputs=[summarize_tab, sentiment_tab, translate_tab, speech_tab, question_tab]
    )
    btn_sent.click(lambda: "sentiment", outputs=hidden_tab_name).then(
        show_tab, inputs=hidden_tab_name, outputs=[summarize_tab, sentiment_tab, translate_tab, speech_tab, question_tab]
    )
    btn_trans.click(lambda: "translate", outputs=hidden_tab_name).then(
        show_tab, inputs=hidden_tab_name, outputs=[summarize_tab, sentiment_tab, translate_tab, speech_tab, question_tab]
    )
    btn_speech.click(lambda: "speech", outputs=hidden_tab_name).then(
        show_tab, inputs=hidden_tab_name, outputs=[summarize_tab, sentiment_tab, translate_tab, speech_tab, question_tab]
    )
    btn_qgen.click(lambda: "question", outputs=hidden_tab_name).then(
        show_tab, inputs=hidden_tab_name, outputs=[summarize_tab, sentiment_tab, translate_tab, speech_tab, question_tab]
    )

demo.launch()