Spaces:
Runtime error
Runtime error
File size: 2,983 Bytes
6c39073 f16c455 53414b0 30972f9 f16c455 30972f9 f16c455 fd41edd 30972f9 8e5bf2a fd41edd c91a77d fd41edd a95975f c91a77d 97369b6 fd41edd 30972f9 200d4a2 30972f9 fd41edd 30972f9 8e5bf2a 8869631 c91a77d 8e5bf2a 8869631 6c39073 |
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 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 |
"""Set up the Gradio interface"""
import gradio as gr
from transformers import pipeline
from TTS.api import TTS
# Load pre-trained emotion detection model
emotion_classifier = pipeline("text-classification", model="bhadresh-savani/distilbert-base-uncased-emotion")
# Load TTS model
tts_model = TTS(model_name="tts_models/en/ljspeech/tacotron2-DDC")
# Emotion-specific settings for pitch and speed
emotion_settings = {
"neutral": {"pitch": 1.0, "speed": 1.0},
"joy": {"pitch": 1.3, "speed": 1.2},
"sadness": {"pitch": 0.8, "speed": 0.9},
"anger": {"pitch": 1.6, "speed": 1.4},
"fear": {"pitch": 1.2, "speed": 0.95},
"surprise": {"pitch": 1.5, "speed": 1.3},
"disgust": {"pitch": 0.9, "speed": 0.95},
"shame": {"pitch": 0.8, "speed": 0.85},
}
import librosa
import soundfile as sf
def adjust_audio_speed(audio_path, speed_factor):
y, sr = librosa.load(audio_path)
y_speeded = librosa.effects.time_stretch(y, speed_factor)
sf.write(audio_path, y_speeded, sr)
def adjust_audio_pitch(audio_path, pitch_factor):
y, sr = librosa.load(audio_path)
y_shifted = librosa.effects.pitch_shift(y, sr, n_steps=pitch_factor)
sf.write(audio_path, y_shifted, sr)
def emotion_aware_tts_pipeline(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"
tts_model.tts_to_file(text=input_text, file_path=audio_path)
# Adjust pitch and speed using librosa
if pitch != 1.0:
adjust_audio_pitch(audio_path, pitch)
if speed != 1.0:
adjust_audio_speed(audio_path, 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,
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() |