-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.py
More file actions
207 lines (170 loc) · 6.96 KB
/
Copy pathapp.py
File metadata and controls
207 lines (170 loc) · 6.96 KB
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
from flask import Flask, request, jsonify, send_file
import requests, os, random, spotipy
from urllib.parse import quote
from dotenv import load_dotenv
import spacy
load_dotenv(dotenv_path="tunebot_env/.env")
app = Flask(__name__)
DEEZER_API_URL = "https://api.deezer.com"
INTENTS = {
"greet": ["hello", "hi", "hey", "hola"],
"goodbye": ["bye", "goodbye", "see you"],
"thanks": ["thanks", "thank you"],
"no": ["no", "nope"]
# "mood": ["happy", "sad", "angry", "relaxed", "excited", "romantic", "energetic"]
}
MOOD_GENRES = {
"happy": "pop",
"sad": "acoustic",
"angry": "rock",
"relaxed": "chill",
"excited": "dance",
"romantic": "love songs",
"energetic": "workout",
"calm": "classical",
"motivated": "hip-hop",
"nostalgic": "oldies",
"melancholy": "indie",
"adventurous": "indie rock",
"party": "EDM",
"chill": "lo-fi"
}
mood_keywords = {
"happy": ["happy", "joyful", "excited", "good", "cheerful", "elated"],
"sad": ["sad", "down", "blue", "unhappy", "mournful", "gloomy"],
"angry": ["angry", "furious", "mad", "upset", "irritated"],
"relaxed": ["relaxed", "calm", "chill", "peaceful", "serene"],
"romantic": ["romantic", "love", "affection", "heart", "sweet", "passionate", "adoring"],
"energetic": ["energetic", "active", "lively", "vibrant", "bouncy"],
"calm": ["calm", "peaceful", "serene", "tranquil", "soothing"],
"motivated": ["motivated", "inspired", "focused", "determined", "ambitious"],
"nostalgic": ["nostalgic", "retro", "throwback", "old", "vintage", "memory"],
"melancholy": ["melancholy", "reflective", "lonely", "nostalgic", "pensive"],
"adventurous": ["adventurous", "exploring", "curious", "wild", "free", "daring"],
"party": ["party", "celebration", "fun", "dance", "club", "festive"],
"chill": ["chill", "relax", "laid-back", "smooth", "mellow", "cozy"]
}
nlp = spacy.load("en_core_web_sm")
@app.route("/")
def home():
return send_file("files/index.html")
def get_spotify_token():
client_id = os.getenv("SPOTIFY_CLIENT_ID")
client_secret = os.getenv("SPOTIFY_CLIENT_SECRET")
if not client_id or not client_secret:
print("Error: Missing Spotify credentials.")
return None
url = "https://accounts.spotify.com/api/token"
headers = {"Content-Type": "application/x-www-form-urlencoded"}
body = {
"grant_type": "client_credentials",
"client_id": client_id,
"client_secret": client_secret,
}
response = requests.post(url, headers=headers, data=body)
if response.status_code == 200:
return response.json().get("access_token")
else:
print(f"Error fetching token: {response.status_code} - {response.text}")
return None
def get_spotify_link(song_name, artist_name):
try:
token = get_spotify_token()
if not token:
print("Error: Could not get Spotify token.")
return None
headers = {"Authorization": f"Bearer {token}"}
query = f"track:{song_name} artist:{artist_name}"
url = f"https://api.spotify.com/v1/search?q={quote(query)}&type=track&limit=1"
response = requests.get(url, headers=headers)
data = response.json()
if not data.get("tracks", {}).get("items"):
url = f"https://api.spotify.com/v1/search?q={quote(song_name)}&type=track&limit=5"
response = requests.get(url, headers=headers)
data = response.json()
for item in data.get("tracks", {}).get("items", []):
for artist in item.get("artists", []):
if artist_name.lower() in artist["name"].lower():
return item["external_urls"]["spotify"]
return None
return data["tracks"]["items"][0]["external_urls"]["spotify"]
except Exception as e:
print(f"Error fetching Spotify link: {e}")
return None
def get_recommendations(genre):
search_url = f"{DEEZER_API_URL}/search"
params = {"q": genre.lower()}
try:
response = requests.get(search_url, params=params)
response.raise_for_status()
data = response.json()
if 'data' in data:
songs = data['data']
song_list = []
for song in songs:
song_name = song['title']
artist_name = song['artist']['name']
song_list.append({
"name": song_name,
"artist": artist_name,
"deezer_link": song['link']
})
# Return up to 5 random songs
random_songs = random.sample(song_list, 5) if len(song_list) >= 5 else song_list
return random_songs
except requests.exceptions.RequestException as e:
print(f"Error fetching data: {e}")
return None
# Chat API
@app.route("/chat", methods=["POST"])
def chat():
user_message = request.json.get("message", "").lower()
doc = nlp(user_message.lower())
genre = None
user_mood = None
asking_mood = False
for mood, keywords in mood_keywords.items():
for keyword in keywords:
if keyword in doc.text:
user_mood = keyword
genre = MOOD_GENRES.get(mood)
asking_mood = True
break
if user_mood:
asking_mood = True
break
if not asking_mood:
for intent, keywords in INTENTS.items():
if any(keyword in user_message for keyword in keywords):
if intent == "greet":
return jsonify({
"reply": "🎵 Hi there! I'm TuneBot. Tell me your mood, and I'll recommend some tunes!"})
elif intent == "goodbye":
return jsonify({"reply": "👋 Goodbye! Have a great day!"})
elif intent == "thanks":
return jsonify({"reply": "😊 You're welcome!"})
elif intent == "no":
return jsonify({"reply": "😔 Pretty Please"})
if not genre:
return jsonify({"reply": "I couldn't detect your mood. Could you tell me more?"})
songs = get_recommendations(genre)
if songs:
song_names = ""
for song in songs:
spotify_link = get_spotify_link(song['name'], song['artist'])
deezer_link = song.get('deezer_link')
if spotify_link:
link = spotify_link
platform = "Spotify"
elif deezer_link:
link = deezer_link
platform = "Deezer"
else:
continue
song_names += f"<br><a href='{link}' target='_blank'>{song['name']} by {song['artist']} ({platform})</a>"
return jsonify({
"reply": f"🎶 Here are some songs for your {user_mood} mood:<br>{song_names}<br><br>Would you like to hear more songs?"})
else:
return jsonify({"reply": "Sorry, I couldn't find any songs for that mood. Try again!"})
if __name__ == "__main__":
app.run(debug=True)