Ali2206 commited on
Commit
40ad293
·
verified ·
1 Parent(s): 9ca9a19

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +19 -13
app.py CHANGED
@@ -3,22 +3,23 @@ import sys
3
  import random
4
  import gradio as gr
5
 
6
- # Add `src` directory to Python path
7
  sys.path.append(os.path.join(os.path.dirname(__file__), "src"))
8
 
9
  from txagent.txagent import TxAgent
10
 
11
- # ==== Environment Setup ====
12
  current_dir = os.path.dirname(os.path.abspath(__file__))
13
  os.environ["MKL_THREADING_LAYER"] = "GNU"
14
  os.environ["TOKENIZERS_PARALLELISM"] = "false"
15
 
16
- # ==== UI Content ====
17
  DESCRIPTION = '''
18
  <div>
19
- <h1 style="text-align: center;">TxAgent: An AI Agent for Therapeutic Reasoning Across a Universe of Tools </h1>
20
  </div>
21
  '''
 
22
  INTRO = "Precision therapeutics require multimodal adaptive models..."
23
  LICENSE = "DISCLAIMER: THIS WEBSITE DOES NOT PROVIDE MEDICAL ADVICE..."
24
 
@@ -49,7 +50,7 @@ chat_css = """
49
  .gr-button svg { width: 32px !important; height: 32px !important; }
50
  """
51
 
52
- # ==== Model Settings ====
53
  model_name = "mims-harvard/TxAgent-T1-Llama-3.1-8B"
54
  rag_model_name = "mims-harvard/ToolRAG-T1-GTE-Qwen2-1.5B"
55
  new_tool_files = {
@@ -61,10 +62,10 @@ question_examples = [
61
  ["A 30-year-old patient is on Prozac for depression and now diagnosed with WHIM syndrome. Is Xolremdi suitable?"]
62
  ]
63
 
64
- # Initialize agent placeholder
65
- agent = None
66
 
67
- # ===== Build Gradio UI =====
68
  def create_ui():
69
  with gr.Blocks(css=css) as demo:
70
  gr.Markdown(DESCRIPTION)
@@ -85,7 +86,7 @@ def create_ui():
85
  show_copy_button=True
86
  )
87
 
88
- # Retry logic
89
  def handle_retry(history, retry_data, temperature, max_new_tokens, max_tokens, multi_agent, conversation, max_round):
90
  agent.update_parameters(seed=random.randint(0, 10000))
91
  new_history = history[:retry_data.index]
@@ -95,7 +96,7 @@ def create_ui():
95
  temperature, max_new_tokens, max_tokens,
96
  multi_agent, conversation, max_round
97
  )
98
- if hasattr(result, "__iter__") and not isinstance(result, (str, dict, list)):
99
  result = list(result)
100
  return result
101
 
@@ -106,10 +107,14 @@ def create_ui():
106
  multi_agent, conversation_state, max_round
107
  )
108
 
109
- # Main handler
110
  def handle_chat(message, history, temperature, max_new_tokens, max_tokens, multi_agent, conversation, max_round):
111
- return agent.run_gradio_chat(message, history, temperature, max_new_tokens, max_tokens, multi_agent, conversation, max_round)
 
 
 
112
 
 
113
  gr.ChatInterface(
114
  fn=handle_chat,
115
  chatbot=chatbot,
@@ -128,7 +133,8 @@ def create_ui():
128
  gr.Markdown(LICENSE)
129
  return demo
130
 
131
- # === VLLM-safe entry point ===
 
132
  if __name__ == "__main__":
133
  agent = TxAgent(
134
  model_name,
 
3
  import random
4
  import gradio as gr
5
 
6
+ # Add `src` to import path
7
  sys.path.append(os.path.join(os.path.dirname(__file__), "src"))
8
 
9
  from txagent.txagent import TxAgent
10
 
11
+ # === Environment setup ===
12
  current_dir = os.path.dirname(os.path.abspath(__file__))
13
  os.environ["MKL_THREADING_LAYER"] = "GNU"
14
  os.environ["TOKENIZERS_PARALLELISM"] = "false"
15
 
16
+ # === UI constants ===
17
  DESCRIPTION = '''
18
  <div>
19
+ <h1 style="text-align: center;">TxAgent: An AI Agent for Therapeutic Reasoning Across a Universe of Tools</h1>
20
  </div>
21
  '''
22
+
23
  INTRO = "Precision therapeutics require multimodal adaptive models..."
24
  LICENSE = "DISCLAIMER: THIS WEBSITE DOES NOT PROVIDE MEDICAL ADVICE..."
25
 
 
50
  .gr-button svg { width: 32px !important; height: 32px !important; }
51
  """
52
 
53
+ # === Model and tool config ===
54
  model_name = "mims-harvard/TxAgent-T1-Llama-3.1-8B"
55
  rag_model_name = "mims-harvard/ToolRAG-T1-GTE-Qwen2-1.5B"
56
  new_tool_files = {
 
62
  ["A 30-year-old patient is on Prozac for depression and now diagnosed with WHIM syndrome. Is Xolremdi suitable?"]
63
  ]
64
 
65
+ agent = None # global agent initialized later
66
+
67
 
68
+ # === Create Gradio UI ===
69
  def create_ui():
70
  with gr.Blocks(css=css) as demo:
71
  gr.Markdown(DESCRIPTION)
 
86
  show_copy_button=True
87
  )
88
 
89
+ # === Retry handler (fixed)
90
  def handle_retry(history, retry_data, temperature, max_new_tokens, max_tokens, multi_agent, conversation, max_round):
91
  agent.update_parameters(seed=random.randint(0, 10000))
92
  new_history = history[:retry_data.index]
 
96
  temperature, max_new_tokens, max_tokens,
97
  multi_agent, conversation, max_round
98
  )
99
+ if hasattr(result, "__iter__") and not isinstance(result, (str, list, dict)):
100
  result = list(result)
101
  return result
102
 
 
107
  multi_agent, conversation_state, max_round
108
  )
109
 
110
+ # === Chat handler
111
  def handle_chat(message, history, temperature, max_new_tokens, max_tokens, multi_agent, conversation, max_round):
112
+ result = agent.run_gradio_chat(message, history, temperature, max_new_tokens, max_tokens, multi_agent, conversation, max_round)
113
+ if hasattr(result, "__iter__") and not isinstance(result, (str, list, dict)):
114
+ result = list(result)
115
+ return result
116
 
117
+ # === Chat Interface setup
118
  gr.ChatInterface(
119
  fn=handle_chat,
120
  chatbot=chatbot,
 
133
  gr.Markdown(LICENSE)
134
  return demo
135
 
136
+
137
+ # === HF Spaces + vLLM-safe entrypoint ===
138
  if __name__ == "__main__":
139
  agent = TxAgent(
140
  model_name,