File size: 1,812 Bytes
c91a77d
fd41edd
 
c91a77d
fd41edd
 
 
 
 
 
 
 
 
 
 
 
c91a77d
fd41edd
a95975f
c91a77d
97369b6
fd41edd
 
c91a77d
 
200d4a2
c91a77d
 
fd41edd
 
 
 
 
 
 
8869631
c91a77d
 
8869631
 
 
 
 
 
 
 
 
 
 
 
 
c91a77d
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# Set up the Gradio interface
import gradio as gr

def emotion_aware_tts_pipeline_gradio(input_text=None, file_input=None):
    try:
        # Get text from input or file
        if file_input:
            with open(file_input.name, 'r') as file:
                input_text = file.read()

        if input_text:
            # Detect emotion
            emotion_data = emotion_classifier(input_text)[0]
            emotion = emotion_data['label']
            confidence = emotion_data['score']

            # Adjust pitch and speed
            settings = emotion_settings.get(emotion.lower(), {"pitch": 1.0, "speed": 1.0})
            pitch = settings["pitch"]
            speed = settings["speed"]

            # Generate audio
            audio_path = "output.wav"
            mel_spectrogram = tts_model.get_mel_spectrogram(input_text)
            audio = vocoder.decode(mel_spectrogram)

            # Post-processing: adjust pitch and speed
            adjust_pitch_and_speed(audio_path, pitch_factor=pitch, speed_factor=speed)

            return f"Detected Emotion: {emotion} (Confidence: {confidence:.2f})", audio_path
        else:
            return "Please provide input text or file", None
    except Exception as e:
        return f"Error: {str(e)}", None

# Define Gradio interface
iface = gr.Interface(
    fn=emotion_aware_tts_pipeline_gradio,
    inputs=[
        gr.Textbox(label="Input Text", placeholder="Enter text here"),
        gr.File(label="Upload a Text File")
    ],
    outputs=[
        gr.Textbox(label="Detected Emotion"),
        gr.Audio(label="Generated Audio")
    ],
    title="Emotion-Aware Text-to-Speech",
    description="Input text or upload a text file to detect the emotion and generate audio with emotion-aware modulation."
)

# Launch Gradio interface
iface.launch()