mobenta commited on
Commit
c8f5c25
·
verified ·
1 Parent(s): d6d36d8

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +11 -53
app.py CHANGED
@@ -92,27 +92,7 @@ def show_video(video_url):
92
  return html_code
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():
@@ -122,21 +102,20 @@ with gr.Blocks(css="""
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>
@@ -144,38 +123,17 @@ with gr.Blocks(css="""
144
  </div>
145
  </div>
146
  '''
147
- html_code += '''
148
- <script>
149
- function getGradioApp() {
150
- const gradioApp = document.querySelector('gradio-app');
151
- if (gradioApp.shadowRoot) {
152
- return gradioApp.shadowRoot;
153
- } else {
154
- return document;
155
- }
156
- }
157
-
158
- function selectVideo(video_id) {
159
- const gradioApp = getGradioApp();
160
- const textbox = gradioApp.querySelector('#selected_video_link textarea');
161
- if (!textbox) {
162
- console.error('Selected video link textbox not found');
163
- return;
164
- }
165
- textbox.value = 'https://www.youtube.com/watch?v=' + video_id;
166
- textbox.dispatchEvent(new Event('input', { bubbles: true }));
167
- }
168
- </script>
169
- '''
170
  html_code += '</div>'
171
  return html_code
172
 
173
- # Play the video when the Play Video button is clicked
174
- def play_video(video_url):
175
- return show_video(video_url)
 
176
 
 
177
  search_button.click(update_search_results, inputs=search_query_input, outputs=search_output)
178
- play_video_button.click(play_video, inputs=selected_video_link, outputs=video_output)
179
 
180
  # Launch the Gradio interface
181
- demo.launch()
 
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():
 
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=False)
 
106
  video_output = gr.HTML(label="Video Player")
107
 
108
  # Define search button behavior
109
  def update_search_results(query):
110
  search_results = youtube_search(query)
111
+ html_code = '<div id="search-results">'
112
  for item in search_results:
113
  video_id = item['video_id']
114
  thumbnail_url = item['thumbnail_url']
115
  title = item['title']
116
  description = item['description']
117
  html_code += f'''
118
+ <div class="search-item" data-video-id="{video_id}">
119
  <img src="{thumbnail_url}" alt="{title}">
120
  <div>
121
  <h3>{title}</h3>
 
123
  </div>
124
  </div>
125
  '''
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
126
  html_code += '</div>'
127
  return html_code
128
 
129
+ # Function to handle video selection and playback
130
+ def select_and_play_video(video_id: str):
131
+ video_url = f"https://www.youtube.com/watch?v={video_id}"
132
+ return video_url, show_video(video_url)
133
 
134
+ # Set up event listeners
135
  search_button.click(update_search_results, inputs=search_query_input, outputs=search_output)
136
+ search_output.select(select_and_play_video, inputs=gr.Textbox(visible=False), outputs=[selected_video_link, video_output])
137
 
138
  # Launch the Gradio interface
139
+ demo.launch()