Diego-0121
commited on
Commit
•
08f7b1b
1
Parent(s):
3478d99
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,68 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from Recomendation import recommend_song_interface
|
2 |
+
import gradio as gr
|
3 |
+
import requests
|
4 |
+
|
5 |
+
|
6 |
+
|
7 |
+
def search_youtube(song, artist, api_key):
|
8 |
+
query = f"{song} by {artist}"
|
9 |
+
search_url = "https://www.googleapis.com/youtube/v3/search"
|
10 |
+
params = {
|
11 |
+
'part': 'snippet',
|
12 |
+
'q': query,
|
13 |
+
'type': 'video',
|
14 |
+
'maxResults': 1,
|
15 |
+
'key': api_key
|
16 |
+
}
|
17 |
+
|
18 |
+
response = requests.get(search_url, params=params)
|
19 |
+
response_json = response.json()
|
20 |
+
|
21 |
+
if 'items' in response_json and response_json['items']:
|
22 |
+
video_id = response_json['items'][0]['id']['videoId']
|
23 |
+
youtube_link = f"https://www.youtube.com/watch?v={video_id}"
|
24 |
+
return youtube_link
|
25 |
+
else:
|
26 |
+
return "No se encontraron resultados."
|
27 |
+
|
28 |
+
def add_youtube_links(recommendations, api_key):
|
29 |
+
recommendations_with_links = []
|
30 |
+
for recommendation in recommendations:
|
31 |
+
if recommendation: # Si la recomendación no es una cadena vacía
|
32 |
+
song, artist = recommendation.split(" by ")
|
33 |
+
youtube_link = search_youtube(song, artist, api_key)
|
34 |
+
recommendations_with_links.append(f"{recommendation} - YouTube Link: {youtube_link}")
|
35 |
+
else:
|
36 |
+
recommendations_with_links.append("")
|
37 |
+
|
38 |
+
return recommendations_with_links
|
39 |
+
|
40 |
+
def recommend_with_youtube_links(song_name, artist_name):
|
41 |
+
api_key = "AIzaSyAp-D7Mfafd6gJQo2gtAXRXwDlG8_uNXnU"
|
42 |
+
recommendations = recommend_song_interface(song_name, artist_name)
|
43 |
+
recommendations_with_links = add_youtube_links(recommendations, api_key)
|
44 |
+
return recommendations_with_links
|
45 |
+
|
46 |
+
# Configuración de la interfaz Gradio
|
47 |
+
iface = gr.Interface(
|
48 |
+
fn=recommend_with_youtube_links,
|
49 |
+
inputs=[
|
50 |
+
gr.Textbox(placeholder="Ingrese el título de la canción", label="Título de la Canción"),
|
51 |
+
gr.Textbox(placeholder="Ingrese el nombre del artista", label="Nombre del Artista")
|
52 |
+
],
|
53 |
+
outputs=[
|
54 |
+
gr.Text(label="Recomendación 1"),
|
55 |
+
gr.Text(label="Recomendación 2"),
|
56 |
+
gr.Text(label="Recomendación 3"),
|
57 |
+
gr.Text(label="Recomendación 4"),],
|
58 |
+
title="Recomendador de Canciones con Enlaces de YouTube",
|
59 |
+
description="Ingrese el título de una canción y el nombre del artista.",
|
60 |
+
theme="dark", # Comenta o elimina si el tema oscuro no está disponible
|
61 |
+
css="""
|
62 |
+
body {font-family: Arial, sans-serif;}
|
63 |
+
.input_text {background-color: #f0f0f0; border-radius: 5px;}
|
64 |
+
.output_text {border: 2px solid #f0f0f0; border-radius: 5px; padding: 10px;}
|
65 |
+
"""
|
66 |
+
)
|
67 |
+
|
68 |
+
iface.launch()
|