Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,63 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import nest_asyncio
|
| 2 |
+
import openai
|
| 3 |
+
from youtube_transcript_api import YouTubeTranscriptApi
|
| 4 |
+
import requests
|
| 5 |
+
import streamlit as st
|
| 6 |
+
import os
|
| 7 |
+
|
| 8 |
+
nest_asyncio.apply()
|
| 9 |
+
|
| 10 |
+
# --- CONFIGURATION ---
|
| 11 |
+
YOUTUBE_API_KEY = os.environ.get("YOUTUBE_API_KEY") # Set this in HuggingFace Secrets
|
| 12 |
+
channel_id = "UCsv3kmQ5k1eIRG2R9mWN" # @icodeguru0
|
| 13 |
+
|
| 14 |
+
# --- FUNCTION: Fetch recent video IDs from YouTube channel ---
|
| 15 |
+
def get_latest_video_ids(channel_id, max_results=5):
|
| 16 |
+
url = f"https://www.googleapis.com/youtube/v3/search?key={YOUTUBE_API_KEY}&channelId={channel_id}&part=snippet,id&order=date&maxResults={max_results}"
|
| 17 |
+
response = requests.get(url)
|
| 18 |
+
videos = response.json().get('items', [])
|
| 19 |
+
return [v['id']['videoId'] for v in videos if v['id']['kind'] == 'youtube#video']
|
| 20 |
+
|
| 21 |
+
# --- FUNCTION: Get video transcripts ---
|
| 22 |
+
def get_video_transcripts(video_ids):
|
| 23 |
+
all_transcripts = []
|
| 24 |
+
for vid in video_ids:
|
| 25 |
+
try:
|
| 26 |
+
transcript = YouTubeTranscriptApi.get_transcript(vid)
|
| 27 |
+
text = " ".join([t['text'] for t in transcript])
|
| 28 |
+
all_transcripts.append(text)
|
| 29 |
+
except:
|
| 30 |
+
continue
|
| 31 |
+
return all_transcripts
|
| 32 |
+
|
| 33 |
+
# --- FUNCTION: Ask OpenAI ---
|
| 34 |
+
def ask_openai(context, question):
|
| 35 |
+
openai.api_key = os.environ.get("OPENAI_API_KEY") # Set this in HuggingFace Secrets
|
| 36 |
+
prompt = f"Context: {context}\n\nQuestion: {question}\nAnswer:"
|
| 37 |
+
response = openai.ChatCompletion.create(
|
| 38 |
+
model="gpt-3.5-turbo",
|
| 39 |
+
messages=[{"role": "user", "content": prompt}]
|
| 40 |
+
)
|
| 41 |
+
return response.choices[0].message.content.strip()
|
| 42 |
+
|
| 43 |
+
# --- STREAMLIT APP ---
|
| 44 |
+
def main():
|
| 45 |
+
st.set_page_config(page_title="EduBot - YouTube Channel QA", layout="wide")
|
| 46 |
+
st.title("🎓 EduBot for @icodeguru0")
|
| 47 |
+
st.markdown("Ask anything based on the channel’s recent videos.")
|
| 48 |
+
|
| 49 |
+
question = st.text_input("💬 Ask your question here:")
|
| 50 |
+
if question:
|
| 51 |
+
with st.spinner("🔍 Fetching videos and transcripts..."):
|
| 52 |
+
video_ids = get_latest_video_ids(channel_id)
|
| 53 |
+
transcripts = get_video_transcripts(video_ids)
|
| 54 |
+
full_context = "\n\n".join(transcripts)
|
| 55 |
+
with st.spinner("🧠 Thinking..."):
|
| 56 |
+
answer = ask_openai(full_context, question)
|
| 57 |
+
st.success(answer)
|
| 58 |
+
|
| 59 |
+
st.markdown("---")
|
| 60 |
+
st.caption("Powered by YouTube + OpenAI | Built for @icodeguru0")
|
| 61 |
+
|
| 62 |
+
if __name__ == "__main__":
|
| 63 |
+
main()
|