Spaces:
Sleeping
Sleeping
File size: 2,159 Bytes
6633acf 523fae0 390db2e 523fae0 390db2e 523fae0 fbdeeab 523fae0 fbdeeab 523fae0 fbdeeab 523fae0 fbdeeab 523fae0 fbdeeab 523fae0 fbdeeab 6633acf fbdeeab | 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 | # main.py
import streamlit as st
from dotenv import load_dotenv
from config import Config
from transcript_utils import extract_video_id, fetch_transcript
from vector_utils import build_vector_store, get_retriever
from qa_pipeline import run_qa
# Load environment variables
load_dotenv()
st.set_page_config(page_title="YouTube Video Chat-QA", layout="centered")
st.title("YouTube Video Chat-QA App")
# Initialize session state
if 'ready' not in st.session_state:
st.session_state.ready = False
if 'messages' not in st.session_state:
st.session_state.messages = []
# Setup section for video link
with st.expander("Setup Video", expanded=not st.session_state.ready):
url = st.text_input("YouTube Video Link", key='input_url')
if st.button("Submit Link"):
vid = extract_video_id(url)
if not vid:
st.error("Invalid YouTube URL.")
else:
st.session_state.video_id = vid
st.session_state.ready = True
# Chat interface after video is ready
def chat_interface():
st.video(f"https://www.youtube.com/watch?v={st.session_state.video_id}")
# Input new message
new_q = st.chat_input("Ask a question about this video...")
if new_q:
st.session_state.messages.append({'role': 'user', 'content': new_q})
transcript = fetch_transcript(st.session_state.video_id, languages=["en", "hi"])
if not transcript:
st.session_state.messages.append({'role': 'assistant', 'content': "Transcript not available."})
else:
vector_store = build_vector_store(transcript, None, Config.EMBEDDING_MODEL)
retriever = get_retriever(vector_store)
answer = run_qa(retriever, new_q)
st.session_state.messages.append({'role': 'assistant', 'content': answer})
# Display chat history
for msg in st.session_state.messages:
st.chat_message(msg['role']).write(msg['content'])
if st.button("Exit Chat"):
for key in ['ready', 'video_id', 'messages', 'input_url']:
if key in st.session_state:
del st.session_state[key]
if st.session_state.ready:
chat_interface() |