Ali2206 commited on
Commit
08baaf7
·
verified ·
1 Parent(s): 3be12c2

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +12 -10
app.py CHANGED
@@ -8,11 +8,10 @@ from importlib.resources import files
8
  logging.basicConfig(level=logging.INFO)
9
  logger = logging.getLogger(__name__)
10
 
11
- tx_app = None # Will be initialized later in on_start
12
 
13
  def init_txagent():
14
- """Initialize the TxAgent with proper tool file paths"""
15
- logger.info("Initializing TxAgent...")
16
 
17
  tool_files = {
18
  "opentarget": str(files('tooluniverse.data').joinpath('opentarget_tools.json')),
@@ -21,8 +20,6 @@ def init_txagent():
21
  "monarch": str(files('tooluniverse.data').joinpath('monarch_tools.json'))
22
  }
23
 
24
- logger.info(f"Using tool files at: {tool_files}")
25
-
26
  agent = TxAgent(
27
  model_name="mims-harvard/TxAgent-T1-Llama-3.1-8B",
28
  rag_model_name="mims-harvard/ToolRAG-T1-GTE-Qwen2-1.5B",
@@ -44,7 +41,7 @@ def init_txagent():
44
  )
45
 
46
  agent.init_model()
47
- logger.info("Model loading complete")
48
  return agent
49
 
50
  def respond(message, chat_history, temperature, max_new_tokens, max_tokens, multi_agent, conversation_state, max_round):
@@ -81,11 +78,11 @@ def respond(message, chat_history, temperature, max_new_tokens, max_tokens, mult
81
  logger.error(f"Error in respond function: {str(e)}")
82
  yield chat_history + [("", f"⚠️ Error: {str(e)}")]
83
 
84
- # Define Gradio UI
85
  with gr.Blocks(title="TxAgent Biomedical Assistant") as app:
86
  gr.Markdown("# 🧠 TxAgent Biomedical Assistant")
87
 
88
- chatbot = gr.Chatbot(label="Conversation", height=600)
89
 
90
  msg = gr.Textbox(
91
  label="Your medical query",
@@ -119,8 +116,13 @@ with gr.Blocks(title="TxAgent Biomedical Assistant") as app:
119
  chatbot
120
  )
121
 
122
- @app.on_start
 
 
123
  def load_model():
124
  global tx_app
125
- logger.info("🔥 Loading TxAgent model in Gradio @on_start")
126
  tx_app = init_txagent()
 
 
 
 
 
8
  logging.basicConfig(level=logging.INFO)
9
  logger = logging.getLogger(__name__)
10
 
11
+ tx_app = None # Global to hold the model
12
 
13
  def init_txagent():
14
+ logger.info("🔥 Initializing TxAgent...")
 
15
 
16
  tool_files = {
17
  "opentarget": str(files('tooluniverse.data').joinpath('opentarget_tools.json')),
 
20
  "monarch": str(files('tooluniverse.data').joinpath('monarch_tools.json'))
21
  }
22
 
 
 
23
  agent = TxAgent(
24
  model_name="mims-harvard/TxAgent-T1-Llama-3.1-8B",
25
  rag_model_name="mims-harvard/ToolRAG-T1-GTE-Qwen2-1.5B",
 
41
  )
42
 
43
  agent.init_model()
44
+ logger.info("Model loading complete")
45
  return agent
46
 
47
  def respond(message, chat_history, temperature, max_new_tokens, max_tokens, multi_agent, conversation_state, max_round):
 
78
  logger.error(f"Error in respond function: {str(e)}")
79
  yield chat_history + [("", f"⚠️ Error: {str(e)}")]
80
 
81
+ # Gradio UI
82
  with gr.Blocks(title="TxAgent Biomedical Assistant") as app:
83
  gr.Markdown("# 🧠 TxAgent Biomedical Assistant")
84
 
85
+ chatbot = gr.Chatbot(label="Conversation", height=600, type="messages")
86
 
87
  msg = gr.Textbox(
88
  label="Your medical query",
 
116
  chatbot
117
  )
118
 
119
+ # Hidden button + load trigger to init model safely
120
+ init_button = gr.Button(visible=False)
121
+
122
  def load_model():
123
  global tx_app
 
124
  tx_app = init_txagent()
125
+ return gr.update(visible=False)
126
+
127
+ app.load(init_button.click(fn=load_model))
128
+