Ali2206 commited on
Commit
782e103
·
verified ·
1 Parent(s): 8fa39c7

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +4 -10
app.py CHANGED
@@ -1,6 +1,6 @@
1
  # ✅ Fully optimized app.py for Hugging Face Space with persistent 150GB storage
2
 
3
- #import sys, os, json, gradio as gr, pandas as pd, pdfplumber, hashlib, shutil, re, time
4
  from concurrent.futures import ThreadPoolExecutor, as_completed
5
  from threading import Thread
6
 
@@ -28,11 +28,6 @@ os.environ.update({
28
  LOCAL_TXAGENT_PATH = os.path.join(model_cache_dir, "mims-harvard", "TxAgent-T1-Llama-3.1-8B")
29
  LOCAL_RAG_PATH = os.path.join(model_cache_dir, "mims-harvard", "ToolRAG-T1-GTE-Qwen2-1.5B")
30
 
31
- # Manual download using snapshot_download (only if needed)
32
- # from huggingface_hub import snapshot_download
33
- # snapshot_download("mims-harvard/TxAgent-T1-Llama-3.1-8B", local_dir=LOCAL_TXAGENT_PATH, local_dir_use_symlinks=False)
34
- # snapshot_download("mims-harvard/ToolRAG-T1-GTE-Qwen2-1.5B", local_dir=LOCAL_RAG_PATH, local_dir_use_symlinks=False)
35
-
36
  sys.path.insert(0, os.path.abspath(os.path.join(os.path.dirname(__file__), "src")))
37
  from txagent.txagent import TxAgent
38
 
@@ -104,7 +99,6 @@ def init_agent():
104
  agent.init_model()
105
  return agent
106
 
107
- # Lazy load
108
  agent_container = {"agent": None}
109
  def get_agent():
110
  if agent_container["agent"] is None:
@@ -142,10 +136,10 @@ Medical Records:\n{extracted[:15000]}
142
  if isinstance(chunk, str): final_response += chunk
143
  elif isinstance(chunk, list): final_response += "".join([c.content for c in chunk if hasattr(c, 'content')])
144
  cleaned = final_response.replace("[TOOL_CALLS]", "").strip()
145
- updated_history = history + [{"role": "user", "content": message}, {"role": "assistant", "content": cleaned}]
146
  return updated_history, None
147
  except Exception as e:
148
- return history + [{"role": "user", "content": message}, {"role": "assistant", "content": f"❌ Error: {str(e)}"}], None
149
 
150
  send_btn.click(analyze, inputs=[msg_input, chatbot, state, file_upload], outputs=[chatbot, gr.File()])
151
  msg_input.submit(analyze, inputs=[msg_input, chatbot, state, file_upload], outputs=[chatbot, gr.File()])
@@ -159,4 +153,4 @@ if __name__ == "__main__":
159
  show_error=True,
160
  allowed_paths=["/data/reports"],
161
  share=False
162
- )
 
1
  # ✅ Fully optimized app.py for Hugging Face Space with persistent 150GB storage
2
 
3
+ import sys, os, json, gradio as gr, pandas as pd, pdfplumber, hashlib, shutil, re, time
4
  from concurrent.futures import ThreadPoolExecutor, as_completed
5
  from threading import Thread
6
 
 
28
  LOCAL_TXAGENT_PATH = os.path.join(model_cache_dir, "mims-harvard", "TxAgent-T1-Llama-3.1-8B")
29
  LOCAL_RAG_PATH = os.path.join(model_cache_dir, "mims-harvard", "ToolRAG-T1-GTE-Qwen2-1.5B")
30
 
 
 
 
 
 
31
  sys.path.insert(0, os.path.abspath(os.path.join(os.path.dirname(__file__), "src")))
32
  from txagent.txagent import TxAgent
33
 
 
99
  agent.init_model()
100
  return agent
101
 
 
102
  agent_container = {"agent": None}
103
  def get_agent():
104
  if agent_container["agent"] is None:
 
136
  if isinstance(chunk, str): final_response += chunk
137
  elif isinstance(chunk, list): final_response += "".join([c.content for c in chunk if hasattr(c, 'content')])
138
  cleaned = final_response.replace("[TOOL_CALLS]", "").strip()
139
+ updated_history = history + [[message, cleaned]]
140
  return updated_history, None
141
  except Exception as e:
142
+ return history + [[message, f"❌ Error: {str(e)}"]], None
143
 
144
  send_btn.click(analyze, inputs=[msg_input, chatbot, state, file_upload], outputs=[chatbot, gr.File()])
145
  msg_input.submit(analyze, inputs=[msg_input, chatbot, state, file_upload], outputs=[chatbot, gr.File()])
 
153
  show_error=True,
154
  allowed_paths=["/data/reports"],
155
  share=False
156
+ )