Spaces:
Sleeping
Sleeping
import gradio as gr | |
from transformers import pipeline | |
from gtts import gTTS | |
import tempfile | |
# Load models from Hugging Face | |
sentiment_model = pipeline("sentiment-analysis") | |
summarizer_model = pipeline("summarization") | |
# Sentiment analysis function | |
def analyze_sentiment(text): | |
result = sentiment_model(text)[0] | |
label = result['label'] | |
score = round(result['score'], 2) | |
return f"Sentiment: {label}, Confidence: {score}" | |
# Summarization function | |
def summarize_text(text): | |
summary = summarizer_model(text, max_length=60, min_length=15, do_sample=False) | |
return summary[0]['summary_text'] | |
# Text-to-speech function | |
def text_to_speech(text): | |
tts = gTTS(text) | |
with tempfile.NamedTemporaryFile(delete=False, suffix=".mp3") as fp: | |
tts.save(fp.name) | |
return fp.name | |
# Build Gradio interface with Tabs | |
with gr.Blocks() as demo: | |
gr.Markdown("## π Homework - Tuwaiq Academy") | |
with gr.Tabs(): | |
with gr.TabItem("π Sentiment Analysis"): | |
input_sentiment = gr.Textbox(label="Enter your text", lines=6, placeholder="Type your text here...") | |
output_sentiment = gr.Textbox(label="Sentiment Result") | |
btn_sentiment = gr.Button("Analyze Sentiment") | |
btn_sentiment.click(analyze_sentiment, inputs=input_sentiment, outputs=output_sentiment) | |
with gr.TabItem("π Summarization"): | |
input_summary = gr.Textbox(label="Enter your text", lines=6, placeholder="Type your text here...") | |
output_summary = gr.Textbox(label="Summary") | |
btn_summarize = gr.Button("Summarize") | |
btn_summarize.click(summarize_text, inputs=input_summary, outputs=output_summary) | |
with gr.TabItem("π Text to Speech"): | |
input_tts = gr.Textbox(label="Enter your text", lines=6, placeholder="Type your text here...") | |
output_audio = gr.Audio(label="Speech Output", type="filepath") | |
btn_tts = gr.Button("Convert to Speech") | |
btn_tts.click(text_to_speech, inputs=input_tts, outputs=output_audio) | |
demo.launch() | |