Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,70 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
from transformers import pipeline
|
| 3 |
+
from gtts import gTTS
|
| 4 |
+
|
| 5 |
+
# Load models
|
| 6 |
+
sentiment_pipeline = pipeline("sentiment-analysis", model="distilbert-base-uncased-finetuned-sst-2-english")
|
| 7 |
+
summarization_pipeline = pipeline("summarization", model="facebook/bart-large-cnn")
|
| 8 |
+
|
| 9 |
+
# Task logic
|
| 10 |
+
def perform_task(task, text):
|
| 11 |
+
if not text.strip():
|
| 12 |
+
return "⚠️ Please enter some text.", None
|
| 13 |
+
|
| 14 |
+
if task == "Sentiment Analysis":
|
| 15 |
+
result = sentiment_pipeline(text)[0]
|
| 16 |
+
label = result['label']
|
| 17 |
+
score = round(result['score'], 3)
|
| 18 |
+
return f"🧠 Sentiment: {label}\n📊 Confidence: {score}", None
|
| 19 |
+
|
| 20 |
+
elif task == "Summarization":
|
| 21 |
+
result = summarization_pipeline(text, max_length=100, min_length=30, do_sample=False)
|
| 22 |
+
return f"✂️ Summary:\n{result[0]['summary_text']}", None
|
| 23 |
+
|
| 24 |
+
elif task == "Text-to-Speech":
|
| 25 |
+
tts = gTTS(text)
|
| 26 |
+
filename = "tts_output.mp3"
|
| 27 |
+
tts.save(filename)
|
| 28 |
+
return "✅ Audio generated below:", filename
|
| 29 |
+
|
| 30 |
+
# UI with custom dark mode
|
| 31 |
+
with gr.Blocks(theme=gr.themes.Base()) as demo:
|
| 32 |
+
# Custom CSS for dark mode
|
| 33 |
+
gr.HTML("""
|
| 34 |
+
<style>
|
| 35 |
+
body, .gradio-container {
|
| 36 |
+
background-color: #111 !important;
|
| 37 |
+
color: #eee !important;
|
| 38 |
+
}
|
| 39 |
+
.gr-input, .gr-button, .gr-box {
|
| 40 |
+
background-color: #222 !important;
|
| 41 |
+
color: #eee !important;
|
| 42 |
+
border: 1px solid #444 !important;
|
| 43 |
+
}
|
| 44 |
+
input::placeholder, textarea::placeholder {
|
| 45 |
+
color: #888 !important;
|
| 46 |
+
}
|
| 47 |
+
</style>
|
| 48 |
+
""")
|
| 49 |
+
|
| 50 |
+
gr.Markdown("# 🤖 Multi-Task Chatbot", elem_id="title")
|
| 51 |
+
|
| 52 |
+
with gr.Row():
|
| 53 |
+
task_selector = gr.Dropdown(
|
| 54 |
+
["Sentiment Analysis", "Summarization", "Text-to-Speech"],
|
| 55 |
+
label="Select Task",
|
| 56 |
+
value="Sentiment Analysis"
|
| 57 |
+
)
|
| 58 |
+
|
| 59 |
+
textbox = gr.Textbox(lines=6, label="Enter your text here")
|
| 60 |
+
output_text = gr.Textbox(label="Output Message")
|
| 61 |
+
output_audio = gr.Audio(label="Generated Speech", type="filepath", visible=False)
|
| 62 |
+
run_button = gr.Button("Run")
|
| 63 |
+
|
| 64 |
+
def handle_all(task, text):
|
| 65 |
+
message, audio_path = perform_task(task, text)
|
| 66 |
+
return message, gr.update(value=audio_path, visible=audio_path is not None)
|
| 67 |
+
|
| 68 |
+
run_button.click(fn=handle_all, inputs=[task_selector, textbox], outputs=[output_text, output_audio])
|
| 69 |
+
|
| 70 |
+
demo.launch()
|