mobenta commited on
Commit
1727a13
·
verified ·
1 Parent(s): 4488768

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +56 -34
app.py CHANGED
@@ -93,64 +93,86 @@ def show_video(video_url):
93
 
94
  # Create the Gradio interface
95
  with gr.Blocks(css="""
96
- #search_output img {
97
- height: 200px !important;
98
- object-fit: cover;
 
 
99
  }
100
- #search_output .label {
101
- font-size: 24px !important;
 
 
102
  }
103
- #search_output .gallery-item {
104
- display: flex !important;
105
- align-items: flex-start !important;
106
  }
107
- #search_output .gallery-caption {
108
- text-align: left !important;
109
- padding-left: 20px;
110
  }
111
  """) as demo:
112
  gr.Markdown("## YouTube Video Search, Selection, and Playback")
113
- video_ids_state = gr.State() # To store video IDs corresponding to the search results
114
 
115
  with gr.Row():
116
  with gr.Column(scale=3):
117
  search_query_input = gr.Textbox(label="Search YouTube", placeholder="Enter your search query here")
118
  search_button = gr.Button("Search")
119
- search_output = gr.Gallery(label="Search Results", columns=1, height="800px", elem_id="search_output")
120
 
121
  with gr.Column(scale=2):
122
- selected_video_link = gr.Textbox(label="Selected Video Link", interactive=False)
123
  play_video_button = gr.Button("Play Video")
124
  video_output = gr.HTML(label="Video Player")
125
 
126
- # Update the search results and store video IDs
127
  def update_search_results(query):
128
  search_results = youtube_search(query)
129
- gallery_items = []
130
- video_ids = []
131
  for item in search_results:
132
- image_url = item['thumbnail_url']
 
133
  title = item['title']
134
  description = item['description']
135
- # Adjust font sizes in caption
136
- caption = f"<div><h3 style='font-size:24px; margin: 0;'>{title}</h3><p style='font-size:18px; margin: 5px 0 0 0;'>{description}</p></div>"
137
- gallery_items.append((image_url, caption))
138
- video_ids.append(item['video_id'])
139
- return gallery_items, video_ids
140
-
141
- # When a video is selected
142
- def on_video_select(evt: gr.SelectData, video_ids):
143
- index = evt.index
144
- selected_video_id = video_ids[index]
145
- video_url = f"https://www.youtube.com/watch?v={selected_video_id}"
146
- return video_url
147
-
148
- # Play the video
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
149
  def play_video(video_url):
150
  return show_video(video_url)
151
 
152
- search_button.click(update_search_results, inputs=search_query_input, outputs=[search_output, video_ids_state])
153
- search_output.select(on_video_select, inputs=video_ids_state, outputs=selected_video_link)
154
  play_video_button.click(play_video, inputs=selected_video_link, outputs=video_output)
155
 
156
  # Launch the Gradio interface
 
93
 
94
  # Create the Gradio interface
95
  with gr.Blocks(css="""
96
+ .search-item {
97
+ display: flex;
98
+ align-items: flex-start;
99
+ margin-bottom: 30px;
100
+ cursor: pointer;
101
  }
102
+ .search-item img {
103
+ width: 300px;
104
+ height: auto;
105
+ margin-right: 20px;
106
  }
107
+ .search-item h3 {
108
+ font-size: 24px;
109
+ margin: 0 0 10px 0;
110
  }
111
+ .search-item p {
112
+ font-size: 18px;
113
+ margin: 0;
114
  }
115
  """) as demo:
116
  gr.Markdown("## YouTube Video Search, Selection, and Playback")
 
117
 
118
  with gr.Row():
119
  with gr.Column(scale=3):
120
  search_query_input = gr.Textbox(label="Search YouTube", placeholder="Enter your search query here")
121
  search_button = gr.Button("Search")
122
+ search_output = gr.HTML(label="Search Results")
123
 
124
  with gr.Column(scale=2):
125
+ selected_video_link = gr.Textbox(label="Selected Video Link", interactive=False, elem_id='selected_video_link')
126
  play_video_button = gr.Button("Play Video")
127
  video_output = gr.HTML(label="Video Player")
128
 
129
+ # Define search button behavior
130
  def update_search_results(query):
131
  search_results = youtube_search(query)
132
+ html_code = '<div>'
 
133
  for item in search_results:
134
+ video_id = item['video_id']
135
+ thumbnail_url = item['thumbnail_url']
136
  title = item['title']
137
  description = item['description']
138
+ # Use 'onclick' to call 'selectVideo' with the video ID
139
+ html_code += f'''
140
+ <div class="search-item" onclick="selectVideo('{video_id}')">
141
+ <img src="{thumbnail_url}" alt="{title}">
142
+ <div>
143
+ <h3>{title}</h3>
144
+ <p>{description}</p>
145
+ </div>
146
+ </div>
147
+ '''
148
+ html_code += '''
149
+ <script>
150
+ function getGradioApp() {
151
+ var elems = document.getElementsByTagName('gradio-app');
152
+ var gradioShadowRoot = elems.length == 0 ? null : elems[0].shadowRoot;
153
+ return gradioShadowRoot || document;
154
+ }
155
+
156
+ function selectVideo(video_id) {
157
+ var gradioApp = getGradioApp();
158
+ var textbox = gradioApp.querySelector('#selected_video_link textarea');
159
+ if (!textbox) {
160
+ console.error('Selected video link textbox not found');
161
+ return;
162
+ }
163
+ textbox.value = 'https://www.youtube.com/watch?v=' + video_id;
164
+ textbox.dispatchEvent(new Event('input', { bubbles: true }));
165
+ }
166
+ </script>
167
+ '''
168
+ html_code += '</div>'
169
+ return html_code
170
+
171
+ # Play the video when the Play Video button is clicked
172
  def play_video(video_url):
173
  return show_video(video_url)
174
 
175
+ search_button.click(update_search_results, inputs=search_query_input, outputs=search_output)
 
176
  play_video_button.click(play_video, inputs=selected_video_link, outputs=video_output)
177
 
178
  # Launch the Gradio interface