Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
@@ -1,14 +1,14 @@
|
|
1 |
import torch
|
2 |
-
from transformers import
|
3 |
import sounddevice as sd
|
4 |
import soundfile as sf
|
5 |
import numpy as np
|
6 |
import requests
|
7 |
import webbrowser
|
8 |
-
|
9 |
|
10 |
-
# Load pre-trained model and tokenizer
|
11 |
-
model_name = "facebook/wav2vec2-large-xlsr-53" #
|
12 |
tokenizer = Wav2Vec2Tokenizer.from_pretrained(model_name)
|
13 |
model = Wav2Vec2ForCTC.from_pretrained(model_name)
|
14 |
|
@@ -20,16 +20,12 @@ def record_audio(duration=5, fs=16000):
|
|
20 |
print("Recording finished.")
|
21 |
return audio.flatten()
|
22 |
|
23 |
-
# Function to save audio file
|
24 |
-
def save_audio(filename, audio, fs=16000):
|
25 |
-
sf.write(filename, audio, fs)
|
26 |
-
|
27 |
# Function for emotion recognition
|
28 |
def recognize_emotion(audio):
|
29 |
-
#
|
30 |
input_values = tokenizer(audio, return_tensors='pt', padding='longest', sampling_rate=16000).input_values
|
31 |
|
32 |
-
#
|
33 |
with torch.no_grad():
|
34 |
logits = model(input_values).logits
|
35 |
predicted_ids = torch.argmax(logits, dim=-1)
|
@@ -39,44 +35,41 @@ def recognize_emotion(audio):
|
|
39 |
|
40 |
return transcription # Return the detected text
|
41 |
|
42 |
-
# Function to
|
43 |
def get_playlist(mood):
|
44 |
url = "https://unsa-unofficial-spotify-api.p.rapidapi.com/search"
|
45 |
-
querystring = {"query": mood, "count":"10", "type": "playlists"}
|
46 |
headers = {
|
47 |
'x-rapidapi-key': "your-api-key", # Replace with your actual API key
|
48 |
'x-rapidapi-host': "unsa-unofficial-spotify-api.p.rapidapi.com"
|
49 |
}
|
50 |
|
51 |
-
|
52 |
-
|
53 |
-
|
54 |
-
|
55 |
-
|
56 |
-
|
|
|
|
|
|
|
|
|
57 |
def open_playlist(playlist_id):
|
58 |
webbrowser.open(f'https://open.spotify.com/playlist/{playlist_id}')
|
59 |
|
60 |
-
# Main function to
|
61 |
def main():
|
62 |
-
|
63 |
-
|
64 |
-
audio = record_audio()
|
65 |
-
|
66 |
-
# Save audio to file
|
67 |
-
filename = "output.wav"
|
68 |
-
save_audio(filename, audio)
|
69 |
|
70 |
-
|
71 |
-
|
72 |
-
|
73 |
|
74 |
-
|
75 |
-
|
|
|
76 |
open_playlist(playlist_id)
|
77 |
|
78 |
-
except Exception as e:
|
79 |
-
print(f"An error occurred: {e}")
|
80 |
-
|
81 |
if __name__ == "__main__":
|
82 |
main()
|
|
|
1 |
import torch
|
2 |
+
from transformers import Wav2Vec2ForCTC, Wav2Vec2Tokenizer
|
3 |
import sounddevice as sd
|
4 |
import soundfile as sf
|
5 |
import numpy as np
|
6 |
import requests
|
7 |
import webbrowser
|
8 |
+
import os
|
9 |
|
10 |
+
# Load pre-trained Wav2Vec2 model and tokenizer
|
11 |
+
model_name = "facebook/wav2vec2-large-xlsr-53" # Model name for audio processing
|
12 |
tokenizer = Wav2Vec2Tokenizer.from_pretrained(model_name)
|
13 |
model = Wav2Vec2ForCTC.from_pretrained(model_name)
|
14 |
|
|
|
20 |
print("Recording finished.")
|
21 |
return audio.flatten()
|
22 |
|
|
|
|
|
|
|
|
|
23 |
# Function for emotion recognition
|
24 |
def recognize_emotion(audio):
|
25 |
+
# Normalize audio if necessary (check your audio data properties if required)
|
26 |
input_values = tokenizer(audio, return_tensors='pt', padding='longest', sampling_rate=16000).input_values
|
27 |
|
28 |
+
# Get the logits (raw predictions) and apply softmax to get probabilities
|
29 |
with torch.no_grad():
|
30 |
logits = model(input_values).logits
|
31 |
predicted_ids = torch.argmax(logits, dim=-1)
|
|
|
35 |
|
36 |
return transcription # Return the detected text
|
37 |
|
38 |
+
# Function to get Spotify playlist based on mood
|
39 |
def get_playlist(mood):
|
40 |
url = "https://unsa-unofficial-spotify-api.p.rapidapi.com/search"
|
41 |
+
querystring = {"query": mood, "count": "10", "type": "playlists"}
|
42 |
headers = {
|
43 |
'x-rapidapi-key': "your-api-key", # Replace with your actual API key
|
44 |
'x-rapidapi-host': "unsa-unofficial-spotify-api.p.rapidapi.com"
|
45 |
}
|
46 |
|
47 |
+
try:
|
48 |
+
response = requests.get(url, headers=headers, params=querystring)
|
49 |
+
response.raise_for_status() # Raises error for bad responses
|
50 |
+
playlist_id = response.json()["Results"][0]["id"] # Get the first playlist
|
51 |
+
return playlist_id
|
52 |
+
except requests.exceptions.RequestException as e:
|
53 |
+
print(f"Error fetching playlist data: {e}")
|
54 |
+
return None
|
55 |
+
|
56 |
+
# Function to open the Spotify playlist in a web browser
|
57 |
def open_playlist(playlist_id):
|
58 |
webbrowser.open(f'https://open.spotify.com/playlist/{playlist_id}')
|
59 |
|
60 |
+
# Main function to record audio and recognize mood
|
61 |
def main():
|
62 |
+
# Record audio
|
63 |
+
audio = record_audio()
|
|
|
|
|
|
|
|
|
|
|
64 |
|
65 |
+
# Recognize the mood/emotion from audio
|
66 |
+
emotion_text = recognize_emotion(audio)
|
67 |
+
print(f"Detected Emotion: {emotion_text}")
|
68 |
|
69 |
+
# Get Spotify playlist based on the detected emotion
|
70 |
+
playlist_id = get_playlist(emotion_text)
|
71 |
+
if playlist_id:
|
72 |
open_playlist(playlist_id)
|
73 |
|
|
|
|
|
|
|
74 |
if __name__ == "__main__":
|
75 |
main()
|