|
import gradio as gr
|
|
import requests
|
|
|
|
|
|
YOUTUBE_API_KEY = "AIzaSyCEZFjFEuqBsFDi0CV12BkFJgGl7Lo6qkQ"
|
|
|
|
def recommend_music(emotion):
|
|
youtube_url = "https://www.googleapis.com/youtube/v3/search"
|
|
params = {
|
|
"part": "snippet",
|
|
"q": f"{emotion} songs",
|
|
"type": "video",
|
|
"maxResults": 4,
|
|
"key": YOUTUBE_API_KEY
|
|
}
|
|
|
|
response = requests.get(youtube_url, params=params)
|
|
if response.status_code == 200:
|
|
items = response.json().get("items", [])
|
|
results = ""
|
|
for item in items:
|
|
video_id = item["id"]["videoId"]
|
|
title = item["snippet"]["title"]
|
|
results += f"<p><a href='https://www.youtube.com/watch?v={video_id}' target='_blank'>{title}</a></p>"
|
|
return results
|
|
else:
|
|
return "Failed to fetch music recommendations."
|
|
|
|
iface = gr.Interface(
|
|
fn=recommend_music,
|
|
inputs=gr.Textbox(label="Detected Emotion"),
|
|
outputs=gr.HTML(label="Recommended Songs"),
|
|
title="πΆ Mood Based Music Recommender",
|
|
description="Input your detected emotion from webcam to get song recommendations instantly π"
|
|
)
|
|
|
|
if __name__ == "__main__":
|
|
iface.launch()
|
|
|