Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -93,64 +93,86 @@ def show_video(video_url):
|
|
93 |
|
94 |
# Create the Gradio interface
|
95 |
with gr.Blocks(css="""
|
96 |
-
|
97 |
-
|
98 |
-
|
|
|
|
|
99 |
}
|
100 |
-
|
101 |
-
|
|
|
|
|
102 |
}
|
103 |
-
|
104 |
-
|
105 |
-
|
106 |
}
|
107 |
-
|
108 |
-
|
109 |
-
|
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.
|
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 |
-
#
|
127 |
def update_search_results(query):
|
128 |
search_results = youtube_search(query)
|
129 |
-
|
130 |
-
video_ids = []
|
131 |
for item in search_results:
|
132 |
-
|
|
|
133 |
title = item['title']
|
134 |
description = item['description']
|
135 |
-
#
|
136 |
-
|
137 |
-
|
138 |
-
|
139 |
-
|
140 |
-
|
141 |
-
|
142 |
-
|
143 |
-
|
144 |
-
|
145 |
-
|
146 |
-
|
147 |
-
|
148 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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=
|
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
|
|
|
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 |
+
# Use 'onclick' to call 'selectVideo' with the video ID
|
139 |
+
html_code += f'''
|
140 |
+
<div class="search-item" onclick="selectVideo('{video_id}')">
|
141 |
+
<img src="{thumbnail_url}" alt="{title}">
|
142 |
+
<div>
|
143 |
+
<h3>{title}</h3>
|
144 |
+
<p>{description}</p>
|
145 |
+
</div>
|
146 |
+
</div>
|
147 |
+
'''
|
148 |
+
html_code += '''
|
149 |
+
<script>
|
150 |
+
function getGradioApp() {
|
151 |
+
var elems = document.getElementsByTagName('gradio-app');
|
152 |
+
var gradioShadowRoot = elems.length == 0 ? null : elems[0].shadowRoot;
|
153 |
+
return gradioShadowRoot || document;
|
154 |
+
}
|
155 |
+
|
156 |
+
function selectVideo(video_id) {
|
157 |
+
var gradioApp = getGradioApp();
|
158 |
+
var textbox = gradioApp.querySelector('#selected_video_link textarea');
|
159 |
+
if (!textbox) {
|
160 |
+
console.error('Selected video link textbox not found');
|
161 |
+
return;
|
162 |
+
}
|
163 |
+
textbox.value = 'https://www.youtube.com/watch?v=' + video_id;
|
164 |
+
textbox.dispatchEvent(new Event('input', { bubbles: true }));
|
165 |
+
}
|
166 |
+
</script>
|
167 |
+
'''
|
168 |
+
html_code += '</div>'
|
169 |
+
return html_code
|
170 |
+
|
171 |
+
# Play the video when the Play Video button is clicked
|
172 |
def play_video(video_url):
|
173 |
return show_video(video_url)
|
174 |
|
175 |
+
search_button.click(update_search_results, inputs=search_query_input, outputs=search_output)
|
|
|
176 |
play_video_button.click(play_video, inputs=selected_video_link, outputs=video_output)
|
177 |
|
178 |
# Launch the Gradio interface
|