Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
@@ -1,7 +1,30 @@
|
|
1 |
-
#
|
|
|
2 |
import gradio as gr
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
3 |
|
4 |
-
|
|
|
|
|
5 |
try:
|
6 |
# Get text from input or file
|
7 |
if file_input:
|
@@ -21,11 +44,9 @@ def emotion_aware_tts_pipeline_gradio(input_text=None, file_input=None):
|
|
21 |
|
22 |
# Generate audio
|
23 |
audio_path = "output.wav"
|
24 |
-
|
25 |
-
|
26 |
|
27 |
-
# Post-processing: adjust pitch and speed
|
28 |
-
adjust_pitch_and_speed(audio_path, pitch_factor=pitch, speed_factor=speed)
|
29 |
|
30 |
return f"Detected Emotion: {emotion} (Confidence: {confidence:.2f})", audio_path
|
31 |
else:
|
@@ -33,9 +54,11 @@ def emotion_aware_tts_pipeline_gradio(input_text=None, file_input=None):
|
|
33 |
except Exception as e:
|
34 |
return f"Error: {str(e)}", None
|
35 |
|
|
|
|
|
36 |
# Define Gradio interface
|
37 |
iface = gr.Interface(
|
38 |
-
fn=
|
39 |
inputs=[
|
40 |
gr.Textbox(label="Input Text", placeholder="Enter text here"),
|
41 |
gr.File(label="Upload a Text File")
|
@@ -49,4 +72,4 @@ iface = gr.Interface(
|
|
49 |
)
|
50 |
|
51 |
# Launch Gradio interface
|
52 |
-
iface.launch()
|
|
|
1 |
+
#Set up the Gradio interface
|
2 |
+
|
3 |
import gradio as gr
|
4 |
+
from transformers import pipeline
|
5 |
+
from TTS.api import TTS
|
6 |
+
|
7 |
+
# Load pre-trained emotion detection model
|
8 |
+
emotion_classifier = pipeline("text-classification", model="bhadresh-savani/distilbert-base-uncased-emotion")
|
9 |
+
|
10 |
+
# Load TTS model
|
11 |
+
tts_model = TTS(model_name="tts_models/en/ljspeech/tacotron2-DDC")
|
12 |
+
|
13 |
+
# Emotion-specific settings for pitch and speed
|
14 |
+
emotion_settings = {
|
15 |
+
"neutral": {"pitch": 1.0, "speed": 1.0},
|
16 |
+
"joy": {"pitch": 1.3, "speed": 1.2},
|
17 |
+
"sadness": {"pitch": 0.8, "speed": 0.9},
|
18 |
+
"anger": {"pitch": 1.6, "speed": 1.4},
|
19 |
+
"fear": {"pitch": 1.2, "speed": 0.95},
|
20 |
+
"surprise": {"pitch": 1.5, "speed": 1.3},
|
21 |
+
"disgust": {"pitch": 0.9, "speed": 0.95},
|
22 |
+
"shame": {"pitch": 0.8, "speed": 0.85},
|
23 |
+
}
|
24 |
|
25 |
+
|
26 |
+
# Function to process text or file input and generate audio
|
27 |
+
def emotion_aware_tts_pipeline(input_text=None, file_input=None):
|
28 |
try:
|
29 |
# Get text from input or file
|
30 |
if file_input:
|
|
|
44 |
|
45 |
# Generate audio
|
46 |
audio_path = "output.wav"
|
47 |
+
tts_model.tts_to_file(text=input_text, file_path=audio_path, speed=speed, pitch=pitch)
|
48 |
+
|
49 |
|
|
|
|
|
50 |
|
51 |
return f"Detected Emotion: {emotion} (Confidence: {confidence:.2f})", audio_path
|
52 |
else:
|
|
|
54 |
except Exception as e:
|
55 |
return f"Error: {str(e)}", None
|
56 |
|
57 |
+
|
58 |
+
|
59 |
# Define Gradio interface
|
60 |
iface = gr.Interface(
|
61 |
+
fn=emotion_aware_tts_pipeline,
|
62 |
inputs=[
|
63 |
gr.Textbox(label="Input Text", placeholder="Enter text here"),
|
64 |
gr.File(label="Upload a Text File")
|
|
|
72 |
)
|
73 |
|
74 |
# Launch Gradio interface
|
75 |
+
iface.launch()
|