Ali2206 commited on
Commit
adec3a7
·
verified ·
1 Parent(s): 00581cd

Update ui/ui_core.py

Browse files
Files changed (1) hide show
  1. ui/ui_core.py +12 -9
ui/ui_core.py CHANGED
@@ -90,6 +90,7 @@ def create_ui(agent: TxAgent):
90
  message_input = gr.Textbox(placeholder="Ask a biomedical question or just upload the files...", show_label=False)
91
  send_button = gr.Button("Send", variant="primary")
92
  conversation_state = gr.State([])
 
93
 
94
  def handle_chat(message: str, history: list, conversation: list, uploaded_files: list, progress=gr.Progress()):
95
  context = (
@@ -102,13 +103,14 @@ def create_ui(agent: TxAgent):
102
  )
103
 
104
  try:
 
 
105
  extracted_text = ""
106
  if uploaded_files and isinstance(uploaded_files, list):
107
  total_files = len(uploaded_files)
108
  for index, file in enumerate(uploaded_files):
109
  if not hasattr(file, 'name'):
110
  continue
111
-
112
  path = file.name
113
  try:
114
  if path.endswith((".csv", ".xls", ".xlsx")):
@@ -123,6 +125,7 @@ def create_ui(agent: TxAgent):
123
 
124
  sanitized = sanitize_utf8(extracted_text.strip())
125
  chunks = chunk_text(sanitized, max_tokens=8192)
 
126
 
127
  for i, chunk in enumerate(chunks):
128
  full_message = (
@@ -139,20 +142,20 @@ def create_ui(agent: TxAgent):
139
  uploaded_files=uploaded_files,
140
  max_round=30
141
  )
142
-
143
  for update in generator:
144
- if isinstance(update, list):
145
- yield update
146
- elif isinstance(update, str):
147
- yield [("assistant", sanitize_utf8(update))]
 
148
 
149
  except Exception as chat_error:
150
  print(f"Chat handling error: {chat_error}")
151
- yield [("assistant", "An error occurred while processing your request. Please try again.")]
152
 
153
  inputs = [message_input, chatbot, conversation_state, file_upload]
154
- send_button.click(fn=handle_chat, inputs=inputs, outputs=chatbot)
155
- message_input.submit(fn=handle_chat, inputs=inputs, outputs=chatbot)
156
 
157
  gr.Examples([
158
  ["Upload your medical form and ask what the doctor might've missed."],
 
90
  message_input = gr.Textbox(placeholder="Ask a biomedical question or just upload the files...", show_label=False)
91
  send_button = gr.Button("Send", variant="primary")
92
  conversation_state = gr.State([])
93
+ status_box = gr.Markdown(visible=False)
94
 
95
  def handle_chat(message: str, history: list, conversation: list, uploaded_files: list, progress=gr.Progress()):
96
  context = (
 
103
  )
104
 
105
  try:
106
+ yield gr.update(visible=True, value="⏳ Processing in progress... Please wait..."), history
107
+
108
  extracted_text = ""
109
  if uploaded_files and isinstance(uploaded_files, list):
110
  total_files = len(uploaded_files)
111
  for index, file in enumerate(uploaded_files):
112
  if not hasattr(file, 'name'):
113
  continue
 
114
  path = file.name
115
  try:
116
  if path.endswith((".csv", ".xls", ".xlsx")):
 
125
 
126
  sanitized = sanitize_utf8(extracted_text.strip())
127
  chunks = chunk_text(sanitized, max_tokens=8192)
128
+ all_responses = ""
129
 
130
  for i, chunk in enumerate(chunks):
131
  full_message = (
 
142
  uploaded_files=uploaded_files,
143
  max_round=30
144
  )
 
145
  for update in generator:
146
+ if isinstance(update, str):
147
+ all_responses += update
148
+
149
+ all_responses = sanitize_utf8(all_responses.strip())
150
+ yield gr.update(visible=False), history + [[message, all_responses]]
151
 
152
  except Exception as chat_error:
153
  print(f"Chat handling error: {chat_error}")
154
+ yield gr.update(visible=False), history + [[message, "An error occurred while processing your request."]]
155
 
156
  inputs = [message_input, chatbot, conversation_state, file_upload]
157
+ send_button.click(fn=handle_chat, inputs=inputs, outputs=[status_box, chatbot])
158
+ message_input.submit(fn=handle_chat, inputs=inputs, outputs=[status_box, chatbot])
159
 
160
  gr.Examples([
161
  ["Upload your medical form and ask what the doctor might've missed."],