Guhanselvam commited on
Commit
1473306
·
verified ·
1 Parent(s): 206dfcf

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +59 -0
app.py ADDED
@@ -0,0 +1,59 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import os
2
+ import numpy as np
3
+ import librosa
4
+ import sounddevice as sd
5
+ import soundfile as sf
6
+ from sklearn.ensemble import RandomForestClassifier
7
+ from sklearn.metrics import accuracy_score, classification_report
8
+ from transformers import Wav2Vec2ForCTC, Wav2Vec2Tokenizer
9
+ import torch
10
+
11
+ # Load Hugging Face Wav2Vec2 Model
12
+ model_name = "facebook/wav2vec2-large-xlsr-53"
13
+ tokenizer = Wav2Vec2Tokenizer.from_pretrained(model_name)
14
+ model = Wav2Vec2ForCTC.from_pretrained(model_name)
15
+
16
+ def extract_features(audio, sample_rate=16000):
17
+ mfccs = librosa.feature.mfcc(y=audio, sr=sample_rate, n_mfcc=40) # Extract MFCCs
18
+ mfccs_scaled = np.mean(mfccs.T, axis=0) # Scale the MFCCs
19
+ return mfccs_scaled
20
+
21
+ # Function to predict emotion based on audio input
22
+ def predict_emotion(audio):
23
+ # Extract features from audio
24
+ features = extract_features(audio).reshape(1, -1) # Reshape for classifier input
25
+ predicted_emotion = model_rf.predict(features)
26
+ return predicted_emotion[0]
27
+
28
+ # Prepare your emotion classification model
29
+ # Replace this section with your own training procedures as necessary
30
+
31
+ # Assume we have trained 'model_rf', for example purposes
32
+ # Here you can load a trained model or define how to train it
33
+ emotions = ['happy', 'sad', 'angry', 'fear', 'surprise'] # Example emotion categories
34
+ # For demonstration purposes, we are creating a dummy classifier.
35
+ # Replace this with the actual model training as demonstrated previously.
36
+ model_rf = RandomForestClassifier(n_estimators=100, random_state=42) # Dummy model for demo
37
+
38
+ # This is a placeholder training step; you would train your model on actual data.
39
+ features_dummy = np.random.rand(100, 40) # Dummy feature data
40
+ labels_dummy = np.random.choice(emotions, 100) # Random dummy labels
41
+ model_rf.fit(features_dummy, labels_dummy) # Dummy fit
42
+
43
+ # Function to record audio and analyze emotion
44
+ def record_and_predict():
45
+ print("Recording... Please speak with emotion...")
46
+ duration = 5 # Duration of recording in seconds
47
+ sample_rate = 16000 # Sample rate for audio recording
48
+
49
+ # Record audio
50
+ audio = sd.rec(int(duration * sample_rate), samplerate=sample_rate, channels=1, dtype='float32')
51
+ sd.wait() # Wait until recording is finished
52
+ print("Recording finished.")
53
+
54
+ # Predict emotion from the recorded audio
55
+ emotion = predict_emotion(audio.flatten())
56
+ print(f'Predicted Emotion: {emotion}')
57
+
58
+ if __name__ == "__main__":
59
+ record_and_predict()