mobenta commited on
Commit
2bb6477
·
verified ·
1 Parent(s): 0b253e1

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +9 -5
app.py CHANGED
@@ -102,9 +102,13 @@ def youtube_api_search(query, max_results=1000):
102
  # Update params with the nextPageToken to get the next batch of results
103
  params['pageToken'] = response.json()['nextPageToken']
104
 
105
- # Create a list of tuples with thumbnail URL and video ID as the caption
106
  gallery_items = [
107
- (result["snippet"]["thumbnails"]["medium"]["url"], result["id"]["videoId"]) for result in all_results
 
 
 
 
108
  ]
109
 
110
  return gallery_items
@@ -172,14 +176,14 @@ with gr.Blocks() as demo:
172
  gallery_items = youtube_api_search(query)
173
  if error_message:
174
  return [], error_message, gr.update(visible=True)
175
- # Display videos even if the title or thumbnail is missing by using placeholders
176
- gallery_items_display = [(item[0], item[1]) for item in gallery_items] # Show thumbnail and video ID only
177
  return gallery_items_display, "", gr.update(visible=False)
178
 
179
  # Update the selected video link field when a video is clicked in the gallery
180
  def on_video_select(evt: gr.SelectData):
181
  # Extract the video ID from the event value, which is a dictionary containing details of the selected item
182
- selected_video_id = evt.value["caption"]
183
  video_url = f"https://www.youtube.com/watch?v={selected_video_id}"
184
  logging.debug(f"Video selected: {video_url}")
185
  return video_url
 
102
  # Update params with the nextPageToken to get the next batch of results
103
  params['pageToken'] = response.json()['nextPageToken']
104
 
105
+ # Create a list of tuples with thumbnail URL, video ID, and video title
106
  gallery_items = [
107
+ (
108
+ result["snippet"].get("thumbnails", {}).get("medium", {}).get("url", "https://via.placeholder.com/150"),
109
+ result["id"]["videoId"],
110
+ result["snippet"].get("title", "No title available")
111
+ ) for result in all_results
112
  ]
113
 
114
  return gallery_items
 
176
  gallery_items = youtube_api_search(query)
177
  if error_message:
178
  return [], error_message, gr.update(visible=True)
179
+ # Display videos with thumbnails, video IDs, and titles
180
+ gallery_items_display = [(item[0], f"{item[2]} ({item[1]})") for item in gallery_items]
181
  return gallery_items_display, "", gr.update(visible=False)
182
 
183
  # Update the selected video link field when a video is clicked in the gallery
184
  def on_video_select(evt: gr.SelectData):
185
  # Extract the video ID from the event value, which is a dictionary containing details of the selected item
186
+ selected_video_id = evt.value["caption"].split('(')[-1][:-1] # Extract video ID from caption
187
  video_url = f"https://www.youtube.com/watch?v={selected_video_id}"
188
  logging.debug(f"Video selected: {video_url}")
189
  return video_url