aaporosh commited on
Commit
9763f1a
Β·
verified Β·
1 Parent(s): 37ec1fd

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +9 -24
app.py CHANGED
@@ -122,7 +122,7 @@ def process_pdf(uploaded_file):
122
  st.error(f"PDF error: {str(e)}")
123
  return None, None, "", ""
124
 
125
- # Improved summarization
126
  def summarize_pdf(text):
127
  logger.info("Generating summary")
128
  try:
@@ -144,7 +144,7 @@ def summarize_pdf(text):
144
  logger.error(f"Summary error: {str(e)}")
145
  return f"Oops, something went wrong summarizing: {str(e)}"
146
 
147
- # FIXED answer_question function
148
  def answer_question(text_vector_store, code_vector_store, query):
149
  logger.info(f"Processing query: {query}")
150
  try:
@@ -161,13 +161,13 @@ def answer_question(text_vector_store, code_vector_store, query):
161
  if not vector_store:
162
  return "No relevant content found for your query."
163
 
164
- docs = vector_store.similarity_search(query, k=4)
165
  context = "\n\n".join(doc.page_content for doc in docs)
166
  prompt = f"Based on this context: {context}\n\nQuestion: {query}\n\nProvide a friendly, concise answer like a helpful assistant:"
167
  response = qa_pipeline(prompt)[0]['generated_text'].strip()
168
 
169
  if is_code_query:
170
- response = f"Here's the relevant code extracted from the PDF:\n```python\n{context}\n```"
171
 
172
  logger.info("Answer generated")
173
  return f"Got it! Here's the answer:\n\n{response}"
@@ -179,7 +179,7 @@ def answer_question(text_vector_store, code_vector_store, query):
179
  try:
180
  st.set_page_config(page_title="Smart PDF Q&A", page_icon="πŸ“„", layout="wide", initial_sidebar_state="expanded")
181
 
182
- # Enhanced CSS with fixed chatbox colors
183
  st.markdown("""
184
  <style>
185
  /* General styling */
@@ -192,24 +192,9 @@ try:
192
  .stSuccess, .stError { border-radius: 6px; padding: 10px; }
193
 
194
  /* Chat messages */
195
- .stChatMessage {
196
- background-color: #000000 !important;
197
- color: #ffffff !important;
198
- border-radius: 10px;
199
- padding: 12px;
200
- margin: 8px 0;
201
- }
202
- .stChatMessage p,
203
- .stChatMessage span,
204
- .stChatMessage div {
205
- color: #ffffff !important;
206
- }
207
- .stChatMessage.user {
208
- background-color: #111111 !important;
209
- }
210
- .stChatMessage.assistant {
211
- background-color: #1a1a1a !important;
212
- }
213
 
214
  /* Code blocks */
215
  pre { background-color: #252525; color: #d4d4d4; padding: 12px; border-radius: 6px; overflow: auto; font-family: 'Consolas', 'Monaco', monospace; }
@@ -288,4 +273,4 @@ try:
288
 
289
  except Exception as e:
290
  logger.error(f"App initialization failed: {str(e)}")
291
- st.error(f"App failed to start: {str(e)}. Please check the logs or contact support.")
 
122
  st.error(f"PDF error: {str(e)}")
123
  return None, None, "", ""
124
 
125
+ # Improved summarization: More chunks, better concatenation
126
  def summarize_pdf(text):
127
  logger.info("Generating summary")
128
  try:
 
144
  logger.error(f"Summary error: {str(e)}")
145
  return f"Oops, something went wrong summarizing: {str(e)}"
146
 
147
+ # Improved Q&A: Better context, code handling
148
  def answer_question(text_vector_store, code_vector_store, query):
149
  logger.info(f"Processing query: {query}")
150
  try:
 
161
  if not vector_store:
162
  return "No relevant content found for your query."
163
 
164
+ docs = vector_store.similarity_search(query, k=4) # Increased for better context
165
  context = "\n\n".join(doc.page_content for doc in docs)
166
  prompt = f"Based on this context: {context}\n\nQuestion: {query}\n\nProvide a friendly, concise answer like a helpful assistant:"
167
  response = qa_pipeline(prompt)[0]['generated_text'].strip()
168
 
169
  if is_code_query:
170
+ response = f"Here's the relevant code extracted from the PDF:\n```python\n{response}\n```"
171
 
172
  logger.info("Answer generated")
173
  return f"Got it! Here's the answer:\n\n{response}"
 
179
  try:
180
  st.set_page_config(page_title="Smart PDF Q&A", page_icon="πŸ“„", layout="wide", initial_sidebar_state="expanded")
181
 
182
+ # Enhanced CSS for modern, dark-theme friendly design (matching the screenshot's dark mode)
183
  st.markdown("""
184
  <style>
185
  /* General styling */
 
192
  .stSuccess, .stError { border-radius: 6px; padding: 10px; }
193
 
194
  /* Chat messages */
195
+ .stChatMessage { border-radius: 12px; padding: 12px; margin: 8px 0; box-shadow: 0 2px 4px rgba(0,0,0,0.2); }
196
+ .stChatMessage.user { background-color: #3a3a3a; color: #ffffff; }
197
+ .stChatMessage.assistant { background-color: #2a2a2a; color: #ffffff; }
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
198
 
199
  /* Code blocks */
200
  pre { background-color: #252525; color: #d4d4d4; padding: 12px; border-radius: 6px; overflow: auto; font-family: 'Consolas', 'Monaco', monospace; }
 
273
 
274
  except Exception as e:
275
  logger.error(f"App initialization failed: {str(e)}")
276
+ st.error(f"App failed to start: {str(e)}. Please check the logs or contact support.")