mobenta commited on
Commit
9ccb040
·
verified ·
1 Parent(s): 811e249

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +57 -38
app.py CHANGED
@@ -61,7 +61,7 @@ def youtube_search(query, max_results=50):
61
 
62
  except requests.exceptions.RequestException as e:
63
  print(f"Error during YouTube API request: {e}")
64
- return []
65
 
66
  # Function to display the video using the video URL
67
  def show_video(video_url):
@@ -92,60 +92,79 @@ def show_video(video_url):
92
  return html_code
93
 
94
  # Create the Gradio interface
95
- with gr.Blocks() as demo:
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
96
  gr.Markdown("## YouTube Video Search, Selection, and Playback")
 
97
 
98
  with gr.Row():
99
  with gr.Column(scale=3):
100
  search_query_input = gr.Textbox(label="Search YouTube", placeholder="Enter your search query here")
101
  search_button = gr.Button("Search")
102
- search_output = gr.HTML(label="Search Results")
103
 
104
  with gr.Column(scale=2):
105
- selected_video_link = gr.Textbox(label="Selected Video Link", interactive=True)
106
- play_button = gr.Button("Play Video")
107
  video_output = gr.HTML(label="Video Player")
108
 
109
- # Define search button behavior
110
  def update_search_results(query):
111
  search_results = youtube_search(query)
112
- html_code = '<div id="search-results">'
 
113
  for item in search_results:
114
- video_id = item['video_id']
115
- thumbnail_url = item['thumbnail_url']
116
  title = item['title']
117
  description = item['description']
118
- video_url = f"https://www.youtube.com/watch?v={video_id}"
119
- html_code += f'''
120
- <div class="search-item" onclick="selectVideo('{video_url}')">
121
- <img src="{thumbnail_url}" alt="{title}">
122
- <div>
123
- <h3>{title}</h3>
124
- <p>{description}</p>
125
- </div>
126
- </div>
127
- '''
128
- html_code += '''
129
- <script>
130
- function selectVideo(videoUrl) {
131
- const textbox = document.querySelector('input[data-testid="textbox"]');
132
- if (textbox) {
133
- textbox.value = videoUrl;
134
- textbox.dispatchEvent(new Event('input', { bubbles: true }));
135
- }
136
- }
137
- </script>
138
- '''
139
- html_code += '</div>'
140
- return html_code
141
-
142
- # Function to play the selected video
143
  def play_video(video_url):
144
  return show_video(video_url)
145
 
146
- # Set up event listeners
147
- search_button.click(update_search_results, inputs=search_query_input, outputs=search_output)
148
- play_button.click(play_video, inputs=selected_video_link, outputs=video_output)
149
 
150
  # Launch the Gradio interface
151
- demo.launch()
 
61
 
62
  except requests.exceptions.RequestException as e:
63
  print(f"Error during YouTube API request: {e}")
64
+ return [], f"Error retrieving video results: {str(e)}"
65
 
66
  # Function to display the video using the video URL
67
  def show_video(video_url):
 
92
  return html_code
93
 
94
  # Create the Gradio interface
95
+ with gr.Blocks(css="""
96
+ #search_output img {
97
+ width: 300px !important;
98
+ height: auto !important;
99
+ margin-right: 20px;
100
+ }
101
+ #search_output .label {
102
+ font-size: 24px !important;
103
+ }
104
+ #search_output .gallery-item {
105
+ display: flex !important;
106
+ align-items: flex-start !important;
107
+ margin-bottom: 30px !important;
108
+ }
109
+ #search_output .gallery-caption {
110
+ text-align: left !important;
111
+ padding-left: 20px;
112
+ }
113
+ #search_output .gallery-item div {
114
+ display: flex;
115
+ flex-direction: column;
116
+ }
117
+ #search_output .gallery-item h3 {
118
+ font-size: 24px;
119
+ margin: 0 0 10px 0;
120
+ }
121
+ #search_output .gallery-item p {
122
+ font-size: 18px;
123
+ margin: 0;
124
+ }
125
+ """) as demo:
126
  gr.Markdown("## YouTube Video Search, Selection, and Playback")
127
+ video_ids_state = gr.State() # To store video IDs corresponding to the search results
128
 
129
  with gr.Row():
130
  with gr.Column(scale=3):
131
  search_query_input = gr.Textbox(label="Search YouTube", placeholder="Enter your search query here")
132
  search_button = gr.Button("Search")
133
+ search_output = gr.Gallery(label="Search Results", columns=1, height="800px", elem_id="search_output")
134
 
135
  with gr.Column(scale=2):
136
+ selected_video_link = gr.Textbox(label="Selected Video Link", interactive=False)
137
+ play_video_button = gr.Button("Play Video")
138
  video_output = gr.HTML(label="Video Player")
139
 
140
+ # Update the search results and store video IDs
141
  def update_search_results(query):
142
  search_results = youtube_search(query)
143
+ gallery_items = []
144
+ video_ids = []
145
  for item in search_results:
146
+ image_url = item['thumbnail_url']
 
147
  title = item['title']
148
  description = item['description']
149
+ caption = f"<div><h3>{title}</h3><p>{description}</p></div>"
150
+ gallery_items.append((image_url, caption))
151
+ video_ids.append(item['video_id'])
152
+ return gallery_items, video_ids
153
+
154
+ # When a video is selected
155
+ def on_video_select(evt: gr.SelectData, video_ids):
156
+ index = evt.index
157
+ selected_video_id = video_ids[index]
158
+ video_url = f"https://www.youtube.com/watch?v={selected_video_id}"
159
+ return video_url
160
+
161
+ # Play the video
 
 
 
 
 
 
 
 
 
 
 
 
162
  def play_video(video_url):
163
  return show_video(video_url)
164
 
165
+ search_button.click(update_search_results, inputs=search_query_input, outputs=[search_output, video_ids_state])
166
+ search_output.select(on_video_select, inputs=video_ids_state, outputs=selected_video_link)
167
+ play_video_button.click(play_video, inputs=selected_video_link, outputs=video_output)
168
 
169
  # Launch the Gradio interface
170
+ demo.launch()