Harmonize / app.py
Diego-0121's picture
Update app.py
add24aa
raw
history blame
2.83 kB
from Recomendation import recommend_song_interface
import gradio as gr
import requests
def search_youtube(song, artist, api_key):
query = f"{song} by {artist}"
search_url = "https://www.googleapis.com/youtube/v3/search"
params = {
'part': 'snippet',
'q': query,
'type': 'video',
'maxResults': 1,
'key': api_key
}
response = requests.get(search_url, params=params)
response_json = response.json()
if 'items' in response_json and response_json['items']:
video_id = response_json['items'][0]['id']['videoId']
youtube_link = f"https://www.youtube.com/watch?v={video_id}"
return youtube_link
else:
return "No se encontraron resultados."
def add_youtube_links(recommendations, api_key):
recommendations_with_links = []
for recommendation in recommendations:
if recommendation: # Si la recomendaci贸n no es una cadena vac铆a
song, artist = recommendation.split(" by ")
youtube_link = search_youtube(song, artist, api_key)
recommendations_with_links.append(f"{recommendation} - YouTube Link: {youtube_link}")
else:
recommendations_with_links.append("")
return recommendations_with_links
def recommend_with_youtube_links(song_name, artist_name):
api_key = "AIzaSyAnFiRh8g13HW_wLhUW7wZwRE2SsPo0aJs"
recommendations = recommend_song_interface(song_name, artist_name)
recommendations_with_links = add_youtube_links(recommendations, api_key)
html_links = [
f"<div style='margin: 10px; padding: 10px; border: 1px solid #ccc; border-radius: 5px;'>"
f"{rec}<br><a href='{link}' target='_blank'>YouTube Link</a></div>"
if rec and link.startswith('http') else ""
for rec, link in recommendations_with_links
]
# Aseg煤rate de devolver la cantidad correcta de salidas esperadas por la interfaz Gradio
while len(html_links) < 4:
html_links.append("")
return "<br>".join(html_links[:4])
# Configuraci贸n de la interfaz Gradio
iface = gr.Interface(
fn=recommend_with_youtube_links,
inputs=[
gr.Textbox(placeholder="Ingrese el t铆tulo de la canci贸n", label="T铆tulo de la Canci贸n"),
gr.Textbox(placeholder="Ingrese el nombre del artista", label="Nombre del Artista")
],
outputs= gr.HTML(),
title="Recomendador de Canciones con Enlaces de YouTube",
description="Ingrese el t铆tulo de una canci贸n y el nombre del artista.",
theme="dark", # Comenta o elimina si el tema oscuro no est谩 disponible
css="""
body {font-family: Arial, sans-serif;}
.input_text {background-color: #f0f0f0; border-radius: 5px;}
.output_text {border: 2px solid #f0f0f0; border-radius: 5px; padding: 10px;}
"""
)
iface.launch()