Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -1,49 +1,52 @@
|
|
1 |
import gradio as gr
|
2 |
from transformers import pipeline
|
3 |
from gtts import gTTS
|
|
|
4 |
|
5 |
-
#
|
6 |
sentiment_model = pipeline("sentiment-analysis")
|
7 |
summarizer_model = pipeline("summarization")
|
8 |
|
9 |
-
#
|
10 |
def analyze_sentiment(text):
|
11 |
result = sentiment_model(text)[0]
|
12 |
label = result['label']
|
13 |
score = round(result['score'], 2)
|
14 |
return f"Sentiment: {label}, Confidence: {score}"
|
15 |
|
16 |
-
#
|
17 |
def summarize_text(text):
|
18 |
summary = summarizer_model(text, max_length=60, min_length=15, do_sample=False)
|
19 |
return summary[0]['summary_text']
|
20 |
|
21 |
-
#
|
22 |
def text_to_speech(text):
|
23 |
-
filename = "output_audio.mp3"
|
24 |
tts = gTTS(text)
|
25 |
-
|
26 |
-
|
|
|
27 |
|
28 |
-
#
|
29 |
with gr.Blocks() as demo:
|
30 |
-
gr.Markdown("##
|
31 |
-
|
32 |
-
with gr.
|
33 |
-
|
34 |
-
|
35 |
-
|
36 |
-
|
37 |
-
|
38 |
-
|
39 |
-
|
40 |
-
|
41 |
-
|
42 |
-
|
43 |
-
|
44 |
-
|
45 |
-
|
46 |
-
|
47 |
-
|
|
|
|
|
48 |
|
49 |
demo.launch()
|
|
|
1 |
import gradio as gr
|
2 |
from transformers import pipeline
|
3 |
from gtts import gTTS
|
4 |
+
import tempfile
|
5 |
|
6 |
+
# Load models from Hugging Face
|
7 |
sentiment_model = pipeline("sentiment-analysis")
|
8 |
summarizer_model = pipeline("summarization")
|
9 |
|
10 |
+
# Sentiment analysis function
|
11 |
def analyze_sentiment(text):
|
12 |
result = sentiment_model(text)[0]
|
13 |
label = result['label']
|
14 |
score = round(result['score'], 2)
|
15 |
return f"Sentiment: {label}, Confidence: {score}"
|
16 |
|
17 |
+
# Summarization function
|
18 |
def summarize_text(text):
|
19 |
summary = summarizer_model(text, max_length=60, min_length=15, do_sample=False)
|
20 |
return summary[0]['summary_text']
|
21 |
|
22 |
+
# Text-to-speech function
|
23 |
def text_to_speech(text):
|
|
|
24 |
tts = gTTS(text)
|
25 |
+
with tempfile.NamedTemporaryFile(delete=False, suffix=".mp3") as fp:
|
26 |
+
tts.save(fp.name)
|
27 |
+
return fp.name
|
28 |
|
29 |
+
# Build Gradio interface with Tabs
|
30 |
with gr.Blocks() as demo:
|
31 |
+
gr.Markdown("## ๐ Homework - Tuwaiq Academy")
|
32 |
+
|
33 |
+
with gr.Tabs():
|
34 |
+
with gr.TabItem("๐ Sentiment Analysis"):
|
35 |
+
input_sentiment = gr.Textbox(label="Enter your text", lines=6, placeholder="Type your text here...")
|
36 |
+
output_sentiment = gr.Textbox(label="Sentiment Result")
|
37 |
+
btn_sentiment = gr.Button("Analyze Sentiment")
|
38 |
+
btn_sentiment.click(analyze_sentiment, inputs=input_sentiment, outputs=output_sentiment)
|
39 |
+
|
40 |
+
with gr.TabItem("๐ Summarization"):
|
41 |
+
input_summary = gr.Textbox(label="Enter your text", lines=6, placeholder="Type your text here...")
|
42 |
+
output_summary = gr.Textbox(label="Summary")
|
43 |
+
btn_summarize = gr.Button("Summarize")
|
44 |
+
btn_summarize.click(summarize_text, inputs=input_summary, outputs=output_summary)
|
45 |
+
|
46 |
+
with gr.TabItem("๐ Text to Speech"):
|
47 |
+
input_tts = gr.Textbox(label="Enter your text", lines=6, placeholder="Type your text here...")
|
48 |
+
output_audio = gr.Audio(label="Speech Output", type="filepath")
|
49 |
+
btn_tts = gr.Button("Convert to Speech")
|
50 |
+
btn_tts.click(text_to_speech, inputs=input_tts, outputs=output_audio)
|
51 |
|
52 |
demo.launch()
|