Ali2206 commited on
Commit
12efdad
·
verified ·
1 Parent(s): c86ea26

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +282 -147
app.py CHANGED
@@ -1,158 +1,293 @@
1
- import os
2
- import logging
3
- import torch
4
- import gradio as gr
5
  from txagent import TxAgent
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
6
 
7
- # Setup logging
8
- logging.basicConfig(
9
- level=logging.INFO,
10
- format='%(asctime)s - %(name)s - %(levelname)s - %(message)s'
11
- )
12
- logger = logging.getLogger(__name__)
13
 
14
- # Configuration
15
- MODEL_NAME = "mims-harvard/TxAgent-T1-Llama-3.1-8B"
16
- RAG_MODEL_NAME = "mims-harvard/ToolRAG-T1-GTE-Qwen2-1.5B"
17
- TOOL_FILE = "data/new_tool.json"
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
18
 
19
- # Environment setup
20
  os.environ["TOKENIZERS_PARALLELISM"] = "false"
21
- os.environ["CUDA_MODULE_LOADING"] = "LAZY"
22
- os.environ["VLLM_WORKER_MULTIPROC_METHOD"] = "spawn"
23
-
24
- class TxAgentSystem:
25
- def __init__(self):
26
- self.agent = None
27
- self.is_initialized = False
28
- self.examples = [
29
- ["A 68-year-old with CKD prescribed metformin. Safe for renal clearance?"],
30
- ["30-year-old on Prozac diagnosed with WHIM. Safe to take Xolremdi?"]
31
- ]
32
-
33
- if not torch.cuda.is_available():
34
- raise RuntimeError("CUDA is not available - GPU required")
35
-
36
- logger.info(f"GPU: {torch.cuda.get_device_name(0)}")
37
- logger.info(f"VRAM: {torch.cuda.get_device_properties(0).total_memory/1e9:.1f}GB")
38
-
39
- self._initialize_system()
40
-
41
- def _initialize_system(self):
42
- try:
43
- os.makedirs("data", exist_ok=True)
44
- if not os.path.exists(TOOL_FILE):
45
- with open(TOOL_FILE, "w") as f:
46
- f.write("[]")
47
-
48
- logger.info("Initializing TxAgent...")
49
-
50
- # Initialize with RAG disabled first
51
- try:
52
- self.agent = TxAgent(
53
- model_name=MODEL_NAME,
54
- rag_model_name=RAG_MODEL_NAME,
55
- tool_files_dict={"new_tool": TOOL_FILE},
56
- force_finish=True,
57
- enable_checker=True,
58
- step_rag_num=10,
59
- seed=100,
60
- enable_rag=True
61
- )
62
- except Exception as e:
63
- logger.warning(f"Failed to initialize with RAG: {str(e)}")
64
- logger.info("Retrying without RAG...")
65
- self.agent = TxAgent(
66
- model_name=MODEL_NAME,
67
- rag_model_name=None,
68
- tool_files_dict={"new_tool": TOOL_FILE},
69
- force_finish=True,
70
- enable_checker=True,
71
- step_rag_num=0,
72
- seed=100,
73
- enable_rag=False
74
- )
75
-
76
- logger.info("Loading main model...")
77
- self.agent.init_model()
78
-
79
- self.is_initialized = True
80
- logger.info("System initialization completed successfully")
81
-
82
- except Exception as e:
83
- logger.error(f"System initialization failed: {str(e)}")
84
- self.is_initialized = False
85
- raise
86
-
87
- def chat_fn(self, message, history, temperature, max_tokens, rag_depth):
88
- if not self.is_initialized:
89
- return "", history + [(message, "System initialization failed. Please check logs.")]
90
-
91
- try:
92
- response = self.agent.run_gradio_chat(
93
- message=message,
94
- history=history,
95
- temperature=temperature,
96
- max_new_tokens=max_tokens,
97
- max_total_tokens=16384,
98
- enable_multi_agent=False,
99
- conv_history=history,
100
- max_steps=rag_depth,
101
- seed=100
102
- )
103
- new_history = history + [(message, response)]
104
- return "", new_history
105
-
106
- except torch.cuda.OutOfMemoryError:
107
- torch.cuda.empty_cache()
108
- return "", history + [(message, "⚠️ GPU memory overflow. Please try a shorter query.")]
109
-
110
- except Exception as e:
111
- logger.error(f"Chat error: {str(e)}")
112
- return "", history + [(message, f"🚨 Error: {str(e)}")]
113
-
114
- def launch_ui(self):
115
- with gr.Blocks(theme=gr.themes.Soft(), title="TxAgent Medical AI") as demo:
116
- gr.Markdown("## 🧠 TxAgent (A100/H100 Optimized)")
117
-
118
- status = gr.Textbox(
119
- value="✅ System ready" if self.is_initialized else "❌ Initialization failed",
120
- label="System Status",
121
- interactive=False
122
- )
123
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
124
  with gr.Row():
125
- with gr.Column(scale=3):
126
- chatbot = gr.Chatbot(height=600, label="Conversation History")
127
- msg = gr.Textbox(label="Enter Medical Query", placeholder="Type your question here...")
128
  with gr.Column(scale=1):
129
- temp = gr.Slider(0.1, 1.0, value=0.7, label="Temperature")
130
- max_tokens = gr.Slider(128, 8192, value=2048, label="Max Response Tokens")
131
- rag_depth = gr.Slider(1, 20, value=10, label="RAG Depth")
132
- clear_btn = gr.Button("Clear History")
133
-
134
- gr.Examples(
135
- examples=self.examples,
136
- inputs=msg,
137
- label="Example Queries"
138
- )
139
-
140
- msg.submit(
141
- self.chat_fn,
142
- inputs=[msg, chatbot, temp, max_tokens, rag_depth],
143
- outputs=[msg, chatbot]
144
- )
145
- clear_btn.click(lambda: None, None, chatbot, queue=False)
146
-
147
- demo.launch(
148
- server_name="0.0.0.0",
149
- server_port=7860
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
150
  )
 
 
151
 
152
  if __name__ == "__main__":
153
- try:
154
- system = TxAgentSystem()
155
- system.launch_ui()
156
- except Exception as e:
157
- logger.critical(f"Fatal error: {str(e)}")
158
- raise
 
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)