Ali2206 commited on
Commit
0cec600
·
verified ·
1 Parent(s): c47b2de

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +285 -127
app.py CHANGED
@@ -1,135 +1,293 @@
 
 
 
 
 
1
  import gradio as gr
2
- import logging
3
  import os
4
- import multiprocessing
5
-
6
- # Setup logging
7
- logging.basicConfig(level=logging.INFO)
8
- logger = logging.getLogger(__name__)
9
-
10
- tx_app = None
11
- TOOL_CACHE_PATH = "/home/user/.cache/tool_embeddings_done"
12
-
13
- # Chatbot response function
14
- def respond(message, chat_history, temperature, max_new_tokens, max_tokens, multi_agent, conversation_state, max_round):
15
- global tx_app
16
- if tx_app is None:
17
- return chat_history + [("", "⚠️ Model is still loading. Please wait a few seconds and try again.")]
18
-
19
- try:
20
- if not isinstance(message, str) or len(message.strip()) < 10:
21
- return chat_history + [("", "Please enter a longer message.")]
22
-
23
- if chat_history and isinstance(chat_history[0], dict):
24
- chat_history = [(h["role"], h["content"]) for h in chat_history if "role" in h and "content" in h]
25
-
26
- response = ""
27
- for chunk in tx_app.run_gradio_chat(
28
- message=message.strip(),
29
- history=chat_history,
30
- temperature=temperature,
31
- max_new_tokens=max_new_tokens,
32
- max_token=max_tokens,
33
- call_agent=multi_agent,
34
- conversation=conversation_state,
35
- max_round=max_round,
36
- seed=42,
37
- ):
38
- if isinstance(chunk, dict):
39
- response += chunk.get("content", "")
40
- elif isinstance(chunk, str):
41
- response += chunk
42
- else:
43
- response += str(chunk)
44
-
45
- yield chat_history + [("user", message), ("assistant", response)]
46
- except Exception as e:
47
- logger.error(f"Respond error: {e}")
48
- yield chat_history + [("", f"⚠️ Error: {e}")]
49
-
50
- # === Gradio UI ===
51
- with gr.Blocks(title="TxAgent Biomedical Assistant") as app:
52
- gr.Markdown("# 🧠 TxAgent Biomedical Assistant")
53
-
54
- chatbot = gr.Chatbot(label="Conversation", height=600, type="messages")
55
- msg = gr.Textbox(label="Your medical query", placeholder="Type your biomedical question...", lines=3)
56
-
57
- with gr.Row():
58
- temp = gr.Slider(0, 1, value=0.3, label="Temperature")
59
- max_new_tokens = gr.Slider(128, 4096, value=1024, label="Max New Tokens")
60
- max_tokens = gr.Slider(128, 81920, value=81920, label="Max Total Tokens")
61
- max_rounds = gr.Slider(1, 30, value=10, label="Max Rounds")
62
- multi_agent = gr.Checkbox(label="Multi-Agent Mode")
63
-
64
- conversation_state = gr.State([])
65
- submit = gr.Button("Submit")
66
- clear = gr.Button("Clear")
67
-
68
- submit.click(
69
- respond,
70
- [msg, chatbot, temp, max_new_tokens, max_tokens, multi_agent, conversation_state, max_rounds],
71
- chatbot
72
- )
73
- clear.click(lambda: [], None, chatbot)
74
- msg.submit(
75
- respond,
76
- [msg, chatbot, temp, max_new_tokens, max_tokens, multi_agent, conversation_state, max_rounds],
77
- chatbot
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
78
  )
79
 
80
- # === Safe model initialization ===
81
- if __name__ == "__main__":
82
- multiprocessing.set_start_method("spawn", force=True)
83
-
84
- import tooluniverse
85
- from txagent import TxAgent
86
- from importlib.resources import files
87
-
88
- # ✅ Patch ToolUniverse to prevent exit() after embedding
89
- original_infer = tooluniverse.ToolUniverse.infer_tool_embeddings
90
-
91
- def patched_infer(self, *args, **kwargs):
92
- original_infer(self, *args, **kwargs)
93
- print("✅ Patched: Skipping forced exit() after embedding.")
94
-
95
- tooluniverse.ToolUniverse.infer_tool_embeddings = patched_infer
96
-
97
- logger.info("🔥 Initializing TxAgent...")
98
-
99
- tool_files = {
100
- "opentarget": str(files('tooluniverse.data').joinpath('opentarget_tools.json')),
101
- "fda_drug_label": str(files('tooluniverse.data').joinpath('fda_drug_labeling_tools.json')),
102
- "special_tools": str(files('tooluniverse.data').joinpath('special_tools.json')),
103
- "monarch": str(files('tooluniverse.data').joinpath('monarch_tools.json'))
104
- }
105
-
106
- tx_app = TxAgent(
107
- model_name="mims-harvard/TxAgent-T1-Llama-3.1-8B",
108
- rag_model_name="mims-harvard/ToolRAG-T1-GTE-Qwen2-1.5B",
109
- tool_files_dict=tool_files,
110
- enable_finish=True,
111
- enable_rag=True,
112
- enable_summary=False,
113
- init_rag_num=0,
114
- step_rag_num=10,
115
- summary_mode='step',
116
- summary_skip_last_k=0,
117
- summary_context_length=None,
118
- force_finish=True,
119
- avoid_repeat=True,
120
- seed=42,
121
- enable_checker=True,
122
- enable_chat=False,
123
- additional_default_tools=["DirectResponse", "RequireClarification"]
124
  )
 
125
 
126
- # 🚀 Run full embedding once, then cache
127
- if not os.path.exists(TOOL_CACHE_PATH):
128
- tx_app.init_model()
129
- os.makedirs(os.path.dirname(TOOL_CACHE_PATH), exist_ok=True)
130
- with open(TOOL_CACHE_PATH, "w") as f:
131
- f.write("done")
 
 
 
 
 
 
 
 
 
 
 
 
 
132
  else:
133
- tx_app.init_model(skip_tool_embedding=True)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
134
 
135
- logger.info("✅ TxAgent ready.")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import random
2
+ import datetime
3
+ import sys
4
+ from txagent import TxAgent
5
+ import spaces
6
  import gradio as gr
 
7
  import os
8
+ import os
9
+
10
+ # Determine the directory where the current file is located
11
+ current_dir = os.path.dirname(os.path.abspath(__file__))
12
+ os.environ["MKL_THREADING_LAYER"] = "GNU"
13
+
14
+ # Set an environment variable
15
+ HF_TOKEN = os.environ.get("HF_TOKEN", None)
16
+
17
+
18
+ DESCRIPTION = '''
19
+ <div>
20
+ <h1 style="text-align: center;">TxAgent: An AI Agent for Therapeutic Reasoning Across a Universe of Tools </h1>
21
+ </div>
22
+ '''
23
+ INTRO = """
24
+ Precision therapeutics require multimodal adaptive models that provide personalized treatment recommendations. We introduce TxAgent, an AI agent that leverages multi-step reasoning and real-time biomedical knowledge retrieval across a toolbox of 211 expert-curated tools to navigate complex drug interactions, contraindications, and patient-specific treatment strategies, delivering evidence-grounded therapeutic decisions. TxAgent executes goal-oriented tool selection and iterative function calls to solve therapeutic tasks that require deep clinical understanding and cross-source validation. The ToolUniverse consolidates 211 tools linked to trusted sources, including all US FDA-approved drugs since 1939 and validated clinical insights from Open Targets.
25
+ """
26
+
27
+ LICENSE = """
28
+ We welcome your feedback and suggestions to enhance your experience with TxAgent, and if you're interested in collaboration, please email Marinka Zitnik and Shanghua Gao.
29
+
30
+ ### Medical Advice Disclaimer
31
+ DISCLAIMER: THIS WEBSITE DOES NOT PROVIDE MEDICAL ADVICE
32
+ The information, including but not limited to, text, graphics, images and other material contained on this website are for informational purposes only. No material on this site is intended to be a substitute for professional medical advice, diagnosis or treatment. Always seek the advice of your physician or other qualified health care provider with any questions you may have regarding a medical condition or treatment and before undertaking a new health care regimen, and never disregard professional medical advice or delay in seeking it because of something you have read on this website.
33
+ """
34
+
35
+ PLACEHOLDER = """
36
+ <div style="padding: 30px; text-align: center; display: flex; flex-direction: column; align-items: center;">
37
+ <h1 style="font-size: 28px; margin-bottom: 2px; opacity: 0.55;">TxAgent</h1>
38
+ <p style="font-size: 18px; margin-bottom: 2px; opacity: 0.65;">Tips before using TxAgent:</p>
39
+ <p style="font-size: 18px; margin-bottom: 2px; opacity: 0.55;">Please click clear🗑️
40
+ (top-right) to remove previous context before sumbmitting a new question.</p>
41
+ <p style="font-size: 18px; margin-bottom: 2px; opacity: 0.55;">Click retry🔄 (below message) to get multiple versions of the answer.</p>
42
+ </div>
43
+ """
44
+
45
+ css = """
46
+ h1 {
47
+ text-align: center;
48
+ display: block;
49
+ }
50
+
51
+ #duplicate-button {
52
+ margin: auto;
53
+ color: white;
54
+ background: #1565c0;
55
+ border-radius: 100vh;
56
+ }
57
+ .small-button button {
58
+ font-size: 12px !important;
59
+ padding: 4px 8px !important;
60
+ height: 6px !important;
61
+ width: 4px !important;
62
+ }
63
+ .gradio-accordion {
64
+ margin-top: 0px !important;
65
+ margin-bottom: 0px !important;
66
+ }
67
+ """
68
+
69
+ chat_css = """
70
+ .gr-button { font-size: 20px !important; } /* Enlarges button icons */
71
+ .gr-button svg { width: 32px !important; height: 32px !important; } /* Enlarges SVG icons */
72
+ """
73
+
74
+ # model_name = '/n/holylfs06/LABS/mzitnik_lab/Lab/shgao/bioagent/bio/alignment-handbook/data_new/L8-qlora-biov49v9v7v16_32k_chat01_merged'
75
+ model_name = 'mims-harvard/TxAgent-T1-Llama-3.1-8B'
76
+ rag_model_name = 'mims-harvard/ToolRAG-T1-GTE-Qwen2-1.5B'
77
+
78
+ os.environ["TOKENIZERS_PARALLELISM"] = "false"
79
+
80
+
81
+ question_examples = [
82
+ ['Given a 50-year-old patient experiencing severe acute pain and considering the use of the newly approved medication, Journavx, how should the dosage be adjusted considering the presence of moderate hepatic impairment?'],
83
+ ['Given a 50-year-old patient experiencing severe acute pain and considering the use of the newly approved medication, Journavx, how should the dosage be adjusted considering the presence of severe hepatic impairment?'],
84
+ ['A 30-year-old patient is taking Prozac to treat their depression. They were recently diagnosed with WHIM syndrome and require a treatment for that condition as well. Is Xolremdi suitable for this patient, considering contraindications?'],
85
+ ]
86
+
87
+ new_tool_files = {
88
+ 'new_tool': os.path.join(current_dir, 'data', 'new_tool.json'),
89
+ }
90
+
91
+ agent = TxAgent(model_name,
92
+ rag_model_name,
93
+ tool_files_dict=new_tool_files,
94
+ force_finish=True,
95
+ enable_checker=True,
96
+ step_rag_num=10,
97
+ seed=100,
98
+ additional_default_tools=['DirectResponse', 'RequireClarification'])
99
+ agent.init_model()
100
+
101
+
102
+ def update_model_parameters(enable_finish, enable_rag, enable_summary,
103
+ init_rag_num, step_rag_num, skip_last_k,
104
+ summary_mode, summary_skip_last_k, summary_context_length, force_finish, seed):
105
+ # Update model instance parameters dynamically
106
+ updated_params = agent.update_parameters(
107
+ enable_finish=enable_finish,
108
+ enable_rag=enable_rag,
109
+ enable_summary=enable_summary,
110
+ init_rag_num=init_rag_num,
111
+ step_rag_num=step_rag_num,
112
+ skip_last_k=skip_last_k,
113
+ summary_mode=summary_mode,
114
+ summary_skip_last_k=summary_skip_last_k,
115
+ summary_context_length=summary_context_length,
116
+ force_finish=force_finish,
117
+ seed=seed,
118
  )
119
 
120
+ return updated_params
121
+
122
+
123
+ def update_seed():
124
+ # Update model instance parameters dynamically
125
+ seed = random.randint(0, 10000)
126
+ updated_params = agent.update_parameters(
127
+ seed=seed,
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
128
  )
129
+ return updated_params
130
 
131
+
132
+ def handle_retry(history, retry_data: gr.RetryData, temperature, max_new_tokens, max_tokens, multi_agent, conversation, max_round):
133
+ print("Updated seed:", update_seed())
134
+ new_history = history[:retry_data.index]
135
+ previous_prompt = history[retry_data.index]['content']
136
+
137
+ print("previous_prompt", previous_prompt)
138
+
139
+ yield from agent.run_gradio_chat(new_history + [{"role": "user", "content": previous_prompt}], temperature, max_new_tokens, max_tokens, multi_agent, conversation, max_round)
140
+
141
+
142
+ PASSWORD = "mypassword"
143
+
144
+ # Function to check if the password is correct
145
+
146
+
147
+ def check_password(input_password):
148
+ if input_password == PASSWORD:
149
+ return gr.update(visible=True), ""
150
  else:
151
+ return gr.update(visible=False), "Incorrect password, try again!"
152
+
153
+
154
+ conversation_state = gr.State([])
155
+
156
+ # Gradio block
157
+ chatbot = gr.Chatbot(height=800, placeholder=PLACEHOLDER,
158
+ label='TxAgent', type="messages", show_copy_button=True)
159
+
160
+ with gr.Blocks(css=css) as demo:
161
+ gr.Markdown(DESCRIPTION)
162
+ gr.Markdown(INTRO)
163
+ default_temperature = 0.3
164
+ default_max_new_tokens = 1024
165
+ default_max_tokens = 81920
166
+ default_max_round = 30
167
+ temperature_state = gr.State(value=default_temperature)
168
+ max_new_tokens_state = gr.State(value=default_max_new_tokens)
169
+ max_tokens_state = gr.State(value=default_max_tokens)
170
+ max_round_state = gr.State(value=default_max_round)
171
+ chatbot.retry(handle_retry, chatbot, chatbot, temperature_state, max_new_tokens_state,
172
+ max_tokens_state, gr.Checkbox(value=False, render=False), conversation_state, max_round_state)
173
 
174
+ gr.ChatInterface(
175
+ fn=agent.run_gradio_chat,
176
+ chatbot=chatbot,
177
+ fill_height=True, fill_width=True, stop_btn=True,
178
+ additional_inputs_accordion=gr.Accordion(
179
+ label="⚙️ Inference Parameters", open=False, render=False),
180
+ additional_inputs=[
181
+ temperature_state, max_new_tokens_state, max_tokens_state,
182
+ gr.Checkbox(
183
+ label="Activate multi-agent reasoning mode (it requires additional time but offers a more comprehensive analysis).", value=False, render=False),
184
+ conversation_state,
185
+ max_round_state,
186
+ gr.Number(label="Seed", value=100, render=False)
187
+ ],
188
+ examples=question_examples,
189
+ cache_examples=False,
190
+ css=chat_css,
191
+ )
192
+
193
+ with gr.Accordion("Settings", open=False):
194
+
195
+ # Define the sliders
196
+ temperature_slider = gr.Slider(
197
+ minimum=0,
198
+ maximum=1,
199
+ step=0.1,
200
+ value=default_temperature,
201
+ label="Temperature"
202
+ )
203
+ max_new_tokens_slider = gr.Slider(
204
+ minimum=128,
205
+ maximum=4096,
206
+ step=1,
207
+ value=default_max_new_tokens,
208
+ label="Max new tokens"
209
+ )
210
+ max_tokens_slider = gr.Slider(
211
+ minimum=128,
212
+ maximum=32000,
213
+ step=1,
214
+ value=default_max_tokens,
215
+ label="Max tokens"
216
+ )
217
+ max_round_slider = gr.Slider(
218
+ minimum=0,
219
+ maximum=50,
220
+ step=1,
221
+ value=default_max_round,
222
+ label="Max round")
223
+
224
+ # Automatically update states when slider values change
225
+ temperature_slider.change(
226
+ lambda x: x, inputs=temperature_slider, outputs=temperature_state)
227
+ max_new_tokens_slider.change(
228
+ lambda x: x, inputs=max_new_tokens_slider, outputs=max_new_tokens_state)
229
+ max_tokens_slider.change(
230
+ lambda x: x, inputs=max_tokens_slider, outputs=max_tokens_state)
231
+ max_round_slider.change(
232
+ lambda x: x, inputs=max_round_slider, outputs=max_round_state)
233
+
234
+ password_input = gr.Textbox(
235
+ label="Enter Password for More Settings", type="password")
236
+ incorrect_message = gr.Textbox(visible=False, interactive=False)
237
+ with gr.Accordion("⚙️ Settings", open=False, visible=False) as protected_accordion:
238
+ with gr.Row():
239
+ with gr.Column(scale=1):
240
+ with gr.Accordion("⚙️ Model Loading", open=False):
241
+ model_name_input = gr.Textbox(
242
+ label="Enter model path", value=model_name)
243
+ load_model_btn = gr.Button(value="Load Model")
244
+ load_model_btn.click(
245
+ agent.load_models, inputs=model_name_input, outputs=gr.Textbox(label="Status"))
246
+ with gr.Column(scale=1):
247
+ with gr.Accordion("⚙️ Functional Parameters", open=False):
248
+ # Create Gradio components for parameter inputs
249
+ enable_finish = gr.Checkbox(
250
+ label="Enable Finish", value=True)
251
+ enable_rag = gr.Checkbox(
252
+ label="Enable RAG", value=True)
253
+ enable_summary = gr.Checkbox(
254
+ label="Enable Summary", value=False)
255
+ init_rag_num = gr.Number(
256
+ label="Initial RAG Num", value=0)
257
+ step_rag_num = gr.Number(
258
+ label="Step RAG Num", value=10)
259
+ skip_last_k = gr.Number(label="Skip Last K", value=0)
260
+ summary_mode = gr.Textbox(
261
+ label="Summary Mode", value='step')
262
+ summary_skip_last_k = gr.Number(
263
+ label="Summary Skip Last K", value=0)
264
+ summary_context_length = gr.Number(
265
+ label="Summary Context Length", value=None)
266
+ force_finish = gr.Checkbox(
267
+ label="Force FinalAnswer", value=True)
268
+ seed = gr.Number(label="Seed", value=100)
269
+ # Button to submit and update parameters
270
+ submit_btn = gr.Button("Update Parameters")
271
+
272
+ # Display the updated parameters
273
+ updated_parameters_output = gr.JSON()
274
+
275
+ # When button is clicked, update parameters
276
+ submit_btn.click(fn=update_model_parameters,
277
+ inputs=[enable_finish, enable_rag, enable_summary, init_rag_num, step_rag_num, skip_last_k,
278
+ summary_mode, summary_skip_last_k, summary_context_length, force_finish, seed],
279
+ outputs=updated_parameters_output)
280
+ # Button to submit the password
281
+ submit_button = gr.Button("Submit")
282
+
283
+ # When the button is clicked, check if the password is correct
284
+ submit_button.click(
285
+ check_password,
286
+ inputs=password_input,
287
+ outputs=[protected_accordion, incorrect_message]
288
+ )
289
+ gr.Markdown(LICENSE)
290
+
291
+
292
+ if __name__ == "__main__":
293
+ demo.launch(share=True)