# ============================ # 🟢 Install & Imports # ============================ import gradio as gr from transformers import pipeline import torch import gtts print("Torch version:", torch.__version__) # ============================ # 💬 Sentiment Analysis # ============================ # Create sentiment analysis pipeline sentiment_pipe = pipeline("sentiment-analysis") def analyze_sentiment(text): result = sentiment_pipe(text)[0] label = result["label"] score = result["score"] return f"Label: {label}\nConfidence: {score:.2f}" # ============================ # 🤖 Chatbot (DialoGPT) # ============================ # Use Microsoft DialoGPT for more relevant replies chatbot_pipe = pipeline("text-generation", model="microsoft/DialoGPT-medium") def chat_response(user_message): # Provide prompt format to simulate a dialog prompt = f"User: {user_message}\nBot:" response = chatbot_pipe(prompt, max_length=100, do_sample=True, temperature=0.7)[0]["generated_text"] # Clean the output to extract only the bot reply reply = response.split("Bot:")[-1].strip() return reply # ============================ # ✨ Summarization # ============================ # Summarization pipeline summarization_pipe = pipeline("summarization", model="facebook/bart-large-cnn") def summarize_text(text): summary = summarization_pipe(text, max_length=130, min_length=30, do_sample=False)[0]["summary_text"] return summary # ============================ # 🔊 Text-to-Speech # ============================ def text_to_speech(text): tts = gtts.gTTS(text) tts.save("output.mp3") return "output.mp3" # ============================ # 🌐 Gradio App (Multi-Tab) # ============================ with gr.Blocks() as demo: gr.Markdown("# 🌟 Multi-Task Language Application\nChoose a tab below to explore different language AI tasks!") with gr.Tab("Sentiment Analysis"): text_input = gr.Textbox(label="Enter text") output = gr.Textbox(label="Sentiment Result") analyze_btn = gr.Button("Analyze") analyze_btn.click(analyze_sentiment, inputs=text_input, outputs=output) with gr.Tab("Chatbot"): chat_input = gr.Textbox(label="Ask something") chat_output = gr.Textbox(label="Bot Reply") chat_btn = gr.Button("Send") chat_btn.click(chat_response, inputs=chat_input, outputs=chat_output) with gr.Tab("Summarization"): long_text = gr.Textbox(label="Paste text", lines=10, placeholder="Paste a long text here...") summary_output = gr.Textbox(label="Summary") summary_btn = gr.Button("Summarize") summary_btn.click(summarize_text, inputs=long_text, outputs=summary_output) with gr.Tab("Text-to-Speech"): tts_text = gr.Textbox(label="Enter text to convert to speech") audio_output = gr.Audio(label="Generated Speech") tts_btn = gr.Button("Generate Voice") tts_btn.click(text_to_speech, inputs=tts_text, outputs=audio_output) demo.launch()