Spaces:
Running
Running
import gradio as gr | |
from transformers import pipeline | |
from gtts import gTTS | |
import tempfile | |
# Load Hugging Face pipelines | |
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 | |
# Gradio app with separate tabs/pages | |
with gr.Blocks() as demo: | |
gr.Markdown("## π Homework - Tuwaiq Academy") | |
with gr.Tab("π Sentiment Analysis"): | |
gr.Markdown("### Analyze the sentiment of your text") | |
input_sent = gr.Textbox(label="Enter your text", lines=6, placeholder="Type something...") | |
output_sent = gr.Textbox(label="Sentiment Result") | |
btn_sent = gr.Button("Analyze") | |
btn_sent.click(analyze_sentiment, inputs=input_sent, outputs=output_sent) | |
with gr.Tab("π Summarization"): | |
gr.Markdown("### Summarize your text") | |
input_sum = gr.Textbox(label="Enter your text", lines=6, placeholder="Paste a paragraph...") | |
output_sum = gr.Textbox(label="Summary") | |
btn_sum = gr.Button("Summarize") | |
btn_sum.click(summarize_text, inputs=input_sum, outputs=output_sum) | |
with gr.Tab("π Text to Speech"): | |
gr.Markdown("### Convert text to speech") | |
input_tts = gr.Textbox(label="Enter your text", lines=6, placeholder="Text for audio...") | |
output_audio = gr.Audio(label="Speech Output", type="filepath") | |
btn_tts = gr.Button("Convert") | |
btn_tts.click(text_to_speech, inputs=input_tts, outputs=output_audio) | |
demo.launch() | |