File size: 28,222 Bytes
312718f
 
122e63b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
8592e17
122e63b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
8592e17
 
122e63b
 
 
 
 
 
 
 
 
8592e17
122e63b
8592e17
122e63b
 
 
 
 
 
8592e17
122e63b
 
 
 
 
8592e17
122e63b
 
 
8592e17
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
ca748ce
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
4d9037a
ecffc5a
242921d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
4d9037a
362e972
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
390755a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
e391969
8a27c51
 
390755a
ff730fe
e391969
 
 
362e972
390755a
 
d7af77a
390755a
e391969
d7af77a
e391969
 
362e972
3ab93d5
362e972
e391969
362e972
5090c0a
 
 
 
 
 
 
 
 
 
f26f21c
 
db7ab39
390755a
362e972
 
 
 
 
e391969
390755a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2b9c13a
 
 
 
 
 
390755a
 
 
 
2b9c13a
390755a
 
362e972
e391969
362e972
 
 
 
 
 
e391969
 
390755a
e391969
 
 
 
 
362e972
 
 
ff730fe
362e972
e391969
362e972
e391969
 
 
bc8673a
e391969
390755a
 
 
 
 
 
 
e391969
 
 
 
 
 
 
 
 
bc8673a
e391969
 
bc8673a
e391969
 
5a5bd5c
 
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
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778


# import nest_asyncio
# from youtube_transcript_api import YouTubeTranscriptApi
# import streamlit as st
# import os
# from groq import Groq

# nest_asyncio.apply()

# # --- CONFIGURATION ---
# YOUTUBE_API_KEY = os.environ.get("YOUTUBE_API_KEY")  # Set in your HuggingFace Secrets
# channel_id = "UCsv3kmQ5k1eIRG2R9mWN"  # @icodeguru0

# # Initialize Groq client once
# groq_client = Groq(api_key=os.environ.get("GROQ_API_KEY"))

# # --- FUNCTION: Fetch recent video IDs from YouTube channel ---
# def get_latest_video_ids(channel_id, max_results=5):
#     import requests
#     url = f"https://www.googleapis.com/youtube/v3/search?key={YOUTUBE_API_KEY}&channelId={channel_id}&part=snippet,id&order=date&maxResults={max_results}"
#     response = requests.get(url)
#     videos = response.json().get('items', [])
#     return [v['id']['videoId'] for v in videos if v['id']['kind'] == 'youtube#video']

# # --- FUNCTION: Get video transcripts ---
# def get_video_transcripts(video_ids):
#     all_transcripts = []
#     for vid in video_ids:
#         try:
#             transcript = YouTubeTranscriptApi.get_transcript(vid)
#             text = " ".join([t['text'] for t in transcript])
#             all_transcripts.append(text)
#         except:
#             continue
#     return all_transcripts

# # --- FUNCTION: Ask Groq API using official client ---
# def ask_groq(context, question):
#     messages = [
#         {"role": "system", "content": "You are a helpful assistant."},
#         {"role": "user", "content": f"Context: {context}\n\nQuestion: {question}\nAnswer:"}
#     ]
#     chat_completion = groq_client.chat.completions.create(
#         model="llama-3.3-70b-versatile",  # Or the model you have access to
#         messages=messages,
#     )
#     return chat_completion.choices[0].message.content.strip()

# # --- STREAMLIT APP ---
# def main():
#     st.set_page_config(page_title="EduBot - YouTube Channel QA", layout="wide")
#     st.title("πŸŽ“ EduBot for @icodeguru0")
#     st.markdown("Ask anything based on the channel’s recent videos.")

#     question = st.text_input("πŸ’¬ Ask your question here:")
#     if question:
#         with st.spinner("πŸ” Fetching videos and transcripts..."):
#             video_ids = get_latest_video_ids(channel_id)
#             transcripts = get_video_transcripts(video_ids)
#             full_context = "\n\n".join(transcripts)
#         with st.spinner("🧠 Thinking..."):
#             answer = ask_groq(full_context, question)
#         st.success(answer)

#     st.markdown("---")
#     st.caption("Powered by YouTube + Groq | Built for @icodeguru0")

# if __name__ == "__main__":
#     main()


# # import nest_asyncio
# # from youtube_transcript_api import YouTubeTranscriptApi
# # import streamlit as st
# # import os
# # from groq import Groq
# # import requests
# # from bs4 import BeautifulSoup

# # nest_asyncio.apply()

# # # --- CONFIGURATION ---
# # YOUTUBE_API_KEY = os.environ.get("YOUTUBE_API_KEY")  # Set in your HuggingFace Secrets
# # channel_id = "UCsv3kmQ5k1eIRG2R9mWN"  # @icodeguru0
# # BASE_URL = "https://icode.guru"

# # # Initialize Groq client once
# # groq_client = Groq(api_key=os.environ.get("GROQ_API_KEY"))

# # # --- FUNCTION: Fetch recent video IDs from YouTube channel ---
# # def get_latest_video_ids(channel_id, max_results=5):
# #     url = f"https://www.googleapis.com/youtube/v3/search?key={YOUTUBE_API_KEY}&channelId={channel_id}&part=snippet,id&order=date&maxResults={max_results}"
# #     response = requests.get(url)
# #     videos = response.json().get('items', [])
# #     return [v['id']['videoId'] for v in videos if v['id']['kind'] == 'youtube#video']

# # # --- FUNCTION: Get video transcripts ---
# # def get_video_transcripts(video_ids):
# #     all_transcripts = []
# #     for vid in video_ids:
# #         try:
# #             transcript = YouTubeTranscriptApi.get_transcript(vid)
# #             text = " ".join([t['text'] for t in transcript])
# #             all_transcripts.append(text)
# #         except:
# #             continue
# #     return all_transcripts

# # # --- NEW FUNCTION: Scrape icode.guru ---
# # def scrape_icodeguru(base_url="https://icode.guru", max_pages=5):
# #     visited = set()
# #     blocks = []

# #     def crawl(url):
# #         if url in visited or len(visited) >= max_pages:
# #             return
# #         visited.add(url)
# #         try:
# #             res = requests.get(url, timeout=10)
# #             soup = BeautifulSoup(res.content, "html.parser")
# #             page_text = soup.get_text(separator=" ", strip=True)
# #             if len(page_text) > 100:
# #                 blocks.append(f"[Source]({url}):\n{page_text[:2000]}")
# #             for link in soup.find_all("a", href=True):
# #                 href = link['href']
# #                 if href.startswith("/"):
# #                     href = base_url + href
# #                 if href.startswith(base_url):
# #                     crawl(href)
# #         except:
# #             pass

# #     crawl(base_url)
# #     return blocks

# # # --- FUNCTION: Ask Groq API using official client ---
# # def ask_groq(context, question):
# #     messages = [
# #         {"role": "system", "content": "You are a helpful assistant. Only answer using the given context (YouTube + icode.guru). Provide links if possible."},
# #         {"role": "user", "content": f"Context:\n{context}\n\nQuestion: {question}\nAnswer:"}
# #     ]
# #     chat_completion = groq_client.chat.completions.create(
# #         model="llama-3.3-70b-versatile",  # Or the model you have access to
# #         messages=messages,
# #     )
# #     return chat_completion.choices[0].message.content.strip()

# # # --- STREAMLIT APP ---
# # def main():
# #     st.set_page_config(page_title="EduBot - YouTube + iCodeGuru QA", layout="wide")
# #     st.title("πŸŽ“ EduBot for @icodeguru0")
# #     st.markdown("Ask anything based on the channel’s recent videos and website content from [icode.guru](https://icode.guru).")

# #     question = st.text_input("πŸ’¬ Ask your question here:")
# #     if question:
# #         with st.spinner("πŸ” Fetching videos and transcripts..."):
# #             video_ids = get_latest_video_ids(channel_id)
# #             transcripts = get_video_transcripts(video_ids)
# #             yt_context = "\n\n".join(transcripts)

# #         with st.spinner("🌐 Scraping icode.guru..."):
# #             site_blocks = scrape_icodeguru(BASE_URL, max_pages=5)
# #             site_context = "\n\n".join(site_blocks)

# #         full_context = yt_context + "\n\n" + site_context

# #         with st.spinner("🧠 Thinking..."):
# #             answer = ask_groq(full_context, question)
# #         st.success(answer)

# #     st.markdown("---")
# #     st.caption("Powered by YouTube + iCodeGuru + Groq | Built for @icodeguru0")

# # if __name__ == "__main__":
# #     main()


#(youtube+web)


# import nest_asyncio
# import streamlit as st
# import os
# import requests
# from youtube_transcript_api import YouTubeTranscriptApi
# from groq import Groq
# from bs4 import BeautifulSoup

# nest_asyncio.apply()

# # --- CONFIGURATION ---
# YOUTUBE_API_KEY = os.environ.get("YOUTUBE_API_KEY")
# GROQ_API_KEY = os.environ.get("GROQ_API_KEY")
# channel_id = "UCsv3kmQ5k1eIRG2R9mWN"  # iCodeGuru
# BASE_URL = "https://icode.guru"

# groq_client = Groq(api_key=GROQ_API_KEY)

# # --- Fetch recent video IDs from YouTube channel ---
# def get_latest_video_ids(channel_id, max_results=5):
#     url = f"https://www.googleapis.com/youtube/v3/search?key={YOUTUBE_API_KEY}&channelId={channel_id}&part=snippet,id&order=date&maxResults={max_results}"
#     response = requests.get(url)
#     videos = response.json().get('items', [])
    
#     valid_videos = []
#     for v in videos:
#         if v['id']['kind'] == 'youtube#video':
#             title = v['snippet']['title']
#             channel_title = v['snippet']['channelTitle']
#             video_id = v['id']['videoId']
#             if "icodeguru" in channel_title.lower():  # βœ… Extra validation
#                 valid_videos.append((video_id, title))
#     return valid_videos


# # --- Get video transcripts ---
# def get_video_transcripts(video_info):
#     results = []
#     for vid, title in video_info:
#         try:
#             transcript = YouTubeTranscriptApi.get_transcript(vid)
#             text = " ".join([t['text'] for t in transcript])
#             video_link = f"https://www.youtube.com/watch?v={vid}"
#             results.append({
#                 "video_id": vid,
#                 "title": title,
#                 "link": video_link,
#                 "transcript": text
#             })
#         except Exception as e:
#             continue
#     return results

# # --- Scrape icode.guru ---
# def scrape_icodeguru(base_url=BASE_URL, max_pages=5):
#     visited = set()
#     blocks = []

#     def crawl(url):
#         if url in visited or len(visited) >= max_pages:
#             return
#         visited.add(url)
#         try:
#             res = requests.get(url, timeout=10)
#             soup = BeautifulSoup(res.content, "html.parser")
#             page_text = soup.get_text(separator=" ", strip=True)
#             if len(page_text) > 100:
#                 blocks.append(f"[{url}]({url}):\n{page_text[:1500]}")
#             for link in soup.find_all("a", href=True):
#                 href = link['href']
#                 if href.startswith("/"):
#                     href = base_url + href
#                 if href.startswith(base_url):
#                     crawl(href)
#         except:
#             pass

#     crawl(base_url)
#     return blocks

# # --- Ask Groq ---
# def ask_groq(context, question):
#     messages = [
#         {"role": "system", "content": "You are a helpful assistant. Always provide relevant video and website links if possible."},
#         {"role": "user", "content": f"Context:\n{context}\n\nQuestion: {question}\nAnswer (include links):"}
#     ]
#     chat_completion = groq_client.chat.completions.create(
#         model="llama-3.3-70b-versatile",
#         messages=messages,
#     )
#     return chat_completion.choices[0].message.content.strip()

# #--- STREAMLIT APP ---
# def main():
#     st.set_page_config(page_title="EduBot for iCodeGuru", layout="wide")
#     st.title("πŸŽ“ EduBot for @icodeguru0")
#     st.markdown("Ask anything based on the latest YouTube videos and website content of [icode.guru](https://icode.guru).")

#     question = st.text_input("πŸ’¬ Ask your question:")
#     if question:
#         with st.spinner("πŸ“Ί Fetching YouTube videos..."):
#             video_info = get_latest_video_ids(channel_id, max_results=5)
#             transcripts = get_video_transcripts(video_info)

#         yt_context = ""
#         relevant_links = []
#         for vid in transcripts:
#             yt_context += f"\n\n[Video: {vid['title']}]({vid['link']}):\n{vid['transcript'][:1500]}"
#             if question.lower() in vid['transcript'].lower():
#                 relevant_links.append(vid['link'])

#         with st.spinner("🌐 Scraping icode.guru..."):
#             site_blocks = scrape_icodeguru(BASE_URL, max_pages=5)
#             site_context = "\n\n".join(site_blocks)

#         full_context = yt_context + "\n\n" + site_context

#         with st.spinner("🧠 Thinking..."):
#             answer = ask_groq(full_context, question)

#         st.success(answer)

#         if relevant_links:
#             st.markdown("### πŸ”— Related YouTube Links")
#             for link in relevant_links:
#                 st.markdown(f"- [Watch Video]({link})")

#     st.markdown("---")
#     st.caption("Powered by YouTube, iCodeGuru, and Groq")

# if __name__ == "__main__":
#     main()

# (vectordb)


# import nest_asyncio
# import streamlit as st
# import os
# import requests
# from youtube_transcript_api import YouTubeTranscriptApi
# from groq import Groq
# from bs4 import BeautifulSoup
# from sentence_transformers import SentenceTransformer
# import chromadb
# from chromadb.utils.embedding_functions import SentenceTransformerEmbeddingFunction

# import json

# nest_asyncio.apply()

# # --- CONFIGURATION ---
# YOUTUBE_API_KEY = os.environ.get("YOUTUBE_API_KEY")
# GROQ_API_KEY = os.environ.get("GROQ_API_KEY")
# channel_id = "UCsv3kmQ5k1eIRG2R9mWN"  # iCodeGuru
# BASE_URL = "https://icode.guru"

# groq_client = Groq(api_key=GROQ_API_KEY)
# from chromadb.utils.embedding_functions import SentenceTransformerEmbeddingFunction

# embedding_function = SentenceTransformerEmbeddingFunction("all-MiniLM-L6-v2")

# chroma_client = chromadb.Client()
# collection = chroma_client.get_or_create_collection("icodeguru_knowledge", embedding_function=embedding_function)

# # --- Upload + load files as vector DB ---
# def load_uploaded_vectors(uploaded_files):
#     data = []
#     for file in uploaded_files:
#         if file.name.endswith(".txt"):
#             text = file.read().decode()
#             data.append({"id": file.name, "content": text})
#         elif file.name.endswith(".json"):
#             content = json.load(file)
#             for i, chunk in enumerate(content):
#                 data.append({"id": f"{file.name}-{i}", "content": chunk})
#     return data

# def search_vector_data(query, data):
#     if not data:
#         return None
#     collection = chroma_client.get_or_create_collection("temp_query", embedding_function=embedding_function)
#     collection.add(documents=[d["content"] for d in data], ids=[d["id"] for d in data])
#     results = collection.query(query_texts=[query], n_results=3)
#     if results and results["documents"]:
#         return "\n\n".join([doc for doc in results["documents"][0]])
#     return None

# # --- Fetch recent video IDs from YouTube channel ---
# def get_latest_video_ids(channel_id, max_results=5):
#     url = f"https://www.googleapis.com/youtube/v3/search?key={YOUTUBE_API_KEY}&channelId={channel_id}&part=snippet,id&order=date&maxResults={max_results}"
#     response = requests.get(url)
#     videos = response.json().get('items', [])
    
#     valid_videos = []
#     for v in videos:
#         if v['id']['kind'] == 'youtube#video':
#             title = v['snippet']['title']
#             channel_title = v['snippet']['channelTitle']
#             video_id = v['id']['videoId']
#             if "icodeguru" in channel_title.lower():
#                 valid_videos.append((video_id, title))
#     return valid_videos

# # --- Get video transcripts ---
# def get_video_transcripts(video_info):
#     results = []
#     for vid, title in video_info:
#         try:
#             transcript = YouTubeTranscriptApi.get_transcript(vid)
#             text = " ".join([t['text'] for t in transcript])
#             video_link = f"https://www.youtube.com/watch?v={vid}"
#             results.append({
#                 "video_id": vid,
#                 "title": title,
#                 "link": video_link,
#                 "transcript": text
#             })
#         except:
#             continue
#     return results

# # --- Scrape icode.guru ---
# def scrape_icodeguru(base_url=BASE_URL, max_pages=5):
#     visited = set()
#     blocks = []

#     def crawl(url):
#         if url in visited or len(visited) >= max_pages:
#             return
#         visited.add(url)
#         try:
#             res = requests.get(url, timeout=10)
#             soup = BeautifulSoup(res.content, "html.parser")
#             page_text = soup.get_text(separator=" ", strip=True)
#             if len(page_text) > 100:
#                 blocks.append(f"[{url}]({url}):\n{page_text[:1500]}")
#             for link in soup.find_all("a", href=True):
#                 href = link['href']
#                 if href.startswith("/"):
#                     href = base_url + href
#                 if href.startswith(base_url):
#                     crawl(href)
#         except:
#             pass

#     crawl(base_url)
#     return blocks

# # --- Ask Groq ---
# def ask_groq(context, question):
#     messages = [
#         {"role": "system", "content": "You are a helpful assistant. Always provide relevant video and website links if possible."},
#         {"role": "user", "content": f"Context:\n{context}\n\nQuestion: {question}\nAnswer (include links):"}
#     ]
#     chat_completion = groq_client.chat.completions.create(
#         model="llama3-8b-8192",
#         messages=messages,
#     )
#     return chat_completion.choices[0].message.content.strip()

# #--- STREAMLIT APP ---
# def main():
#     st.set_page_config(page_title="EduBot for iCodeGuru", layout="wide")
#     st.title("πŸŽ“ EduBot for @icodeguru0")
#     st.markdown("Ask anything based on the latest YouTube videos and website content of [icode.guru](https://icode.guru).")

#     uploaded_files = st.file_uploader("πŸ“ Optionally upload your knowledge files (txt or json)", type=['txt', 'json'], accept_multiple_files=True)
#     user_question = st.text_input("πŸ’¬ Ask your question:")

#     if user_question:
#         vector_data = load_uploaded_vectors(uploaded_files) if uploaded_files else []

#         # Try vector DB first
#         vector_context = search_vector_data(user_question, vector_data)
#         if vector_context:
#             with st.spinner("🧠 Answering from uploaded knowledge..."):
#                 answer = ask_groq(vector_context, user_question)
#                 st.success(answer)
#         else:
#             # Fallback to real-time data
#             with st.spinner("πŸ“Ί Fetching YouTube videos..."):
#                 video_info = get_latest_video_ids(channel_id, max_results=5)
#                 transcripts = get_video_transcripts(video_info)

#             yt_context = ""
#             relevant_links = []
#             for vid in transcripts:
#                 yt_context += f"\n\n[Video: {vid['title']}]({vid['link']}):\n{vid['transcript'][:1500]}"
#                 if user_question.lower() in vid['transcript'].lower():
#                     relevant_links.append(vid['link'])

#             with st.spinner("🌐 Scraping icode.guru..."):
#                 site_blocks = scrape_icodeguru(BASE_URL, max_pages=5)
#                 site_context = "\n\n".join(site_blocks)

#             full_context = yt_context + "\n\n" + site_context

#             with st.spinner("🧠 Thinking..."):
#                 answer = ask_groq(full_context, user_question)
#                 st.success(answer)

#             if relevant_links:
#                 st.markdown("### πŸ”— Related YouTube Links")
#                 for link in relevant_links:
#                     st.markdown(f"- [Watch Video]({link})")

#     st.markdown("---")
#     st.caption("Powered by YouTube, iCodeGuru, and Groq")

# if __name__ == "__main__":
#     main()



# import nest_asyncio
# import streamlit as st
# import os
# from groq import Groq
# from sentence_transformers import SentenceTransformer
# import chromadb
# from chromadb.utils.embedding_functions import SentenceTransformerEmbeddingFunction

# nest_asyncio.apply()

# # --- CONFIGURATION ---
# GROQ_API_KEY = os.environ.get("GROQ_API_KEY")

# groq_client = Groq(api_key=GROQ_API_KEY)
# embedding_function = SentenceTransformerEmbeddingFunction(
#     model_name="all-MiniLM-L6-v2",
#     device="cpu"
# )


# chroma_client = chromadb.Client()
# collection = chroma_client.get_or_create_collection("icodeguru_knowledge", embedding_function=embedding_function)

# # --- Search persistent vector DB ---
# def search_vector_data(query):
#     results = collection.query(query_texts=[query], n_results=3)
#     if results and results["documents"]:
#         return "\n\n".join([doc for doc in results["documents"][0]])
#     return None

# # --- Ask Groq ---
# def ask_groq(context, question):
#     messages = [
#         {"role": "system", "content": "You are a helpful assistant. Always provide relevant video and website links if possible."},
#         {"role": "user", "content": f"Context:\n{context}\n\nQuestion: {question}\nAnswer (include links):"}
#     ]
#     chat_completion = groq_client.chat.completions.create(
#         model="llama3-8b-8192",
#         messages=messages,
#     )
#     return chat_completion.choices[0].message.content.strip()

# #--- STREAMLIT APP ---
# def main():
#     st.set_page_config(page_title="EduBot for iCodeGuru", layout="wide")
#     st.title("πŸŽ“ EduBot for @icodeguru0")
#     st.markdown("Ask anything based on pre-loaded iCodeGuru knowledge.")

#     user_question = st.text_input("πŸ’¬ Ask your question:")

#     if user_question:
#         # Try vector DB first
#         vector_context = search_vector_data(user_question)
#         if vector_context:
#             with st.spinner("🧠 Answering from knowledge base..."):
#                 answer = ask_groq(vector_context, user_question)
#                 st.success(answer)
#         else:
#             st.warning("⚠️ No relevant answer found in the embedded knowledge.")

#     st.markdown("---")
#     st.caption("Powered by ChromaDB 🧠 and Groq ⚑")

# if __name__ == "__main__":
#     main()





# import nest_asyncio
# import streamlit as st
# import os
# from groq import Groq
# from sentence_transformers import SentenceTransformer
# import chromadb
# from chromadb.utils.embedding_functions import SentenceTransformerEmbeddingFunction
# from chromadb.config import Settings

# # Apply asyncio patch (required for nested event loops in Streamlit)
# nest_asyncio.apply()

# # --- CONFIGURATION ---
# GROQ_API_KEY = os.environ.get("GROQ_API_KEY")
# GROQ_MODEL = "llama3-8b-8192"

# # Initialize Groq client
# groq_client = Groq(api_key=GROQ_API_KEY)

# # Initialize embedding model
# embedding_function = SentenceTransformerEmbeddingFunction(
#     model_name="all-MiniLM-L6-v2",
#     device="cpu"
# )

# # Initialize ChromaDB with persistence
# chroma_client = chromadb.PersistentClient(path="./chroma_db", settings=Settings(anonymized_telemetry=False))
# collection = chroma_client.get_or_create_collection(
#     name="icodeguru_knowledge",
#     embedding_function=embedding_function
# )

# # --- Search embedded knowledge ---
# def search_vector_data(query):
#     try:
#         results = collection.query(query_texts=[query], n_results=3)
#         if results and results["documents"]:
#             return "\n\n".join(results["documents"][0])
#     except Exception as e:
#         st.error(f"Vector search error: {e}")
#     return None

# # --- Ask Groq ---
# def ask_groq(context, question):
#     messages = [
#         {"role": "system", "content": "You are a helpful assistant. Always provide relevant video and website links if possible."},
#         {"role": "user", "content": f"Context:\n{context}\n\nQuestion: {question}\nAnswer (include links):"}
#     ]
#     response = groq_client.chat.completions.create(
#         model=GROQ_MODEL,
#         messages=messages
#     )
#     return response.choices[0].message.content.strip()

# # --- Streamlit UI ---
# def main():
#     st.set_page_config(page_title="EduBot for iCodeGuru", layout="wide")
#     st.title("πŸŽ“ EduBot for @icodeguru0")
#     st.markdown("Ask anything based on pre-loaded iCodeGuru knowledge.")

#     user_question = st.text_input("πŸ’¬ Ask your question:")

#     if user_question:
#         vector_context = search_vector_data(user_question)
#         if vector_context:
#             with st.spinner("🧠 Answering from knowledge base..."):
#                 answer = ask_groq(vector_context, user_question)
#                 st.success(answer)
#         else:
#             st.warning("⚠️ No relevant answer found in the embedded knowledge.")

#     st.markdown("---")
#     st.caption("Powered by ChromaDB 🧠 and Groq ⚑")


#     # βœ… This is the correct way to run the app
# if __name__ == "__main__":
#     main()







import nest_asyncio
import streamlit as st
import os
import json
from groq import Groq
from sentence_transformers import SentenceTransformer
import chromadb
from chromadb.utils.embedding_functions import SentenceTransformerEmbeddingFunction
from chromadb.config import Settings
from langchain.document_loaders import JSONLoader
from langchain.text_splitter import RecursiveCharacterTextSplitter

# Apply asyncio patch (Streamlit fix)
nest_asyncio.apply()

# --- CONFIGURATION ---
GROQ_API_KEY = os.environ.get("GROQ_API_KEY")
GROQ_MODEL = "llama3-8b-8192"

# Initialize Groq client
groq_client = Groq(api_key=GROQ_API_KEY)

# # Explicitly load SentenceTransformer model first to avoid meta tensor bug
# embedding_model = SentenceTransformer "(all-MiniLM-L6-v2)"

# # Pass this model into Chroma's embedding function
# embedding_function = SentenceTransformerEmbeddingFunction(embedding_model=embedding_model)

embedding_function = SentenceTransformerEmbeddingFunction(
    model_name="all-MiniLM-L6-v2",
    device="cpu"
)



# Initialize ChromaDB Persistent Client
chroma_client = chromadb.PersistentClient(path="./chroma_db", settings=Settings(anonymized_telemetry=False))
collection = chroma_client.get_or_create_collection(
    name="icodeguru_knowledge",
    embedding_function=embedding_function
)

# --- Ingest JSON Files from /docs/ ---
def ingest_docs_to_chroma():
    folder_path = "./docs"
    all_docs = []
    for filename in os.listdir(folder_path):
        if filename.endswith(".json"):
            file_path = os.path.join(folder_path, filename)
            loader = JSONLoader(file_path=file_path, jq_schema='.[]')
            docs = loader.load()
            all_docs.extend(docs)
            st.write(f"Loaded {len(docs)} documents from {filename}")

    # Chunk Documents
    text_splitter = RecursiveCharacterTextSplitter(chunk_size=500, chunk_overlap=50)
    chunks = text_splitter.split_documents(all_docs)
    st.write(f"Total chunks created: {len(chunks)}")

    # Add Chunks to ChromaDB
    for chunk in chunks:
        # Flatten list content if necessary
        if isinstance(chunk.page_content, list):
            content = " ".join(str(item) for item in chunk.page_content).strip()
        else:
            content = str(chunk.page_content).strip()
        
        metadata = chunk.metadata
        doc_id = str(hash(content))
        collection.add(documents=[content], metadatas=[metadata], ids=[doc_id])


    st.success("βœ… Knowledge Base Updated Successfully!")

# --- Search embedded knowledge ---
def search_vector_data(query):
    try:
        results = collection.query(query_texts=[query], n_results=3)
        if results and results["documents"]:
            return "\n\n".join(results["documents"][0])
    except Exception as e:
        st.error(f"Vector search error: {e}")
    return None

# --- Ask Groq LLM ---
def ask_groq(context, question):
    messages = [
        {"role": "system", "content": "You are a helpful assistant. Always provide relevant video and website links if possible."},
        {"role": "user", "content": f"Context:\n{context}\n\nQuestion: {question}\nAnswer (include links):"}
    ]
    response = groq_client.chat.completions.create(
        model=GROQ_MODEL,
        messages=messages
    )
    return response.choices[0].message.content.strip()

# --- Streamlit UI ---
def main():
    st.set_page_config(page_title="EduBot for iCodeGuru", layout="wide")
    st.title("πŸŽ“ EduBot for @icodeguru0")
    st.markdown("Ask anything based on pre-loaded iCodeGuru knowledge.")

    # --- Auto Update Knowledge Base at App Start ---
    st.info("πŸ”„ Updating Knowledge Base from /docs/...")
    ingest_docs_to_chroma()
    st.success("βœ… Knowledge Base Loaded Successfully!")

    st.markdown("---")

    user_question = st.text_input("πŸ’¬ Ask your question:")

    if user_question:
        vector_context = search_vector_data(user_question)
        if vector_context:
            with st.spinner("🧠 Answering from knowledge base..."):
                answer = ask_groq(vector_context, user_question)
                st.success(answer)
        else:
            st.warning("⚠️ No relevant answer found in the embedded knowledge.")

    st.markdown("---")
    st.caption("Powered by ChromaDB 🧠 and Groq ⚑")

if __name__ == "__main__":
    main()