Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -102,55 +102,46 @@ def show_video(video_url):
|
|
102 |
# Create the Gradio interface
|
103 |
with gr.Blocks() as demo:
|
104 |
gr.Markdown("## YouTube Video Search, Selection, and Playback")
|
|
|
105 |
|
106 |
with gr.Row():
|
107 |
with gr.Column(scale=3):
|
108 |
search_query_input = gr.Textbox(label="Search YouTube", placeholder="Enter your search query here")
|
109 |
search_button = gr.Button("Search")
|
110 |
-
search_output = gr.
|
111 |
-
|
112 |
with gr.Column(scale=2):
|
113 |
-
selected_video_link = gr.Textbox(label="Selected Video Link", interactive=False
|
114 |
play_video_button = gr.Button("Play Video")
|
115 |
video_output = gr.HTML(label="Video Player")
|
116 |
|
117 |
-
#
|
118 |
def update_search_results(query):
|
119 |
search_results = youtube_search(query)
|
120 |
-
|
|
|
121 |
for item in search_results:
|
122 |
-
|
123 |
-
thumbnail_url = item['thumbnail_url']
|
124 |
title = item['title']
|
125 |
description = item['description']
|
126 |
-
|
127 |
-
|
128 |
-
|
129 |
-
|
130 |
-
|
131 |
-
|
132 |
-
|
133 |
-
|
134 |
-
|
135 |
-
|
136 |
-
|
137 |
-
|
138 |
-
|
139 |
-
const gradioApp = document.getElementsByTagName('gradio-app')[0].shadowRoot || document;
|
140 |
-
const textbox = gradioApp.querySelector('#selected_video_link textarea');
|
141 |
-
textbox.value = 'https://www.youtube.com/watch?v=' + video_id;
|
142 |
-
textbox.dispatchEvent(new Event('input', { bubbles: true }));
|
143 |
-
}
|
144 |
-
</script>
|
145 |
-
'''
|
146 |
-
html_code += '</div>'
|
147 |
-
return html_code
|
148 |
-
|
149 |
-
# Play the video when the Play Video button is clicked
|
150 |
def play_video(video_url):
|
151 |
return show_video(video_url)
|
152 |
|
153 |
-
search_button.click(update_search_results, inputs=search_query_input, outputs=search_output)
|
|
|
154 |
play_video_button.click(play_video, inputs=selected_video_link, outputs=video_output)
|
155 |
|
156 |
# Launch the Gradio interface
|
|
|
102 |
# Create the Gradio interface
|
103 |
with gr.Blocks() as demo:
|
104 |
gr.Markdown("## YouTube Video Search, Selection, and Playback")
|
105 |
+
video_ids_state = gr.State() # To store video IDs corresponding to the search results
|
106 |
|
107 |
with gr.Row():
|
108 |
with gr.Column(scale=3):
|
109 |
search_query_input = gr.Textbox(label="Search YouTube", placeholder="Enter your search query here")
|
110 |
search_button = gr.Button("Search")
|
111 |
+
search_output = gr.Gallery(label="Search Results", columns=1, height="800px")
|
112 |
+
|
113 |
with gr.Column(scale=2):
|
114 |
+
selected_video_link = gr.Textbox(label="Selected Video Link", interactive=False)
|
115 |
play_video_button = gr.Button("Play Video")
|
116 |
video_output = gr.HTML(label="Video Player")
|
117 |
|
118 |
+
# Update the search results and store video IDs
|
119 |
def update_search_results(query):
|
120 |
search_results = youtube_search(query)
|
121 |
+
gallery_items = []
|
122 |
+
video_ids = []
|
123 |
for item in search_results:
|
124 |
+
image_url = item['thumbnail_url']
|
|
|
125 |
title = item['title']
|
126 |
description = item['description']
|
127 |
+
caption = f"{title}\n{description}"
|
128 |
+
gallery_items.append((image_url, caption))
|
129 |
+
video_ids.append(item['video_id'])
|
130 |
+
return gallery_items, video_ids
|
131 |
+
|
132 |
+
# When a video is selected
|
133 |
+
def on_video_select(evt: gr.SelectData, video_ids):
|
134 |
+
index = evt.index
|
135 |
+
selected_video_id = video_ids[index]
|
136 |
+
video_url = f"https://www.youtube.com/watch?v={selected_video_id}"
|
137 |
+
return video_url
|
138 |
+
|
139 |
+
# Play the video
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
140 |
def play_video(video_url):
|
141 |
return show_video(video_url)
|
142 |
|
143 |
+
search_button.click(update_search_results, inputs=search_query_input, outputs=[search_output, video_ids_state])
|
144 |
+
search_output.select(on_video_select, inputs=video_ids_state, outputs=selected_video_link)
|
145 |
play_video_button.click(play_video, inputs=selected_video_link, outputs=video_output)
|
146 |
|
147 |
# Launch the Gradio interface
|