Anita-19 commited on
Commit
d582602
·
verified ·
1 Parent(s): 8e5bf2a

Update main.py

Browse files
Files changed (1) hide show
  1. main.py +33 -30
main.py CHANGED
@@ -1,12 +1,13 @@
1
  from google.colab import drive
2
  drive.mount('/content/drive')
3
 
4
- #Install Dependencies"""
5
- """
6
  pip install transformers librosa torch soundfile numba numpy TTS datasets gradio protobuf==3.20.3
7
 
8
- #Emotion Detection (Using Text Dataset)
9
 
 
10
 
11
  !pip install --upgrade numpy tensorflow transformers TTS
12
 
@@ -28,7 +29,7 @@ text = "I am feeling excited today!"
28
  emotion, confidence = detect_emotion(text)
29
  print(f"Detected Emotion: {emotion}, Confidence: {confidence}")
30
 
31
- #Emotion-Aware TTS (Using Tacotron 2 or Similar)"""
32
 
33
  import torch
34
  import librosa
@@ -52,27 +53,6 @@ def generate_emotional_speech(text, emotion):
52
  "shame": {"pitch": 0.8, "speed": 0.85}, # Quiet, subdued tone
53
 
54
  }
55
-
56
- import librosa
57
- import soundfile as sf
58
-
59
- def adjust_pitch(audio_path, pitch_factor):
60
- # Load audio
61
- y, sr = librosa.load(audio_path)
62
- # Adjust pitch
63
- y_shifted = librosa.effects.pitch_shift(y, sr, n_steps=pitch_factor)
64
- # Save adjusted audio
65
- sf.write(audio_path, y_shifted, sr)
66
-
67
- def adjust_speed(audio_path, speed_factor):
68
- # Load the audio file
69
- y, sr = librosa.load(audio_path)
70
-
71
- # Adjust the speed (this alters the duration of the audio)
72
- y_speeded = librosa.effects.time_stretch(y, speed_factor)
73
-
74
- # Save the adjusted audio
75
- sf.write(audio_path, y_speeded, sr)
76
 
77
  # Retrieve pitch and speed based on detected emotion
78
  settings = emotion_settings.get(emotion, {"pitch": 1.0, "speed": 1.0})
@@ -97,6 +77,29 @@ emotion = "happy"
97
  output_audio = generate_emotional_speech("Welcome to the smart library!", emotion)
98
  print(f"Generated Speech Saved At: {output_audio}")
99
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
100
  """Integrating the Workflow"""
101
 
102
  from IPython.display import Audio, display
@@ -213,7 +216,7 @@ tokenizer.save_pretrained(tokenizer_save_path)
213
 
214
  print("Model and tokenizer saved to Google Drive.")
215
 
216
- #Reload the Fine-Tuned Model"""
217
 
218
  from transformers import AutoModelForSequenceClassification, AutoTokenizer
219
 
@@ -231,7 +234,7 @@ tokenizer = AutoTokenizer.from_pretrained(tokenizer_save_path)
231
 
232
  print("Fine-tuned model and tokenizer loaded successfully.")
233
 
234
- #Test the Reloaded Model"""
235
 
236
  from transformers import pipeline
237
 
@@ -243,7 +246,7 @@ text = "I feel so upset today!"
243
  result = emotion_classifier(text)
244
  print(result)
245
 
246
- #Fine-tuning the TTS System"""
247
 
248
  from TTS.api import TTS
249
  from TTS.utils.audio import AudioProcessor
@@ -271,7 +274,7 @@ save_path = "/content/drive/My Drive/fine_tuned_tacotron2.pth"
271
  torch.save(model.state_dict(), save_path)
272
 
273
 
274
- #Set up the Gradio interface
275
 
276
  import gradio as gr
277
  from transformers import pipeline
@@ -345,4 +348,4 @@ iface = gr.Interface(
345
  )
346
 
347
  # Launch Gradio interface
348
- iface.launch()
 
1
  from google.colab import drive
2
  drive.mount('/content/drive')
3
 
4
+ """Install Dependencies
5
+
6
  pip install transformers librosa torch soundfile numba numpy TTS datasets gradio protobuf==3.20.3
7
 
8
+ """Emotion Detection (Using Text Dataset)
9
 
10
+ """
11
 
12
  !pip install --upgrade numpy tensorflow transformers TTS
13
 
 
29
  emotion, confidence = detect_emotion(text)
30
  print(f"Detected Emotion: {emotion}, Confidence: {confidence}")
31
 
32
+ """Emotion-Aware TTS (Using Tacotron 2 or Similar)"""
33
 
34
  import torch
35
  import librosa
 
53
  "shame": {"pitch": 0.8, "speed": 0.85}, # Quiet, subdued tone
54
 
55
  }
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
56
 
57
  # Retrieve pitch and speed based on detected emotion
58
  settings = emotion_settings.get(emotion, {"pitch": 1.0, "speed": 1.0})
 
77
  output_audio = generate_emotional_speech("Welcome to the smart library!", emotion)
78
  print(f"Generated Speech Saved At: {output_audio}")
79
 
80
+ #
81
+ import librosa
82
+ import soundfile as sf
83
+
84
+ def adjust_pitch(audio_path, pitch_factor):
85
+ # Load audio
86
+ y, sr = librosa.load(audio_path)
87
+ # Adjust pitch
88
+ y_shifted = librosa.effects.pitch_shift(y, sr, n_steps=pitch_factor)
89
+ # Save adjusted audio
90
+ sf.write(audio_path, y_shifted, sr)
91
+
92
+ def adjust_speed(audio_path, speed_factor):
93
+ # Load the audio file
94
+ y, sr = librosa.load(audio_path)
95
+
96
+ # Adjust the speed (this alters the duration of the audio)
97
+ y_speeded = librosa.effects.time_stretch(y, speed_factor)
98
+
99
+ # Save the adjusted audio
100
+ sf.write(audio_path, y_speeded, sr)
101
+
102
+
103
  """Integrating the Workflow"""
104
 
105
  from IPython.display import Audio, display
 
216
 
217
  print("Model and tokenizer saved to Google Drive.")
218
 
219
+ """Reload the Fine-Tuned Model"""
220
 
221
  from transformers import AutoModelForSequenceClassification, AutoTokenizer
222
 
 
234
 
235
  print("Fine-tuned model and tokenizer loaded successfully.")
236
 
237
+ """Test the Reloaded Model"""
238
 
239
  from transformers import pipeline
240
 
 
246
  result = emotion_classifier(text)
247
  print(result)
248
 
249
+ """Fine-tuning the TTS System"""
250
 
251
  from TTS.api import TTS
252
  from TTS.utils.audio import AudioProcessor
 
274
  torch.save(model.state_dict(), save_path)
275
 
276
 
277
+ """Set up the Gradio interface"""
278
 
279
  import gradio as gr
280
  from transformers import pipeline
 
348
  )
349
 
350
  # Launch Gradio interface
351
+ iface.launch()