Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +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()
|