Upload folder using huggingface_hub
Browse files- app.py +33 -2
- downloader.py +20 -0
- modules/db.py +9 -3
app.py
CHANGED
|
@@ -5,6 +5,7 @@ import threading
|
|
| 5 |
import gradio as gr
|
| 6 |
from gradio_modal import Modal
|
| 7 |
import chromadb
|
|
|
|
| 8 |
from modules.collector import fetch_all_channel_videos
|
| 9 |
from modules.db import (
|
| 10 |
delete_channel_from_collection,
|
|
@@ -229,6 +230,18 @@ def handle_query(query: str, search_channel_id: str):
|
|
| 229 |
with gr.Blocks() as demo:
|
| 230 |
gr.Markdown("### 📺 YouTube Channel Surfer")
|
| 231 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 232 |
# Modal to show channel videos
|
| 233 |
with Modal(visible=False) as videos_list_modal:
|
| 234 |
gr.Markdown("### Videos List")
|
|
@@ -331,6 +344,13 @@ with gr.Blocks() as demo:
|
|
| 331 |
)
|
| 332 |
|
| 333 |
with gr.Row():
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 334 |
show_videos_btn = gr.Button(
|
| 335 |
"🎬Videos",
|
| 336 |
size="sm",
|
|
@@ -455,7 +475,7 @@ with gr.Blocks() as demo:
|
|
| 455 |
)
|
| 456 |
|
| 457 |
submitted_question = gr.Markdown()
|
| 458 |
-
ask_status = gr.Markdown()
|
| 459 |
answer = gr.Markdown()
|
| 460 |
video_embed = gr.HTML() # iframe embeds
|
| 461 |
|
|
@@ -482,7 +502,7 @@ with gr.Blocks() as demo:
|
|
| 482 |
|
| 483 |
channel_radio.change(
|
| 484 |
enable_if_not_none, inputs=[channel_radio], outputs=[show_videos_btn]
|
| 485 |
-
)
|
| 486 |
show_videos_btn.click(disable_component, outputs=[show_videos_btn]).then(
|
| 487 |
close_component, outputs=[my_sidebar]
|
| 488 |
).then(
|
|
@@ -532,6 +552,17 @@ with gr.Blocks() as demo:
|
|
| 532 |
[channel_radio, current_page],
|
| 533 |
[modal_html, page_info, current_page],
|
| 534 |
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 535 |
|
| 536 |
if __name__ == "__main__":
|
| 537 |
for msg in init():
|
|
|
|
| 5 |
import gradio as gr
|
| 6 |
from gradio_modal import Modal
|
| 7 |
import chromadb
|
| 8 |
+
from downloader import export_channel_json
|
| 9 |
from modules.collector import fetch_all_channel_videos
|
| 10 |
from modules.db import (
|
| 11 |
delete_channel_from_collection,
|
|
|
|
| 230 |
with gr.Blocks() as demo:
|
| 231 |
gr.Markdown("### 📺 YouTube Channel Surfer")
|
| 232 |
|
| 233 |
+
with Modal(visible=False) as download_modal:
|
| 234 |
+
with gr.Row():
|
| 235 |
+
gr.Column()
|
| 236 |
+
download_status = gr.Markdown("## Preparing the file ...")
|
| 237 |
+
gr.Column()
|
| 238 |
+
with gr.Row():
|
| 239 |
+
gr.Column()
|
| 240 |
+
download_ready_btn = gr.DownloadButton(
|
| 241 |
+
label="Click to Download", visible=False, variant="primary", scale=0
|
| 242 |
+
)
|
| 243 |
+
gr.Column()
|
| 244 |
+
|
| 245 |
# Modal to show channel videos
|
| 246 |
with Modal(visible=False) as videos_list_modal:
|
| 247 |
gr.Markdown("### Videos List")
|
|
|
|
| 344 |
)
|
| 345 |
|
| 346 |
with gr.Row():
|
| 347 |
+
export_btn = gr.Button(
|
| 348 |
+
"⏬ Download",
|
| 349 |
+
size="sm",
|
| 350 |
+
scale=0,
|
| 351 |
+
variant="primary",
|
| 352 |
+
interactive=False,
|
| 353 |
+
)
|
| 354 |
show_videos_btn = gr.Button(
|
| 355 |
"🎬Videos",
|
| 356 |
size="sm",
|
|
|
|
| 475 |
)
|
| 476 |
|
| 477 |
submitted_question = gr.Markdown()
|
| 478 |
+
ask_status = gr.Markdown()
|
| 479 |
answer = gr.Markdown()
|
| 480 |
video_embed = gr.HTML() # iframe embeds
|
| 481 |
|
|
|
|
| 502 |
|
| 503 |
channel_radio.change(
|
| 504 |
enable_if_not_none, inputs=[channel_radio], outputs=[show_videos_btn]
|
| 505 |
+
).then(enable_if_not_none, inputs=[channel_radio], outputs=[export_btn])
|
| 506 |
show_videos_btn.click(disable_component, outputs=[show_videos_btn]).then(
|
| 507 |
close_component, outputs=[my_sidebar]
|
| 508 |
).then(
|
|
|
|
| 552 |
[channel_radio, current_page],
|
| 553 |
[modal_html, page_info, current_page],
|
| 554 |
)
|
| 555 |
+
export_btn.click(close_component, outputs=[my_sidebar]).then(
|
| 556 |
+
show_component, outputs=[download_status]
|
| 557 |
+
).then(hide_component, outputs=[download_ready_btn]).then(
|
| 558 |
+
show_component, outputs=[download_modal]
|
| 559 |
+
).then(
|
| 560 |
+
export_channel_json, inputs=channel_radio, outputs=download_ready_btn
|
| 561 |
+
).then(
|
| 562 |
+
hide_component, outputs=[download_status]
|
| 563 |
+
).then(
|
| 564 |
+
show_component, outputs=[download_ready_btn]
|
| 565 |
+
)
|
| 566 |
|
| 567 |
if __name__ == "__main__":
|
| 568 |
for msg in init():
|
downloader.py
ADDED
|
@@ -0,0 +1,20 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
import json
|
| 3 |
+
import tempfile
|
| 4 |
+
import os
|
| 5 |
+
|
| 6 |
+
from modules.db import fetch_channel_data
|
| 7 |
+
|
| 8 |
+
def json_serializer(obj):
|
| 9 |
+
if hasattr(obj, "tolist"): # NumPy arrays
|
| 10 |
+
return obj.tolist()
|
| 11 |
+
return str(obj)
|
| 12 |
+
|
| 13 |
+
def export_channel_json(channel_id):
|
| 14 |
+
data = fetch_channel_data(channel_id)
|
| 15 |
+
|
| 16 |
+
# Save to a temporary JSON file
|
| 17 |
+
fd, path = tempfile.mkstemp(suffix=".json")
|
| 18 |
+
with os.fdopen(fd, "w", encoding="utf-8") as f:
|
| 19 |
+
json.dump(data, f, indent=2, ensure_ascii=False, default=json_serializer)
|
| 20 |
+
return path # return file path for gr.File / gr.DownloadButton
|
modules/db.py
CHANGED
|
@@ -28,7 +28,7 @@ def get_collection():
|
|
| 28 |
|
| 29 |
|
| 30 |
# modules/db.py
|
| 31 |
-
def get_indexed_channels(collection
|
| 32 |
results = collection.get(include=["metadatas"])
|
| 33 |
channels = {}
|
| 34 |
|
|
@@ -49,7 +49,13 @@ def delete_channel_from_collection(channel_id: str):
|
|
| 49 |
"""Remove a channel from the index and refresh the radio choices."""
|
| 50 |
# Delete all videos for this channel
|
| 51 |
# print("Deleting channel", channel_id)
|
| 52 |
-
|
| 53 |
# print("data = ", data)
|
| 54 |
get_collection().delete(where={"channel_id": channel_id})
|
| 55 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 28 |
|
| 29 |
|
| 30 |
# modules/db.py
|
| 31 |
+
def get_indexed_channels(collection=get_collection()):
|
| 32 |
results = collection.get(include=["metadatas"])
|
| 33 |
channels = {}
|
| 34 |
|
|
|
|
| 49 |
"""Remove a channel from the index and refresh the radio choices."""
|
| 50 |
# Delete all videos for this channel
|
| 51 |
# print("Deleting channel", channel_id)
|
| 52 |
+
|
| 53 |
# print("data = ", data)
|
| 54 |
get_collection().delete(where={"channel_id": channel_id})
|
| 55 |
+
|
| 56 |
+
|
| 57 |
+
def fetch_channel_data(channel_id: str):
|
| 58 |
+
data = get_collection().get(
|
| 59 |
+
where={"channel_id": channel_id}, include=["embeddings", "metadatas", "documents"]
|
| 60 |
+
)
|
| 61 |
+
return data
|