Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -91,8 +91,33 @@ def show_video(video_url):
|
|
91 |
'''
|
92 |
return html_code
|
93 |
|
|
|
|
|
|
|
|
|
|
|
94 |
# Create the Gradio interface
|
95 |
-
with gr.Blocks(
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
96 |
gr.Markdown("## YouTube Video Search, Selection, and Playback")
|
97 |
|
98 |
with gr.Row():
|
@@ -103,19 +128,20 @@ with gr.Blocks() as demo:
|
|
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
|
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"
|
119 |
<img src="{thumbnail_url}" alt="{title}">
|
120 |
<div>
|
121 |
<h3>{title}</h3>
|
@@ -123,17 +149,41 @@ with gr.Blocks() as demo:
|
|
123 |
</div>
|
124 |
</div>
|
125 |
'''
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
126 |
html_code += '</div>'
|
127 |
return html_code
|
128 |
|
129 |
-
#
|
130 |
-
def
|
131 |
-
video_url
|
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 |
-
|
|
|
137 |
|
138 |
# Launch the Gradio interface
|
139 |
-
demo.launch()
|
|
|
91 |
'''
|
92 |
return html_code
|
93 |
|
94 |
+
# Function to handle video selection
|
95 |
+
def select_video(video_id):
|
96 |
+
video_url = f"https://www.youtube.com/watch?v={video_id}"
|
97 |
+
return video_url
|
98 |
+
|
99 |
# Create the Gradio interface
|
100 |
+
with gr.Blocks(css="""
|
101 |
+
.search-item {
|
102 |
+
display: flex;
|
103 |
+
align-items: flex-start;
|
104 |
+
margin-bottom: 30px;
|
105 |
+
cursor: pointer;
|
106 |
+
}
|
107 |
+
.search-item img {
|
108 |
+
width: 300px;
|
109 |
+
height: auto;
|
110 |
+
margin-right: 20px;
|
111 |
+
}
|
112 |
+
.search-item h3 {
|
113 |
+
font-size: 24px;
|
114 |
+
margin: 0 0 10px 0;
|
115 |
+
}
|
116 |
+
.search-item p {
|
117 |
+
font-size: 18px;
|
118 |
+
margin: 0;
|
119 |
+
}
|
120 |
+
""") as demo:
|
121 |
gr.Markdown("## YouTube Video Search, Selection, and Playback")
|
122 |
|
123 |
with gr.Row():
|
|
|
128 |
|
129 |
with gr.Column(scale=2):
|
130 |
selected_video_link = gr.Textbox(label="Selected Video Link", interactive=False)
|
131 |
+
play_video_button = gr.Button("Play Video")
|
132 |
video_output = gr.HTML(label="Video Player")
|
133 |
|
134 |
# Define search button behavior
|
135 |
def update_search_results(query):
|
136 |
search_results = youtube_search(query)
|
137 |
+
html_code = '<div>'
|
138 |
for item in search_results:
|
139 |
video_id = item['video_id']
|
140 |
thumbnail_url = item['thumbnail_url']
|
141 |
title = item['title']
|
142 |
description = item['description']
|
143 |
html_code += f'''
|
144 |
+
<div class="search-item" onclick="selectVideo('{video_id}')">
|
145 |
<img src="{thumbnail_url}" alt="{title}">
|
146 |
<div>
|
147 |
<h3>{title}</h3>
|
|
|
149 |
</div>
|
150 |
</div>
|
151 |
'''
|
152 |
+
html_code += '''
|
153 |
+
<script>
|
154 |
+
function selectVideo(video_id) {
|
155 |
+
fetch('/select_video', {
|
156 |
+
method: 'POST',
|
157 |
+
headers: { 'Content-Type': 'application/json' },
|
158 |
+
body: JSON.stringify({ 'video_id': video_id })
|
159 |
+
})
|
160 |
+
.then(response => response.json())
|
161 |
+
.then(data => {
|
162 |
+
const gradioApp = document.querySelector('gradio-app').shadowRoot || document;
|
163 |
+
const textbox = gradioApp.querySelector('#selected_video_link textarea');
|
164 |
+
if (!textbox) {
|
165 |
+
console.error('Selected video link textbox not found');
|
166 |
+
return;
|
167 |
+
}
|
168 |
+
textbox.value = data;
|
169 |
+
textbox.dispatchEvent(new Event('input', { bubbles: true }));
|
170 |
+
})
|
171 |
+
.catch(error => {
|
172 |
+
console.error('Error selecting video:', error);
|
173 |
+
});
|
174 |
+
}
|
175 |
+
</script>
|
176 |
+
'''
|
177 |
html_code += '</div>'
|
178 |
return html_code
|
179 |
|
180 |
+
# Play the video when the Play Video button is clicked
|
181 |
+
def play_video(video_url):
|
182 |
+
return show_video(video_url)
|
|
|
183 |
|
|
|
184 |
search_button.click(update_search_results, inputs=search_query_input, outputs=search_output)
|
185 |
+
play_video_button.click(play_video, inputs=selected_video_link, outputs=video_output)
|
186 |
+
select_video_event = gr.Event(fn=select_video, inputs="video_id", outputs=selected_video_link, api_name="select_video")
|
187 |
|
188 |
# Launch the Gradio interface
|
189 |
+
demo.launch()
|