File size: 12,244 Bytes
b10cf5d
 
 
47f9e3c
d582602
8573520
 
47f9e3c
 
d582602
47f9e3c
15dbab2
b10cf5d
47f9e3c
8573520
b10cf5d
 
8573520
b10cf5d
 
 
 
 
 
 
 
 
 
 
 
 
d582602
8573520
b10cf5d
 
 
 
 
 
 
 
15dbab2
bbd5b58
 
 
 
 
 
 
 
 
 
 
df98826
bbd5b58
47f9e3c
8573520
bbd5b58
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
b10cf5d
bbd5b58
 
 
 
b10cf5d
bbd5b58
b10cf5d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
8573520
 
b10cf5d
 
8573520
 
b10cf5d
 
 
 
 
 
 
 
 
 
 
8573520
 
 
b10cf5d
 
 
 
 
 
 
 
 
 
8573520
 
 
b10cf5d
 
8573520
b10cf5d
8573520
 
b10cf5d
 
 
8573520
 
 
b10cf5d
 
 
8573520
 
 
b10cf5d
 
 
 
 
 
 
 
8573520
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
b10cf5d
 
8573520
b10cf5d
 
 
 
 
 
 
 
 
 
 
8573520
b10cf5d
 
8573520
 
b10cf5d
 
 
 
 
d582602
8573520
b10cf5d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
d582602
8573520
b10cf5d
 
 
 
 
 
 
 
 
 
d582602
8573520
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
4fd5ac6
 
d582602
8573520
b10cf5d
8573520
 
 
 
 
 
 
 
 
 
 
bbd5b58
 
 
 
 
 
 
 
8573520
 
d981fc3
4b3b013
 
 
 
 
 
 
 
 
 
 
8573520
 
b10cf5d
 
 
 
 
 
cac8321
b10cf5d
 
 
 
 
ca163b3
b10cf5d
f67015b
ca163b3
f67015b
b10cf5d
 
4b3b013
d0ae34f
4b3b013
 
 
 
 
b10cf5d
 
 
 
 
 
 
4b3b013
8573520
b10cf5d
15dbab2
8573520
b10cf5d
 
 
 
 
 
 
 
 
 
 
 
 
47f9e3c
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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
from google.colab import drive
drive.mount('/content/drive')

"""Install Dependencies"""

pip install transformers librosa torch soundfile numba numpy TTS datasets gradio protobuf==3.20.3

"""Emotion Detection (Using Text Dataset)

"""

!pip install --upgrade numpy tensorflow transformers TTS

!pip freeze > requirements.txt

from transformers import pipeline

# Load pre-trained model for emotion detection
emotion_classifier = pipeline("text-classification", model="bhadresh-savani/distilbert-base-uncased-emotion")

def detect_emotion(text):
    result = emotion_classifier(text)
    emotion = result[0]['label']
    confidence = result[0]['score']
    return emotion, confidence

# Example usage
text = "I am feeling excited today!"
emotion, confidence = detect_emotion(text)
print(f"Detected Emotion: {emotion}, Confidence: {confidence}")

"""Emotion-Aware TTS (Using Tacotron 2 or Similar)"""

import torch
import librosa
import numpy as np
from TTS.api import TTS  # Using Coqui TTS for simplicity

# Load TTS model and vocoder automatically during initialization
tts_model = TTS(model_name="tts_models/en/ljspeech/tacotron2-DDC")


def generate_emotional_speech(text, emotion):
    # Map emotion to voice modulation parameters (pitch, speed)
    emotion_settings = {
    "neutral": {"pitch": 1.0, "speed": 1.0},     # Baseline conversational tone
    "joy": {"pitch": 1.3, "speed": 1.2},         # Upbeat and energetic
    "sadness": {"pitch": 0.8, "speed": 0.9},     # Subdued, slow tone
    "anger": {"pitch": 1.6, "speed": 1.4},       # Intense and sharp
    "fear": {"pitch": 1.2, "speed": 0.95},       # Tense and slightly slow
    "surprise": {"pitch": 1.5, "speed": 1.3},    # Excitement with high pitch and fast speech
    "disgust": {"pitch": 0.9, "speed": 0.95},    # Low and deliberate
    "shame": {"pitch": 0.8, "speed": 0.85},      # Quiet, subdued tone

}


    # Retrieve pitch and speed based on detected emotion
    settings = emotion_settings.get(emotion, {"pitch": 1.0, "speed": 1.0})
    # Generate speech with the TTS model
    # Instead of directly passing speed and pitch to tts_to_file,
    # We adjust the text to simulate the effect. This is a temporary solution.
    # You might need to fine-tune these adjustments or consider a different TTS library
    # with better control over speech parameters.
    adjusted_text = text
    if settings['speed'] > 1.0:
        adjusted_text = adjusted_text.replace(" ", ".")  # Simulate faster speech
    elif settings['speed'] < 1.0:
        adjusted_text = adjusted_text.replace(" ", "...")  # Simulate slower speech

    # Explicitly specify the output path
    audio_path = "output.wav"  # Or any desired filename
    tts_model.tts_to_file(text=adjusted_text, file_path=audio_path)  # Pass file_path argument
    return audio_path

# Example usage
emotion = "happy"
output_audio = generate_emotional_speech("Welcome to the smart library!", emotion)
print(f"Generated Speech Saved At: {output_audio}")

"""Integrating the Workflow"""

from IPython.display import Audio, display

def emotion_aware_tts_pipeline(text):
    emotion, confidence = detect_emotion(text)
    print(f"Emotion Detected: {emotion} with Confidence: {confidence:.2f}")

    audio_path = generate_emotional_speech(text, emotion)
    print(f"Audio Generated: {audio_path}")

    # Display and play the audio
    display(Audio(audio_path, autoplay=True))

# Example usage
emotion_aware_tts_pipeline("I can’t stooop smiiiling, everything feels perrrfect!")

"""Fine-tuning the Emotion Detection Model"""

import os
os.environ["WANDB_DISABLED"] = "true"

from google.colab import drive
from transformers import Trainer, TrainingArguments, AutoModelForSequenceClassification, AutoTokenizer
from datasets import load_dataset

# Load dataset
dataset = load_dataset('/content/drive/MyDrive/Emotion_Model')  #path_to_your_dataset

# Preprocess data
tokenizer = AutoTokenizer.from_pretrained("bhadresh-savani/distilbert-base-uncased-emotion")

# Define a function to map emotion labels to integers
def map_emotion_to_int(example):
    # Assuming your dataset has an 'emotion' column with string labels
    # Replace this with your actual emotion labels and their corresponding integers
    # *Change 'emotion' to the actual column name in your dataset*
    emotion_mapping = {
        "neutral": 0,
        "joy": 1,
        "sad": 2,
        "anger": 3,
        "fear": 4,
        "surprise": 5,
        "disgust": 6,
        "shame": 7,
    }
    # Assuming your emotion column is named 'label'
    # example['label'] = emotion_mapping[example['emotion']] # Create a new 'label' column with integer values
    example['label'] = emotion_mapping.get(example['label'], -1) # If the label is not in the emotion mapping then we set it to -1. We can later filter these examples out
    return example


def preprocess_data(example):
    return tokenizer(example['text'], truncation=True, padding=True, max_length=512) # Added max_length for consistency


# Apply emotion mapping before tokenization
dataset = dataset.map(map_emotion_to_int, batched=False)
# *Keep the 'label' column for training. Only remove 'text'*
# Filter out examples with labels not in emotion_mapping (-1)
dataset = dataset.filter(lambda example: example['label'] != -1) # Filter out examples with label -1
tokenized_dataset = dataset.map(preprocess_data, batched=True, remove_columns=['text'])

# Load model
# model = AutoModelForSequenceClassification.from_pretrained("bhadresh-savani/distilbert-base-uncased-emotion", num_labels=8)

# Load model with ignore_mismatched_sizes=True
model = AutoModelForSequenceClassification.from_pretrained(
    "bhadresh-savani/distilbert-base-uncased-emotion",
    num_labels=8,
    ignore_mismatched_sizes=True
)

# Training arguments
training_args = TrainingArguments(
    output_dir="./results",    # Directory for model checkpoints and logs
    evaluation_strategy="epoch",  # Evaluate after every epoch
    learning_rate=5e-5,  # Start with 5e-5 (slightly higher than default 2e-5)
    per_device_train_batch_size=16,  # Use 16 for balance between memory usage and training speed
    gradient_accumulation_steps=4,  # Accumulate gradients to simulate larger batch size
    num_train_epochs=5,  # Train for 4-5 epochs (typically enough for fine-tuning)
    weight_decay=0.01,  # Regularization to avoid overfitting
    save_strategy="epoch",  # Save checkpoints after each epoch
    logging_dir="./logs",  # Directory for logging
    logging_steps=100,  # Log every 100 steps
    warmup_steps=500,  # Gradual learning rate increase for the first 500 steps
    save_total_limit=3,  # Keep only the last 3 checkpoints
    fp16=True,  # Enable mixed precision for faster training if GPU supports it
    load_best_model_at_end=True,  # Load the best model at the end of training
    metric_for_best_model="eval_loss",  # Use evaluation loss to select the best model
    greater_is_better=False,  # Lower loss is better
)


# Train model
trainer = Trainer(
    model=model,
    args=training_args,
    train_dataset=tokenized_dataset['train'],
    eval_dataset=tokenized_dataset['validation'],
    tokenizer=tokenizer,
)

trainer.train()

# Save the model and tokenizer to Google Drive
model_save_path = "/content/drive/My Drive/emotion_detection_model1"
tokenizer_save_path = "/content/drive/My Drive/emotion_detection_model1"

# Save the fine-tuned model
model.save_pretrained(model_save_path)
tokenizer.save_pretrained(tokenizer_save_path)

print("Model and tokenizer saved to Google Drive.")

"""Reload the Fine-Tuned Model"""

from transformers import AutoModelForSequenceClassification, AutoTokenizer

# Mount Google Drive
from google.colab import drive
drive.mount('/content/drive')

# Path to the saved model and tokenizer
model_save_path = "/content/drive/My Drive/emotion_detection_model"
tokenizer_save_path = "/content/drive/My Drive/emotion_detection_model"

# Load the fine-tuned model and tokenizer
model = AutoModelForSequenceClassification.from_pretrained(model_save_path)
tokenizer = AutoTokenizer.from_pretrained(tokenizer_save_path)

print("Fine-tuned model and tokenizer loaded successfully.")

"""Test the Reloaded Model"""

from transformers import pipeline

# Create a text classification pipeline with the loaded model
emotion_classifier = pipeline("text-classification", model=model, tokenizer=tokenizer)

# Test with a sample text
text = "I feel so upset today!"
result = emotion_classifier(text)
print(result)

"""Fine-tuning the TTS System"""

from TTS.api import TTS
from TTS.utils.audio import AudioProcessor
from TTS.tts.models.tacotron2 import Tacotron2
import torch

# Load pre-trained model
#model = Tacotron2.load_model("tts_models/en/ljspeech/tacotron2-DDC")
tts = TTS(model_name="tts_models/en/ljspeech/tacotron2-DDC") # Use TTS for model loading

# Access the Tacotron2 model from the TTS object
model = tts.synthesizer.tts_model

# Fine-tuning parameters
model.config.dataset_path = "/content/drive/MyDrive/RAVDESS"
model.config.num_epochs = 10

# Train
model.train()

# Define the save path on Google Drive
save_path = "/content/drive/My Drive/fine_tuned_tacotron2.pth"

# Save the model's state dictionary using torch.save
torch.save(model.state_dict(), save_path)
    

"""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()