Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,95 +1,23 @@
|
|
| 1 |
|
| 2 |
|
| 3 |
-
# import nest_asyncio
|
| 4 |
-
# from youtube_transcript_api import YouTubeTranscriptApi
|
| 5 |
-
# import streamlit as st
|
| 6 |
-
# import os
|
| 7 |
-
# from groq import Groq
|
| 8 |
-
|
| 9 |
-
# nest_asyncio.apply()
|
| 10 |
-
|
| 11 |
-
# # --- CONFIGURATION ---
|
| 12 |
-
# YOUTUBE_API_KEY = os.environ.get("YOUTUBE_API_KEY") # Set in your HuggingFace Secrets
|
| 13 |
-
# channel_id = "UCsv3kmQ5k1eIRG2R9mWN" # @icodeguru0
|
| 14 |
-
|
| 15 |
-
# # Initialize Groq client once
|
| 16 |
-
# groq_client = Groq(api_key=os.environ.get("GROQ_API_KEY"))
|
| 17 |
-
|
| 18 |
-
# # --- FUNCTION: Fetch recent video IDs from YouTube channel ---
|
| 19 |
-
# def get_latest_video_ids(channel_id, max_results=5):
|
| 20 |
-
# import requests
|
| 21 |
-
# url = f"https://www.googleapis.com/youtube/v3/search?key={YOUTUBE_API_KEY}&channelId={channel_id}&part=snippet,id&order=date&maxResults={max_results}"
|
| 22 |
-
# response = requests.get(url)
|
| 23 |
-
# videos = response.json().get('items', [])
|
| 24 |
-
# return [v['id']['videoId'] for v in videos if v['id']['kind'] == 'youtube#video']
|
| 25 |
-
|
| 26 |
-
# # --- FUNCTION: Get video transcripts ---
|
| 27 |
-
# def get_video_transcripts(video_ids):
|
| 28 |
-
# all_transcripts = []
|
| 29 |
-
# for vid in video_ids:
|
| 30 |
-
# try:
|
| 31 |
-
# transcript = YouTubeTranscriptApi.get_transcript(vid)
|
| 32 |
-
# text = " ".join([t['text'] for t in transcript])
|
| 33 |
-
# all_transcripts.append(text)
|
| 34 |
-
# except:
|
| 35 |
-
# continue
|
| 36 |
-
# return all_transcripts
|
| 37 |
-
|
| 38 |
-
# # --- FUNCTION: Ask Groq API using official client ---
|
| 39 |
-
# def ask_groq(context, question):
|
| 40 |
-
# messages = [
|
| 41 |
-
# {"role": "system", "content": "You are a helpful assistant."},
|
| 42 |
-
# {"role": "user", "content": f"Context: {context}\n\nQuestion: {question}\nAnswer:"}
|
| 43 |
-
# ]
|
| 44 |
-
# chat_completion = groq_client.chat.completions.create(
|
| 45 |
-
# model="llama-3.3-70b-versatile", # Or the model you have access to
|
| 46 |
-
# messages=messages,
|
| 47 |
-
# )
|
| 48 |
-
# return chat_completion.choices[0].message.content.strip()
|
| 49 |
-
|
| 50 |
-
# # --- STREAMLIT APP ---
|
| 51 |
-
# def main():
|
| 52 |
-
# st.set_page_config(page_title="EduBot - YouTube Channel QA", layout="wide")
|
| 53 |
-
# st.title("🎓 EduBot for @icodeguru0")
|
| 54 |
-
# st.markdown("Ask anything based on the channel’s recent videos.")
|
| 55 |
-
|
| 56 |
-
# question = st.text_input("💬 Ask your question here:")
|
| 57 |
-
# if question:
|
| 58 |
-
# with st.spinner("🔍 Fetching videos and transcripts..."):
|
| 59 |
-
# video_ids = get_latest_video_ids(channel_id)
|
| 60 |
-
# transcripts = get_video_transcripts(video_ids)
|
| 61 |
-
# full_context = "\n\n".join(transcripts)
|
| 62 |
-
# with st.spinner("🧠 Thinking..."):
|
| 63 |
-
# answer = ask_groq(full_context, question)
|
| 64 |
-
# st.success(answer)
|
| 65 |
-
|
| 66 |
-
# st.markdown("---")
|
| 67 |
-
# st.caption("Powered by YouTube + Groq | Built for @icodeguru0")
|
| 68 |
-
|
| 69 |
-
# if __name__ == "__main__":
|
| 70 |
-
# main()
|
| 71 |
-
|
| 72 |
-
|
| 73 |
import nest_asyncio
|
| 74 |
from youtube_transcript_api import YouTubeTranscriptApi
|
| 75 |
import streamlit as st
|
| 76 |
import os
|
| 77 |
from groq import Groq
|
| 78 |
-
import requests
|
| 79 |
-
from bs4 import BeautifulSoup
|
| 80 |
|
| 81 |
nest_asyncio.apply()
|
| 82 |
|
| 83 |
# --- CONFIGURATION ---
|
| 84 |
YOUTUBE_API_KEY = os.environ.get("YOUTUBE_API_KEY") # Set in your HuggingFace Secrets
|
| 85 |
channel_id = "UCsv3kmQ5k1eIRG2R9mWN" # @icodeguru0
|
| 86 |
-
website_url = "https://icode.guru/" # Automatically included website
|
| 87 |
|
| 88 |
# Initialize Groq client once
|
| 89 |
groq_client = Groq(api_key=os.environ.get("GROQ_API_KEY"))
|
| 90 |
|
| 91 |
# --- FUNCTION: Fetch recent video IDs from YouTube channel ---
|
| 92 |
def get_latest_video_ids(channel_id, max_results=5):
|
|
|
|
| 93 |
url = f"https://www.googleapis.com/youtube/v3/search?key={YOUTUBE_API_KEY}&channelId={channel_id}&part=snippet,id&order=date&maxResults={max_results}"
|
| 94 |
response = requests.get(url)
|
| 95 |
videos = response.json().get('items', [])
|
|
@@ -107,62 +35,36 @@ def get_video_transcripts(video_ids):
|
|
| 107 |
continue
|
| 108 |
return all_transcripts
|
| 109 |
|
| 110 |
-
# --- FUNCTION: Fetch and parse website text content ---
|
| 111 |
-
def fetch_website_text(url):
|
| 112 |
-
try:
|
| 113 |
-
resp = requests.get(url, timeout=10)
|
| 114 |
-
resp.raise_for_status()
|
| 115 |
-
soup = BeautifulSoup(resp.text, "html.parser")
|
| 116 |
-
|
| 117 |
-
# Remove scripts, styles, and nav/footer tags for cleaner text
|
| 118 |
-
for tag in soup(["script", "style", "nav", "footer", "header", "form", "noscript"]):
|
| 119 |
-
tag.decompose()
|
| 120 |
-
|
| 121 |
-
# Get visible text and clean up spaces
|
| 122 |
-
text = soup.get_text(separator=" ", strip=True)
|
| 123 |
-
return text
|
| 124 |
-
except Exception as e:
|
| 125 |
-
print(f"Error fetching website: {e}")
|
| 126 |
-
return ""
|
| 127 |
-
|
| 128 |
# --- FUNCTION: Ask Groq API using official client ---
|
| 129 |
def ask_groq(context, question):
|
| 130 |
messages = [
|
| 131 |
-
"role": "system", "content":
|
| 132 |
-
|
| 133 |
{"role": "user", "content": f"Context: {context}\n\nQuestion: {question}\nAnswer:"}
|
| 134 |
]
|
| 135 |
chat_completion = groq_client.chat.completions.create(
|
| 136 |
-
model="llama-3.3-70b-versatile", # Or
|
| 137 |
messages=messages,
|
| 138 |
)
|
| 139 |
return chat_completion.choices[0].message.content.strip()
|
| 140 |
|
| 141 |
# --- STREAMLIT APP ---
|
| 142 |
def main():
|
| 143 |
-
st.set_page_config(page_title="EduBot - YouTube Channel
|
| 144 |
st.title("🎓 EduBot for @icodeguru0")
|
| 145 |
-
st.markdown(
|
| 146 |
-
st.markdown("Ask anything based on the channel’s recent videos and the included website content.")
|
| 147 |
|
| 148 |
question = st.text_input("💬 Ask your question here:")
|
| 149 |
-
|
| 150 |
if question:
|
| 151 |
-
with st.spinner("🔍 Fetching
|
| 152 |
video_ids = get_latest_video_ids(channel_id)
|
| 153 |
transcripts = get_video_transcripts(video_ids)
|
| 154 |
full_context = "\n\n".join(transcripts)
|
| 155 |
-
|
| 156 |
-
website_text = fetch_website_text(website_url)
|
| 157 |
-
if website_text:
|
| 158 |
-
full_context += "\n\nWebsite Content:\n" + website_text
|
| 159 |
-
|
| 160 |
with st.spinner("🧠 Thinking..."):
|
| 161 |
answer = ask_groq(full_context, question)
|
| 162 |
st.success(answer)
|
| 163 |
|
| 164 |
st.markdown("---")
|
| 165 |
-
st.caption("Powered by YouTube +
|
| 166 |
|
| 167 |
if __name__ == "__main__":
|
| 168 |
main()
|
|
|
|
| 1 |
|
| 2 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 3 |
import nest_asyncio
|
| 4 |
from youtube_transcript_api import YouTubeTranscriptApi
|
| 5 |
import streamlit as st
|
| 6 |
import os
|
| 7 |
from groq import Groq
|
|
|
|
|
|
|
| 8 |
|
| 9 |
nest_asyncio.apply()
|
| 10 |
|
| 11 |
# --- CONFIGURATION ---
|
| 12 |
YOUTUBE_API_KEY = os.environ.get("YOUTUBE_API_KEY") # Set in your HuggingFace Secrets
|
| 13 |
channel_id = "UCsv3kmQ5k1eIRG2R9mWN" # @icodeguru0
|
|
|
|
| 14 |
|
| 15 |
# Initialize Groq client once
|
| 16 |
groq_client = Groq(api_key=os.environ.get("GROQ_API_KEY"))
|
| 17 |
|
| 18 |
# --- FUNCTION: Fetch recent video IDs from YouTube channel ---
|
| 19 |
def get_latest_video_ids(channel_id, max_results=5):
|
| 20 |
+
import requests
|
| 21 |
url = f"https://www.googleapis.com/youtube/v3/search?key={YOUTUBE_API_KEY}&channelId={channel_id}&part=snippet,id&order=date&maxResults={max_results}"
|
| 22 |
response = requests.get(url)
|
| 23 |
videos = response.json().get('items', [])
|
|
|
|
| 35 |
continue
|
| 36 |
return all_transcripts
|
| 37 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 38 |
# --- FUNCTION: Ask Groq API using official client ---
|
| 39 |
def ask_groq(context, question):
|
| 40 |
messages = [
|
| 41 |
+
{"role": "system", "content": "You are a helpful assistant."},
|
|
|
|
| 42 |
{"role": "user", "content": f"Context: {context}\n\nQuestion: {question}\nAnswer:"}
|
| 43 |
]
|
| 44 |
chat_completion = groq_client.chat.completions.create(
|
| 45 |
+
model="llama-3.3-70b-versatile", # Or the model you have access to
|
| 46 |
messages=messages,
|
| 47 |
)
|
| 48 |
return chat_completion.choices[0].message.content.strip()
|
| 49 |
|
| 50 |
# --- STREAMLIT APP ---
|
| 51 |
def main():
|
| 52 |
+
st.set_page_config(page_title="EduBot - YouTube Channel QA", layout="wide")
|
| 53 |
st.title("🎓 EduBot for @icodeguru0")
|
| 54 |
+
st.markdown("Ask anything based on the channel’s recent videos.")
|
|
|
|
| 55 |
|
| 56 |
question = st.text_input("💬 Ask your question here:")
|
|
|
|
| 57 |
if question:
|
| 58 |
+
with st.spinner("🔍 Fetching videos and transcripts..."):
|
| 59 |
video_ids = get_latest_video_ids(channel_id)
|
| 60 |
transcripts = get_video_transcripts(video_ids)
|
| 61 |
full_context = "\n\n".join(transcripts)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 62 |
with st.spinner("🧠 Thinking..."):
|
| 63 |
answer = ask_groq(full_context, question)
|
| 64 |
st.success(answer)
|
| 65 |
|
| 66 |
st.markdown("---")
|
| 67 |
+
st.caption("Powered by YouTube + Groq | Built for @icodeguru0")
|
| 68 |
|
| 69 |
if __name__ == "__main__":
|
| 70 |
main()
|