Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -1,179 +1,97 @@
|
|
1 |
import streamlit as st
|
2 |
-
import os
|
3 |
-
import tempfile
|
4 |
from langchain_community.document_loaders import PyPDFLoader
|
5 |
from langchain.text_splitter import RecursiveCharacterTextSplitter
|
6 |
-
from
|
7 |
-
from
|
8 |
-
from
|
9 |
-
from langchain.
|
10 |
-
from langchain.
|
11 |
-
|
12 |
-
|
13 |
-
|
14 |
-
|
15 |
-
|
16 |
-
st.
|
17 |
-
|
18 |
-
|
19 |
-
|
20 |
-
|
|
|
|
|
21 |
)
|
22 |
|
23 |
-
#
|
24 |
-
|
25 |
-
|
26 |
-
|
27 |
-
|
28 |
-
|
29 |
-
|
30 |
-
|
31 |
-
st.
|
32 |
-
|
33 |
-
|
34 |
-
|
35 |
-
|
36 |
-
|
37 |
-
|
38 |
-
|
39 |
-
|
40 |
-
|
41 |
-
|
42 |
-
|
43 |
-
|
44 |
-
|
45 |
-
|
46 |
-
|
47 |
-
#
|
48 |
-
|
49 |
-
|
50 |
-
|
51 |
-
tmp_path = tmp_file.name
|
52 |
-
|
53 |
-
loader = PyPDFLoader(tmp_path)
|
54 |
-
docs = loader.load()
|
55 |
-
|
56 |
-
text_splitter = RecursiveCharacterTextSplitter(
|
57 |
-
chunk_size=1000,
|
58 |
-
chunk_overlap=200,
|
59 |
-
length_function=len
|
60 |
-
)
|
61 |
-
chunks = text_splitter.split_documents(docs)
|
62 |
-
|
63 |
-
embeddings = HuggingFaceEmbeddings(model_name="BAAI/bge-base-en-v1.5")
|
64 |
-
vector_store = FAISS.from_documents(chunks, embeddings)
|
65 |
-
|
66 |
-
os.unlink(tmp_path)
|
67 |
-
return vector_store
|
68 |
-
|
69 |
-
# RAG Setup
|
70 |
-
def setup_qa_chain(vector_store):
|
71 |
-
llm = ChatOllama(model=MODEL_NAME, temperature=0.3)
|
72 |
-
|
73 |
-
custom_prompt = """
|
74 |
-
You are an expert academic assistant. Answer the question based only on the following context:
|
75 |
-
{context}
|
76 |
-
|
77 |
-
Question: {question}
|
78 |
-
|
79 |
-
Provide a clear, concise answer with page number references. If unsure, say "I couldn't find this information in the document".
|
80 |
-
"""
|
81 |
-
|
82 |
-
prompt = PromptTemplate(
|
83 |
-
template=custom_prompt,
|
84 |
-
input_variables=["context", "question"]
|
85 |
-
)
|
86 |
-
|
87 |
-
retriever = vector_store.as_retriever(search_kwargs={"k": 3})
|
88 |
-
|
89 |
-
qa_chain = (
|
90 |
-
{"context": retriever, "question": RunnablePassthrough()}
|
91 |
-
| prompt
|
92 |
-
| llm
|
93 |
-
| StrOutputParser()
|
94 |
-
)
|
95 |
-
|
96 |
-
return qa_chain
|
97 |
-
|
98 |
-
# Generate questions from chapter
|
99 |
-
def generate_chapter_questions(vector_store, chapter_title):
|
100 |
-
llm = ChatOllama(model=MODEL_NAME, temperature=0.7)
|
101 |
-
|
102 |
-
prompt = PromptTemplate(
|
103 |
-
input_variables=["chapter_title"],
|
104 |
-
template="""
|
105 |
-
You are an expert educator. Generate 5 important questions and answers about '{chapter_title}'
|
106 |
-
that would help students understand key concepts. Format as:
|
107 |
-
|
108 |
-
Q1: [Question]
|
109 |
-
A1: [Answer with page reference]
|
110 |
-
|
111 |
-
Q2: [Question]
|
112 |
-
A2: [Answer with page reference]
|
113 |
-
..."""
|
114 |
)
|
115 |
-
|
116 |
-
chain = prompt | llm | StrOutputParser()
|
117 |
-
return chain.invoke({"chapter_title": chapter_title})
|
118 |
|
119 |
-
#
|
120 |
-
st.
|
121 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
122 |
|
123 |
-
if uploaded_file:
|
124 |
-
with st.spinner("Processing PDF..."):
|
125 |
-
st.session_state.vector_store = process_pdf(uploaded_file)
|
126 |
-
st.success("PDF processed successfully! You can now ask questions.")
|
127 |
-
|
128 |
-
# Main content columns
|
129 |
-
col1, col2 = st.columns([1, 2])
|
130 |
-
|
131 |
-
# Chapter-based Q&A Generator
|
132 |
-
with col1:
|
133 |
-
st.subheader("π Generate Chapter Questions")
|
134 |
-
chapter_title = st.text_input("Enter chapter title/section name:")
|
135 |
-
|
136 |
-
if st.button("Generate Q&A") and chapter_title and st.session_state.vector_store:
|
137 |
-
with st.spinner(f"Generating questions about {chapter_title}..."):
|
138 |
-
questions = generate_chapter_questions(
|
139 |
-
st.session_state.vector_store,
|
140 |
-
chapter_title
|
141 |
-
)
|
142 |
-
st.markdown(f"<div class='qa-box'>{questions}</div>", unsafe_allow_html=True)
|
143 |
-
elif chapter_title and not st.session_state.vector_store:
|
144 |
-
st.warning("Please upload a PDF first")
|
145 |
-
|
146 |
-
# Chat interface
|
147 |
-
with col2:
|
148 |
-
st.subheader("π¬ Ask Anything About the Document")
|
149 |
-
|
150 |
-
for message in st.session_state.messages:
|
151 |
-
with st.chat_message(message["role"]):
|
152 |
-
st.markdown(message["content"])
|
153 |
-
|
154 |
-
if prompt := st.chat_input("Your question..."):
|
155 |
-
if not st.session_state.vector_store:
|
156 |
-
st.warning("Please upload a PDF first")
|
157 |
-
st.stop()
|
158 |
-
|
159 |
-
st.session_state.messages.append({"role": "user", "content": prompt})
|
160 |
-
with st.chat_message("user"):
|
161 |
-
st.markdown(prompt)
|
162 |
-
|
163 |
-
with st.chat_message("assistant"):
|
164 |
-
with st.spinner("Thinking..."):
|
165 |
-
qa_chain = setup_qa_chain(st.session_state.vector_store)
|
166 |
-
response = qa_chain.invoke(prompt)
|
167 |
-
st.markdown(response)
|
168 |
-
st.session_state.messages.append({"role": "assistant", "content": response})
|
169 |
-
|
170 |
-
# Footer
|
171 |
-
st.markdown("---")
|
172 |
-
st.markdown(
|
173 |
-
"""
|
174 |
-
<div class="footer">
|
175 |
-
<p>EduQuery - Helping students learn smarter β’ Powered by Nous-Hermes2 and LangChain</p>
|
176 |
-
</div>
|
177 |
-
""",
|
178 |
-
unsafe_allow_html=True
|
179 |
-
)
|
|
|
1 |
import streamlit as st
|
|
|
|
|
2 |
from langchain_community.document_loaders import PyPDFLoader
|
3 |
from langchain.text_splitter import RecursiveCharacterTextSplitter
|
4 |
+
from langchain.embeddings import HuggingFaceEmbeddings
|
5 |
+
from langchain.vectorstores import FAISS
|
6 |
+
from langchain.chains import ConversationalRetrievalChain
|
7 |
+
from langchain.llms import HuggingFaceHub
|
8 |
+
from langchain.memory import ConversationBufferMemory
|
9 |
+
import os
|
10 |
+
|
11 |
+
# App title and color theme
|
12 |
+
st.set_page_config(page_title="π PDF Q&A Agent", layout="centered", page_icon="π")
|
13 |
+
|
14 |
+
st.markdown(
|
15 |
+
\"\"\"
|
16 |
+
<div style="background-color:#E3E8FF;padding:10px;border-radius:10px">
|
17 |
+
<h2 style="color:#3C3C88;text-align:center">π Student PDF Assistant</h2>
|
18 |
+
<p style="color:#444;text-align:center">Ask questions from your uploaded PDF and generate Q&A for chapters!</p>
|
19 |
+
</div>
|
20 |
+
\"\"\", unsafe_allow_html=True
|
21 |
)
|
22 |
|
23 |
+
# Upload PDF
|
24 |
+
uploaded_file = st.file_uploader("π Upload your PDF file", type=["pdf"])
|
25 |
+
|
26 |
+
if uploaded_file:
|
27 |
+
# Save PDF temporarily
|
28 |
+
with open("uploaded.pdf", "wb") as f:
|
29 |
+
f.write(uploaded_file.read())
|
30 |
+
|
31 |
+
st.success("β
PDF uploaded successfully!")
|
32 |
+
|
33 |
+
# Load and split PDF
|
34 |
+
loader = PyPDFLoader("uploaded.pdf")
|
35 |
+
pages = loader.load_and_split()
|
36 |
+
text_splitter = RecursiveCharacterTextSplitter(chunk_size=800, chunk_overlap=150)
|
37 |
+
chunks = text_splitter.split_documents(pages)
|
38 |
+
|
39 |
+
# Embedding
|
40 |
+
embeddings = HuggingFaceEmbeddings(model_name="sentence-transformers/all-MiniLM-L6-v2")
|
41 |
+
vectordb = FAISS.from_documents(chunks, embeddings)
|
42 |
+
|
43 |
+
# Load Open Source LLM from Hugging Face (Mistral or any lightweight LLM)
|
44 |
+
repo_id = "mistralai/Mistral-7B-Instruct-v0.1"
|
45 |
+
llm = HuggingFaceHub(repo_id=repo_id, model_kwargs={"temperature":0.5, "max_new_tokens":500})
|
46 |
+
|
47 |
+
# Memory and Chain
|
48 |
+
memory = ConversationBufferMemory(memory_key="chat_history", return_messages=True)
|
49 |
+
qa_chain = ConversationalRetrievalChain.from_llm(
|
50 |
+
llm, retriever=vectordb.as_retriever(), memory=memory
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
51 |
)
|
|
|
|
|
|
|
52 |
|
53 |
+
# Chat Interface
|
54 |
+
st.markdown("---")
|
55 |
+
st.markdown("π¬ **Ask a question from the PDF:**")
|
56 |
+
|
57 |
+
if "chat_history" not in st.session_state:
|
58 |
+
st.session_state.chat_history = []
|
59 |
+
|
60 |
+
question = st.text_input("Type your question here...", key="user_input")
|
61 |
+
|
62 |
+
if question:
|
63 |
+
result = qa_chain.run(question)
|
64 |
+
st.session_state.chat_history.append(("You", question))
|
65 |
+
st.session_state.chat_history.append(("Bot", result))
|
66 |
+
|
67 |
+
# Show chat history
|
68 |
+
for sender, msg in st.session_state.chat_history[::-1]:
|
69 |
+
st.markdown(f"**{sender}:** {msg}")
|
70 |
+
|
71 |
+
# Question Generation Button
|
72 |
+
st.markdown("---")
|
73 |
+
if st.button("π Generate Q&A from all chapters"):
|
74 |
+
st.info("Generating questions and answers from the content...")
|
75 |
+
questions = [
|
76 |
+
"What is the main idea of this chapter?",
|
77 |
+
"What are the key points discussed?",
|
78 |
+
"Can you summarize this section?",
|
79 |
+
"Are there any definitions or terms introduced?"
|
80 |
+
]
|
81 |
+
for i, chunk in enumerate(chunks[:3]): # Limit to first 3 chunks for demo
|
82 |
+
st.markdown(f"**Chapter Section {i+1}:**")
|
83 |
+
for q in questions:
|
84 |
+
answer = llm.invoke(q + "\\n" + chunk.page_content[:1000])
|
85 |
+
st.markdown(f"**Q:** {q}")
|
86 |
+
st.markdown(f"**A:** {answer}")
|
87 |
+
st.markdown("---")
|
88 |
+
|
89 |
+
"""
|
90 |
+
|
91 |
+
# Save both files to /mnt/data for user download or deployment
|
92 |
+
with open("/mnt/data/requirements.txt", "w") as f:
|
93 |
+
f.write(requirements_txt.strip())
|
94 |
+
|
95 |
+
with open("/mnt/data/app.py", "w") as f:
|
96 |
+
f.write(app_py.strip())
|
97 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|