Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -492,17 +492,10 @@
|
|
| 492 |
# if __name__ == "__main__":
|
| 493 |
# main()
|
| 494 |
|
| 495 |
-
|
| 496 |
-
#(only stored data)
|
| 497 |
-
|
| 498 |
-
|
| 499 |
import nest_asyncio
|
| 500 |
import streamlit as st
|
| 501 |
import os
|
| 502 |
-
import requests
|
| 503 |
-
from youtube_transcript_api import YouTubeTranscriptApi
|
| 504 |
from groq import Groq
|
| 505 |
-
from bs4 import BeautifulSoup
|
| 506 |
from sentence_transformers import SentenceTransformer
|
| 507 |
import chromadb
|
| 508 |
from chromadb.utils.embedding_functions import SentenceTransformerEmbeddingFunction
|
|
@@ -510,11 +503,7 @@ from chromadb.utils.embedding_functions import SentenceTransformerEmbeddingFunct
|
|
| 510 |
nest_asyncio.apply()
|
| 511 |
|
| 512 |
# --- CONFIGURATION ---
|
| 513 |
-
YOUTUBE_API_KEY = os.environ.get("YOUTUBE_API_KEY")
|
| 514 |
GROQ_API_KEY = os.environ.get("GROQ_API_KEY")
|
| 515 |
-
channel_id="UCsv3kmQ5k1eIRG2R9mWN-QA" #channelId
|
| 516 |
-
|
| 517 |
-
BASE_URL = "https://icode.guru"
|
| 518 |
|
| 519 |
groq_client = Groq(api_key=GROQ_API_KEY)
|
| 520 |
embedding_function = SentenceTransformerEmbeddingFunction("all-MiniLM-L6-v2")
|
|
@@ -529,67 +518,6 @@ def search_vector_data(query):
|
|
| 529 |
return "\n\n".join([doc for doc in results["documents"][0]])
|
| 530 |
return None
|
| 531 |
|
| 532 |
-
# --- Fetch recent video IDs from YouTube channel ---
|
| 533 |
-
def get_latest_video_ids(channel_id, max_results=5):
|
| 534 |
-
url = f"https://www.googleapis.com/youtube/v3/search?key={YOUTUBE_API_KEY}&channelId={channel_id}&part=snippet,id&order=date&maxResults={max_results}"
|
| 535 |
-
response = requests.get(url)
|
| 536 |
-
videos = response.json().get('items', [])
|
| 537 |
-
|
| 538 |
-
valid_videos = []
|
| 539 |
-
for v in videos:
|
| 540 |
-
if v['id']['kind'] == 'youtube#video':
|
| 541 |
-
title = v['snippet']['title']
|
| 542 |
-
channel_title = v['snippet']['channelTitle']
|
| 543 |
-
video_id = v['id']['videoId']
|
| 544 |
-
if "icodeguru" in channel_title.lower():
|
| 545 |
-
valid_videos.append((video_id, title))
|
| 546 |
-
return valid_videos
|
| 547 |
-
|
| 548 |
-
# --- Get video transcripts ---
|
| 549 |
-
def get_video_transcripts(video_info):
|
| 550 |
-
results = []
|
| 551 |
-
for vid, title in video_info:
|
| 552 |
-
try:
|
| 553 |
-
transcript = YouTubeTranscriptApi.get_transcript(vid)
|
| 554 |
-
text = " ".join([t['text'] for t in transcript])
|
| 555 |
-
video_link = f"https://www.youtube.com/watch?v={vid}"
|
| 556 |
-
results.append({
|
| 557 |
-
"video_id": vid,
|
| 558 |
-
"title": title,
|
| 559 |
-
"link": video_link,
|
| 560 |
-
"transcript": text
|
| 561 |
-
})
|
| 562 |
-
except:
|
| 563 |
-
continue
|
| 564 |
-
return results
|
| 565 |
-
|
| 566 |
-
# --- Scrape icode.guru ---
|
| 567 |
-
def scrape_icodeguru(base_url=BASE_URL, max_pages=5):
|
| 568 |
-
visited = set()
|
| 569 |
-
blocks = []
|
| 570 |
-
|
| 571 |
-
def crawl(url):
|
| 572 |
-
if url in visited or len(visited) >= max_pages:
|
| 573 |
-
return
|
| 574 |
-
visited.add(url)
|
| 575 |
-
try:
|
| 576 |
-
res = requests.get(url, timeout=10)
|
| 577 |
-
soup = BeautifulSoup(res.content, "html.parser")
|
| 578 |
-
page_text = soup.get_text(separator=" ", strip=True)
|
| 579 |
-
if len(page_text) > 100:
|
| 580 |
-
blocks.append(f"[{url}]({url}):\n{page_text[:1500]}")
|
| 581 |
-
for link in soup.find_all("a", href=True):
|
| 582 |
-
href = link['href']
|
| 583 |
-
if href.startswith("/"):
|
| 584 |
-
href = base_url + href
|
| 585 |
-
if href.startswith(base_url):
|
| 586 |
-
crawl(href)
|
| 587 |
-
except:
|
| 588 |
-
pass
|
| 589 |
-
|
| 590 |
-
crawl(base_url)
|
| 591 |
-
return blocks
|
| 592 |
-
|
| 593 |
# --- Ask Groq ---
|
| 594 |
def ask_groq(context, question):
|
| 595 |
messages = [
|
|
@@ -606,7 +534,7 @@ def ask_groq(context, question):
|
|
| 606 |
def main():
|
| 607 |
st.set_page_config(page_title="EduBot for iCodeGuru", layout="wide")
|
| 608 |
st.title("π EduBot for @icodeguru0")
|
| 609 |
-
st.markdown("Ask anything based on
|
| 610 |
|
| 611 |
user_question = st.text_input("π¬ Ask your question:")
|
| 612 |
|
|
@@ -618,35 +546,10 @@ def main():
|
|
| 618 |
answer = ask_groq(vector_context, user_question)
|
| 619 |
st.success(answer)
|
| 620 |
else:
|
| 621 |
-
|
| 622 |
-
with st.spinner("πΊ Fetching YouTube videos..."):
|
| 623 |
-
video_info = get_latest_video_ids(channel_id, max_results=5)
|
| 624 |
-
transcripts = get_video_transcripts(video_info)
|
| 625 |
-
|
| 626 |
-
yt_context = ""
|
| 627 |
-
relevant_links = []
|
| 628 |
-
for vid in transcripts:
|
| 629 |
-
yt_context += f"\n\n[Video: {vid['title']}]({vid['link']}):\n{vid['transcript'][:1500]}"
|
| 630 |
-
if user_question.lower() in vid['transcript'].lower():
|
| 631 |
-
relevant_links.append(vid['link'])
|
| 632 |
-
|
| 633 |
-
with st.spinner("π Scraping icode.guru..."):
|
| 634 |
-
site_blocks = scrape_icodeguru(BASE_URL, max_pages=5)
|
| 635 |
-
site_context = "\n\n".join(site_blocks)
|
| 636 |
-
|
| 637 |
-
full_context = yt_context + "\n\n" + site_context
|
| 638 |
-
|
| 639 |
-
with st.spinner("π§ Thinking..."):
|
| 640 |
-
answer = ask_groq(full_context, user_question)
|
| 641 |
-
st.success(answer)
|
| 642 |
-
|
| 643 |
-
if relevant_links:
|
| 644 |
-
st.markdown("### π Related YouTube Links")
|
| 645 |
-
for link in relevant_links:
|
| 646 |
-
st.markdown(f"- [Watch Video]({link})")
|
| 647 |
|
| 648 |
st.markdown("---")
|
| 649 |
-
st.caption("Powered by
|
| 650 |
|
| 651 |
if __name__ == "__main__":
|
| 652 |
main()
|
|
|
|
| 492 |
# if __name__ == "__main__":
|
| 493 |
# main()
|
| 494 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 495 |
import nest_asyncio
|
| 496 |
import streamlit as st
|
| 497 |
import os
|
|
|
|
|
|
|
| 498 |
from groq import Groq
|
|
|
|
| 499 |
from sentence_transformers import SentenceTransformer
|
| 500 |
import chromadb
|
| 501 |
from chromadb.utils.embedding_functions import SentenceTransformerEmbeddingFunction
|
|
|
|
| 503 |
nest_asyncio.apply()
|
| 504 |
|
| 505 |
# --- CONFIGURATION ---
|
|
|
|
| 506 |
GROQ_API_KEY = os.environ.get("GROQ_API_KEY")
|
|
|
|
|
|
|
|
|
|
| 507 |
|
| 508 |
groq_client = Groq(api_key=GROQ_API_KEY)
|
| 509 |
embedding_function = SentenceTransformerEmbeddingFunction("all-MiniLM-L6-v2")
|
|
|
|
| 518 |
return "\n\n".join([doc for doc in results["documents"][0]])
|
| 519 |
return None
|
| 520 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 521 |
# --- Ask Groq ---
|
| 522 |
def ask_groq(context, question):
|
| 523 |
messages = [
|
|
|
|
| 534 |
def main():
|
| 535 |
st.set_page_config(page_title="EduBot for iCodeGuru", layout="wide")
|
| 536 |
st.title("π EduBot for @icodeguru0")
|
| 537 |
+
st.markdown("Ask anything based on pre-loaded iCodeGuru knowledge.")
|
| 538 |
|
| 539 |
user_question = st.text_input("π¬ Ask your question:")
|
| 540 |
|
|
|
|
| 546 |
answer = ask_groq(vector_context, user_question)
|
| 547 |
st.success(answer)
|
| 548 |
else:
|
| 549 |
+
st.warning("β οΈ No relevant answer found in the embedded knowledge.")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 550 |
|
| 551 |
st.markdown("---")
|
| 552 |
+
st.caption("Powered by ChromaDB π§ and Groq β‘")
|
| 553 |
|
| 554 |
if __name__ == "__main__":
|
| 555 |
main()
|