Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,91 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# ============================
|
| 2 |
+
# π’ Install & Imports
|
| 3 |
+
# ============================
|
| 4 |
+
|
| 5 |
+
import gradio as gr
|
| 6 |
+
from transformers import pipeline
|
| 7 |
+
import torch
|
| 8 |
+
import gtts
|
| 9 |
+
|
| 10 |
+
print("Torch version:", torch.__version__)
|
| 11 |
+
|
| 12 |
+
# ============================
|
| 13 |
+
# π¬ Sentiment Analysis
|
| 14 |
+
# ============================
|
| 15 |
+
|
| 16 |
+
# Create sentiment analysis pipeline
|
| 17 |
+
sentiment_pipe = pipeline("sentiment-analysis")
|
| 18 |
+
|
| 19 |
+
def analyze_sentiment(text):
|
| 20 |
+
result = sentiment_pipe(text)[0]
|
| 21 |
+
label = result["label"]
|
| 22 |
+
score = result["score"]
|
| 23 |
+
return f"Label: {label}\nConfidence: {score:.2f}"
|
| 24 |
+
|
| 25 |
+
# ============================
|
| 26 |
+
# π€ Chatbot (DialoGPT)
|
| 27 |
+
# ============================
|
| 28 |
+
|
| 29 |
+
# Use Microsoft DialoGPT for more relevant replies
|
| 30 |
+
chatbot_pipe = pipeline("text-generation", model="microsoft/DialoGPT-medium")
|
| 31 |
+
|
| 32 |
+
def chat_response(user_message):
|
| 33 |
+
# Provide prompt format to simulate a dialog
|
| 34 |
+
prompt = f"User: {user_message}\nBot:"
|
| 35 |
+
response = chatbot_pipe(prompt, max_length=100, do_sample=True, temperature=0.7)[0]["generated_text"]
|
| 36 |
+
# Clean the output to extract only the bot reply
|
| 37 |
+
reply = response.split("Bot:")[-1].strip()
|
| 38 |
+
return reply
|
| 39 |
+
|
| 40 |
+
# ============================
|
| 41 |
+
# β¨ Summarization
|
| 42 |
+
# ============================
|
| 43 |
+
|
| 44 |
+
# Summarization pipeline
|
| 45 |
+
summarization_pipe = pipeline("summarization", model="facebook/bart-large-cnn")
|
| 46 |
+
|
| 47 |
+
def summarize_text(text):
|
| 48 |
+
summary = summarization_pipe(text, max_length=130, min_length=30, do_sample=False)[0]["summary_text"]
|
| 49 |
+
return summary
|
| 50 |
+
|
| 51 |
+
# ============================
|
| 52 |
+
# π Text-to-Speech
|
| 53 |
+
# ============================
|
| 54 |
+
|
| 55 |
+
def text_to_speech(text):
|
| 56 |
+
tts = gtts.gTTS(text)
|
| 57 |
+
tts.save("output.mp3")
|
| 58 |
+
return "output.mp3"
|
| 59 |
+
|
| 60 |
+
# ============================
|
| 61 |
+
# π Gradio App (Multi-Tab)
|
| 62 |
+
# ============================
|
| 63 |
+
|
| 64 |
+
with gr.Blocks() as demo:
|
| 65 |
+
gr.Markdown("# π Multi-Task Language Application\nChoose a tab below to explore different language AI tasks!")
|
| 66 |
+
|
| 67 |
+
with gr.Tab("Sentiment Analysis"):
|
| 68 |
+
text_input = gr.Textbox(label="Enter text")
|
| 69 |
+
output = gr.Textbox(label="Sentiment Result")
|
| 70 |
+
analyze_btn = gr.Button("Analyze")
|
| 71 |
+
analyze_btn.click(analyze_sentiment, inputs=text_input, outputs=output)
|
| 72 |
+
|
| 73 |
+
with gr.Tab("Chatbot"):
|
| 74 |
+
chat_input = gr.Textbox(label="Ask something")
|
| 75 |
+
chat_output = gr.Textbox(label="Bot Reply")
|
| 76 |
+
chat_btn = gr.Button("Send")
|
| 77 |
+
chat_btn.click(chat_response, inputs=chat_input, outputs=chat_output)
|
| 78 |
+
|
| 79 |
+
with gr.Tab("Summarization"):
|
| 80 |
+
long_text = gr.Textbox(label="Paste text", lines=10, placeholder="Paste a long text here...")
|
| 81 |
+
summary_output = gr.Textbox(label="Summary")
|
| 82 |
+
summary_btn = gr.Button("Summarize")
|
| 83 |
+
summary_btn.click(summarize_text, inputs=long_text, outputs=summary_output)
|
| 84 |
+
|
| 85 |
+
with gr.Tab("Text-to-Speech"):
|
| 86 |
+
tts_text = gr.Textbox(label="Enter text to convert to speech")
|
| 87 |
+
audio_output = gr.Audio(label="Generated Speech")
|
| 88 |
+
tts_btn = gr.Button("Generate Voice")
|
| 89 |
+
tts_btn.click(text_to_speech, inputs=tts_text, outputs=audio_output)
|
| 90 |
+
|
| 91 |
+
demo.launch()
|