Skip to content

Commit

Permalink
Update back.py
Browse files Browse the repository at this point in the history
poojaharihar03 authored Jun 20, 2024
1 parent c44cf18 commit 6ce457e
Showing 1 changed file with 5 additions and 4 deletions.
9 changes: 5 additions & 4 deletions emotion-based music recommendtion/back.py
Original file line number Diff line number Diff line change
@@ -4,6 +4,8 @@
# YouTube API credentials
api_key = 'Your- API- Key'


# Initialize emotion classifier
model_name = 'bhadresh-savani/bert-base-go-emotion'
model = AutoModelForSequenceClassification.from_pretrained(model_name, from_tf=False)
tokenizer = AutoTokenizer.from_pretrained(model_name)
@@ -29,7 +31,7 @@ def map_emotion_to_query(emotion):
else:
return 'relaxing music'

def get_youtube_recommendations(query, language=None, order='relevance'):
def get_youtube_recommendations(query, language=None):
youtube = googleapiclient.discovery.build('youtube', 'v3', developerKey=api_key)
if language and language.lower() != 'i am feeling lucky':
query = f"{query} {language} songs"
@@ -39,8 +41,7 @@ def get_youtube_recommendations(query, language=None, order='relevance'):
q=query,
type='video',
videoDuration='medium',
videoCategoryId='10',
order=order
videoCategoryId='10'
)
search_response = search_request.execute()
video_ids = [item['id']['videoId'] for item in search_response['items']]
@@ -51,4 +52,4 @@ def get_youtube_recommendations(query, language=None, order='relevance'):
video_response = video_request.execute()
videos = sorted(video_response['items'], key=lambda x: int(x['statistics'].get('likeCount', 0)), reverse=True)
top_videos = [f"{item['snippet']['title']} (https://www.youtube.com/watch?v={item['id']}) with {item['statistics'].get('likeCount', 0)} likes" for item in videos[:5]]
return top_videos if top_videos else ["No recommendations available for this emotion and language"]
return top_videos if top_videos else ["No recommendations available for this emotion and language"]

0 comments on commit 6ce457e

Please sign in to comment.