-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.py
More file actions
62 lines (55 loc) · 2.78 KB
/
app.py
File metadata and controls
62 lines (55 loc) · 2.78 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
from flask import Flask, render_template, request, jsonify
from transformers import pipeline
app = Flask(__name__)
# Load emotion detection model
emotion = pipeline("text-classification", model="bhadresh-savani/distilbert-base-uncased-emotion")
# Predefined songs for each mood
mood_playlist = {
"joy": [
{"title": "Happy", "artist": "Pharrell Williams", "url": "https://open.spotify.com/track/60nZcImufyMA1MKQY3dcCH"},
{"title": "Can't Stop the Feeling!", "artist": "Justin Timberlake", "url": "https://open.spotify.com/track/6JV2JOEocMgcZxYSZelKcc"}
],
"sadness": [
{"title": "Someone Like You", "artist": "Adele", "url": "https://open.spotify.com/track/4kflIGfjdZJW4ot2ioixTB"},
{"title": "Let Her Go", "artist": "Passenger", "url": "https://open.spotify.com/track/3ZFTkvIE7kyPt6Nu3PEa7V"}
],
"anger": [
{"title": "Lose Yourself", "artist": "Eminem", "url": "https://open.spotify.com/track/1u8c2t2Cy7UBoG4ArRcF5g"},
{"title": "Break Stuff", "artist": "Limp Bizkit", "url": "https://open.spotify.com/track/5vV3bFXnN6D6N3iUUcFvFc"}
],
"fear": [
{"title": "Creep", "artist": "Radiohead", "url": "https://open.spotify.com/track/3HfB5hBU0dmBt8T0iCmH42"},
{"title": "Everybody's Got Something to Hide", "artist": "The Beatles", "url": "https://open.spotify.com/track/3KfbEIOC7YIv90FIfNSZpo"}
],
"love": [
{"title": "Perfect", "artist": "Ed Sheeran", "url": "https://open.spotify.com/track/0tgVpDi06FyKpA1z0VMD4v"},
{"title": "All of Me", "artist": "John Legend", "url": "https://open.spotify.com/track/3U4isOIWM3VvDubwSI3y7a"},
{"title": "Dooron Dooron", "artist": "Paresh Pahuja", "url": "https://open.spotify.com/track/42PTJxN"}
],
"surprise": [
{"title": "Uptown Funk", "artist": "Mark Ronson ft. Bruno Mars", "url": "https://open.spotify.com/track/32OlwWuMpZ6b0aN2RZOeMS"},
{"title": "Shake It Off", "artist": "Taylor Swift", "url": "https://open.spotify.com/track/5WfhX6gQXxZzWEuPZP4gFe"}
],
"neutral": [
{"title": "Sunflower", "artist": "Post Malone & Swae Lee", "url": "https://open.spotify.com/track/3KkXRkHbMCARz0aVfEt68P"},
{"title": "Blinding Lights", "artist": "The Weeknd", "url": "https://open.spotify.com/track/0VjIjW4GlUZAMYd2vXMi3b"}
]
}
# Routes
@app.route("/")
def index():
return render_template("index.html")
@app.route("/analyze", methods=["POST"])
def analyze():
data = request.get_json()
text = data.get("text", "")
if not text.strip():
mood = "neutral"
else:
result = emotion(text)
mood = result[0]['label'].lower()
if mood not in mood_playlist:
mood = "neutral"
return jsonify({"mood": mood, "songs": mood_playlist[mood]})
if __name__ == "__main__":
app.run(debug=True)