-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrunsite.py
276 lines (209 loc) · 8.75 KB
/
runsite.py
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
import flask
import flask_session
import spotifyapi
import os
import authlib
import authlib.integrations
import authlib.integrations.flask_client
import dotenv
import database
import inference
import flask_cors
import openai
import random
if not dotenv.find_dotenv():
raise RuntimeError("No .env file found")
class EmotionSupport:
def __init__(self):
self.emotion = ""
self.emotion_override = ""
detector = inference.RestDetector('torchscript_model_0_66_49_wo_gl.pth')
dotenv.load_dotenv()
app = flask.Flask(__name__)
flask_cors.CORS(app)
ec = EmotionSupport()
oai = openai.OpenAI(api_key=os.getenv("OPENAI_API_KEY"))
app.config['SECRET_KEY'] = os.urandom(64)
app.config['SESSION_TYPE'] = 'filesystem'
app.config['SESSION_FILE_DIR'] = './.flask_session/'
flask_session.Session(app)
auth = authlib.integrations.flask_client.OAuth(app)
auth.register(
"auth0",
client_id=os.environ.get("AUTH0_CLIENT_ID"),
client_secret=os.environ.get("AUTH0_CLIENT_SECRET"),
client_kwargs={
"scope": "openid profile email",
},
server_metadata_url=f'https://{os.environ.get("AUTH0_DOMAIN")}/.well-known/openid-configuration',
)
spapi = spotifyapi.SpotifyAPI(flask.session)
cache_handler = spapi.get_cache_handler()
auth_manager = spapi.get_auth_manager()
sp = spapi.get_spotify()
userdb = database.UserDatabase("users.json")
songdb = database.SongDatabase("songs.json")
def get_emotion_support():
if 'emotion_support' not in flask.session:
flask.session['emotion_support'] = EmotionSupport()
return flask.session['emotion_support']
@app.route("/login")
def login():
return auth.auth0.authorize_redirect(
redirect_uri=flask.url_for("callback", _external=True)
)
@app.route("/callback", methods=["GET", "POST"])
def callback():
token = auth.auth0.authorize_access_token()
flask.session["user"] = token
if not userdb.user_exists(flask.session.get("user")['userinfo']['name']):
userdb.add_user(flask.session.get("user")['userinfo']['name'],
{
"spotify": None,
"liked_songs": []
})
if userdb.user_linked_spotify(token["access_token"]):
return flask.redirect("/dashboard")
else:
return flask.redirect("/link_spotify")
@app.route("/link_spotify")
def link_spotify():
return flask.redirect(auth_manager.get_authorize_url())
@app.route("/dashboard")
def dashboard():
if flask.request.args.get("code"):
auth_manager.get_access_token(flask.request.args.get("code"))
userdb.modify_user(flask.session.get("user")['userinfo']['name'], {
"spotify": sp.current_user()["id"],
"liked_songs": userdb.get_user(flask.session.get("user")['userinfo']['name'])["liked_songs"]
})
return flask.redirect("/dashboard")
if not auth_manager.validate_token(cache_handler.get_cached_token()):
return flask.redirect("/link_spotify")
saved_tracks = spapi.get_all_saved_tracks()
for track in saved_tracks:
if track['track']["id"] not in userdb.get_user(flask.session.get("user")['userinfo']['name'])["liked_songs"]:
userdb.modify_user(flask.session.get("user")['userinfo']['name'], {
"spotify": userdb.get_user(flask.session.get("user")['userinfo']['name'])["spotify"],
"liked_songs": userdb.get_user(flask.session.get("user")['userinfo']['name'])["liked_songs"] + [track['track']["id"]]
})
emotion = spapi.get_song_emotion(track['track']["id"])
if emotion == "happy":
songdb.songs["happy"].append(track['track']["id"])
elif emotion == "uplifting":
songdb.songs["uplifting"].append(track['track']["id"])
else:
songdb.songs["calming"].append(track['track']["id"])
songdb.save_db()
start_playback()
return flask.render_template("dashboard.html", session = flask.session.get("user"))
@app.route("/emotioninference", methods=['POST'])
def emotioninference():
print((spapi.get_song_length())/1000)
print((spapi.check_song_time())/1000)
print(f"Time left: {(spapi.get_song_length() - spapi.check_song_time())/1000}")
if spapi.get_song_length() != 0 and spapi.check_song_time() != 0:
if (spapi.get_song_length() - spapi.check_song_time())/1000 < 10 and (spapi.get_song_length() - spapi.check_song_time())/1000 > 5:
queue_next_song()
file = flask.request.files['file']
emotion = detector.detect_emotion(file)
if emotion in ["Neutral", "Joy"]:
ec.emotion = "happy"
elif emotion in ["Sadness", "Stressed"]:
ec.emotion = "uplifting"
else:
ec.emotion = "calming"
return flask.jsonify({"emotion": emotion})
@app.route('/analyze_text', methods=['POST'])
def analyze_text():
data = flask.request.get_json()
user_text = data['text']
prompt = f"Analyze the following text for simple emotions such as happy, scared, angry. If no real emotion, then neutral: \"{user_text}\""
response = oai.chat.completions.create(
model="gpt-3.5-turbo",
messages=[
{"role": "system", "content": "You're a emotion therapist."},
{"role": "user", "content": prompt}
],
max_tokens=50
)
analysis = response.choices[0].message.content
emotion_override = "Neutral"
music = "happy"
if any(keyword in analysis for keyword in ["sad", "down", "unhappy"]):
emotion_override = "Sadness"
music = "uplifting"
elif any(keyword in analysis for keyword in ["stressed", "anxious", "overwhelmed"]):
emotion_override = "Stressed"
music = "calming"
elif any(keyword in analysis for keyword in ["happy", "joyful", "excited"]):
emotion_override = "Joy"
elif any(keyword in analysis for keyword in ["angry", "mad", "furious"]):
emotion_override = "Anger"
music = "calming"
elif any(keyword in analysis for keyword in ["scared", "fearful", "afraid"]):
emotion_override = "Fear"
music = "calming"
elif any(keyword in analysis for keyword in ["surprised", "amazed", "astonished"]):
emotion_override = "Surprise"
music = "calming"
ec.emotion_override = music
return flask.jsonify({"emotion": emotion_override})
@app.route('/current_track', methods=['GET'])
def current_track():
current_playback = sp.current_playback()
if current_playback and current_playback['item']:
track_info = {
'cover': spapi.get_cover(),
'title': spapi.get_title(),
'artist': spapi.get_artist()
}
return flask.jsonify(track_info)
else:
return flask.jsonify({'error': 'No track currently playing'}), 404
@app.route('/start_playback', methods=['POST'])
def start_playback():
# check there is at least one active device
devices = sp.devices()
print(devices)
active_device = False
for device in devices['devices']:
if device['is_active']:
active_device = True
break
if not active_device:
return flask.jsonify({"status": "error", "message": "No active device found"})
if ec.emotion_override != "":
songType = ec.emotion_override
else:
songType = ec.emotion
if songType == "happy":
sp.start_playback(uris=["spotify:track:" + songdb.songs["happy"][random.randint(0, len(songdb.songs["happy"]) - 1)]])
elif songType == "uplifting":
sp.start_playback(uris=["spotify:track:" + songdb.songs["uplifting"][random.randint(0, len(songdb.songs["uplifting"]) - 1)]])
else:
sp.start_playback(uris=["spotify:track:" + songdb.songs["calming"][random.randint(0, len(songdb.songs["calming"]) - 1)]])
return flask.jsonify({"status": "success"})
def queue_next_song():
if ec.emotion_override != "":
songType = ec.emotion_override
else:
songType = ec.emotion
if songType == "happy":
print("CALLED", "spotify:track:", "HAPPY")
elif songType == "uplifting":
print("CALLED", "spotify:track:", "UPLIFTING")
else:
print("CALLED", "spotify:track:", "CALMING")
if songType == "happy":
spapi.the_add_to_queue("spotify:track:" + songdb.songs["happy"][random.randint(0, len(songdb.songs["happy"]) - 1)])
elif songType == "uplifting":
spapi.the_add_to_queue("spotify:track:" + songdb.songs["uplifting"][random.randint(0, len(songdb.songs["uplifting"]) - 1)])
else:
spapi.the_add_to_queue("spotify:track:" + songdb.songs["calming"][random.randint(0, len(songdb.songs["calming"]) - 1)])
return flask.jsonify({"status": "success"})
@app.route('/')
def index():
return flask.render_template("homepage.html")
if __name__ == '__main__':
app.run(debug=True, port=3000)