Anita-19's picture
Update app.py
8b40da9 verified
raw
history blame
1.81 kB
# 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()