-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathYT_Sum_Frontend.py
More file actions
70 lines (58 loc) · 2.06 KB
/
Copy pathYT_Sum_Frontend.py
File metadata and controls
70 lines (58 loc) · 2.06 KB
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
import streamlit as st
from backend import build_index # ← your backend file
import os
st.set_page_config(page_title="YouTube RAG QA", layout="wide")
st.title("🎥 YouTube Transcript RAG – Chat Interface")
# -----------------------------
# Sidebar for API Key
# -----------------------------
st.sidebar.header("Configuration")
api_key = st.sidebar.text_input("OpenAI API Key")
if api_key:
os.environ["OPENAI_API_KEY"]=api_key
# -----------------------------
# Session State
# -----------------------------
if "main_chain" not in st.session_state:
st.session_state.main_chain = None
# -----------------------------
# Input: YouTube Video ID
# -----------------------------
video_id = st.text_input("Enter YouTube Video URL or ID")
def extract_video_id(url):
if "youtube.com" in url:
return url.split("v=")[-1].split("&")[0]
if "youtu.be" in url:
return url.split("/")[-1]
return url.strip()
# -----------------------------
# Build Index Button
# -----------------------------
if st.button("Build Index"):
if not api_key:
st.error("Please enter your OpenAI API key.")
if not video_id:
st.error("Please enter a YouTube link or ID.")
else:
vid = extract_video_id(video_id)
with st.spinner("Fetching transcript & building vector index..."):
vector_store, retriever, main_chain = build_index(vid)
st.session_state.main_chain = main_chain
st.success("Index successfully built! Now ask questions.")
st.divider()
# -----------------------------
# Ask Questions
# -----------------------------
if st.session_state.main_chain:
st.subheader("Ask a question about the video")
user_q = st.text_input("Your Question")
if st.button("Ask"):
if not user_q.strip():
st.warning("Please enter a valid question.")
else:
with st.spinner("Thinking..."):
answer = st.session_state.main_chain.invoke(user_q)
st.write("### Answer:")
st.write(answer)
else:
st.info("Build the transcript index first.")