Spaces:
Sleeping
Sleeping
File size: 2,079 Bytes
0cd6b01 e0e9835 cbdbe66 0cd6b01 cbdbe66 0cd6b01 cbdbe66 0cd6b01 e0e9835 0cd6b01 cbdbe66 0cd6b01 cbdbe66 0cd6b01 e0e9835 cbdbe66 0cd6b01 cbdbe66 0cd6b01 cbdbe66 0cd6b01 |
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 |
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()
|