mobenta commited on
Commit
4488768
·
verified ·
1 Parent(s): 664eb2d

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +34 -51
app.py CHANGED
@@ -93,81 +93,64 @@ def show_video(video_url):
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
- html_code += f'''
139
- <div class="search-item" onclick="selectVideo('{video_id}')">
140
- <img src="{thumbnail_url}" alt="{title}">
141
- <div>
142
- <h3>{title}</h3>
143
- <p>{description}</p>
144
- </div>
145
- </div>
146
- '''
147
- html_code += '''
148
- <script>
149
- function getGradioApp() {
150
- const elems = document.getElementsByTagName('gradio-app');
151
- const gradioShadowRoot = elems.length == 0 ? null : elems[0].shadowRoot;
152
- return gradioShadowRoot ? gradioShadowRoot : document;
153
- }
154
-
155
- function selectVideo(video_id) {
156
- const gradioApp = getGradioApp();
157
- const textbox = gradioApp.querySelector('#selected_video_link textarea');
158
- textbox.value = 'https://www.youtube.com/watch?v=' + video_id;
159
- textbox.dispatchEvent(new Event('input', { bubbles: true }));
160
- }
161
- </script>
162
- '''
163
- html_code += '</div>'
164
- return html_code
165
-
166
- # Play the video when the Play Video button is clicked
167
  def play_video(video_url):
168
  return show_video(video_url)
169
 
170
- search_button.click(update_search_results, inputs=search_query_input, outputs=search_output)
 
171
  play_video_button.click(play_video, inputs=selected_video_link, outputs=video_output)
172
 
173
  # Launch the Gradio interface
 
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