-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.py
More file actions
343 lines (261 loc) · 9.84 KB
/
Copy pathapp.py
File metadata and controls
343 lines (261 loc) · 9.84 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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
import streamlit as st
from pdf_processor import extract_text_from_pdf
from rag_pipeline import split_text_into_chunks, create_vector_database, get_relevant_chunks
from gemini_handler import generate_summary, extract_key_insights, generate_citation, answer_question
st.set_page_config(
page_title="ScholarAI",
page_icon="🔬",
layout="wide"
)
# Add this right here ↓
# st.markdown("""
# <style>
# .stSpinner { margin-top: -50px; }
# </style>
# """, unsafe_allow_html=True)
# Initialize session state variables at the very top
# This ensures they always exist before anything else runs
if "processed" not in st.session_state:
st.session_state.processed = False
if "text" not in st.session_state:
st.session_state.text = None
if "vector_db" not in st.session_state:
st.session_state.vector_db = None
if "summary" not in st.session_state:
st.session_state.summary = None
if "insights" not in st.session_state:
st.session_state.insights = None
if "citations" not in st.session_state:
st.session_state.citations = None
if "messages" not in st.session_state:
st.session_state.messages = []
if "file_name" not in st.session_state:
st.session_state.file_name = None
if "input_counter" not in st.session_state:
st.session_state.input_counter = 0
# Header
st.title("🔬 ScholarAI")
st.write("Your Intelligent Research Paper Assistant powered by Google Gemini")
st.markdown("---")
# Sidebar
with st.sidebar:
st.header("Upload Research Paper")
uploaded_file = st.file_uploader(
"Choose a PDF file only",
type=["pdf"]
)
if uploaded_file is not None:
# If a new file is uploaded reset everything
if st.session_state.file_name != uploaded_file.name:
st.session_state.processed = False
st.session_state.text = None
st.session_state.vector_db = None
st.session_state.summary = None
st.session_state.insights = None
st.session_state.citations = None
st.session_state.messages = []
st.session_state.file_name = uploaded_file.name
st.success("Paper uploaded Successfully.")
st.write(f"File : {uploaded_file.name}")
# Main content
if uploaded_file is None:
st.info("Please upload a research paper PDF from the sidebar to get started!")
st.markdown("""
### What ScholarAI can do:
- 📄 **Summarize** your research paper instantly
- 🔍 **Extract** key insights and findings
- 📚 **Generate** academic citations (APA, MLA, Chicago)
- 💬 **Answer** any question about the paper
""")
else:
# Process the file only once
if not st.session_state.processed:
with st.spinner("Reading and processing your paper..."):
text = extract_text_from_pdf(uploaded_file)
chunks = split_text_into_chunks(text)
vector_db = create_vector_database(chunks)
st.session_state.text = text
st.session_state.vector_db = vector_db
st.session_state.processed = True
st.success("Paper processed successfully!")
# Always show tabs as long as file is uploaded
tab1, tab2, tab3, tab4 = st.tabs([
"📄 Summary",
"🔍 Key Insights",
"📚 Citations",
"💬 Ask Questions"
])
# TAB 1 — SUMMARY
with tab1:
st.subheader("Paper Summary")
if st.button("Generate Summary"):
with st.spinner("Generating summary..."):
st.session_state.summary = generate_summary(
st.session_state.text
)
if st.session_state.summary is not None:
st.markdown(st.session_state.summary)
# TAB 2 — KEY INSIGHTS
with tab2:
st.subheader("Key Insights")
if st.button("Extract Key Insights"):
with st.spinner("Extracting key insights..."):
st.session_state.insights = extract_key_insights(
st.session_state.text
)
if st.session_state.insights is not None:
st.markdown(st.session_state.insights)
# TAB 3 — CITATIONS
with tab3:
st.subheader("Academic Citations")
if st.button("Generate Citations"):
with st.spinner("Generating citations..."):
st.session_state.citations = generate_citation(
st.session_state.text
)
if st.session_state.citations is not None:
st.markdown(st.session_state.citations)
# TAB 4 — Q&A CHAT
# with tab4:
# st.subheader("Ask Questions About the Paper")
# # Clear chat button only when messages exist
# if len(st.session_state.messages) > 0:
# if st.button("Clear Chat"):
# st.session_state.messages = []
# st.session_state.question_input = ""
# st.rerun()
# st.markdown("---")
# # Display all previous messages
# for message in st.session_state.messages:
# with st.chat_message(message["role"]):
# st.write(message["content"])
# st.markdown("---")
# # Question input at bottom
# col1, col2 = st.columns([5, 1])
# with col1:
# question = st.text_input(
# label="question",
# key="question_input",
# label_visibility="collapsed",
# placeholder="Ask anything about the paper..."
# )
# with col2:
# ask_button = st.button("Ask")
# # When user clicks Ask button
# if ask_button and question:
# # Save question before clearing input
# user_question = question
# # Clear input box
# st.session_state.question_input = ""
# # Add question to history
# st.session_state.messages.append({
# "role": "user",
# "content": user_question
# })
# # Get answer from Gemini
# with st.spinner("Thinking..."):
# context = get_relevant_chunks(
# st.session_state.vector_db,
# user_question
# )
# answer = answer_question(
# context,
# user_question,
# st.session_state.messages
# )
# # Add answer to history
# st.session_state.messages.append({
# "role": "assistant",
# "content": answer
# })
# # Rerun to refresh page with new messages
# st.rerun()
# TAB 4 — Q&A CHAT
with tab4:
st.subheader("Ask Questions About the Paper")
# Clear chat button only when messages exist
if len(st.session_state.messages) > 0:
if st.button("Clear Chat"):
st.session_state.messages = []
st.rerun()
st.markdown("---")
# Display all previous messages
for message in st.session_state.messages:
with st.chat_message(message["role"]):
st.write(message["content"])
st.markdown("---")
# Question input at bottom
col1, col2 = st.columns([5, 1])
with col1:
question = st.text_input(
label="question",
label_visibility="collapsed",
placeholder="Ask anything about the paper...",
key=f"question_{st.session_state.input_counter}"
)
with col2:
ask_button = st.button("Ask")
# When user clicks Ask button
if ask_button and question:
# Add question to history
st.session_state.messages.append({
"role": "user",
"content": question
})
# Get answer from Gemini
with st.spinner("Thinking..."):
context = get_relevant_chunks(
st.session_state.vector_db,
question
)
answer = answer_question(
context,
question,
st.session_state.messages
)
# Add answer to history
st.session_state.messages.append({
"role": "assistant",
"content": answer
})
# Increment counter to create new input widget
# This effectively clears the input box
st.session_state.input_counter += 1
# Rerun to refresh page
st.rerun()
# Question input at bottom
# col1, col2 = st.columns([5, 1])
# with col1:
# question = st.text_input(
# label="question",
# label_visibility="collapsed",
# placeholder="Ask anything about the paper...",
# value=""
# )
# with col2:
# ask_button = st.button("Ask")
# # When user clicks Ask button
# if ask_button and question:
# # Add question to history
# st.session_state.messages.append({
# "role": "user",
# "content": question
# })
# # Get answer from Gemini
# with st.spinner("Thinking..."):
# context = get_relevant_chunks(
# st.session_state.vector_db,
# question
# )
# answer = answer_question(
# context,
# question,
# st.session_state.messages
# )
# # Add answer to history
# st.session_state.messages.append({
# "role": "assistant",
# "content": answer
# })
# # Rerun to refresh page with new messages
# st.rerun()