Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -61,7 +61,7 @@ def youtube_search(query, max_results=50):
|
|
61 |
|
62 |
except requests.exceptions.RequestException as e:
|
63 |
print(f"Error during YouTube API request: {e}")
|
64 |
-
return []
|
65 |
|
66 |
# Function to display the video using the video URL
|
67 |
def show_video(video_url):
|
@@ -91,33 +91,8 @@ def show_video(video_url):
|
|
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(
|
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():
|
@@ -127,21 +102,22 @@ with gr.Blocks(css="""
|
|
127 |
search_output = gr.HTML(label="Search Results")
|
128 |
|
129 |
with gr.Column(scale=2):
|
130 |
-
selected_video_link = gr.Textbox(label="Selected Video Link", interactive=
|
131 |
-
|
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('{
|
145 |
<img src="{thumbnail_url}" alt="{title}">
|
146 |
<div>
|
147 |
<h3>{title}</h3>
|
@@ -151,39 +127,25 @@ with gr.Blocks(css="""
|
|
151 |
'''
|
152 |
html_code += '''
|
153 |
<script>
|
154 |
-
function selectVideo(
|
155 |
-
|
156 |
-
|
157 |
-
|
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 |
-
#
|
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 |
-
|
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()
|
|
|
61 |
|
62 |
except requests.exceptions.RequestException as e:
|
63 |
print(f"Error during YouTube API request: {e}")
|
64 |
+
return []
|
65 |
|
66 |
# Function to display the video using the video URL
|
67 |
def show_video(video_url):
|
|
|
91 |
'''
|
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=True)
|
106 |
+
play_button = gr.Button("Play Video")
|
107 |
video_output = gr.HTML(label="Video Player")
|
108 |
|
109 |
# Define search button behavior
|
110 |
def update_search_results(query):
|
111 |
search_results = youtube_search(query)
|
112 |
+
html_code = '<div id="search-results">'
|
113 |
for item in search_results:
|
114 |
video_id = item['video_id']
|
115 |
thumbnail_url = item['thumbnail_url']
|
116 |
title = item['title']
|
117 |
description = item['description']
|
118 |
+
video_url = f"https://www.youtube.com/watch?v={video_id}"
|
119 |
html_code += f'''
|
120 |
+
<div class="search-item" onclick="selectVideo('{video_url}')">
|
121 |
<img src="{thumbnail_url}" alt="{title}">
|
122 |
<div>
|
123 |
<h3>{title}</h3>
|
|
|
127 |
'''
|
128 |
html_code += '''
|
129 |
<script>
|
130 |
+
function selectVideo(videoUrl) {
|
131 |
+
const textbox = document.querySelector('input[data-testid="textbox"]');
|
132 |
+
if (textbox) {
|
133 |
+
textbox.value = videoUrl;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
134 |
textbox.dispatchEvent(new Event('input', { bubbles: true }));
|
135 |
+
}
|
|
|
|
|
|
|
136 |
}
|
137 |
</script>
|
138 |
'''
|
139 |
html_code += '</div>'
|
140 |
return html_code
|
141 |
|
142 |
+
# Function to play the selected video
|
143 |
def play_video(video_url):
|
144 |
return show_video(video_url)
|
145 |
|
146 |
+
# Set up event listeners
|
147 |
search_button.click(update_search_results, inputs=search_query_input, outputs=search_output)
|
148 |
+
play_button.click(play_video, inputs=selected_video_link, outputs=video_output)
|
|
|
149 |
|
150 |
# Launch the Gradio interface
|
151 |
+
demo.launch()
|