Spaces:
Sleeping
Sleeping
File size: 3,040 Bytes
2609d36 |
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 |
# ============================
# π’ 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() |