mobenta commited on
Commit
58cc2af
·
verified ·
1 Parent(s): 6e08d8f

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +17 -8
app.py CHANGED
@@ -14,13 +14,13 @@ import logging
14
  logging.basicConfig(level=logging.DEBUG, format='%(asctime)s - %(levelname)s - %(message)s')
15
 
16
  # Function to search YouTube videos using yt-dlp for better reliability
17
- def youtube_search(query, max_results=50):
18
  ydl_opts = {
19
  'quiet': False, # Set to False to get more detailed output from yt-dlp
20
- 'extract_flat': 'in_playlist',
21
  'logger': logging.getLogger(), # Use the logging module to capture yt-dlp logs
22
  'simulate': True,
23
- 'noplaylist': True, # To avoid playlist entries
 
24
  }
25
  search_url = f"ytsearch{max_results}:{query}"
26
 
@@ -35,11 +35,18 @@ def youtube_search(query, max_results=50):
35
  logging.debug(f"Number of entries found: {len(result['entries'])}")
36
  for entry in result['entries']:
37
  video_id = entry.get('id')
38
- thumbnail_url = entry.get('thumbnail') if entry.get('thumbnail') else "https://via.placeholder.com/150"
 
39
  video_title = entry.get('title', "Unknown Title")
 
40
 
41
  if video_id:
42
- gallery_items.append((thumbnail_url, video_id, video_title))
 
 
 
 
 
43
  logging.debug(f"Added video: ID={video_id}, Thumbnail={thumbnail_url}, Title={video_title}")
44
  else:
45
  logging.debug(f"Missing video ID for entry: {entry}")
@@ -94,7 +101,7 @@ with gr.Blocks() as demo:
94
  with gr.Column(scale=3):
95
  search_query_input = gr.Textbox(label="Search YouTube", placeholder="Enter your search query here")
96
  search_button = gr.Button("Search")
97
- search_output = gr.Gallery(label="Search Results", columns=5, height="1500px")
98
  error_output = gr.Textbox(label="Error Message", interactive=False, visible=False)
99
 
100
  with gr.Column(scale=2):
@@ -107,8 +114,10 @@ with gr.Blocks() as demo:
107
  gallery_items, error_message = youtube_search(query)
108
  if error_message:
109
  return [], error_message, gr.update(visible=True)
110
- # Display videos even if the title or thumbnail is missing by using placeholders
111
- gallery_items_display = [(item[0], item[1]) for item in gallery_items] # Show thumbnail and video ID only
 
 
112
  return gallery_items_display, "", gr.update(visible=False)
113
 
114
  # Update the selected video link field when a video is clicked in the gallery
 
14
  logging.basicConfig(level=logging.DEBUG, format='%(asctime)s - %(levelname)s - %(message)s')
15
 
16
  # Function to search YouTube videos using yt-dlp for better reliability
17
+ def youtube_search(query, max_results=10):
18
  ydl_opts = {
19
  'quiet': False, # Set to False to get more detailed output from yt-dlp
 
20
  'logger': logging.getLogger(), # Use the logging module to capture yt-dlp logs
21
  'simulate': True,
22
+ 'noplaylist': True, # Avoid extracting playlists
23
+ 'format': 'best',
24
  }
25
  search_url = f"ytsearch{max_results}:{query}"
26
 
 
35
  logging.debug(f"Number of entries found: {len(result['entries'])}")
36
  for entry in result['entries']:
37
  video_id = entry.get('id')
38
+ # Fallback to YouTube static thumbnails if missing
39
+ thumbnail_url = entry.get('thumbnail') if entry.get('thumbnail') else f"https://img.youtube.com/vi/{video_id}/hqdefault.jpg"
40
  video_title = entry.get('title', "Unknown Title")
41
+ video_description = entry.get('description', "No description available.")
42
 
43
  if video_id:
44
+ gallery_items.append({
45
+ "thumbnail": thumbnail_url,
46
+ "video_id": video_id,
47
+ "title": video_title,
48
+ "description": video_description
49
+ })
50
  logging.debug(f"Added video: ID={video_id}, Thumbnail={thumbnail_url}, Title={video_title}")
51
  else:
52
  logging.debug(f"Missing video ID for entry: {entry}")
 
101
  with gr.Column(scale=3):
102
  search_query_input = gr.Textbox(label="Search YouTube", placeholder="Enter your search query here")
103
  search_button = gr.Button("Search")
104
+ search_output = gr.Gallery(label="Search Results", columns=2, height="1000px", elem_id="gallery")
105
  error_output = gr.Textbox(label="Error Message", interactive=False, visible=False)
106
 
107
  with gr.Column(scale=2):
 
114
  gallery_items, error_message = youtube_search(query)
115
  if error_message:
116
  return [], error_message, gr.update(visible=True)
117
+
118
+ # Prepare gallery items
119
+ gallery_items_display = [(item["thumbnail"], f"{item['title']}\n{item['description']}", item["video_id"]) for item in gallery_items]
120
+
121
  return gallery_items_display, "", gr.update(visible=False)
122
 
123
  # Update the selected video link field when a video is clicked in the gallery