-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgpt.py
More file actions
86 lines (71 loc) · 3.3 KB
/
Copy pathgpt.py
File metadata and controls
86 lines (71 loc) · 3.3 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
import openai
import json
import os
from dotenv import load_dotenv
# Load environment variables from .env file
load_dotenv()
# Get the API key from environment variables
API_KEY = os.getenv("KEY")
openai.api_key = API_KEY
# Store OpenAI client instance
openai_client = openai
# Store previously recommended songs for each emotion
previous_recommendations = {}
# Define available emotions
emotions = [
"Mildly Positive & Confident", "Slightly Positive but Hesitant", "Calm & Neutral",
"Relaxed but Withdrawn", "Frustrated but Assertive", "Stressed & Overwhelmed",
"Indifferent & Passive", "Sad & Low Energy"
]
def get_song_recommendation(emotion):
if emotion not in emotions:
return "Invalid emotion. Please provide a valid emotion from the list."
prompt = (
f'Generate a list of exactly 10 songs that match the emotion: "{emotion}". '
"Each song must be suitable for a semi-professional setting. "
"Ensure that all 10 slots are filled. Format your response strictly as follows:\n\n"
"1. Song, Singer\n"
"2. Song, Singer\n"
"3. Song, Singer\n"
"4. Song, Singer\n"
"5. Song, Singer\n"
"6. Song, Singer\n"
"7. Song, Singer\n"
"8. Song, Singer\n"
"9. Song, Singer\n"
"10. Song, Singer\n\n"
"Provide no additional text, explanations, or numbering deviations. "
"Do not return fewer than 10 songs under any circumstances."
)
response = openai_client.ChatCompletion.create(
model="gpt-4o-mini",
messages=[{"role": "system", "content": "You are a helpful music recommendation assistant."},
{"role": "user", "content": prompt}],
max_tokens=200 # Increase to ensure full output
)
song_list = response["choices"][0]["message"]["content"].strip().split("\n")
song_list = [song.strip().split(". ", 1)[-1] for song in song_list] # Remove numbering
# Ensure no duplicate song recommendations for the same emotion
if emotion not in previous_recommendations:
previous_recommendations[emotion] = set()
# Filter out previously recommended songs
new_songs = [song for song in song_list if song not in previous_recommendations[emotion]]
# If all songs are duplicates, regenerate
while len(new_songs) == 0:
response = openai_client.ChatCompletion.create(
model="gpt-4o-mini",
messages=[{"role": "system", "content": "You are a helpful music recommendation assistant."},
{"role": "user", "content": prompt}],
max_tokens=200
)
song_list = response["choices"][0]["message"]["content"].strip().split("\n")
song_list = [song.strip().split(". ", 1)[-1] for song in song_list]
new_songs = [song for song in song_list if song not in previous_recommendations[emotion]]
# Add new songs to the recommendation history
previous_recommendations[emotion].update(new_songs)
return new_songs # Returns the list of songs instead of one
# Example usage
if __name__ == "__main__":
user_emotion = "Mildly Positive & Confident" # Change this to test other emotions
recommendations = get_song_recommendation(user_emotion)
print("\n".join(recommendations))