chawitzoon's picture
Create app.py (#1)
bfd5f76 verified
Raw
History Blame
3.28 kB
import gradio as gr
import lyricsgenius
import os
from langdetect import detect
# Get the Genius API token from an environment variable
GENIUS_API_TOKEN = os.environ.get("GENIUS_API_TOKEN")
# Initialize the lyricsgenius client
genius = lyricsgenius.Genius(GENIUS_API_TOKEN, verbose=False, timeout=15)
def get_song_info(song_name: str, artist_name: str):
"""
Searches for a song's lyrics, determines the language, and gets a YouTube link.
Args:
song_name: The name of the song to search for.
artist_name: The name of the artist (optional).
Returns:
A tuple containing the song's original lyrics, a placeholder translation,
and the YouTube embed HTML.
"""
if not song_name:
return "Please enter a song name.", "", ""
# Search for the song using lyricsgenius
try:
# --- MODIFIED: Pass both song_name and artist_name to the search method ---
song = genius.search_song(song_name, artist_name, get_full_info=False)
if not song:
return "Song not found.", "", ""
except Exception as e:
return f"Error searching for song: {e}", "", ""
original_lyrics = song.lyrics
# Detect the language of the lyrics
try:
language = detect(original_lyrics)
except Exception:
language = "unknown"
# Placeholder for translation/romanization
translated_lyrics = ""
if language == "zh": # Chinese
translated_lyrics = "Pinyin placeholder and a translation here."
elif language == "ja": # Japanese
translated_lyrics = "Romanji placeholder and a translation here."
elif language == "en": # English
translated_lyrics = "Original lyrics are in English."
else:
translated_lyrics = f"Translation for language: {language}"
# Placeholder for YouTube embed
# In a real-world scenario, you would use a library to search for the song on YouTube.
youtube_url = "https://www.youtube.com/embed/dQw4w9WgXcQ" # A classic Rick Roll for now!
youtube_embed = f'<iframe width="560" height="315" src="{youtube_url}" frameborder="0" allowfullscreen></iframe>'
return original_lyrics, translated_lyrics, youtube_embed
# Define the Gradio interface
with gr.Blocks() as demo:
gr.Markdown("## Song Lyrics and Translation App")
gr.Markdown("Search for a song name to get its lyrics, translation, and a YouTube video.")
with gr.Row():
song_input = gr.Textbox(label="Enter Song Name", placeholder="e.g., Bohemian Rhapsody")
# --- NEW: Add a textbox for the artist name ---
artist_input = gr.Textbox(label="Enter Artist Name (Optional)", placeholder="e.g., Queen")
search_button = gr.Button("Search")
with gr.Column():
lyrics_output = gr.Textbox(label="Original Lyrics", lines=10)
translation_output = gr.Textbox(label="Translation / Romanization", lines=5)
video_embed_output = gr.HTML(label="YouTube Video")
search_button.click(
fn=get_song_info,
# --- MODIFIED: Pass both input components to the function ---
inputs=[song_input, artist_input],
outputs=[lyrics_output, translation_output, video_embed_output]
)
# Launch the Gradio app
if __name__ == "__main__":
demo.launch()