Ali2206 commited on
Commit
511fb62
·
verified ·
1 Parent(s): 4791a42

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +196 -192
app.py CHANGED
@@ -1,14 +1,14 @@
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 torch
9
  import logging
 
10
  from importlib.resources import files
11
- import traceback
 
 
12
 
13
  # Set up logging
14
  logging.basicConfig(
@@ -20,6 +20,7 @@ logger = logging.getLogger(__name__)
20
  # Determine the directory where the current file is located
21
  current_dir = os.path.dirname(os.path.abspath(__file__))
22
  os.environ["MKL_THREADING_LAYER"] = "GNU"
 
23
 
24
  # Configuration
25
  CONFIG = {
@@ -35,33 +36,36 @@ CONFIG = {
35
  }
36
  }
37
 
38
- # Set an environment variable
39
- HF_TOKEN = os.environ.get("HF_TOKEN", None)
40
-
41
  DESCRIPTION = '''
42
  <div>
43
- <h1 style="text-align: center;">TxAgent: An AI Agent for Therapeutic Reasoning Across a Universe of Tools </h1>
44
  </div>
45
  '''
 
46
  INTRO = """
47
- 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.
 
 
 
48
  """
49
 
50
  LICENSE = """
51
- 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.
 
52
 
53
  ### Medical Advice Disclaimer
54
  DISCLAIMER: THIS WEBSITE DOES NOT PROVIDE MEDICAL ADVICE
55
- 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.
 
 
56
  """
57
 
58
  PLACEHOLDER = """
59
  <div style="padding: 30px; text-align: center; display: flex; flex-direction: column; align-items: center;">
60
  <h1 style="font-size: 28px; margin-bottom: 2px; opacity: 0.55;">TxAgent</h1>
61
  <p style="font-size: 18px; margin-bottom: 2px; opacity: 0.65;">Tips before using TxAgent:</p>
62
- <p style="font-size: 18px; margin-bottom: 2px; opacity: 0.55;">Please click clear🗑️
63
- (top-right) to remove previous context before sumbmitting a new question.</p>
64
- <p style="font-size: 18px; margin-bottom: 2px; opacity: 0.55;">Click retry🔄 (below message) to get multiple versions of the answer.</p>
65
  </div>
66
  """
67
 
@@ -90,27 +94,17 @@ h1 {
90
  """
91
 
92
  chat_css = """
93
- .gr-button { font-size: 20px !important; } /* Enlarges button icons */
94
- .gr-button svg { width: 32px !important; height: 32px !important; } /* Enlarges SVG icons */
95
  """
96
 
97
- os.environ["TOKENIZERS_PARALLELISM"] = "false"
98
-
99
- question_examples = [
100
- ['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?'],
101
- ['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?'],
102
- ['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?'],
103
- ]
104
-
105
  def safe_load_embeddings(filepath: str) -> any:
106
  """Safely load embeddings with proper weights_only handling"""
107
  try:
108
- # First try with weights_only=True (secure mode)
109
  return torch.load(filepath, weights_only=True)
110
  except Exception as e:
111
  logger.warning(f"Secure load failed, trying with weights_only=False: {str(e)}")
112
  try:
113
- # Try with the safe_globals context manager
114
  with torch.serialization.safe_globals([torch.serialization._reconstruct]):
115
  return torch.load(filepath, weights_only=False)
116
  except Exception as e:
@@ -130,10 +124,8 @@ def patch_embedding_loading():
130
  logger.error(f"Embedding file not found: {CONFIG['embedding_filename']}")
131
  return False
132
 
133
- # Load embeddings safely
134
  self.tool_desc_embedding = safe_load_embeddings(CONFIG["embedding_filename"])
135
 
136
- # Handle tool count mismatch
137
  tools = tooluniverse.get_all_tools()
138
  current_count = len(tools)
139
  embedding_count = len(self.tool_desc_embedding)
@@ -147,9 +139,7 @@ def patch_embedding_loading():
147
  else:
148
  last_embedding = self.tool_desc_embedding[-1]
149
  padding = [last_embedding] * (current_count - embedding_count)
150
- self.tool_desc_embedding = torch.cat(
151
- [self.tool_desc_embedding] + padding
152
- )
153
  logger.info(f"Padded embeddings to match {current_count} tools")
154
 
155
  return True
@@ -158,7 +148,6 @@ def patch_embedding_loading():
158
  logger.error(f"Failed to load embeddings: {str(e)}")
159
  return False
160
 
161
- # Apply the patch
162
  ToolRAGModel.load_tool_desc_embedding = patched_load
163
  logger.info("Successfully patched embedding loading")
164
 
@@ -177,27 +166,31 @@ def prepare_tool_files():
177
  json.dump(tools, f, indent=2)
178
  logger.info(f"Saved {len(tools)} tools to {CONFIG['tool_files']['new_tool']}")
179
 
180
- # Apply the embedding patch before creating the agent
181
- patch_embedding_loading()
182
- prepare_tool_files()
183
-
184
- # Initialize the agent
185
- agent = TxAgent(
186
- CONFIG["model_name"],
187
- CONFIG["rag_model_name"],
188
- tool_files_dict=CONFIG["tool_files"],
189
- force_finish=True,
190
- enable_checker=True,
191
- step_rag_num=10,
192
- seed=100,
193
- additional_default_tools=['DirectResponse', 'RequireClarification']
194
- )
195
- agent.init_model()
196
-
197
- def update_model_parameters(enable_finish, enable_rag, enable_summary,
198
- init_rag_num, step_rag_num, skip_last_k,
199
- summary_mode, summary_skip_last_k, summary_context_length, force_finish, seed):
200
- # Update model instance parameters dynamically
 
 
 
 
201
  updated_params = agent.update_parameters(
202
  enable_finish=enable_finish,
203
  enable_rag=enable_rag,
@@ -211,169 +204,180 @@ def update_model_parameters(enable_finish, enable_rag, enable_summary,
211
  force_finish=force_finish,
212
  seed=seed,
213
  )
214
-
215
  return updated_params
216
 
217
- def update_seed():
218
- # Update model instance parameters dynamically
219
  seed = random.randint(0, 10000)
220
- updated_params = agent.update_parameters(
221
- seed=seed,
222
- )
223
  return updated_params
224
 
225
- def handle_retry(history, retry_data: gr.RetryData, temperature, max_new_tokens, max_tokens, multi_agent, conversation, max_round):
226
- print("Updated seed:", update_seed())
 
227
  new_history = history[:retry_data.index]
228
  previous_prompt = history[retry_data.index]['content']
229
-
230
  print("previous_prompt", previous_prompt)
231
-
232
- yield from agent.run_gradio_chat(new_history + [{"role": "user", "content": previous_prompt}], temperature, max_new_tokens, max_tokens, multi_agent, conversation, max_round)
233
 
234
  PASSWORD = "mypassword"
235
 
236
  def check_password(input_password):
 
237
  if input_password == PASSWORD:
238
  return gr.update(visible=True), ""
239
  else:
240
  return gr.update(visible=False), "Incorrect password, try again!"
241
 
242
- conversation_state = gr.State([])
243
-
244
- # Gradio block
245
- chatbot = gr.Chatbot(height=800, placeholder=PLACEHOLDER,
246
- label='TxAgent', type="messages", show_copy_button=True)
247
-
248
- with gr.Blocks(css=css) as demo:
249
- gr.Markdown(DESCRIPTION)
250
- gr.Markdown(INTRO)
251
  default_temperature = 0.3
252
  default_max_new_tokens = 1024
253
  default_max_tokens = 81920
254
  default_max_round = 30
255
- temperature_state = gr.State(value=default_temperature)
256
- max_new_tokens_state = gr.State(value=default_max_new_tokens)
257
- max_tokens_state = gr.State(value=default_max_tokens)
258
- max_round_state = gr.State(value=default_max_round)
259
- chatbot.retry(handle_retry, chatbot, chatbot, temperature_state, max_new_tokens_state,
260
- max_tokens_state, gr.Checkbox(value=False, render=False), conversation_state, max_round_state)
261
-
262
- gr.ChatInterface(
263
- fn=agent.run_gradio_chat,
264
- chatbot=chatbot,
265
- fill_height=True, fill_width=True, stop_btn=True,
266
- additional_inputs_accordion=gr.Accordion(
267
- label="⚙️ Inference Parameters", open=False, render=False),
268
- additional_inputs=[
269
- temperature_state, max_new_tokens_state, max_tokens_state,
270
- gr.Checkbox(
271
- label="Activate multi-agent reasoning mode (it requires additional time but offers a more comprehensive analysis).", value=False, render=False),
272
- conversation_state,
273
- max_round_state,
274
- gr.Number(label="Seed", value=100, render=False)
275
- ],
276
- examples=question_examples,
277
- cache_examples=False,
278
- css=chat_css,
279
- )
280
-
281
- with gr.Accordion("Settings", open=False):
282
- # Define the sliders
283
- temperature_slider = gr.Slider(
284
- minimum=0,
285
- maximum=1,
286
- step=0.1,
287
- value=default_temperature,
288
- label="Temperature"
289
- )
290
- max_new_tokens_slider = gr.Slider(
291
- minimum=128,
292
- maximum=4096,
293
- step=1,
294
- value=default_max_new_tokens,
295
- label="Max new tokens"
296
- )
297
- max_tokens_slider = gr.Slider(
298
- minimum=128,
299
- maximum=32000,
300
- step=1,
301
- value=default_max_tokens,
302
- label="Max tokens"
303
  )
304
- max_round_slider = gr.Slider(
305
- minimum=0,
306
- maximum=50,
307
- step=1,
308
- value=default_max_round,
309
- label="Max round")
310
-
311
- # Automatically update states when slider values change
312
- temperature_slider.change(
313
- lambda x: x, inputs=temperature_slider, outputs=temperature_state)
314
- max_new_tokens_slider.change(
315
- lambda x: x, inputs=max_new_tokens_slider, outputs=max_new_tokens_state)
316
- max_tokens_slider.change(
317
- lambda x: x, inputs=max_tokens_slider, outputs=max_tokens_state)
318
- max_round_slider.change(
319
- lambda x: x, inputs=max_round_slider, outputs=max_round_state)
320
 
321
- password_input = gr.Textbox(
322
- label="Enter Password for More Settings", type="password")
323
- incorrect_message = gr.Textbox(visible=False, interactive=False)
324
- with gr.Accordion("⚙️ Settings", open=False, visible=False) as protected_accordion:
325
- with gr.Row():
326
- with gr.Column(scale=1):
327
- with gr.Accordion("⚙️ Model Loading", open=False):
328
- model_name_input = gr.Textbox(
329
- label="Enter model path", value=CONFIG["model_name"])
330
- load_model_btn = gr.Button(value="Load Model")
331
- load_model_btn.click(
332
- agent.load_models, inputs=model_name_input, outputs=gr.Textbox(label="Status"))
333
- with gr.Column(scale=1):
334
- with gr.Accordion("⚙️ Functional Parameters", open=False):
335
- # Create Gradio components for parameter inputs
336
- enable_finish = gr.Checkbox(
337
- label="Enable Finish", value=True)
338
- enable_rag = gr.Checkbox(
339
- label="Enable RAG", value=True)
340
- enable_summary = gr.Checkbox(
341
- label="Enable Summary", value=False)
342
- init_rag_num = gr.Number(
343
- label="Initial RAG Num", value=0)
344
- step_rag_num = gr.Number(
345
- label="Step RAG Num", value=10)
346
- skip_last_k = gr.Number(label="Skip Last K", value=0)
347
- summary_mode = gr.Textbox(
348
- label="Summary Mode", value='step')
349
- summary_skip_last_k = gr.Number(
350
- label="Summary Skip Last K", value=0)
351
- summary_context_length = gr.Number(
352
- label="Summary Context Length", value=None)
353
- force_finish = gr.Checkbox(
354
- label="Force FinalAnswer", value=True)
355
- seed = gr.Number(label="Seed", value=100)
356
- # Button to submit and update parameters
357
- submit_btn = gr.Button("Update Parameters")
358
-
359
- # Display the updated parameters
360
- updated_parameters_output = gr.JSON()
361
 
362
- # When button is clicked, update parameters
363
- submit_btn.click(fn=update_model_parameters,
364
- inputs=[enable_finish, enable_rag, enable_summary, init_rag_num, step_rag_num, skip_last_k,
365
- summary_mode, summary_skip_last_k, summary_context_length, force_finish, seed],
366
- outputs=updated_parameters_output)
367
- # Button to submit the password
368
- submit_button = gr.Button("Submit")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
369
 
370
- # When the button is clicked, check if the password is correct
371
- submit_button.click(
372
- check_password,
373
- inputs=password_input,
374
- outputs=[protected_accordion, incorrect_message]
375
- )
376
- gr.Markdown(LICENSE)
377
 
378
  if __name__ == "__main__":
379
- demo.launch(share=True)
 
1
  import random
2
  import datetime
3
  import sys
 
 
 
4
  import os
5
  import torch
6
  import logging
7
+ import json
8
  from importlib.resources import files
9
+ from txagent import TxAgent
10
+ from tooluniverse import ToolUniverse
11
+ import gradio as gr
12
 
13
  # Set up logging
14
  logging.basicConfig(
 
20
  # Determine the directory where the current file is located
21
  current_dir = os.path.dirname(os.path.abspath(__file__))
22
  os.environ["MKL_THREADING_LAYER"] = "GNU"
23
+ os.environ["TOKENIZERS_PARALLELISM"] = "false"
24
 
25
  # Configuration
26
  CONFIG = {
 
36
  }
37
  }
38
 
 
 
 
39
  DESCRIPTION = '''
40
  <div>
41
+ <h1 style="text-align: center;">TxAgent: An AI Agent for Therapeutic Reasoning Across a Universe of Tools</h1>
42
  </div>
43
  '''
44
+
45
  INTRO = """
46
+ Precision therapeutics require multimodal adaptive models that provide personalized treatment recommendations.
47
+ We introduce TxAgent, an AI agent that leverages multi-step reasoning and real-time biomedical knowledge
48
+ retrieval across a toolbox of 211 expert-curated tools to navigate complex drug interactions,
49
+ contraindications, and patient-specific treatment strategies, delivering evidence-grounded therapeutic decisions.
50
  """
51
 
52
  LICENSE = """
53
+ We welcome your feedback and suggestions to enhance your experience with TxAgent, and if you're interested
54
+ in collaboration, please email Marinka Zitnik and Shanghua Gao.
55
 
56
  ### Medical Advice Disclaimer
57
  DISCLAIMER: THIS WEBSITE DOES NOT PROVIDE MEDICAL ADVICE
58
+ The information, including but not limited to, text, graphics, images and other material contained on this
59
+ website are for informational purposes only. No material on this site is intended to be a substitute for
60
+ professional medical advice, diagnosis or treatment.
61
  """
62
 
63
  PLACEHOLDER = """
64
  <div style="padding: 30px; text-align: center; display: flex; flex-direction: column; align-items: center;">
65
  <h1 style="font-size: 28px; margin-bottom: 2px; opacity: 0.55;">TxAgent</h1>
66
  <p style="font-size: 18px; margin-bottom: 2px; opacity: 0.65;">Tips before using TxAgent:</p>
67
+ <p style="font-size: 18px; margin-bottom: 2px; opacity: 0.55;">Please click clear🗑️ (top-right) to remove previous context before submitting a new question.</p>
68
+ <p style="font-size: 18px; margin-bottom: 2px; opacity: 0.55;">Click retry🔄 (below message) to get multiple versions of the answer.</p>
 
69
  </div>
70
  """
71
 
 
94
  """
95
 
96
  chat_css = """
97
+ .gr-button { font-size: 20px !important; }
98
+ .gr-button svg { width: 32px !important; height: 32px !important; }
99
  """
100
 
 
 
 
 
 
 
 
 
101
  def safe_load_embeddings(filepath: str) -> any:
102
  """Safely load embeddings with proper weights_only handling"""
103
  try:
 
104
  return torch.load(filepath, weights_only=True)
105
  except Exception as e:
106
  logger.warning(f"Secure load failed, trying with weights_only=False: {str(e)}")
107
  try:
 
108
  with torch.serialization.safe_globals([torch.serialization._reconstruct]):
109
  return torch.load(filepath, weights_only=False)
110
  except Exception as e:
 
124
  logger.error(f"Embedding file not found: {CONFIG['embedding_filename']}")
125
  return False
126
 
 
127
  self.tool_desc_embedding = safe_load_embeddings(CONFIG["embedding_filename"])
128
 
 
129
  tools = tooluniverse.get_all_tools()
130
  current_count = len(tools)
131
  embedding_count = len(self.tool_desc_embedding)
 
139
  else:
140
  last_embedding = self.tool_desc_embedding[-1]
141
  padding = [last_embedding] * (current_count - embedding_count)
142
+ self.tool_desc_embedding = torch.cat([self.tool_desc_embedding] + padding)
 
 
143
  logger.info(f"Padded embeddings to match {current_count} tools")
144
 
145
  return True
 
148
  logger.error(f"Failed to load embeddings: {str(e)}")
149
  return False
150
 
 
151
  ToolRAGModel.load_tool_desc_embedding = patched_load
152
  logger.info("Successfully patched embedding loading")
153
 
 
166
  json.dump(tools, f, indent=2)
167
  logger.info(f"Saved {len(tools)} tools to {CONFIG['tool_files']['new_tool']}")
168
 
169
+ def create_agent():
170
+ """Create and initialize the TxAgent"""
171
+ # Apply the embedding patch before creating the agent
172
+ patch_embedding_loading()
173
+ prepare_tool_files()
174
+
175
+ # Initialize the agent
176
+ agent = TxAgent(
177
+ CONFIG["model_name"],
178
+ CONFIG["rag_model_name"],
179
+ tool_files_dict=CONFIG["tool_files"],
180
+ force_finish=True,
181
+ enable_checker=True,
182
+ step_rag_num=10,
183
+ seed=100,
184
+ additional_default_tools=['DirectResponse', 'RequireClarification']
185
+ )
186
+ agent.init_model()
187
+ return agent
188
+
189
+ def update_model_parameters(agent, enable_finish, enable_rag, enable_summary,
190
+ init_rag_num, step_rag_num, skip_last_k,
191
+ summary_mode, summary_skip_last_k, summary_context_length,
192
+ force_finish, seed):
193
+ """Update model parameters"""
194
  updated_params = agent.update_parameters(
195
  enable_finish=enable_finish,
196
  enable_rag=enable_rag,
 
204
  force_finish=force_finish,
205
  seed=seed,
206
  )
 
207
  return updated_params
208
 
209
+ def update_seed(agent):
210
+ """Update random seed"""
211
  seed = random.randint(0, 10000)
212
+ updated_params = agent.update_parameters(seed=seed)
 
 
213
  return updated_params
214
 
215
+ def handle_retry(agent, history, retry_data: gr.RetryData, temperature, max_new_tokens, max_tokens, multi_agent, conversation, max_round):
216
+ """Handle retry functionality"""
217
+ print("Updated seed:", update_seed(agent))
218
  new_history = history[:retry_data.index]
219
  previous_prompt = history[retry_data.index]['content']
 
220
  print("previous_prompt", previous_prompt)
221
+ yield from agent.run_gradio_chat(new_history + [{"role": "user", "content": previous_prompt}],
222
+ temperature, max_new_tokens, max_tokens, multi_agent, conversation, max_round)
223
 
224
  PASSWORD = "mypassword"
225
 
226
  def check_password(input_password):
227
+ """Check password for protected settings"""
228
  if input_password == PASSWORD:
229
  return gr.update(visible=True), ""
230
  else:
231
  return gr.update(visible=False), "Incorrect password, try again!"
232
 
233
+ def create_demo(agent):
234
+ """Create the Gradio interface"""
 
 
 
 
 
 
 
235
  default_temperature = 0.3
236
  default_max_new_tokens = 1024
237
  default_max_tokens = 81920
238
  default_max_round = 30
239
+
240
+ question_examples = [
241
+ ['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?'],
242
+ ['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?'],
243
+ ['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?'],
244
+ ]
245
+
246
+ chatbot = gr.Chatbot(height=800, placeholder=PLACEHOLDER,
247
+ label='TxAgent', type="messages", show_copy_button=True)
248
+
249
+ with gr.Blocks(css=css) as demo:
250
+ gr.Markdown(DESCRIPTION)
251
+ gr.Markdown(INTRO)
252
+
253
+ temperature_state = gr.State(value=default_temperature)
254
+ max_new_tokens_state = gr.State(value=default_max_new_tokens)
255
+ max_tokens_state = gr.State(value=default_max_tokens)
256
+ max_round_state = gr.State(value=default_max_round)
257
+
258
+ chatbot.retry(
259
+ lambda *args: handle_retry(agent, *args),
260
+ inputs=[chatbot, chatbot, temperature_state, max_new_tokens_state,
261
+ max_tokens_state, gr.Checkbox(value=False, render=False),
262
+ gr.State([]), max_round_state]
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
263
  )
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
264
 
265
+ gr.ChatInterface(
266
+ fn=lambda *args: agent.run_gradio_chat(*args),
267
+ chatbot=chatbot,
268
+ fill_height=True,
269
+ fill_width=True,
270
+ stop_btn=True,
271
+ additional_inputs_accordion=gr.Accordion(
272
+ label="⚙️ Inference Parameters", open=False, render=False),
273
+ additional_inputs=[
274
+ temperature_state, max_new_tokens_state, max_tokens_state,
275
+ gr.Checkbox(
276
+ label="Activate multi-agent reasoning mode",
277
+ value=False,
278
+ render=False),
279
+ gr.State([]),
280
+ max_round_state,
281
+ gr.Number(label="Seed", value=100, render=False)
282
+ ],
283
+ examples=question_examples,
284
+ cache_examples=False,
285
+ css=chat_css,
286
+ )
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
287
 
288
+ with gr.Accordion("Settings", open=False):
289
+ temperature_slider = gr.Slider(
290
+ minimum=0,
291
+ maximum=1,
292
+ step=0.1,
293
+ value=default_temperature,
294
+ label="Temperature"
295
+ )
296
+ max_new_tokens_slider = gr.Slider(
297
+ minimum=128,
298
+ maximum=4096,
299
+ step=1,
300
+ value=default_max_new_tokens,
301
+ label="Max new tokens"
302
+ )
303
+ max_tokens_slider = gr.Slider(
304
+ minimum=128,
305
+ maximum=32000,
306
+ step=1,
307
+ value=default_max_tokens,
308
+ label="Max tokens"
309
+ )
310
+ max_round_slider = gr.Slider(
311
+ minimum=0,
312
+ maximum=50,
313
+ step=1,
314
+ value=default_max_round,
315
+ label="Max round")
316
+
317
+ temperature_slider.change(
318
+ lambda x: x, inputs=temperature_slider, outputs=temperature_state)
319
+ max_new_tokens_slider.change(
320
+ lambda x: x, inputs=max_new_tokens_slider, outputs=max_new_tokens_state)
321
+ max_tokens_slider.change(
322
+ lambda x: x, inputs=max_tokens_slider, outputs=max_tokens_state)
323
+ max_round_slider.change(
324
+ lambda x: x, inputs=max_round_slider, outputs=max_round_state)
325
+
326
+ password_input = gr.Textbox(
327
+ label="Enter Password for More Settings", type="password")
328
+ incorrect_message = gr.Textbox(visible=False, interactive=False)
329
+
330
+ with gr.Accordion("⚙️ Settings", open=False, visible=False) as protected_accordion:
331
+ with gr.Row():
332
+ with gr.Column(scale=1):
333
+ with gr.Accordion("⚙️ Model Loading", open=False):
334
+ model_name_input = gr.Textbox(
335
+ label="Enter model path", value=CONFIG["model_name"])
336
+ load_model_btn = gr.Button(value="Load Model")
337
+ load_model_btn.click(
338
+ agent.load_models,
339
+ inputs=model_name_input,
340
+ outputs=gr.Textbox(label="Status"))
341
+ with gr.Column(scale=1):
342
+ with gr.Accordion("⚙️ Functional Parameters", open=False):
343
+ enable_finish = gr.Checkbox(label="Enable Finish", value=True)
344
+ enable_rag = gr.Checkbox(label="Enable RAG", value=True)
345
+ enable_summary = gr.Checkbox(label="Enable Summary", value=False)
346
+ init_rag_num = gr.Number(label="Initial RAG Num", value=0)
347
+ step_rag_num = gr.Number(label="Step RAG Num", value=10)
348
+ skip_last_k = gr.Number(label="Skip Last K", value=0)
349
+ summary_mode = gr.Textbox(label="Summary Mode", value='step')
350
+ summary_skip_last_k = gr.Number(label="Summary Skip Last K", value=0)
351
+ summary_context_length = gr.Number(label="Summary Context Length", value=None)
352
+ force_finish = gr.Checkbox(label="Force FinalAnswer", value=True)
353
+ seed = gr.Number(label="Seed", value=100)
354
+ submit_btn = gr.Button("Update Parameters")
355
+ updated_parameters_output = gr.JSON()
356
+ submit_btn.click(
357
+ lambda *args: update_model_parameters(agent, *args),
358
+ inputs=[enable_finish, enable_rag, enable_summary,
359
+ init_rag_num, step_rag_num, skip_last_k,
360
+ summary_mode, summary_skip_last_k,
361
+ summary_context_length, force_finish, seed],
362
+ outputs=updated_parameters_output
363
+ )
364
+
365
+ submit_button = gr.Button("Submit")
366
+ submit_button.click(
367
+ check_password,
368
+ inputs=password_input,
369
+ outputs=[protected_accordion, incorrect_message]
370
+ )
371
+
372
+ gr.Markdown(LICENSE)
373
+
374
+ return demo
375
 
376
+ def main():
377
+ """Main function to run the application"""
378
+ agent = create_agent()
379
+ demo = create_demo(agent)
380
+ demo.launch(share=True)
 
 
381
 
382
  if __name__ == "__main__":
383
+ main()