Ali2206 commited on
Commit
c3cd8cd
·
verified ·
1 Parent(s): 7b8eace

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +25 -9
app.py CHANGED
@@ -64,7 +64,7 @@ def patch_embedding_loading():
64
  self.tool_desc_embedding = safe_load_embeddings(CONFIG["embedding_filename"])
65
 
66
  # Handle tool count mismatch
67
- tools = tooluniverse.get_all_tools()
68
  current_count = len(tools)
69
  embedding_count = len(self.tool_desc_embedding)
70
 
@@ -133,26 +133,32 @@ class TxAgentApp:
133
 
134
  def chat(self, message, history):
135
  if not self.is_initialized:
136
- return history + [(message, "⚠️ Please initialize the model first")]
137
 
138
  try:
139
  response = ""
 
140
  for chunk in self.agent.run_gradio_chat(
141
  message=message,
142
  history=history,
143
  temperature=0.3,
144
  max_new_tokens=1024,
145
- max_tokens=8192,
146
  multi_agent=False,
147
  conversation=[],
148
  max_round=30
149
  ):
150
  response += chunk
151
- yield history + [(message, response)]
 
 
 
 
 
152
 
153
  except Exception as e:
154
  logger.error(f"Chat error: {str(e)}")
155
- yield history + [(message, f"Error: {str(e)}")]
156
 
157
  def create_interface():
158
  app = TxAgentApp()
@@ -175,7 +181,7 @@ def create_interface():
175
  chatbot = gr.Chatbot(
176
  height=500,
177
  label="Conversation",
178
- type="messages" # Fixing the deprecation warning
179
  )
180
  msg = gr.Textbox(label="Your clinical question")
181
  clear_btn = gr.Button("Clear Chat")
@@ -189,13 +195,23 @@ def create_interface():
189
  inputs=msg
190
  )
191
 
 
 
 
 
 
 
 
 
 
 
192
  init_btn.click(
193
- fn=app.initialize,
194
- outputs=init_status
195
  )
196
 
197
  msg.submit(
198
- fn=app.chat,
199
  inputs=[msg, chatbot],
200
  outputs=chatbot
201
  )
 
64
  self.tool_desc_embedding = safe_load_embeddings(CONFIG["embedding_filename"])
65
 
66
  # Handle tool count mismatch
67
+ tools = tooluniverse.tools # Changed from get_all_tools()
68
  current_count = len(tools)
69
  embedding_count = len(self.tool_desc_embedding)
70
 
 
133
 
134
  def chat(self, message, history):
135
  if not self.is_initialized:
136
+ return {"role": "assistant", "content": "⚠️ Please initialize the model first"}
137
 
138
  try:
139
  response = ""
140
+ # Modified to use the correct parameter name (max_length instead of max_tokens)
141
  for chunk in self.agent.run_gradio_chat(
142
  message=message,
143
  history=history,
144
  temperature=0.3,
145
  max_new_tokens=1024,
146
+ max_length=8192, # Changed from max_tokens
147
  multi_agent=False,
148
  conversation=[],
149
  max_round=30
150
  ):
151
  response += chunk
152
+
153
+ # Format response in the expected messages format
154
+ return [
155
+ {"role": "user", "content": message},
156
+ {"role": "assistant", "content": response}
157
+ ]
158
 
159
  except Exception as e:
160
  logger.error(f"Chat error: {str(e)}")
161
+ return {"role": "assistant", "content": f"Error: {str(e)}"}
162
 
163
  def create_interface():
164
  app = TxAgentApp()
 
181
  chatbot = gr.Chatbot(
182
  height=500,
183
  label="Conversation",
184
+ bubble_full_width=False
185
  )
186
  msg = gr.Textbox(label="Your clinical question")
187
  clear_btn = gr.Button("Clear Chat")
 
195
  inputs=msg
196
  )
197
 
198
+ def wrapper_initialize():
199
+ status = app.initialize()
200
+ return status, gr.update(interactive=False)
201
+
202
+ def wrapper_chat(message, chat_history):
203
+ response = app.chat(message, chat_history)
204
+ if isinstance(response, dict): # Error case
205
+ return chat_history + [response]
206
+ return response # Normal case
207
+
208
  init_btn.click(
209
+ fn=wrapper_initialize,
210
+ outputs=[init_status, init_btn]
211
  )
212
 
213
  msg.submit(
214
+ fn=wrapper_chat,
215
  inputs=[msg, chatbot],
216
  outputs=chatbot
217
  )