Ali2206 commited on
Commit
83c8341
·
verified ·
1 Parent(s): 229805b

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +51 -72
app.py CHANGED
@@ -17,7 +17,7 @@ class TxAgentApp:
17
  """Initialize the TxAgent with proper tool file paths"""
18
  try:
19
  logger.info("Initializing TxAgent...")
20
-
21
  # Get absolute paths to tool files from package installation
22
  tool_files = {
23
  "opentarget": str(files('tooluniverse.data').joinpath('opentarget_tools.json')),
@@ -25,9 +25,9 @@ class TxAgentApp:
25
  "special_tools": str(files('tooluniverse.data').joinpath('special_tools.json')),
26
  "monarch": str(files('tooluniverse.data').joinpath('monarch_tools.json'))
27
  }
28
-
29
  logger.info(f"Using tool files at: {tool_files}")
30
-
31
  agent = TxAgent(
32
  model_name="mims-harvard/TxAgent-T1-Llama-3.1-8B",
33
  rag_model_name="mims-harvard/ToolRAG-T1-GTE-Qwen2-1.5B",
@@ -47,12 +47,11 @@ class TxAgentApp:
47
  enable_chat=False,
48
  additional_default_tools=["DirectResponse", "RequireClarification"]
49
  )
50
-
51
- # Explicitly initialize the model
52
  agent.init_model()
53
-
54
  logger.info("Model loading complete")
55
  return agent
 
56
  except Exception as e:
57
  logger.error(f"Initialization failed: {str(e)}")
58
  raise
@@ -63,7 +62,6 @@ class TxAgentApp:
63
  if not isinstance(message, str) or len(message.strip()) <= 10:
64
  return chat_history + [("", "Please provide a valid message longer than 10 characters.")]
65
 
66
- # Convert chat history to list of tuples if needed
67
  if chat_history and isinstance(chat_history[0], dict):
68
  chat_history = [(h["role"], h["content"]) for h in chat_history if "role" in h and "content" in h]
69
 
@@ -85,74 +83,55 @@ class TxAgentApp:
85
  response += chunk
86
  else:
87
  response += str(chunk)
88
-
89
  yield chat_history + [("user", message), ("assistant", response)]
90
 
91
  except Exception as e:
92
  logger.error(f"Error in respond function: {str(e)}")
93
  yield chat_history + [("", f"⚠️ Error: {str(e)}")]
94
 
95
- def create_demo():
96
- """Create and configure the Gradio interface"""
97
- app = TxAgentApp()
98
-
99
- with gr.Blocks(title="TxAgent Medical AI") as demo:
100
- gr.Markdown("# TxAgent Biomedical Assistant")
101
-
102
- chatbot = gr.Chatbot(
103
- label="Conversation",
104
- height=600
105
- )
106
-
107
- msg = gr.Textbox(
108
- label="Your medical query",
109
- placeholder="Enter your biomedical question...",
110
- lines=3
111
- )
112
-
113
- with gr.Row():
114
- temp = gr.Slider(0, 1, value=0.3, label="Temperature")
115
- max_new_tokens = gr.Slider(128, 4096, value=1024, label="Max New Tokens")
116
- max_tokens = gr.Slider(128, 81920, value=81920, label="Max Total Tokens")
117
- max_rounds = gr.Slider(1, 30, value=10, label="Max Rounds")
118
- multi_agent = gr.Checkbox(label="Multi-Agent Mode")
119
-
120
- submit = gr.Button("Submit")
121
- clear = gr.Button("Clear")
122
-
123
- conversation_state = gr.State([])
124
-
125
- submit.click(
126
- app.respond,
127
- [msg, chatbot, temp, max_new_tokens, max_tokens, multi_agent, conversation_state, max_rounds],
128
- chatbot
129
- )
130
-
131
- clear.click(lambda: [], None, chatbot)
132
-
133
- msg.submit(
134
- app.respond,
135
- [msg, chatbot, temp, max_new_tokens, max_tokens, multi_agent, conversation_state, max_rounds],
136
- chatbot
137
- )
138
-
139
- return demo
140
-
141
- def main():
142
- """Main entry point for the application"""
143
- try:
144
- logger.info("Starting TxAgent application...")
145
- demo = create_demo()
146
-
147
- logger.info("Launching Gradio interface...")
148
- demo.launch(
149
- server_name="0.0.0.0",
150
- server_port=7860,
151
- share=False
152
- )
153
- except Exception as e:
154
- logger.error(f"Application failed to start: {str(e)}")
155
- raise
156
-
157
- if __name__ == "__main__":
158
- main()
 
17
  """Initialize the TxAgent with proper tool file paths"""
18
  try:
19
  logger.info("Initializing TxAgent...")
20
+
21
  # Get absolute paths to tool files from package installation
22
  tool_files = {
23
  "opentarget": str(files('tooluniverse.data').joinpath('opentarget_tools.json')),
 
25
  "special_tools": str(files('tooluniverse.data').joinpath('special_tools.json')),
26
  "monarch": str(files('tooluniverse.data').joinpath('monarch_tools.json'))
27
  }
28
+
29
  logger.info(f"Using tool files at: {tool_files}")
30
+
31
  agent = TxAgent(
32
  model_name="mims-harvard/TxAgent-T1-Llama-3.1-8B",
33
  rag_model_name="mims-harvard/ToolRAG-T1-GTE-Qwen2-1.5B",
 
47
  enable_chat=False,
48
  additional_default_tools=["DirectResponse", "RequireClarification"]
49
  )
50
+
 
51
  agent.init_model()
 
52
  logger.info("Model loading complete")
53
  return agent
54
+
55
  except Exception as e:
56
  logger.error(f"Initialization failed: {str(e)}")
57
  raise
 
62
  if not isinstance(message, str) or len(message.strip()) <= 10:
63
  return chat_history + [("", "Please provide a valid message longer than 10 characters.")]
64
 
 
65
  if chat_history and isinstance(chat_history[0], dict):
66
  chat_history = [(h["role"], h["content"]) for h in chat_history if "role" in h and "content" in h]
67
 
 
83
  response += chunk
84
  else:
85
  response += str(chunk)
86
+
87
  yield chat_history + [("user", message), ("assistant", response)]
88
 
89
  except Exception as e:
90
  logger.error(f"Error in respond function: {str(e)}")
91
  yield chat_history + [("", f"⚠️ Error: {str(e)}")]
92
 
93
+ # Initialize app instance
94
+ tx_app = TxAgentApp()
95
+
96
+ # Define Gradio UI blocks
97
+ with gr.Blocks(title="TxAgent Biomedical Assistant") as app:
98
+ gr.Markdown("# 🧠 TxAgent Biomedical Assistant")
99
+
100
+ chatbot = gr.Chatbot(
101
+ label="Conversation",
102
+ height=600
103
+ )
104
+
105
+ msg = gr.Textbox(
106
+ label="Your medical query",
107
+ placeholder="Enter your biomedical question...",
108
+ lines=3
109
+ )
110
+
111
+ with gr.Row():
112
+ temp = gr.Slider(0, 1, value=0.3, label="Temperature")
113
+ max_new_tokens = gr.Slider(128, 4096, value=1024, label="Max New Tokens")
114
+ max_tokens = gr.Slider(128, 81920, value=81920, label="Max Total Tokens")
115
+ max_rounds = gr.Slider(1, 30, value=10, label="Max Rounds")
116
+ multi_agent = gr.Checkbox(label="Multi-Agent Mode")
117
+
118
+ submit = gr.Button("Submit")
119
+ clear = gr.Button("Clear")
120
+
121
+ conversation_state = gr.State([])
122
+
123
+ submit.click(
124
+ tx_app.respond,
125
+ [msg, chatbot, temp, max_new_tokens, max_tokens, multi_agent, conversation_state, max_rounds],
126
+ chatbot
127
+ )
128
+
129
+ clear.click(lambda: [], None, chatbot)
130
+
131
+ msg.submit(
132
+ tx_app.respond,
133
+ [msg, chatbot, temp, max_new_tokens, max_tokens, multi_agent, conversation_state, max_rounds],
134
+ chatbot
135
+ )
136
+
137
+ # `app` will be auto-discovered and served by Hugging Face Spaces