abjasrees commited on
Commit
e79963f
·
verified ·
1 Parent(s): 1124c64

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +46 -14
app.py CHANGED
@@ -1,6 +1,8 @@
1
  # app.py
 
2
  import os
3
  import tempfile
 
4
  import gradio as gr
5
 
6
  from embedding_manager import EmbeddingManager
@@ -10,21 +12,57 @@ from hedis_engine import HedisComplianceEngine
10
  APP_TITLE = "ChartWise AI"
11
  DEFAULT_MEASURE_YEAR = 2024
12
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
13
  # --- Handlers ---
14
  def generate_patient_summary(pdf_file):
15
  try:
16
  if pdf_file is None:
17
  return "⚠️ Please upload a PDF file first."
18
- with tempfile.NamedTemporaryFile(delete=False, suffix=".pdf") as tmp:
19
- tmp.write(pdf_file)
20
- pdf_path = tmp.name
21
 
22
- manager = EmbeddingManager(pdf_path)
23
- vectordb = manager.get_or_create_embeddings()
24
 
25
  summarizer = PatientChartSummarizer(vectordb)
26
  result = summarizer.summarize_chart()
27
- os.unlink(pdf_path)
28
  return result
29
  except Exception as e:
30
  return f"❌ Error processing chart: {e}"
@@ -38,16 +76,11 @@ def generate_hedis_analysis(pdf_file, measure_name, measurement_year):
38
  if not measurement_year:
39
  return "⚠️ Please enter a measurement year."
40
 
41
- with tempfile.NamedTemporaryFile(delete=False, suffix=".pdf") as tmp:
42
- tmp.write(pdf_file)
43
- pdf_path = tmp.name
44
-
45
- manager = EmbeddingManager(pdf_path)
46
- vectordb = manager.get_or_create_embeddings()
47
 
48
  hedis = HedisComplianceEngine(vectordb, measure_name, int(measurement_year))
49
  result = hedis.run()
50
- os.unlink(pdf_path)
51
  return result
52
  except Exception as e:
53
  return f"❌ Error processing HEDIS analysis: {e}"
@@ -109,5 +142,4 @@ with gr.Blocks(theme=simple_theme, title="ChartWise AI") as interface:
109
 
110
  # Spaces auto-runs the script; these hints are fine, too:
111
  if __name__ == "__main__":
112
- # On Spaces, port/host are managed, but these envs are also supported by Gradio. [oai_citation:6‡Gradio](https://www.gradio.app/guides/environment-variables?utm_source=chatgpt.com)
113
  interface.queue().launch(server_name="0.0.0.0", server_port=int(os.environ.get("GRADIO_SERVER_PORT", "7860")))
 
1
  # app.py
2
+
3
  import os
4
  import tempfile
5
+ import hashlib
6
  import gradio as gr
7
 
8
  from embedding_manager import EmbeddingManager
 
12
  APP_TITLE = "ChartWise AI"
13
  DEFAULT_MEASURE_YEAR = 2024
14
 
15
+ # --- Simple in-process cache (per Space runtime) ---
16
+ # Maps content_hash -> vectordb
17
+ _VDB_CACHE = {}
18
+ _LAST_HASH = None # optional: for quick reuse/debug
19
+
20
+ def _pdf_hash(pdf_bytes: bytes) -> str:
21
+ return hashlib.sha256(pdf_bytes).hexdigest()
22
+
23
+ def _get_vectordb_from_bytes(pdf_bytes: bytes):
24
+ """
25
+ Return a cached vectordb for this PDF content if available,
26
+ otherwise build it once and cache it for subsequent calls.
27
+ """
28
+ global _VDB_CACHE, _LAST_HASH
29
+
30
+ content_hash = _pdf_hash(pdf_bytes)
31
+ _LAST_HASH = content_hash
32
+
33
+ if content_hash in _VDB_CACHE:
34
+ print(f"[CACHE] Using cached vectordb for hash {content_hash[:12]}...")
35
+ return _VDB_CACHE[content_hash]
36
+
37
+ # Not cached yet: create temp file, build embeddings once, then cache
38
+ with tempfile.NamedTemporaryFile(delete=False, suffix=".pdf") as tmp:
39
+ tmp.write(pdf_bytes)
40
+ pdf_path = tmp.name
41
+
42
+ try:
43
+ manager = EmbeddingManager(pdf_path) # persists under ./embeddings/<temp_stem>
44
+ vectordb = manager.get_or_create_embeddings()
45
+ _VDB_CACHE[content_hash] = vectordb
46
+ print(f"[CACHE] Cached vectordb for hash {content_hash[:12]}.")
47
+ return vectordb
48
+ finally:
49
+ # Remove only the temp PDF; embeddings are persisted separately by EmbeddingManager
50
+ try:
51
+ os.unlink(pdf_path)
52
+ except Exception:
53
+ pass
54
+
55
  # --- Handlers ---
56
  def generate_patient_summary(pdf_file):
57
  try:
58
  if pdf_file is None:
59
  return "⚠️ Please upload a PDF file first."
 
 
 
60
 
61
+ # Reuse embeddings if already built for this file
62
+ vectordb = _get_vectordb_from_bytes(pdf_file)
63
 
64
  summarizer = PatientChartSummarizer(vectordb)
65
  result = summarizer.summarize_chart()
 
66
  return result
67
  except Exception as e:
68
  return f"❌ Error processing chart: {e}"
 
76
  if not measurement_year:
77
  return "⚠️ Please enter a measurement year."
78
 
79
+ # Reuse embeddings if already built for this file
80
+ vectordb = _get_vectordb_from_bytes(pdf_file)
 
 
 
 
81
 
82
  hedis = HedisComplianceEngine(vectordb, measure_name, int(measurement_year))
83
  result = hedis.run()
 
84
  return result
85
  except Exception as e:
86
  return f"❌ Error processing HEDIS analysis: {e}"
 
142
 
143
  # Spaces auto-runs the script; these hints are fine, too:
144
  if __name__ == "__main__":
 
145
  interface.queue().launch(server_name="0.0.0.0", server_port=int(os.environ.get("GRADIO_SERVER_PORT", "7860")))