Ali2206 commited on
Commit
696fd36
·
verified ·
1 Parent(s): 08baaf7

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +11 -15
app.py CHANGED
@@ -4,11 +4,10 @@ from txagent import TxAgent
4
  from tooluniverse import ToolUniverse
5
  from importlib.resources import files
6
 
7
- # Configure logging
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...")
@@ -41,11 +40,14 @@ def init_txagent():
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):
48
  global tx_app
 
 
 
49
  try:
50
  if not isinstance(message, str) or len(message.strip()) <= 10:
51
  return chat_history + [("", "Please provide a valid message longer than 10 characters.")]
@@ -78,17 +80,12 @@ def respond(message, chat_history, temperature, max_new_tokens, max_tokens, mult
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",
89
- placeholder="Enter your biomedical question...",
90
- lines=3
91
- )
92
 
93
  with gr.Row():
94
  temp = gr.Slider(0, 1, value=0.3, label="Temperature")
@@ -99,7 +96,6 @@ with gr.Blocks(title="TxAgent Biomedical Assistant") as app:
99
 
100
  submit = gr.Button("Submit")
101
  clear = gr.Button("Clear")
102
-
103
  conversation_state = gr.State([])
104
 
105
  submit.click(
@@ -116,13 +112,13 @@ with gr.Blocks(title="TxAgent Biomedical Assistant") as app:
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
 
 
4
  from tooluniverse import ToolUniverse
5
  from importlib.resources import files
6
 
 
7
  logging.basicConfig(level=logging.INFO)
8
  logger = logging.getLogger(__name__)
9
 
10
+ tx_app = None # Global TxAgent instance
11
 
12
  def init_txagent():
13
  logger.info("🔥 Initializing TxAgent...")
 
40
  )
41
 
42
  agent.init_model()
43
+ logger.info("✅ TxAgent fully initialized")
44
  return agent
45
 
46
  def respond(message, chat_history, temperature, max_new_tokens, max_tokens, multi_agent, conversation_state, max_round):
47
  global tx_app
48
+ if tx_app is None:
49
+ return chat_history + [("", "⚠️ Model not ready yet. Please wait a few seconds and try again.")]
50
+
51
  try:
52
  if not isinstance(message, str) or len(message.strip()) <= 10:
53
  return chat_history + [("", "Please provide a valid message longer than 10 characters.")]
 
80
  logger.error(f"Error in respond function: {str(e)}")
81
  yield chat_history + [("", f"⚠️ Error: {str(e)}")]
82
 
83
+ # Top-level app object that HF Spaces can detect
84
  with gr.Blocks(title="TxAgent Biomedical Assistant") as app:
85
  gr.Markdown("# 🧠 TxAgent Biomedical Assistant")
86
 
87
  chatbot = gr.Chatbot(label="Conversation", height=600, type="messages")
88
+ msg = gr.Textbox(label="Your medical query", placeholder="Enter your biomedical question...", lines=3)
 
 
 
 
 
89
 
90
  with gr.Row():
91
  temp = gr.Slider(0, 1, value=0.3, label="Temperature")
 
96
 
97
  submit = gr.Button("Submit")
98
  clear = gr.Button("Clear")
 
99
  conversation_state = gr.State([])
100
 
101
  submit.click(
 
112
  chatbot
113
  )
114
 
115
+ # hidden init trigger on page load
116
+ hidden_button = gr.Button(visible=False)
117
 
118
+ def initialize_agent():
119
  global tx_app
120
  tx_app = init_txagent()
121
  return gr.update(visible=False)
122
 
123
+ app.load(hidden_button.click(fn=initialize_agent))
124