dnj0 commited on
Commit
54040b2
Β·
verified Β·
1 Parent(s): e3fc89e

Update src/app.py

Browse files
Files changed (1) hide show
  1. src/app.py +61 -20
src/app.py CHANGED
@@ -10,7 +10,7 @@ from pathlib import Path
10
  # Import optimized versions
11
  from pdf_parser import PDFParser
12
  from vector_store import VectorStore
13
- from rag_system import VisualMultimodalRAG # NEW - Vision model
14
  from config import UPLOAD_FOLDER, MAX_PDF_SIZE_MB
15
 
16
 
@@ -59,6 +59,9 @@ if 'current_tables' not in st.session_state:
59
  if 'processing_results' not in st.session_state: # NEW
60
  st.session_state.processing_results = None
61
 
 
 
 
62
 
63
  # ============================================================================
64
  # MAIN HEADER
@@ -317,13 +320,22 @@ if st.button("πŸ–ΌοΈ Analyze Images Visually & Store Components"):
317
  st.divider()
318
  st.header("❓ Ask Questions About Document")
319
 
 
 
 
 
 
 
 
 
 
320
  question = st.text_area(
321
  "Enter your question:",
322
  height=100,
323
  placeholder="What does the document say about...?"
324
  )
325
 
326
- if st.button("πŸ” Search & Answer"):
327
  if not st.session_state.api_key_set:
328
  st.error("❌ Please set OpenAI API key first")
329
  elif st.session_state.current_text is None:
@@ -332,7 +344,7 @@ if st.button("πŸ” Search & Answer"):
332
  st.error("❌ Please enter a question")
333
  else:
334
  try:
335
- with st.spinner("πŸ”„ Searching and generating answer..."):
336
  print(f"\n{'='*70}")
337
  print(f"QUESTION: {question}")
338
  print(f"{'='*70}")
@@ -340,32 +352,61 @@ if st.button("πŸ” Search & Answer"):
340
  # Search vector store
341
  store = st.session_state.vector_store
342
 
343
- # Add documents to store first if not already added
344
  doc_name = st.session_state.current_document or "current_doc"
345
  doc_data = {
346
  'text': st.session_state.current_text,
347
- 'images': [], # Images already stored via visual analysis
348
- 'tables': [] # Tables already stored via visual analysis
349
  }
350
  store.add_documents(doc_data, doc_name)
351
 
352
- # Search
353
  search_results = store.search(question, n_results=5)
354
 
355
- # Display results
356
- st.write("### Search Results")
357
- for idx, result in enumerate(search_results, 1):
358
- content_type = result.get('type', 'unknown')
359
- distance = result.get('distance', 0)
360
- content = result.get('content', '')
361
-
362
- with st.expander(
363
- f"Result {idx} - {content_type.upper()} "
364
- f"(relevance: {1-distance:.2%})"
365
- ):
366
- st.write(content)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
367
 
368
- st.success("βœ… Search complete! Use results above to understand the document.")
369
 
370
  except Exception as e:
371
  st.error(f"❌ Error processing question: {e}")
 
10
  # Import optimized versions
11
  from pdf_parser import PDFParser
12
  from vector_store import VectorStore
13
+ from rag_system import VisualMultimodalRAG, AnsweringRAG # NEW - Vision model
14
  from config import UPLOAD_FOLDER, MAX_PDF_SIZE_MB
15
 
16
 
 
59
  if 'processing_results' not in st.session_state: # NEW
60
  st.session_state.processing_results = None
61
 
62
+ if 'answering_rag' not in st.session_state:
63
+ st.session_state.answering_rag = None
64
+
65
 
66
  # ============================================================================
67
  # MAIN HEADER
 
320
  st.divider()
321
  st.header("❓ Ask Questions About Document")
322
 
323
+ # Initialize answering system if not done
324
+ if 'answering_rag' not in st.session_state:
325
+ st.session_state.answering_rag = None
326
+
327
+ # Create answering system when API key is set
328
+ if st.session_state.api_key_set and st.session_state.answering_rag is None:
329
+ from rag_system_answering import AnsweringRAG
330
+ st.session_state.answering_rag = AnsweringRAG(api_key=st.session_state.api_key, debug=True)
331
+
332
  question = st.text_area(
333
  "Enter your question:",
334
  height=100,
335
  placeholder="What does the document say about...?"
336
  )
337
 
338
+ if st.button("πŸ” Search & Generate Answer"):
339
  if not st.session_state.api_key_set:
340
  st.error("❌ Please set OpenAI API key first")
341
  elif st.session_state.current_text is None:
 
344
  st.error("❌ Please enter a question")
345
  else:
346
  try:
347
+ with st.spinner("πŸ”„ Searching document and analyzing..."):
348
  print(f"\n{'='*70}")
349
  print(f"QUESTION: {question}")
350
  print(f"{'='*70}")
 
352
  # Search vector store
353
  store = st.session_state.vector_store
354
 
355
+ # Add documents to store if needed
356
  doc_name = st.session_state.current_document or "current_doc"
357
  doc_data = {
358
  'text': st.session_state.current_text,
359
+ 'images': [],
360
+ 'tables': []
361
  }
362
  store.add_documents(doc_data, doc_name)
363
 
364
+ # Search for relevant results
365
  search_results = store.search(question, n_results=5)
366
 
367
+ print(f"\nπŸ“Š Search Results Found: {len(search_results)}")
368
+
369
+ # Analyze results and generate answer
370
+ answering_rag = st.session_state.answering_rag
371
+ result = answering_rag.analyze_and_answer(question, search_results)
372
+
373
+ # Display answer prominently
374
+ st.success("βœ… Analysis complete!")
375
+
376
+ st.subheader("πŸ“ Answer")
377
+
378
+ # Show confidence level
379
+ col1, col2, col3 = st.columns(3)
380
+ with col1:
381
+ confidence_color = {
382
+ 'high': '🟒',
383
+ 'medium': '🟑',
384
+ 'low': 'πŸ”΄'
385
+ }.get(result['confidence'], 'βšͺ')
386
+ st.metric("Confidence", f"{confidence_color} {result['confidence'].upper()}")
387
+ with col2:
388
+ st.metric("Sources Used", result['sources_used'])
389
+ with col3:
390
+ if result['sources_used'] > 0:
391
+ st.metric("Avg Relevance", f"{sum(1-r.get('distance',0) for r in search_results)/len(search_results):.0%}")
392
+
393
+ # Display the generated answer
394
+ st.write(result['answer'])
395
+
396
+ # Show sources
397
+ if st.checkbox("πŸ“š Show Source Documents"):
398
+ st.subheader("Sources Used in Answer")
399
+ for idx, source in enumerate(result['formatted_sources'], 1):
400
+ relevance = source['relevance']
401
+ relevance_bar = "β–ˆ" * int(relevance * 10) + "β–‘" * (10 - int(relevance * 10))
402
+
403
+ with st.expander(
404
+ f"Source {idx} - {source['type'].upper()} "
405
+ f"[{relevance_bar}] {relevance:.0%}"
406
+ ):
407
+ st.write(source['content'])
408
 
409
+ print(f"\nβœ… Answer generation complete!")
410
 
411
  except Exception as e:
412
  st.error(f"❌ Error processing question: {e}")