Update app.py
Browse files
app.py
CHANGED
@@ -11,11 +11,6 @@ import shutil
|
|
11 |
import re
|
12 |
import psutil
|
13 |
import subprocess
|
14 |
-
import logging
|
15 |
-
|
16 |
-
# Configure logging
|
17 |
-
logging.basicConfig(level=logging.DEBUG, format='%(asctime)s - %(name)s - %(levelname)s - %(message)s')
|
18 |
-
logger = logging.getLogger(__name__)
|
19 |
|
20 |
# Persistent directory
|
21 |
persistent_dir = "/data/hf_cache"
|
@@ -65,16 +60,13 @@ def extract_priority_pages(file_path: str, max_pages: int = 20) -> str:
|
|
65 |
text_chunks.append(f"=== Page {i} ===\n{page_text.strip()}")
|
66 |
return "\n\n".join(text_chunks)
|
67 |
except Exception as e:
|
68 |
-
logger.error(f"PDF processing error for {file_path}: {e}")
|
69 |
return f"PDF processing error: {str(e)}"
|
70 |
|
71 |
def convert_file_to_json(file_path: str, file_type: str) -> str:
|
72 |
-
logger.debug(f"Converting file {file_path} (type: {file_type})")
|
73 |
try:
|
74 |
h = file_hash(file_path)
|
75 |
cache_path = os.path.join(file_cache_dir, f"{h}.json")
|
76 |
if os.path.exists(cache_path):
|
77 |
-
logger.debug(f"Using cached JSON for {file_path}")
|
78 |
with open(cache_path, "r", encoding="utf-8") as f:
|
79 |
return f.read()
|
80 |
|
@@ -97,54 +89,47 @@ def convert_file_to_json(file_path: str, file_type: str) -> str:
|
|
97 |
result = json.dumps({"error": f"Unsupported file type: {file_type}"})
|
98 |
with open(cache_path, "w", encoding="utf-8") as f:
|
99 |
f.write(result)
|
100 |
-
logger.debug(f"Cached JSON for {file_path}")
|
101 |
return result
|
102 |
except Exception as e:
|
103 |
-
logger.error(f"Error processing {file_path}: {e}")
|
104 |
return json.dumps({"error": f"Error processing {os.path.basename(file_path)}: {str(e)}"})
|
105 |
|
106 |
def log_system_usage(tag=""):
|
107 |
try:
|
108 |
cpu = psutil.cpu_percent(interval=1)
|
109 |
mem = psutil.virtual_memory()
|
110 |
-
|
111 |
result = subprocess.run(
|
112 |
["nvidia-smi", "--query-gpu=memory.used,memory.total,utilization.gpu", "--format=csv,nounits,noheader"],
|
113 |
capture_output=True, text=True
|
114 |
)
|
115 |
if result.returncode == 0:
|
116 |
used, total, util = result.stdout.strip().split(", ")
|
117 |
-
|
118 |
except Exception as e:
|
119 |
-
|
120 |
|
121 |
def init_agent():
|
122 |
-
|
123 |
log_system_usage("Before Load")
|
124 |
default_tool_path = os.path.abspath("data/new_tool.json")
|
125 |
target_tool_path = os.path.join(tool_cache_dir, "new_tool.json")
|
126 |
if not os.path.exists(target_tool_path):
|
127 |
-
logger.debug(f"Copying tool file from {default_tool_path} to {target_tool_path}")
|
128 |
shutil.copy(default_tool_path, target_tool_path)
|
129 |
|
130 |
-
|
131 |
-
|
132 |
-
|
133 |
-
|
134 |
-
|
135 |
-
|
136 |
-
|
137 |
-
|
138 |
-
|
139 |
-
|
140 |
-
|
141 |
-
|
142 |
-
|
143 |
-
|
144 |
-
return agent
|
145 |
-
except Exception as e:
|
146 |
-
logger.error(f"Failed to initialize agent: {e}", exc_info=True)
|
147 |
-
raise
|
148 |
|
149 |
def create_ui(agent):
|
150 |
with gr.Blocks(theme=gr.themes.Soft()) as demo:
|
@@ -156,34 +141,19 @@ def create_ui(agent):
|
|
156 |
download_output = gr.File(label="Download Full Report")
|
157 |
|
158 |
def analyze(message: str, history: List[dict], files: List):
|
159 |
-
logger.debug(f"Analyze called with message: {message[:100]}, history length: {len(history)}, files: {len(files)}")
|
160 |
-
|
161 |
-
# Initialize history if empty
|
162 |
-
if not history:
|
163 |
-
history = []
|
164 |
-
|
165 |
# Append user message
|
166 |
history.append({"role": "user", "content": message})
|
167 |
history.append({"role": "assistant", "content": "β³ Analyzing records for potential oversights..."})
|
168 |
yield history, None
|
169 |
-
logger.debug("Yielded initial history with analyzing message")
|
170 |
|
171 |
extracted = ""
|
172 |
file_hash_value = ""
|
173 |
if files:
|
174 |
-
|
175 |
-
|
176 |
-
|
177 |
-
|
178 |
-
|
179 |
-
extracted = "\n".join(results)
|
180 |
-
file_hash_value = file_hash(files[0].name) if files else ""
|
181 |
-
logger.debug(f"Extracted file content: {extracted[:100]}")
|
182 |
-
except Exception as e:
|
183 |
-
logger.error(f"File processing failed: {e}")
|
184 |
-
history.append({"role": "assistant", "content": f"β File processing error: {str(e)}"})
|
185 |
-
yield history, None
|
186 |
-
return
|
187 |
|
188 |
prompt = f"""Review these medical records and identify EXACTLY what might have been missed:
|
189 |
1. List potential missed diagnoses
|
@@ -194,54 +164,44 @@ Medical Records:
|
|
194 |
{extracted[:12000]}
|
195 |
### Potential Oversights:
|
196 |
"""
|
197 |
-
logger.debug(f"Constructed prompt: {prompt[:100]}")
|
198 |
|
199 |
try:
|
200 |
# Remove the temporary "Analyzing..." message
|
201 |
if history and history[-1]["content"].startswith("β³"):
|
202 |
history.pop()
|
203 |
-
logger.debug("Removed analyzing message")
|
204 |
|
205 |
# Process agent response
|
206 |
for chunk in agent.run_gradio_chat(
|
207 |
message=prompt,
|
208 |
-
history=
|
209 |
temperature=0.2,
|
210 |
max_new_tokens=2048,
|
211 |
max_token=4096,
|
212 |
call_agent=False,
|
213 |
conversation=[],
|
214 |
):
|
215 |
-
logger.debug(f"Received chunk: {chunk}")
|
216 |
if chunk is None:
|
217 |
-
logger.warning("Chunk is None, skipping")
|
218 |
continue
|
219 |
|
220 |
-
# Handle chunk as a list of ChatMessage objects
|
221 |
if isinstance(chunk, list):
|
222 |
for m in chunk:
|
223 |
if hasattr(m, 'content') and m.content:
|
224 |
history.append({"role": m.role, "content": sanitize_utf8(m.content)})
|
225 |
-
logger.debug(f"Appended message: {m.content[:50]}")
|
226 |
yield history, None
|
227 |
-
# Handle chunk as a string
|
228 |
elif isinstance(chunk, str) and chunk.strip():
|
|
|
229 |
if history and history[-1]["role"] == "assistant":
|
230 |
-
history[-1]["content"] +=
|
231 |
else:
|
232 |
history.append({"role": "assistant", "content": sanitize_utf8(chunk)})
|
233 |
-
logger.debug(f"Updated history with string chunk: {chunk[:50]}")
|
234 |
yield history, None
|
235 |
-
else:
|
236 |
-
logger.warning(f"Unexpected chunk type: {type(chunk)}")
|
237 |
|
238 |
# Provide report path if available
|
239 |
report_path = os.path.join(report_dir, f"{file_hash_value}_report.txt") if file_hash_value else None
|
240 |
-
logger.debug(f"Report path: {report_path}")
|
241 |
yield history, report_path if report_path and os.path.exists(report_path) else None
|
242 |
|
243 |
except Exception as e:
|
244 |
-
|
245 |
history.append({"role": "assistant", "content": f"β Error occurred: {str(e)}"})
|
246 |
yield history, None
|
247 |
|
@@ -250,18 +210,13 @@ Medical Records:
|
|
250 |
return demo
|
251 |
|
252 |
if __name__ == "__main__":
|
253 |
-
|
254 |
-
|
255 |
-
|
256 |
-
|
257 |
-
|
258 |
-
|
259 |
-
|
260 |
-
|
261 |
-
|
262 |
-
|
263 |
-
debug=True # Enable debug mode for better error reporting
|
264 |
-
)
|
265 |
-
except Exception as e:
|
266 |
-
logger.error(f"Failed to launch app: {e}", exc_info=True)
|
267 |
-
raise
|
|
|
11 |
import re
|
12 |
import psutil
|
13 |
import subprocess
|
|
|
|
|
|
|
|
|
|
|
14 |
|
15 |
# Persistent directory
|
16 |
persistent_dir = "/data/hf_cache"
|
|
|
60 |
text_chunks.append(f"=== Page {i} ===\n{page_text.strip()}")
|
61 |
return "\n\n".join(text_chunks)
|
62 |
except Exception as e:
|
|
|
63 |
return f"PDF processing error: {str(e)}"
|
64 |
|
65 |
def convert_file_to_json(file_path: str, file_type: str) -> str:
|
|
|
66 |
try:
|
67 |
h = file_hash(file_path)
|
68 |
cache_path = os.path.join(file_cache_dir, f"{h}.json")
|
69 |
if os.path.exists(cache_path):
|
|
|
70 |
with open(cache_path, "r", encoding="utf-8") as f:
|
71 |
return f.read()
|
72 |
|
|
|
89 |
result = json.dumps({"error": f"Unsupported file type: {file_type}"})
|
90 |
with open(cache_path, "w", encoding="utf-8") as f:
|
91 |
f.write(result)
|
|
|
92 |
return result
|
93 |
except Exception as e:
|
|
|
94 |
return json.dumps({"error": f"Error processing {os.path.basename(file_path)}: {str(e)}"})
|
95 |
|
96 |
def log_system_usage(tag=""):
|
97 |
try:
|
98 |
cpu = psutil.cpu_percent(interval=1)
|
99 |
mem = psutil.virtual_memory()
|
100 |
+
print(f"[{tag}] CPU: {cpu}% | RAM: {mem.used // (1024**2)}MB / {mem.total // (1024**2)}MB")
|
101 |
result = subprocess.run(
|
102 |
["nvidia-smi", "--query-gpu=memory.used,memory.total,utilization.gpu", "--format=csv,nounits,noheader"],
|
103 |
capture_output=True, text=True
|
104 |
)
|
105 |
if result.returncode == 0:
|
106 |
used, total, util = result.stdout.strip().split(", ")
|
107 |
+
print(f"[{tag}] GPU: {used}MB / {total}MB | Utilization: {util}%")
|
108 |
except Exception as e:
|
109 |
+
print(f"[{tag}] GPU/CPU monitor failed: {e}")
|
110 |
|
111 |
def init_agent():
|
112 |
+
print("π Initializing model...")
|
113 |
log_system_usage("Before Load")
|
114 |
default_tool_path = os.path.abspath("data/new_tool.json")
|
115 |
target_tool_path = os.path.join(tool_cache_dir, "new_tool.json")
|
116 |
if not os.path.exists(target_tool_path):
|
|
|
117 |
shutil.copy(default_tool_path, target_tool_path)
|
118 |
|
119 |
+
agent = TxAgent(
|
120 |
+
model_name="mims-harvard/TxAgent-T1-Llama-3.1-8B",
|
121 |
+
rag_model_name="mims-harvard/ToolRAG-T1-GTE-Qwen2-1.5B",
|
122 |
+
tool_files_dict={"new_tool": target_tool_path},
|
123 |
+
force_finish=True,
|
124 |
+
enable_checker=True,
|
125 |
+
step_rag_num=8,
|
126 |
+
seed=100,
|
127 |
+
additional_default_tools=[],
|
128 |
+
)
|
129 |
+
agent.init_model()
|
130 |
+
log_system_usage("After Load")
|
131 |
+
print("β
Agent Ready")
|
132 |
+
return agent
|
|
|
|
|
|
|
|
|
133 |
|
134 |
def create_ui(agent):
|
135 |
with gr.Blocks(theme=gr.themes.Soft()) as demo:
|
|
|
141 |
download_output = gr.File(label="Download Full Report")
|
142 |
|
143 |
def analyze(message: str, history: List[dict], files: List):
|
|
|
|
|
|
|
|
|
|
|
|
|
144 |
# Append user message
|
145 |
history.append({"role": "user", "content": message})
|
146 |
history.append({"role": "assistant", "content": "β³ Analyzing records for potential oversights..."})
|
147 |
yield history, None
|
|
|
148 |
|
149 |
extracted = ""
|
150 |
file_hash_value = ""
|
151 |
if files:
|
152 |
+
with ThreadPoolExecutor(max_workers=4) as executor:
|
153 |
+
futures = [executor.submit(convert_file_to_json, f.name, f.name.split(".")[-1].lower()) for f in files]
|
154 |
+
results = [sanitize_utf8(f.result()) for f in as_completed(futures)]
|
155 |
+
extracted = "\n".join(results)
|
156 |
+
file_hash_value = file_hash(files[0].name) if files else ""
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
157 |
|
158 |
prompt = f"""Review these medical records and identify EXACTLY what might have been missed:
|
159 |
1. List potential missed diagnoses
|
|
|
164 |
{extracted[:12000]}
|
165 |
### Potential Oversights:
|
166 |
"""
|
|
|
167 |
|
168 |
try:
|
169 |
# Remove the temporary "Analyzing..." message
|
170 |
if history and history[-1]["content"].startswith("β³"):
|
171 |
history.pop()
|
|
|
172 |
|
173 |
# Process agent response
|
174 |
for chunk in agent.run_gradio_chat(
|
175 |
message=prompt,
|
176 |
+
history=[],
|
177 |
temperature=0.2,
|
178 |
max_new_tokens=2048,
|
179 |
max_token=4096,
|
180 |
call_agent=False,
|
181 |
conversation=[],
|
182 |
):
|
|
|
183 |
if chunk is None:
|
|
|
184 |
continue
|
185 |
|
|
|
186 |
if isinstance(chunk, list):
|
187 |
for m in chunk:
|
188 |
if hasattr(m, 'content') and m.content:
|
189 |
history.append({"role": m.role, "content": sanitize_utf8(m.content)})
|
|
|
190 |
yield history, None
|
|
|
191 |
elif isinstance(chunk, str) and chunk.strip():
|
192 |
+
# Append new assistant message or update the last one
|
193 |
if history and history[-1]["role"] == "assistant":
|
194 |
+
history[-1]["content"] += sanitize_utf8(chunk)
|
195 |
else:
|
196 |
history.append({"role": "assistant", "content": sanitize_utf8(chunk)})
|
|
|
197 |
yield history, None
|
|
|
|
|
198 |
|
199 |
# Provide report path if available
|
200 |
report_path = os.path.join(report_dir, f"{file_hash_value}_report.txt") if file_hash_value else None
|
|
|
201 |
yield history, report_path if report_path and os.path.exists(report_path) else None
|
202 |
|
203 |
except Exception as e:
|
204 |
+
print("π¨ ERROR:", e)
|
205 |
history.append({"role": "assistant", "content": f"β Error occurred: {str(e)}"})
|
206 |
yield history, None
|
207 |
|
|
|
210 |
return demo
|
211 |
|
212 |
if __name__ == "__main__":
|
213 |
+
print("π Launching app...")
|
214 |
+
agent = init_agent()
|
215 |
+
demo = create_ui(agent)
|
216 |
+
demo.queue(api_open=False).launch(
|
217 |
+
server_name="0.0.0.0",
|
218 |
+
server_port=7860,
|
219 |
+
show_error=True,
|
220 |
+
allowed_paths=[report_dir],
|
221 |
+
share=False
|
222 |
+
)
|
|
|
|
|
|
|
|
|
|