Update app.py
Browse files
app.py
CHANGED
@@ -12,6 +12,15 @@ import re
|
|
12 |
import psutil
|
13 |
import subprocess
|
14 |
import traceback
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
15 |
|
16 |
# Persistent directory setup
|
17 |
persistent_dir = "/data/hf_cache"
|
@@ -26,6 +35,7 @@ vllm_cache_dir = os.path.join(persistent_dir, "vllm_cache")
|
|
26 |
for directory in [model_cache_dir, tool_cache_dir, file_cache_dir, report_dir, vllm_cache_dir]:
|
27 |
os.makedirs(directory, exist_ok=True)
|
28 |
|
|
|
29 |
os.environ["HF_HOME"] = model_cache_dir
|
30 |
os.environ["TRANSFORMERS_CACHE"] = model_cache_dir
|
31 |
os.environ["VLLM_CACHE_DIR"] = vllm_cache_dir
|
@@ -38,7 +48,7 @@ sys.path.insert(0, src_path)
|
|
38 |
|
39 |
from txagent.txagent import TxAgent
|
40 |
|
41 |
-
# Medical
|
42 |
MEDICAL_KEYWORDS = {'diagnosis', 'assessment', 'plan', 'results', 'medications',
|
43 |
'allergies', 'summary', 'impression', 'findings', 'recommendations'}
|
44 |
|
@@ -53,11 +63,9 @@ def extract_priority_pages(file_path: str, max_pages: int = 20) -> str:
|
|
53 |
try:
|
54 |
text_chunks = []
|
55 |
with pdfplumber.open(file_path) as pdf:
|
56 |
-
# Always include the first three pages
|
57 |
for i, page in enumerate(pdf.pages[:3]):
|
58 |
text = page.extract_text() or ""
|
59 |
text_chunks.append(f"=== Page {i+1} ===\n{text.strip()}")
|
60 |
-
# Include further pages if they contain any medical keywords
|
61 |
for i, page in enumerate(pdf.pages[3:max_pages], start=4):
|
62 |
page_text = page.extract_text() or ""
|
63 |
if any(re.search(rf'\b{kw}\b', page_text.lower()) for kw in MEDICAL_KEYWORDS):
|
@@ -138,6 +146,8 @@ def init_agent():
|
|
138 |
seed=100,
|
139 |
additional_default_tools=[],
|
140 |
)
|
|
|
|
|
141 |
agent.init_model()
|
142 |
log_system_usage("After Load")
|
143 |
print("✅ Agent Ready")
|
@@ -166,7 +176,10 @@ def create_ui(agent):
|
|
166 |
file_hash_value = ""
|
167 |
if files:
|
168 |
with ThreadPoolExecutor(max_workers=4) as executor:
|
169 |
-
futures = [
|
|
|
|
|
|
|
170 |
results = []
|
171 |
for future in as_completed(futures):
|
172 |
try:
|
@@ -194,8 +207,6 @@ Medical Records:
|
|
194 |
print(prompt)
|
195 |
|
196 |
full_response = ""
|
197 |
-
finish_detected = False
|
198 |
-
|
199 |
for chunk in agent.run_gradio_chat(
|
200 |
message=prompt,
|
201 |
history=[],
|
@@ -215,23 +226,19 @@ Medical Records:
|
|
215 |
chunk_content = "".join([c.content for c in chunk if hasattr(c, "content") and c.content])
|
216 |
print("DEBUG: Received list chunk:", chunk_content)
|
217 |
full_response += chunk_content
|
218 |
-
if '"name": "Finish"' in chunk_content:
|
219 |
-
finish_detected = True
|
220 |
else:
|
221 |
print("DEBUG: Received unknown type chunk", type(chunk))
|
222 |
except Exception as e:
|
223 |
print("❌ Error processing chunk:", str(e))
|
224 |
traceback.print_exc()
|
225 |
|
226 |
-
# Yield intermediate
|
227 |
history[-1] = {"role": "assistant", "content": full_response}
|
228 |
yield history, None
|
229 |
|
230 |
-
# Final processing
|
231 |
if not full_response:
|
232 |
full_response = "⚠️ No clear oversights identified or model output was invalid."
|
233 |
|
234 |
-
# Save full report
|
235 |
report_path = None
|
236 |
if file_hash_value:
|
237 |
report_path = os.path.join(report_dir, f"{file_hash_value}_report.txt")
|
|
|
12 |
import psutil
|
13 |
import subprocess
|
14 |
import traceback
|
15 |
+
import torch # For checking CUDA availability
|
16 |
+
|
17 |
+
# Set VLLM logging level to DEBUG for detailed output
|
18 |
+
os.environ["VLLM_LOGGING_LEVEL"] = "DEBUG"
|
19 |
+
|
20 |
+
# If no GPU is available, force CPU usage by hiding CUDA devices
|
21 |
+
if not torch.cuda.is_available():
|
22 |
+
print("No GPU detected. Forcing CPU mode by setting CUDA_VISIBLE_DEVICES to an empty string.")
|
23 |
+
os.environ["CUDA_VISIBLE_DEVICES"] = ""
|
24 |
|
25 |
# Persistent directory setup
|
26 |
persistent_dir = "/data/hf_cache"
|
|
|
35 |
for directory in [model_cache_dir, tool_cache_dir, file_cache_dir, report_dir, vllm_cache_dir]:
|
36 |
os.makedirs(directory, exist_ok=True)
|
37 |
|
38 |
+
# Update environment variables to use HF_HOME
|
39 |
os.environ["HF_HOME"] = model_cache_dir
|
40 |
os.environ["TRANSFORMERS_CACHE"] = model_cache_dir
|
41 |
os.environ["VLLM_CACHE_DIR"] = vllm_cache_dir
|
|
|
48 |
|
49 |
from txagent.txagent import TxAgent
|
50 |
|
51 |
+
# Medical keywords for processing PDF files
|
52 |
MEDICAL_KEYWORDS = {'diagnosis', 'assessment', 'plan', 'results', 'medications',
|
53 |
'allergies', 'summary', 'impression', 'findings', 'recommendations'}
|
54 |
|
|
|
63 |
try:
|
64 |
text_chunks = []
|
65 |
with pdfplumber.open(file_path) as pdf:
|
|
|
66 |
for i, page in enumerate(pdf.pages[:3]):
|
67 |
text = page.extract_text() or ""
|
68 |
text_chunks.append(f"=== Page {i+1} ===\n{text.strip()}")
|
|
|
69 |
for i, page in enumerate(pdf.pages[3:max_pages], start=4):
|
70 |
page_text = page.extract_text() or ""
|
71 |
if any(re.search(rf'\b{kw}\b', page_text.lower()) for kw in MEDICAL_KEYWORDS):
|
|
|
146 |
seed=100,
|
147 |
additional_default_tools=[],
|
148 |
)
|
149 |
+
# This call attempts to load the models. If device inference fails,
|
150 |
+
# it will now produce DEBUG-level logs.
|
151 |
agent.init_model()
|
152 |
log_system_usage("After Load")
|
153 |
print("✅ Agent Ready")
|
|
|
176 |
file_hash_value = ""
|
177 |
if files:
|
178 |
with ThreadPoolExecutor(max_workers=4) as executor:
|
179 |
+
futures = [
|
180 |
+
executor.submit(convert_file_to_json, f.name, f.name.split(".")[-1].lower())
|
181 |
+
for f in files
|
182 |
+
]
|
183 |
results = []
|
184 |
for future in as_completed(futures):
|
185 |
try:
|
|
|
207 |
print(prompt)
|
208 |
|
209 |
full_response = ""
|
|
|
|
|
210 |
for chunk in agent.run_gradio_chat(
|
211 |
message=prompt,
|
212 |
history=[],
|
|
|
226 |
chunk_content = "".join([c.content for c in chunk if hasattr(c, "content") and c.content])
|
227 |
print("DEBUG: Received list chunk:", chunk_content)
|
228 |
full_response += chunk_content
|
|
|
|
|
229 |
else:
|
230 |
print("DEBUG: Received unknown type chunk", type(chunk))
|
231 |
except Exception as e:
|
232 |
print("❌ Error processing chunk:", str(e))
|
233 |
traceback.print_exc()
|
234 |
|
235 |
+
# Yield intermediate raw response for debugging purposes
|
236 |
history[-1] = {"role": "assistant", "content": full_response}
|
237 |
yield history, None
|
238 |
|
|
|
239 |
if not full_response:
|
240 |
full_response = "⚠️ No clear oversights identified or model output was invalid."
|
241 |
|
|
|
242 |
report_path = None
|
243 |
if file_hash_value:
|
244 |
report_path = os.path.join(report_dir, f"{file_hash_value}_report.txt")
|