ksumhs commited on
Commit
cbdbe66
ยท
verified ยท
1 Parent(s): e0e9835

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +29 -26
app.py CHANGED
@@ -1,49 +1,52 @@
1
  import gradio as gr
2
  from transformers import pipeline
3
  from gtts import gTTS
 
4
 
5
- # ุชุญู…ูŠู„ ุงู„ู†ู…ุงุฐุฌ ู…ู† Hugging Face
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
- tts.save(filename)
26
- return filename
 
27
 
28
- # ุจู†ุงุก ุงู„ูˆุงุฌู‡ุฉ ุจู€ Gradio
29
  with gr.Blocks() as demo:
30
- gr.Markdown("## ๐Ÿง  NLP Tools: Sentiment Analysis | Summarization | Text-to-Speech")
31
-
32
- with gr.Row():
33
- input_text = gr.Textbox(label="Enter your text", lines=6, placeholder="Type or paste your text here...")
34
-
35
- with gr.Row():
36
- btn_sentiment = gr.Button("๐Ÿ” Analyze Sentiment")
37
- btn_summarize = gr.Button("๐Ÿ“ Summarize")
38
- btn_tts = gr.Button("๐Ÿ”Š Convert to Speech")
39
-
40
- with gr.Row():
41
- output_sentiment = gr.Textbox(label="Sentiment Result")
42
- output_summary = gr.Textbox(label="Summary")
43
- output_audio = gr.Audio(label="Text to Speech Output")
44
-
45
- btn_sentiment.click(analyze_sentiment, inputs=input_text, outputs=output_sentiment)
46
- btn_summarize.click(summarize_text, inputs=input_text, outputs=output_summary)
47
- btn_tts.click(text_to_speech, inputs=input_text, outputs=output_audio)
 
 
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()