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

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +24 -24
app.py CHANGED
@@ -1,49 +1,49 @@
1
  import gradio as gr
2
  from transformers import pipeline
3
- import pyttsx3
4
 
5
- # Load pipelines
6
  sentiment_model = pipeline("sentiment-analysis")
7
  summarizer_model = pipeline("summarization")
8
- # Text-to-Speech using pyttsx3 (offline TTS engine)
9
- engine = pyttsx3.init()
10
 
11
- # Define Sentiment Analysis function
12
  def analyze_sentiment(text):
13
  result = sentiment_model(text)[0]
14
- return f"Sentiment: {result['label']}, Confidence: {result['score']:.2f}"
 
 
15
 
16
- # Define Summarization function
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
- # Define TTS function and save to file
22
  def text_to_speech(text):
23
- filename = "output_audio.wav"
24
- engine.save_to_file(text, filename)
25
- engine.runAndWait()
26
  return filename
27
 
28
- # Create Gradio interface
29
  with gr.Blocks() as demo:
30
- gr.Markdown("## ๐Ÿง  NLP Tools: Sentiment | Summarization | Text-to-Speech")
31
-
32
  with gr.Row():
33
- input_text = gr.Textbox(label="Enter your text here", lines=6, placeholder="Type or paste your text here...")
34
 
35
  with gr.Row():
36
- sentiment_btn = gr.Button("๐Ÿ” Analyze Sentiment")
37
- summarize_btn = gr.Button("๐Ÿ“ Summarize")
38
- tts_btn = gr.Button("๐Ÿ”Š Text to Speech")
39
 
40
  with gr.Row():
41
- sentiment_output = gr.Textbox(label="Sentiment Result")
42
- summary_output = gr.Textbox(label="Summary")
43
- audio_output = gr.Audio(label="Audio Output")
44
 
45
- sentiment_btn.click(analyze_sentiment, inputs=input_text, outputs=sentiment_output)
46
- summarize_btn.click(summarize_text, inputs=input_text, outputs=summary_output)
47
- tts_btn.click(text_to_speech, inputs=input_text, outputs=audio_output)
48
 
49
  demo.launch()
 
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()