File size: 16,718 Bytes
e63f83d f315fdc e63f83d f315fdc 5ae4d47 e4e8447 82f7076 02a5da0 f315fdc e63f83d e51e296 e4e8447 e51e296 f315fdc 3485c54 9133ff3 3485c54 9133ff3 3485c54 d838a4b f315fdc 4e7113a f315fdc e51e296 f315fdc e51e296 5b6fd84 e51e296 9133ff3 e51e296 f315fdc 9133ff3 4332a60 9133ff3 4332a60 f315fdc 02a5da0 e51e296 f315fdc 9133ff3 e51e296 f315fdc e51e296 f315fdc e51e296 9133ff3 f315fdc 3485c54 f315fdc 3485c54 02a5da0 3485c54 9cbc004 3485c54 9cbc004 82f7076 3485c54 f315fdc 3485c54 9133ff3 f315fdc 3485c54 1cc7949 8858ad6 3485c54 f315fdc 3485c54 f315fdc 5184cba 3485c54 5ae4d47 1a6b60f 5ae4d47 3485c54 5b6fd84 e4e8447 5b6fd84 3485c54 f315fdc 857d724 360354c 857d724 5184cba 4332a60 857d724 c99147a 857d724 5184cba 857d724 82f7076 857d724 f315fdc 3485c54 f315fdc e51e296 3485c54 4e7113a e51e296 4e7113a 3485c54 e51e296 4e7113a e51e296 3485c54 f315fdc 5ae4d47 3485c54 d838a4b 3485c54 e51e296 91aed1d e51e296 3485c54 91aed1d e51e296 91aed1d e4e8447 3485c54 d838a4b 3485c54 d838a4b 3485c54 02a5da0 3485c54 f315fdc 3485c54 f315fdc 3485c54 f315fdc 3485c54 1cc7949 f315fdc 3485c54 4e7113a f315fdc 4e7113a f315fdc 4e7113a f315fdc 0aa7e77 1cc7949 3485c54 dc03444 dc56dff 1cc7949 10bc0fb 1cc7949 dc56dff 1a6b60f 9133ff3 dc56dff 9133ff3 dc56dff f315fdc 005ef35 1cc7949 dc03444 5184cba f315fdc 9133ff3 5ae4d47 f315fdc 3485c54 9133ff3 f315fdc 9133ff3 1cc7949 9133ff3 d838a4b 3485c54 9cbc004 82f7076 e4e8447 3485c54 d838a4b 5ae4d47 3485c54 e4e8447 3485c54 dc03444 5184cba 24ca1ac 5184cba 24ca1ac 12e720e 7bb8beb 4332a60 12e720e 5ae4d47 3485c54 f315fdc 4332a60 e63f83d f315fdc |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 |
import asyncio
import os
import re
import threading
import gradio as gr
from gradio_modal import Modal
import chromadb
from downloader import export_channel_json
from modules.channel_utils import fetch_channel_dataframe
from modules.collector import fetch_all_channel_videos
from modules.db import (
delete_channel_from_collection,
get_collection,
get_indexed_channels,
)
from modules.indexer import index_videos
from modules.answerer import answer_query, LLMAnswer, VideoItem, build_video_html
from dotenv import load_dotenv
from youtube_poller import start_poll
from youtube_sync import sync_channels_from_youtube
import pandas as pd
load_dotenv()
# -------------------------------
# Utility functions
# -------------------------------
def refresh_channel_list():
return gr.update(choices=list_channels_radio())
def show_component():
return gr.update(visible=True)
def hide_component():
return gr.update(visible=False)
def open_component():
return gr.update(open=True)
def close_component():
return gr.update(open=False)
def enable_component():
return gr.update(interactive=True)
def disable_component():
return gr.update(interactive=False)
def clear_component():
return gr.update(value="")
def show_loading(question):
return gr.update(value=f"⏳Fetching details on [{question}]...")
def enable_if_not_none(question):
if question is None:
return disable_component()
else:
return enable_component()
def index_channels(channel_urls: str):
yield "saving ...", gr.update(), gr.update()
yt_api_key = os.environ["YOUTUBE_API_KEY"]
urls = [u.strip() for u in re.split(r"[\n,]+", channel_urls) if u.strip()]
total_videos = 0
# sync all channels, streaming progress
for message, videos_count in sync_channels_from_youtube(yt_api_key, urls):
total_videos = videos_count # accumulate actual number of videos indexed
yield message, gr.update(), gr.update()
# final UI update
yield (
f"✅ Indexed {total_videos} videos from {len(urls)} channels.",
refresh_channel_list(),
list_channels_radio(),
)
def init(progress: gr.Progress = None):
channels = "https://www.youtube.com/@onedayonepasuram6126,https://www.youtube.com/@srisookthi,https://www.youtube.com/@learn-aksharam,https://www.youtube.com/@SriYadugiriYathirajaMutt,https://www.youtube.com/@akivasudev"
for msg, upd, upd in index_channels(channels):
# print(resp)
yield msg
def refresh_all_channels():
yt_api_key = os.environ["YOUTUBE_API_KEY"]
channels = get_indexed_channels(get_collection())
if not channels:
return "⚠️ No channels available to refresh.", refresh_channel_list()
# build list of URLs
urls = []
for key, val in channels.items():
url = val.get("channel_url") if isinstance(val, dict) else key
if url:
urls.append(url)
# re-index all at once
total_videos = sync_channels_from_youtube(yt_api_key, urls)
return (
f"🔄 Refreshed {len(urls)} channels, re-indexed {total_videos} videos.",
refresh_channel_list(),
)
# -------------------------------
# Channel selection as radio
# -------------------------------
def list_channels_radio():
channels = get_indexed_channels(get_collection())
choices = []
for key, val in channels.items():
if isinstance(val, dict):
channel_display_name = val.get("channel_title", "Unknown")
channel_id = val.get("channel_url")
else:
channel_display_name = val
channel_id = key
if channel_id:
choices.append((channel_display_name, channel_id))
# print("choices= ", choices)
return choices
# Delete a channel
# -------------------------------
def delete_channel(channel_url: str):
delete_channel_from_collection(channel_url)
# Return updated radio choices
return refresh_channel_list()
# -------------------------------
# LLM query
# -------------------------------
def handle_query(query: str, search_channel_id: str):
answer_text, video_html = answer_query(
query, get_collection(), channel_id=search_channel_id, top_k=10
)
if not answer_text:
answer_text = "No answer available."
if not video_html or not isinstance(video_html, str):
video_html = "" # ensure string for gr.HTML
return answer_text, video_html
# -------------------------------
# Gradio UI
# -------------------------------
with gr.Blocks() as demo:
gr.Markdown("### 📺 YouTube Channel Surfer")
with Modal(visible=False) as download_modal:
with gr.Row():
gr.Column()
download_status = gr.Markdown("## Preparing the file ...")
gr.Column()
with gr.Row():
gr.Column()
download_ready_btn = gr.DownloadButton(
label="Click to Download",
visible=False,
variant="primary",
scale=0,
)
gr.Column()
# Modal to show channel videos
with Modal(visible=False) as videos_list_modal:
gr.Markdown("### Videos List")
# the HTML table that shows one page of videos
# modal_html = gr.HTML()
channel_videos_df = gr.DataFrame(
show_search=True,
show_copy_button=True,
show_fullscreen_button=True,
datatype=[
"int",
"str",
"str",
"html",
],
headers=["#", "title", "description", "url"],
column_widths=["5%","25%","60%","10%"],
wrap=True,
col_count=(4, "fixed"),
)
# Modal to add new channels
with Modal(visible=False) as add_channel_modal:
channel_input = gr.Textbox(
label="Channel URLs",
placeholder="Paste one or more YouTube channel URLs (comma or newline separated)",
)
examples = {
"Comma Separated Channels Example": "https://www.youtube.com/@onedayonepasuram6126,https://www.youtube.com/@srisookthi,https://www.youtube.com/@learn-aksharam,https://www.youtube.com/@SriYadugiriYathirajaMutt",
"Newline Separated Channels Example": "https://www.youtube.com/@onedayonepasuram6126\nhttps://www.youtube.com/@srisookthi\nhttps://www.youtube.com/@learn-aksharam\nhttps://www.youtube.com/@SriYadugiriYathirajaMutt",
"One Day One Pasuram": "https://www.youtube.com/@onedayonepasuram6126",
"Sri Sookthi": "https://www.youtube.com/@srisookthi",
"Aksharam": "https://www.youtube.com/@learn-aksharam",
"Cricinfo": "https://www.youtube.com/@espncricinfo",
"Chanakyaa": "https://www.youtube.com/@ChanakyaaTV",
"Aptitude Guru": "https://www.youtube.com/@AptitudeGuruHem",
"Universe Genius": "https://www.youtube.com/@UniverseGenius",
"Praveen Mohan": "https://www.youtube.com/@RealPraveenMohan",
"Yathiraja Mutt": "https://www.youtube.com/@SriYadugiriYathirajaMutt",
"Vasudevan Srinivasachariar": "https://www.youtube.com/@akivasudev",
}
def set_example(label):
return examples[label]
gr.Markdown("Click on any example below and then click on add channels button.")
with gr.Row():
for label in examples:
gr.Button(label, size="sm", variant="huggingface", scale=0).click(
fn=set_example,
inputs=gr.State(label),
outputs=channel_input,
)
with gr.Row():
gr.Column()
save_add_channels_btn = gr.Button(
"Add Channel(s)", scale=0, variant="primary"
)
gr.Column()
index_status = gr.Markdown(label="Index Status", container=False)
with gr.Row():
# Sidebar
with gr.Sidebar() as my_sidebar:
gr.Markdown("### 📺 Channels")
channel_list_values = list_channels_radio()
channel_list_state = gr.State(channel_list_values)
no_channels_message = gr.Markdown(
"⚠️ **No channels available.**",
visible=False if channel_list_values else True,
)
channel_radio = gr.Radio(
choices=channel_list_values,
label="Select a Channel",
visible=True if channel_list_values else False,
)
with gr.Row():
export_btn = gr.Button(
"⏬ Download",
size="sm",
scale=0,
variant="primary",
interactive=False,
)
show_videos_btn = gr.Button(
"🎬Videos",
size="sm",
scale=0,
variant="secondary",
interactive=False,
)
refresh_btn = gr.Button(
"⭮ Refresh",
size="sm",
scale=0,
variant="huggingface",
)
refresh_all_btn = gr.Button(
"🔄 Sync from YouTube",
size="sm",
scale=0,
variant="stop",
visible=False,
)
add_channels_btn = gr.Button(
"➕ Add", size="sm", scale=0, variant="primary"
)
delete_channel_btn = gr.Button(
"🗑️ Delete", size="sm", scale=0, variant="stop"
)
refresh_status = gr.Markdown(label="Refresh Status", container=False)
refresh_all_btn.click(
fn=refresh_all_channels,
inputs=None,
outputs=[refresh_status, channel_radio],
)
refresh_btn.click(fn=refresh_channel_list, outputs=[channel_radio]).then(
fn=list_channels_radio, outputs=[channel_list_state]
)
add_channels_btn.click(close_component, outputs=[my_sidebar]).then(
show_component, outputs=[add_channel_modal]
)
def toggle_no_data_found(channel_list):
if channel_list:
return show_component(), hide_component()
else:
return hide_component(), show_component()
save_add_channels_btn.click(
disable_component, outputs=[save_add_channels_btn]
).then(
index_channels,
inputs=[channel_input],
outputs=[index_status, channel_radio, channel_list_state],
).then(
hide_component, outputs=[add_channel_modal]
).then(
open_component, outputs=[my_sidebar]
).then(
enable_component, outputs=[save_add_channels_btn]
).then(
toggle_no_data_found,
inputs=[channel_list_state],
outputs=[channel_radio, no_channels_message],
)
## Onload refresh the channel list.
gr.on(fn=refresh_channel_list, outputs=[channel_radio]).then(
fn=list_channels_radio, outputs=[channel_list_state]
)
# Main Column
main_content_no_channels_html = gr.HTML(
"""
<div style="
display: flex;
justify-content: center;
align-items: center;
height: 150px;
">
<div style="
border: 2px solid #FFA500;
background-color: #FFF8E1;
color: #FF6F00;
padding: 20px 30px;
border-radius: 12px;
font-weight: bold;
font-size: 1.2rem;
text-align: center;
box-shadow: 0 4px 10px rgba(0,0,0,0.1);
">
⚠️ No channels added.<br>
Please add channels from the side bar
</div>
</div>
""",
visible=True if not channel_list_state.value else False,
)
with gr.Column(
scale=3, visible=True if channel_list_state.value else False
) as main_content:
with gr.Row():
search_channel = gr.Dropdown(
label="Select a Channel",
choices=[("All Channels", None)] + channel_list_state.value,
value=None,
)
question = gr.Textbox(
label="Ask a Question",
placeholder="e.g., How to write the letter Aa in grantham?",
submit_btn=True,
)
gr.Column(scale=2)
gr.Examples(
[
"Srirangam",
"Gajendra moksham",
"Poorvikalyani",
"Virutham from chathusloki",
"Lesson 9.15 from Aksharam",
],
inputs=question,
)
submitted_question = gr.Markdown()
ask_status = gr.Markdown()
answer = gr.Markdown()
video_embed = gr.HTML() # iframe embeds
def get_question(q):
return f"## You asked : {q}\n---"
# question.change(enable_if_not_none, inputs=[question], outputs=[question])
question.submit(show_loading, inputs=[question], outputs=[ask_status]).then(
get_question, inputs=[question], outputs=[submitted_question]
).then(disable_component, outputs=[question]).then(
handle_query,
inputs=[question, search_channel],
outputs=[answer, video_embed],
).then(
enable_component, outputs=[question]
).then(
clear_component, outputs=[ask_status]
)
# Show videos modal when button clicked
def show_selected_channel_videos(selected_channel_id):
# print("selected_channel_id = ", selected_channel_id)
df = fetch_channel_dataframe(selected_channel_id)
return gr.update(value=df, label=f"{len(df)} videos")
channel_radio.change(
enable_if_not_none, inputs=[channel_radio], outputs=[show_videos_btn]
).then(enable_if_not_none, inputs=[channel_radio], outputs=[export_btn])
show_videos_btn.click(disable_component, outputs=[show_videos_btn]).then(
close_component, outputs=[my_sidebar]
).then(
show_selected_channel_videos,
inputs=[channel_radio],
outputs=[channel_videos_df],
).then(
show_component, outputs=[videos_list_modal]
).then(
enable_component, outputs=[show_videos_btn]
)
delete_channel_btn.click(
disable_component, outputs=[delete_channel_btn]
).then(
delete_channel, # function
inputs=[channel_radio], # selected channel name
outputs=[channel_radio], # update the radio choices
).then(
enable_component, outputs=[delete_channel_btn]
)
channel_list_state.change(
toggle_no_data_found,
inputs=[channel_list_state],
outputs=[main_content, main_content_no_channels_html],
).then(
toggle_no_data_found,
inputs=[channel_list_state],
outputs=[channel_radio, no_channels_message],
)
def get_channel_choices(channel_list):
return gr.update(choices=[("All Channels", None)] + channel_list)
channel_list_state.change(
get_channel_choices, inputs=[channel_list_state], outputs=[search_channel]
)
export_btn.click(close_component, outputs=[my_sidebar]).then(
show_component, outputs=[download_status]
).then(hide_component, outputs=[download_ready_btn]).then(
show_component, outputs=[download_modal]
).then(
export_channel_json, inputs=channel_radio, outputs=download_ready_btn
).then(
hide_component, outputs=[download_status]
).then(
show_component, outputs=[download_ready_btn]
)
if __name__ == "__main__":
for msg in init():
print(msg)
# Start polling in a background thread
poll_thread = threading.Thread(target=start_poll, daemon=True)
poll_thread.start()
demo.launch()
|