Ali2206 commited on
Commit
edb2500
·
verified ·
1 Parent(s): 8125741

Update ui/ui_core.py

Browse files
Files changed (1) hide show
  1. ui/ui_core.py +22 -6
ui/ui_core.py CHANGED
@@ -80,8 +80,8 @@ def chunk_text(text: str, max_tokens: int = 8192) -> List[str]:
80
  def create_ui(agent: TxAgent):
81
  with gr.Blocks(theme=gr.themes.Soft()) as demo:
82
  gr.Markdown("<h1 style='text-align: center;'>📋 CPS: Clinical Patient Support System</h1>")
83
- chatbot = gr.Chatbot(label="CPS Assistant", height=600, type="messages")
84
 
 
85
  file_upload = gr.File(
86
  label="Upload Medical File",
87
  file_types=[".pdf", ".txt", ".docx", ".jpg", ".png", ".csv", ".xls", ".xlsx"],
@@ -90,7 +90,12 @@ 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
- 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,8 +108,10 @@ def create_ui(agent: TxAgent):
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)
@@ -123,8 +130,10 @@ def create_ui(agent: TxAgent):
123
  extracted_text += f"[Error processing file: {os.path.basename(path)}] — {str(file_error)}\n"
124
  continue
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):
@@ -144,14 +153,20 @@ def create_ui(agent: TxAgent):
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])
@@ -164,3 +179,4 @@ def create_ui(agent: TxAgent):
164
  ], inputs=message_input)
165
 
166
  return demo
 
 
80
  def create_ui(agent: TxAgent):
81
  with gr.Blocks(theme=gr.themes.Soft()) as demo:
82
  gr.Markdown("<h1 style='text-align: center;'>📋 CPS: Clinical Patient Support System</h1>")
 
83
 
84
+ chatbot = gr.Chatbot(label="CPS Assistant", height=600, type="messages", show_copy_button=True)
85
  file_upload = gr.File(
86
  label="Upload Medical File",
87
  file_types=[".pdf", ".txt", ".docx", ".jpg", ".png", ".csv", ".xls", ".xlsx"],
 
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
+ # Centered status box styled
95
+ status_box = gr.Markdown(
96
+ "<div style='text-align:center; font-size: 18px;'>⏳ Processing in progress... Please wait...</div>",
97
+ visible=False
98
+ )
99
 
100
  def handle_chat(message: str, history: list, conversation: list, uploaded_files: list, progress=gr.Progress()):
101
  context = (
 
108
  )
109
 
110
  try:
111
+ # Show loading status
112
+ yield gr.update(visible=True), history
113
 
114
+ # Extract file text
115
  extracted_text = ""
116
  if uploaded_files and isinstance(uploaded_files, list):
117
  total_files = len(uploaded_files)
 
130
  extracted_text += f"[Error processing file: {os.path.basename(path)}] — {str(file_error)}\n"
131
  continue
132
 
133
+ # Sanitize and chunk
134
  sanitized = sanitize_utf8(extracted_text.strip())
135
  chunks = chunk_text(sanitized, max_tokens=8192)
136
+
137
  all_responses = ""
138
 
139
  for i, chunk in enumerate(chunks):
 
153
  )
154
  for update in generator:
155
  if isinstance(update, str):
156
+ all_responses += update # collect only final string output
157
 
158
  all_responses = sanitize_utf8(all_responses.strip())
159
+
160
+ # Convert to gr.Chatbot message format
161
+ history.append({"role": "user", "content": message})
162
+ history.append({"role": "assistant", "content": all_responses})
163
+ yield gr.update(visible=False), history
164
 
165
  except Exception as chat_error:
166
  print(f"Chat handling error: {chat_error}")
167
+ history.append({"role": "user", "content": message})
168
+ history.append({"role": "assistant", "content": "❌ An error occurred while processing your request. Please try again."})
169
+ yield gr.update(visible=False), history
170
 
171
  inputs = [message_input, chatbot, conversation_state, file_upload]
172
  send_button.click(fn=handle_chat, inputs=inputs, outputs=[status_box, chatbot])
 
179
  ], inputs=message_input)
180
 
181
  return demo
182
+