shgao commited on
Commit
b3d2534
·
1 Parent(s): 1850b4a
Files changed (4) hide show
  1. app.py +0 -0
  2. specialties.json +2 -1
  3. subspecialties.json +2 -1
  4. utils.py +38 -8
app.py CHANGED
The diff for this file is too large to render. See raw diff
 
specialties.json CHANGED
@@ -34,5 +34,6 @@
34
  "Radiation Oncology",
35
  "Thoracic and Cardiac Surgery",
36
  "Urology",
37
- "Vascular Surgery"
 
38
  ]
 
34
  "Radiation Oncology",
35
  "Thoracic and Cardiac Surgery",
36
  "Urology",
37
+ "Vascular Surgery",
38
+ "None"
39
  ]
subspecialties.json CHANGED
@@ -90,5 +90,6 @@
90
  "Transplant Hepatology",
91
  "Undersea and Hyperbaric Medicine",
92
  "Urogynecology and Reconstructive Pelvic Surgery",
93
- "Vascular Neurology"
 
94
  ]
 
90
  "Transplant Hepatology",
91
  "Undersea and Hyperbaric Medicine",
92
  "Urogynecology and Reconstructive Pelvic Surgery",
93
+ "Vascular Neurology",
94
+ "None"
95
  ]
utils.py CHANGED
@@ -141,6 +141,7 @@ def append_to_sheet(user_data=None, custom_row_dict=None, custom_sheet_name=None
141
  """
142
  Append a new row to a Google Sheet. If 'custom_row' is provided, append that row.
143
  Otherwise, append a default row constructed from the provided user_data.
 
144
  """
145
  if custom_sheet_name is None:
146
  custom_sheet_name = GSHEET_NAME
@@ -165,6 +166,7 @@ def append_to_sheet(user_data=None, custom_row_dict=None, custom_sheet_name=None
165
  existing_values = sheet.get_all_values()
166
  is_empty = (existing_values == [[]]) #indicates empty spreadsheet that was cleared in the past
167
 
 
168
  if (is_new or is_empty) and add_header_when_create_sheet:
169
  # headers come from the keys of our row dict
170
  if custom_row_dict is not None:
@@ -172,9 +174,23 @@ def append_to_sheet(user_data=None, custom_row_dict=None, custom_sheet_name=None
172
  else:
173
  headers = list(user_data.keys())
174
  sheet.append_row(headers)
175
-
 
 
 
 
176
  if custom_row_dict is not None:
177
- custom_row = [custom_row_dict.get(header) for header in list(custom_row_dict.keys())]
 
 
 
 
 
 
 
 
 
 
178
  else:
179
  # Construct the default row with a timestamp and user_data fields
180
  custom_row = [str(datetime.datetime.now()), user_data["question"], user_data["final_answer"], user_data["trace"]]
@@ -211,7 +227,7 @@ def format_chat(response, tool_database_labels):
211
  title = f"🛠️ {name}"
212
  for db_label, tool_list in tool_database_labels.items():
213
  if name in tool_list:
214
- title = f"🛠️ {name} (**retrieves** {db_label})"
215
  database_label = " (" + db_label + ")"
216
  break
217
 
@@ -229,15 +245,29 @@ def format_chat(response, tool_database_labels):
229
  chat_history.append(
230
  gr.ChatMessage(
231
  role="assistant",
232
- content=f"Input: {json.dumps(args)}\n\nResponse{database_label}:\n{pretty}",
233
  metadata={
234
- "title": title
 
 
235
  }
236
  )
237
  )
238
 
239
  # Clear after rendering
240
  last_tool_calls = []
241
-
242
- return chat_history
243
-
 
 
 
 
 
 
 
 
 
 
 
 
 
141
  """
142
  Append a new row to a Google Sheet. If 'custom_row' is provided, append that row.
143
  Otherwise, append a default row constructed from the provided user_data.
144
+ Ensures that each value is aligned with the correct column header.
145
  """
146
  if custom_sheet_name is None:
147
  custom_sheet_name = GSHEET_NAME
 
166
  existing_values = sheet.get_all_values()
167
  is_empty = (existing_values == [[]]) #indicates empty spreadsheet that was cleared in the past
168
 
169
+ # --- Always ensure header row is present and get headers ---
170
  if (is_new or is_empty) and add_header_when_create_sheet:
171
  # headers come from the keys of our row dict
172
  if custom_row_dict is not None:
 
174
  else:
175
  headers = list(user_data.keys())
176
  sheet.append_row(headers)
177
+ else:
178
+ # Read headers from the first row of the sheet
179
+ headers = sheet.row_values(1) if sheet.row_count > 0 else []
180
+
181
+ # --- Check for new headers in custom_row_dict and add them if needed ---
182
  if custom_row_dict is not None:
183
+ new_headers = [key for key in custom_row_dict.keys() if key not in headers]
184
+ if new_headers:
185
+ # Add new headers to the existing header row
186
+ headers.extend(new_headers)
187
+ # Update the first row in the sheet with the new headers
188
+ sheet.update('1:1', [headers])
189
+
190
+ # --- Build row aligned to headers ---
191
+ if custom_row_dict is not None:
192
+ # Ensure all values are aligned to headers, fill missing with ""
193
+ custom_row = [custom_row_dict.get(header, "") for header in headers]
194
  else:
195
  # Construct the default row with a timestamp and user_data fields
196
  custom_row = [str(datetime.datetime.now()), user_data["question"], user_data["final_answer"], user_data["trace"]]
 
227
  title = f"🛠️ {name}"
228
  for db_label, tool_list in tool_database_labels.items():
229
  if name in tool_list:
230
+ title = f"🛠️ {name}\n(**Info** {db_label} [Click to view])"
231
  database_label = " (" + db_label + ")"
232
  break
233
 
 
245
  chat_history.append(
246
  gr.ChatMessage(
247
  role="assistant",
248
+ content=f"Tool Response{database_label}:\n{pretty}",
249
  metadata={
250
+ "title": title,
251
+ "log": json.dumps(args),
252
+ "status": 'done'
253
  }
254
  )
255
  )
256
 
257
  # Clear after rendering
258
  last_tool_calls = []
259
+ # Post-process: replace [FinalAnswer] with "\n***Answer:**\n" in the last message's content if present
260
+ # Insert "**Rasoning:**\n" at the beginning of the first assistant message if [FinalAnswer] is in the last assistant message
261
+ if chat_history:
262
+ last_msg = chat_history[-1]
263
+ if isinstance(last_msg.content, str) and "[FinalAnswer]" in last_msg.content:
264
+ # Find the first assistant message
265
+ for msg in chat_history:
266
+ if msg.role == "assistant" and isinstance(msg.content, str):
267
+ msg.content = "**Reasoning:**\n" + msg.content
268
+ break
269
+ if chat_history:
270
+ last_msg = chat_history[-1]
271
+ if isinstance(last_msg.content, str) and "[FinalAnswer]" in last_msg.content:
272
+ last_msg.content = last_msg.content.replace("[FinalAnswer]", "\n**Answer:**\n")
273
+ return chat_history