Ali2206 commited on
Commit
3dfd69d
·
verified ·
1 Parent(s): a6968c2

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +65 -138
app.py CHANGED
@@ -11,16 +11,11 @@ import shutil
11
  import re
12
  import psutil
13
  import subprocess
14
- import traceback
15
- import torch
16
-
17
- os.environ["VLLM_LOGGING_LEVEL"] = "DEBUG"
18
- if not torch.cuda.is_available():
19
- print("No GPU detected. Forcing CPU mode by setting CUDA_VISIBLE_DEVICES to an empty string.")
20
- os.environ["CUDA_VISIBLE_DEVICES"] = ""
21
 
 
22
  persistent_dir = "/data/hf_cache"
23
  os.makedirs(persistent_dir, exist_ok=True)
 
24
  model_cache_dir = os.path.join(persistent_dir, "txagent_models")
25
  tool_cache_dir = os.path.join(persistent_dir, "tool_cache")
26
  file_cache_dir = os.path.join(persistent_dir, "cache")
@@ -65,10 +60,7 @@ 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
- debug_msg = f"PDF processing error: {str(e)}"
69
- print(debug_msg)
70
- traceback.print_exc()
71
- return debug_msg
72
 
73
  def convert_file_to_json(file_path: str, file_type: str) -> str:
74
  try:
@@ -99,10 +91,7 @@ def convert_file_to_json(file_path: str, file_type: str) -> str:
99
  f.write(result)
100
  return result
101
  except Exception as e:
102
- error_msg = f"Error processing {os.path.basename(file_path)}: {str(e)}"
103
- print(error_msg)
104
- traceback.print_exc()
105
- return json.dumps({"error": error_msg})
106
 
107
  def log_system_usage(tag=""):
108
  try:
@@ -118,35 +107,29 @@ def log_system_usage(tag=""):
118
  print(f"[{tag}] GPU: {used}MB / {total}MB | Utilization: {util}%")
119
  except Exception as e:
120
  print(f"[{tag}] GPU/CPU monitor failed: {e}")
121
- traceback.print_exc()
122
 
123
  def init_agent():
124
- try:
125
- print("\U0001F501 Initializing model...")
126
- log_system_usage("Before Load")
127
- default_tool_path = os.path.abspath("data/new_tool.json")
128
- target_tool_path = os.path.join(tool_cache_dir, "new_tool.json")
129
- if not os.path.exists(target_tool_path):
130
- shutil.copy(default_tool_path, target_tool_path)
131
-
132
- agent = TxAgent(
133
- model_name="mims-harvard/TxAgent-T1-Llama-3.1-8B",
134
- rag_model_name="mims-harvard/ToolRAG-T1-GTE-Qwen2-1.5B",
135
- tool_files_dict={"new_tool": target_tool_path},
136
- force_finish=True,
137
- enable_checker=True,
138
- step_rag_num=8,
139
- seed=100,
140
- additional_default_tools=[],
141
- )
142
- agent.init_model()
143
- log_system_usage("After Load")
144
- print("✅ Agent Ready")
145
- return agent
146
- except Exception as e:
147
- print("❌ Error initializing agent:", str(e))
148
- traceback.print_exc()
149
- raise e
150
 
151
  def create_ui(agent):
152
  with gr.Blocks(theme=gr.themes.Soft()) as demo:
@@ -158,40 +141,30 @@ def create_ui(agent):
158
  download_output = gr.File(label="Download Full Report")
159
 
160
  def analyze(message: str, history: list, files: list):
161
- try:
162
- history.append({"role": "user", "content": message})
163
- history.append({"role": "assistant", "content": "⏳ Analyzing records for potential oversights..."})
164
- yield history, None
165
-
166
- extracted = ""
167
- file_hash_value = ""
168
- if files:
169
- with ThreadPoolExecutor(max_workers=4) as executor:
170
- futures = [executor.submit(convert_file_to_json, f.name, f.name.split(".")[-1].lower()) for f in files]
171
- results = []
172
- for future in as_completed(futures):
173
- try:
174
- results.append(sanitize_utf8(future.result()))
175
- except Exception as e:
176
- print("❌ Error in file processing:", str(e))
177
- traceback.print_exc()
178
- extracted = "\n".join(results)
179
- file_hash_value = file_hash(files[0].name)
180
-
181
- max_content_length = 8000
182
- prompt = f"""Review these medical records and identify EXACTLY what might have been missed:
183
  1. List potential missed diagnoses
184
  2. Flag any medication conflicts
185
  3. Note incomplete assessments
186
  4. Highlight abnormal results needing follow-up
187
  Medical Records:
188
- {extracted[:max_content_length]}
189
  ### Potential Oversights:
190
  """
191
-
192
- full_response = ""
193
- response_chunks = []
194
-
195
  for chunk in agent.run_gradio_chat(
196
  message=prompt,
197
  history=[],
@@ -199,68 +172,26 @@ Medical Records:
199
  max_new_tokens=2048,
200
  max_token=4096,
201
  call_agent=False,
202
- conversation=[]
203
  ):
204
- try:
205
- chunk_content = ""
206
- if isinstance(chunk, str):
207
- chunk_content = chunk
208
- elif hasattr(chunk, 'content'):
209
- chunk_content = chunk.content
210
- elif isinstance(chunk, list):
211
- chunk_content = "".join([c.content for c in chunk if hasattr(c, "content") and c.content])
212
-
213
- if not chunk_content:
214
- continue
215
-
216
- response_chunks.append(chunk_content)
217
- full_response = "".join(response_chunks)
218
-
219
- display_response = re.split(r"\\[TOOL_CALLS\\].*?$", full_response, flags=re.DOTALL)[0].strip()
220
- display_response = display_response.replace('[TxAgent]', '').strip()
221
-
222
- if len(history) > 1 and history[-2]["role"] == "assistant" and history[-2]["content"] == display_response:
223
- pass
224
- else:
225
- if len(history) > 0 and history[-1]["role"] == "assistant":
226
- history[-1]["content"] = display_response
227
- else:
228
- history.append({"role": "assistant", "content": display_response})
229
-
230
- yield history, None
231
- except Exception as e:
232
- print("❌ Error processing chunk:", str(e))
233
- traceback.print_exc()
234
  continue
 
 
 
 
235
 
236
- if not full_response:
237
- full_response = "⚠️ No clear oversights identified or model output was invalid."
238
- else:
239
- full_response = re.split(r"\\[TOOL_CALLS\\].*?$", full_response, flags=re.DOTALL)[0].strip()
240
- full_response = full_response.replace('[TxAgent]', '').strip()
241
-
242
- report_path = None
243
- if file_hash_value:
244
- report_path = os.path.join(report_dir, f"{file_hash_value}_report.txt")
245
- try:
246
- with open(report_path, "w", encoding="utf-8") as f:
247
- f.write(full_response)
248
- except Exception as e:
249
- print("❌ Error saving report:", str(e))
250
- traceback.print_exc()
251
-
252
- if len(history) > 0 and history[-1]["role"] == "assistant":
253
- history[-1]["content"] = full_response
254
- else:
255
- history.append({"role": "assistant", "content": full_response})
256
 
 
 
257
  yield history, report_path if report_path and os.path.exists(report_path) else None
258
 
259
  except Exception as e:
260
- error_message = f" An error occurred in analyze: {str(e)}"
261
- print(error_message)
262
- traceback.print_exc()
263
- history.append({"role": "assistant", "content": error_message})
264
  yield history, None
265
 
266
  send_btn.click(analyze, inputs=[msg_input, gr.State([]), file_upload], outputs=[chatbot, download_output])
@@ -268,17 +199,13 @@ Medical Records:
268
  return demo
269
 
270
  if __name__ == "__main__":
271
- try:
272
- print("🚀 Launching app...")
273
- agent = init_agent()
274
- demo = create_ui(agent)
275
- demo.queue(api_open=False).launch(
276
- server_name="0.0.0.0",
277
- server_port=7860,
278
- show_error=True,
279
- allowed_paths=[report_dir],
280
- share=False
281
- )
282
- except Exception as e:
283
- print("❌ Fatal error during launch:", str(e))
284
- traceback.print_exc()
 
11
  import re
12
  import psutil
13
  import subprocess
 
 
 
 
 
 
 
14
 
15
+ # Persistent directory
16
  persistent_dir = "/data/hf_cache"
17
  os.makedirs(persistent_dir, exist_ok=True)
18
+
19
  model_cache_dir = os.path.join(persistent_dir, "txagent_models")
20
  tool_cache_dir = os.path.join(persistent_dir, "tool_cache")
21
  file_cache_dir = os.path.join(persistent_dir, "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:
 
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:
 
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, files: list):
144
+ history = history + [{"role": "user", "content": message},
145
+ {"role": "assistant", "content": "⏳ Analyzing records for potential oversights..."}]
146
+ yield history, None
147
+
148
+ extracted = ""
149
+ file_hash_value = ""
150
+ if files:
151
+ with ThreadPoolExecutor(max_workers=4) as executor:
152
+ futures = [executor.submit(convert_file_to_json, f.name, f.name.split(".")[-1].lower()) for f in files]
153
+ results = [sanitize_utf8(f.result()) for f in as_completed(futures)]
154
+ extracted = "\n".join(results)
155
+ file_hash_value = file_hash(files[0].name)
156
+
157
+ prompt = f"""Review these medical records and identify EXACTLY what might have been missed:
 
 
 
 
 
 
 
 
158
  1. List potential missed diagnoses
159
  2. Flag any medication conflicts
160
  3. Note incomplete assessments
161
  4. Highlight abnormal results needing follow-up
162
  Medical Records:
163
+ {extracted[:12000]}
164
  ### Potential Oversights:
165
  """
166
+ response = ""
167
+ try:
 
 
168
  for chunk in agent.run_gradio_chat(
169
  message=prompt,
170
  history=[],
 
172
  max_new_tokens=2048,
173
  max_token=4096,
174
  call_agent=False,
175
+ conversation=[],
176
  ):
177
+ if chunk is None:
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
178
  continue
179
+ if isinstance(chunk, str):
180
+ response += chunk
181
+ elif isinstance(chunk, list):
182
+ response += "".join([c.content for c in chunk if hasattr(c, 'content') and c.content])
183
 
184
+ cleaned = response.split("[TOOL_CALLS]")[0].strip()
185
+ if not cleaned:
186
+ cleaned = "No clear oversights identified. Recommend comprehensive review."
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
187
 
188
+ history[-1] = {"role": "assistant", "content": cleaned}
189
+ report_path = os.path.join(report_dir, f"{file_hash_value}_report.txt") if file_hash_value else None
190
  yield history, report_path if report_path and os.path.exists(report_path) else None
191
 
192
  except Exception as e:
193
+ print("🚨 ERROR:", e)
194
+ history[-1] = {"role": "assistant", "content": f"❌ Error occurred: {str(e)}"}
 
 
195
  yield history, None
196
 
197
  send_btn.click(analyze, inputs=[msg_input, gr.State([]), file_upload], outputs=[chatbot, download_output])
 
199
  return demo
200
 
201
  if __name__ == "__main__":
202
+ print("🚀 Launching app...")
203
+ agent = init_agent()
204
+ demo = create_ui(agent)
205
+ demo.queue(api_open=False).launch(
206
+ server_name="0.0.0.0",
207
+ server_port=7860,
208
+ show_error=True,
209
+ allowed_paths=[report_dir],
210
+ share=False
211
+ )