Harmonize / app.py
Diego-0121's picture
Create app.py
08f7b1b
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 = "AIzaSyAp-D7Mfafd6gJQo2gtAXRXwDlG8_uNXnU"
recommendations = recommend_song_interface(song_name, artist_name)
recommendations_with_links = add_youtube_links(recommendations, api_key)
return recommendations_with_links
# 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.Text(label="Recomendaci贸n 1"),
gr.Text(label="Recomendaci贸n 2"),
gr.Text(label="Recomendaci贸n 3"),
gr.Text(label="Recomendaci贸n 4"),],
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()