-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
78 lines (66 loc) · 2.44 KB
/
main.py
File metadata and controls
78 lines (66 loc) · 2.44 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
71
72
73
74
75
76
77
78
import streamlit as st
from dotenv import load_dotenv
from ingest.loader import load_pdf
from ingest.vector_store import create_vector_store
from chains.question_generator import get_question_chain
from chains.feedback import get_feedback_chain
import random
import os
# Load environment and OpenAI API key
load_dotenv()
openai_api_key = os.getenv("OPENAI_API_KEY")
# Streamlit page setup
st.set_page_config(page_title="Study Buddy", page_icon="📚")
st.title("📚 Study Buddy Chatbot")
# Session state
if "question" not in st.session_state:
st.session_state.question = None
st.session_state.context = None
st.session_state.feedback = None
st.session_state.history = []
# Load and index content
@st.cache_resource
def setup_vector_store():
docs = load_pdf("sample.pdf") # updated from main.py
return create_vector_store(docs)
db = setup_vector_store()
q_chain = get_question_chain()
f_chain = get_feedback_chain()
# Sidebar controls
st.sidebar.header("Study Settings")
topic = st.sidebar.text_input("Topic", value="GPU")
if st.sidebar.button("🔄 New Question"):
retrieved = db.similarity_search(topic)
context = random.choice(retrieved).page_content
question = q_chain.invoke({"context": context}).content
st.session_state.question = question
st.session_state.context = context
st.session_state.feedback = None
# Main interface
if st.session_state.question:
st.subheader("🧠 Question")
st.markdown(st.session_state.question)
user_input = st.text_input("✍️ Your Answer")
if st.button("✅ Submit Answer") and user_input:
feedback = f_chain.invoke({
"question": st.session_state.question,
"user_answer": user_input,
"context": st.session_state.context
}).content
st.session_state.feedback = feedback
st.session_state.history.append({
"question": st.session_state.question,
"answer": user_input,
"feedback": feedback
})
if st.session_state.feedback:
st.subheader("🔍 Feedback")
st.success(st.session_state.feedback)
# Review past Q&A
if st.session_state.history:
with st.expander("📜 Review History"):
for idx, item in enumerate(reversed(st.session_state.history), 1):
st.markdown(f"**Q{idx}:** {item['question']}")
st.markdown(f"**Your Answer:** {item['answer']}")
st.markdown(f"**Feedback:** {item['feedback']}")
st.markdown("---")