Spaces:
Runtime error
Runtime error
Create app.py
#1
by chawitzoon - opened
app.py
ADDED
|
@@ -0,0 +1,87 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
import lyricsgenius
|
| 3 |
+
import os
|
| 4 |
+
from langdetect import detect
|
| 5 |
+
|
| 6 |
+
# Get the Genius API token from an environment variable
|
| 7 |
+
GENIUS_API_TOKEN = os.environ.get("GENIUS_API_TOKEN")
|
| 8 |
+
|
| 9 |
+
# Initialize the lyricsgenius client
|
| 10 |
+
genius = lyricsgenius.Genius(GENIUS_API_TOKEN, verbose=False, timeout=15)
|
| 11 |
+
|
| 12 |
+
def get_song_info(song_name: str, artist_name: str):
|
| 13 |
+
"""
|
| 14 |
+
Searches for a song's lyrics, determines the language, and gets a YouTube link.
|
| 15 |
+
|
| 16 |
+
Args:
|
| 17 |
+
song_name: The name of the song to search for.
|
| 18 |
+
artist_name: The name of the artist (optional).
|
| 19 |
+
|
| 20 |
+
Returns:
|
| 21 |
+
A tuple containing the song's original lyrics, a placeholder translation,
|
| 22 |
+
and the YouTube embed HTML.
|
| 23 |
+
"""
|
| 24 |
+
if not song_name:
|
| 25 |
+
return "Please enter a song name.", "", ""
|
| 26 |
+
|
| 27 |
+
# Search for the song using lyricsgenius
|
| 28 |
+
try:
|
| 29 |
+
# --- MODIFIED: Pass both song_name and artist_name to the search method ---
|
| 30 |
+
song = genius.search_song(song_name, artist_name, get_full_info=False)
|
| 31 |
+
if not song:
|
| 32 |
+
return "Song not found.", "", ""
|
| 33 |
+
except Exception as e:
|
| 34 |
+
return f"Error searching for song: {e}", "", ""
|
| 35 |
+
|
| 36 |
+
original_lyrics = song.lyrics
|
| 37 |
+
|
| 38 |
+
# Detect the language of the lyrics
|
| 39 |
+
try:
|
| 40 |
+
language = detect(original_lyrics)
|
| 41 |
+
except Exception:
|
| 42 |
+
language = "unknown"
|
| 43 |
+
|
| 44 |
+
# Placeholder for translation/romanization
|
| 45 |
+
translated_lyrics = ""
|
| 46 |
+
if language == "zh": # Chinese
|
| 47 |
+
translated_lyrics = "Pinyin placeholder and a translation here."
|
| 48 |
+
elif language == "ja": # Japanese
|
| 49 |
+
translated_lyrics = "Romanji placeholder and a translation here."
|
| 50 |
+
elif language == "en": # English
|
| 51 |
+
translated_lyrics = "Original lyrics are in English."
|
| 52 |
+
else:
|
| 53 |
+
translated_lyrics = f"Translation for language: {language}"
|
| 54 |
+
|
| 55 |
+
# Placeholder for YouTube embed
|
| 56 |
+
# In a real-world scenario, you would use a library to search for the song on YouTube.
|
| 57 |
+
youtube_url = "https://www.youtube.com/embed/dQw4w9WgXcQ" # A classic Rick Roll for now!
|
| 58 |
+
youtube_embed = f'<iframe width="560" height="315" src="{youtube_url}" frameborder="0" allowfullscreen></iframe>'
|
| 59 |
+
|
| 60 |
+
return original_lyrics, translated_lyrics, youtube_embed
|
| 61 |
+
|
| 62 |
+
# Define the Gradio interface
|
| 63 |
+
with gr.Blocks() as demo:
|
| 64 |
+
gr.Markdown("## Song Lyrics and Translation App")
|
| 65 |
+
gr.Markdown("Search for a song name to get its lyrics, translation, and a YouTube video.")
|
| 66 |
+
|
| 67 |
+
with gr.Row():
|
| 68 |
+
song_input = gr.Textbox(label="Enter Song Name", placeholder="e.g., Bohemian Rhapsody")
|
| 69 |
+
# --- NEW: Add a textbox for the artist name ---
|
| 70 |
+
artist_input = gr.Textbox(label="Enter Artist Name (Optional)", placeholder="e.g., Queen")
|
| 71 |
+
search_button = gr.Button("Search")
|
| 72 |
+
|
| 73 |
+
with gr.Column():
|
| 74 |
+
lyrics_output = gr.Textbox(label="Original Lyrics", lines=10)
|
| 75 |
+
translation_output = gr.Textbox(label="Translation / Romanization", lines=5)
|
| 76 |
+
video_embed_output = gr.HTML(label="YouTube Video")
|
| 77 |
+
|
| 78 |
+
search_button.click(
|
| 79 |
+
fn=get_song_info,
|
| 80 |
+
# --- MODIFIED: Pass both input components to the function ---
|
| 81 |
+
inputs=[song_input, artist_input],
|
| 82 |
+
outputs=[lyrics_output, translation_output, video_embed_output]
|
| 83 |
+
)
|
| 84 |
+
|
| 85 |
+
# Launch the Gradio app
|
| 86 |
+
if __name__ == "__main__":
|
| 87 |
+
demo.launch()
|