Spaces:
Paused
Paused
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,106 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
from ytmusicapi import YTMusic
|
| 3 |
+
from typing import List, Dict, Optional
|
| 4 |
+
|
| 5 |
+
class SearchResponse:
|
| 6 |
+
def __init__(self, title: str, duration: str, video_id: str, artists: List[str], album: Dict[str, str],
|
| 7 |
+
youtube_music_url: str, youtube_video_url: str, song_thumbnail_url: str, album_thumbnail_url: Optional[str]):
|
| 8 |
+
self.title = title
|
| 9 |
+
self.duration = duration
|
| 10 |
+
self.video_id = video_id
|
| 11 |
+
self.artists = artists
|
| 12 |
+
self.album = album
|
| 13 |
+
self.youtube_music_url = youtube_music_url
|
| 14 |
+
self.youtube_video_url = youtube_video_url
|
| 15 |
+
self.song_thumbnail_url = song_thumbnail_url
|
| 16 |
+
self.album_thumbnail_url = album_thumbnail_url
|
| 17 |
+
|
| 18 |
+
def __repr__(self):
|
| 19 |
+
return f"SearchResponse(title={self.title}, duration={self.duration}, artists={self.artists})"
|
| 20 |
+
|
| 21 |
+
def search_songs(query: str, filter_type: str = 'songs', limit: int = 5) -> List[SearchResponse]:
|
| 22 |
+
try:
|
| 23 |
+
ytmusic = YTMusic()
|
| 24 |
+
search_results = ytmusic.search(query, filter=filter_type, limit=limit)
|
| 25 |
+
|
| 26 |
+
if not search_results:
|
| 27 |
+
raise ValueError("No results found")
|
| 28 |
+
|
| 29 |
+
enriched_results = []
|
| 30 |
+
|
| 31 |
+
for song in search_results:
|
| 32 |
+
video_id = song.get('videoId', 'N/A')
|
| 33 |
+
artists = song.get('artists', [])
|
| 34 |
+
artist_names = [artist['name'] for artist in artists] if artists else ['N/A']
|
| 35 |
+
album = song.get('album', {})
|
| 36 |
+
album_id = album.get('id', None)
|
| 37 |
+
album_data = {
|
| 38 |
+
'name': album.get('name', 'N/A'),
|
| 39 |
+
'id': album_id,
|
| 40 |
+
'url': f"https://music.youtube.com/browse/{album_id}" if album_id else "N/A"
|
| 41 |
+
}
|
| 42 |
+
album_thumbnail_url = None
|
| 43 |
+
if album_id:
|
| 44 |
+
try:
|
| 45 |
+
album_details = ytmusic.get_album(album_id)
|
| 46 |
+
album_thumbnail_url = album_details.get('thumbnails', [{}])[-1].get('url', None)
|
| 47 |
+
except Exception:
|
| 48 |
+
pass
|
| 49 |
+
youtube_music_url = f"https://music.youtube.com/watch?v={video_id}" if video_id != 'N/A' else 'N/A'
|
| 50 |
+
youtube_video_url = f"https://www.youtube.com/watch?v={video_id}" if video_id != 'N/A' else 'N/A'
|
| 51 |
+
song_thumbnail_url = song.get('thumbnails', [{}])[-1].get('url', 'N/A')
|
| 52 |
+
result = SearchResponse(
|
| 53 |
+
title=song.get('title', 'N/A'),
|
| 54 |
+
duration=song.get('duration', 'N/A'),
|
| 55 |
+
video_id=video_id,
|
| 56 |
+
artists=artist_names,
|
| 57 |
+
album=album_data,
|
| 58 |
+
youtube_music_url=youtube_music_url,
|
| 59 |
+
youtube_video_url=youtube_video_url,
|
| 60 |
+
song_thumbnail_url=song_thumbnail_url,
|
| 61 |
+
album_thumbnail_url=album_thumbnail_url
|
| 62 |
+
)
|
| 63 |
+
enriched_results.append(result)
|
| 64 |
+
|
| 65 |
+
return enriched_results
|
| 66 |
+
|
| 67 |
+
except Exception as e:
|
| 68 |
+
print(f"Error: {e}")
|
| 69 |
+
return []
|
| 70 |
+
|
| 71 |
+
def display_results(query):
|
| 72 |
+
results = search_songs(query)
|
| 73 |
+
if not results:
|
| 74 |
+
return "No results found."
|
| 75 |
+
|
| 76 |
+
song_info = []
|
| 77 |
+
artist_info = set()
|
| 78 |
+
album_info = set()
|
| 79 |
+
|
| 80 |
+
for result in results:
|
| 81 |
+
song_info.append(f"**Title:** [{result.title}]({result.youtube_music_url}) \n"
|
| 82 |
+
f"**Duration:** {result.duration} \n"
|
| 83 |
+
f"**Artists:** {', '.join(result.artists)} \n"
|
| 84 |
+
f"**Album:** [{result.album['name']}]({result.album['url']}) \n"
|
| 85 |
+
f" \n")
|
| 86 |
+
artist_info.update(result.artists)
|
| 87 |
+
album_info.add(result.album['name'])
|
| 88 |
+
|
| 89 |
+
artist_info = '\n'.join(f"- {artist}" for artist in artist_info)
|
| 90 |
+
album_info = '\n'.join(f"- {album}" for album in album_info)
|
| 91 |
+
|
| 92 |
+
return f"### Songs:\n{''.join(song_info)}\n\n### Artists:\n{artist_info}\n\n### Albums:\n{album_info}"
|
| 93 |
+
|
| 94 |
+
with gr.Blocks() as demo:
|
| 95 |
+
gr.Markdown("# YouTube Music Search")
|
| 96 |
+
with gr.Row():
|
| 97 |
+
with gr.Column():
|
| 98 |
+
query = gr.Textbox(label="Search Query")
|
| 99 |
+
search_button = gr.Button("Search")
|
| 100 |
+
with gr.Column():
|
| 101 |
+
output = gr.Markdown()
|
| 102 |
+
|
| 103 |
+
search_button.click(fn=display_results, inputs=query, outputs=output)
|
| 104 |
+
|
| 105 |
+
if __name__ == "__main__":
|
| 106 |
+
demo.launch()
|