vikramvasudevan commited on
Commit
5b6fd84
·
verified ·
1 Parent(s): e51e296

Upload folder using huggingface_hub

Browse files
Files changed (1) hide show
  1. app.py +75 -10
app.py CHANGED
@@ -69,7 +69,7 @@ def index_channels(channel_urls: str):
69
 
70
  # sync all channels, streaming progress
71
  for message, videos_count in sync_channels_from_youtube(yt_api_key, urls):
72
- total_videos += videos_count # accumulate actual number of videos indexed
73
  yield message, gr.update(), gr.update()
74
 
75
  # final UI update
@@ -123,27 +123,42 @@ def list_channels_radio():
123
 
124
 
125
  # -------------------------------
126
- # Fetch channel videos as HTML table
127
  # -------------------------------
128
- def fetch_channel_html(channel_id: str):
129
- # query your collection/db instead of YouTube API
130
  collection = get_collection()
 
 
 
 
 
 
 
 
 
 
131
  results = collection.get(
132
- where={"channel_id": channel_id}, include=["documents", "metadatas"]
 
 
 
133
  )
134
 
 
135
  if not results or not results.get("metadatas"):
136
- return """
137
  <div style="display:flex;justify-content:center;align-items:center;
138
  height:200px;flex-direction:column;color:#666;">
139
- ⚠️ No videos found for this channel.
140
  </div>
141
  """
142
 
143
  videos = results["metadatas"]
144
 
145
  # build table
146
- html = """
 
 
147
  <table border="1" style="border-collapse:collapse;width:100%;font-family:sans-serif;">
148
  <thead style="background:#f0f0f0;">
149
  <tr>
@@ -155,11 +170,12 @@ def fetch_channel_html(channel_id: str):
155
  </thead>
156
  <tbody>
157
  """
 
158
 
159
- for idx, v in enumerate(videos):
160
  html += f"""
161
  <tr>
162
- <td>{idx+1}</td>
163
  <td>{v.get('video_title','')}</td>
164
  <td><a href="https://youtube.com/watch?v={v.get('video_id')}"
165
  target="_blank">Watch Video</a></td>
@@ -200,8 +216,46 @@ with gr.Blocks() as demo:
200
  # Modal to show channel videos
201
  with Modal(visible=False) as videos_list_modal:
202
  gr.Markdown("### Videos List")
 
 
203
  modal_html = gr.HTML()
204
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
205
  # Modal to add new channels
206
  with Modal(visible=False) as add_channel_modal:
207
  channel_input = gr.Textbox(
@@ -436,6 +490,17 @@ with gr.Blocks() as demo:
436
  inputs=[channel_list_state],
437
  outputs=[main_content, main_content_no_channels_html],
438
  )
 
 
 
 
 
 
 
 
 
 
 
439
 
440
 
441
  def init():
 
69
 
70
  # sync all channels, streaming progress
71
  for message, videos_count in sync_channels_from_youtube(yt_api_key, urls):
72
+ total_videos = videos_count # accumulate actual number of videos indexed
73
  yield message, gr.update(), gr.update()
74
 
75
  # final UI update
 
123
 
124
 
125
  # -------------------------------
126
+ # Fetch channel videos as HTML table with pagination
127
  # -------------------------------
128
+ def fetch_channel_html(channel_id: str, page: int = 1, page_size: int = 10):
 
129
  collection = get_collection()
130
+ offset = (page - 1) * page_size
131
+
132
+ all_results = collection.get(
133
+ where={"channel_id": channel_id}, include=["metadatas"]
134
+ )
135
+ total_count = (
136
+ len(all_results["metadatas"])
137
+ if all_results and "metadatas" in all_results
138
+ else 0
139
+ )
140
  results = collection.get(
141
+ where={"channel_id": channel_id},
142
+ include=["documents", "metadatas"],
143
+ limit=page_size,
144
+ offset=offset,
145
  )
146
 
147
+ # handle empty
148
  if not results or not results.get("metadatas"):
149
+ return f"""
150
  <div style="display:flex;justify-content:center;align-items:center;
151
  height:200px;flex-direction:column;color:#666;">
152
+ ⚠️ No videos found for this channel (page {page}).
153
  </div>
154
  """
155
 
156
  videos = results["metadatas"]
157
 
158
  # build table
159
+ html = (
160
+ f"<div>Total: {total_count} videos</div>"
161
+ + """
162
  <table border="1" style="border-collapse:collapse;width:100%;font-family:sans-serif;">
163
  <thead style="background:#f0f0f0;">
164
  <tr>
 
170
  </thead>
171
  <tbody>
172
  """
173
+ )
174
 
175
+ for idx, v in enumerate(videos, start=offset + 1):
176
  html += f"""
177
  <tr>
178
+ <td>{idx}</td>
179
  <td>{v.get('video_title','')}</td>
180
  <td><a href="https://youtube.com/watch?v={v.get('video_id')}"
181
  target="_blank">Watch Video</a></td>
 
216
  # Modal to show channel videos
217
  with Modal(visible=False) as videos_list_modal:
218
  gr.Markdown("### Videos List")
219
+
220
+ # the HTML table that shows one page of videos
221
  modal_html = gr.HTML()
222
 
223
+ # row for pagination controls
224
+ with gr.Row(equal_height=True):
225
+ gr.Column()
226
+ prev_btn = gr.Button("⬅️ Prev", size="sm", variant="huggingface", scale=0)
227
+ page_info = gr.Textbox(
228
+ value="Page 1",
229
+ interactive=False,
230
+ show_label=False,
231
+ container=False,
232
+ scale=0,
233
+ )
234
+ next_btn = gr.Button("Next ➡️", size="sm", variant="huggingface", scale=0)
235
+ gr.Column()
236
+
237
+ current_page = gr.State(1)
238
+ page_size = 10 # change if you like
239
+
240
+ def update_table(channel_id, page):
241
+ return fetch_channel_html(channel_id, page, page_size), f"Page {page}"
242
+
243
+ def prev_page(channel_id, page):
244
+ new_page = max(1, page - 1)
245
+ return (
246
+ fetch_channel_html(channel_id, new_page, page_size),
247
+ f"Page {new_page}",
248
+ new_page,
249
+ )
250
+
251
+ def next_page(channel_id, page):
252
+ new_page = page + 1
253
+ return (
254
+ fetch_channel_html(channel_id, new_page, page_size),
255
+ f"Page {new_page}",
256
+ new_page,
257
+ )
258
+
259
  # Modal to add new channels
260
  with Modal(visible=False) as add_channel_modal:
261
  channel_input = gr.Textbox(
 
490
  inputs=[channel_list_state],
491
  outputs=[main_content, main_content_no_channels_html],
492
  )
493
+ prev_btn.click(
494
+ prev_page,
495
+ [channel_radio, current_page], # you’ll need to pass channel_id here
496
+ [modal_html, page_info, current_page],
497
+ )
498
+
499
+ next_btn.click(
500
+ next_page,
501
+ [channel_radio, current_page],
502
+ [modal_html, page_info, current_page],
503
+ )
504
 
505
 
506
  def init():