Ali2206 commited on
Commit
8126e99
·
verified ·
1 Parent(s): 853633a

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +22 -17
app.py CHANGED
@@ -12,7 +12,7 @@ import re
12
  import psutil
13
  import subprocess
14
 
15
- # Persistent directory
16
  persistent_dir = "/data/hf_cache"
17
  os.makedirs(persistent_dir, exist_ok=True)
18
 
@@ -132,20 +132,25 @@ def init_agent():
132
  return agent
133
 
134
  def clean_response(response: str) -> str:
135
- """Clean the response by removing tool calls and duplicate content."""
136
- # Remove all tool call blocks
137
- response = re.sub(r'\[TOOL_CALLS\].*?$', '', response, flags=re.DOTALL)
 
 
 
 
138
 
139
- # Remove duplicate content by keeping only the last occurrence
140
- if "Based on the medical records provided" in response:
141
- last_occurrence = response.rfind("Based on the medical records provided")
142
- response = response[last_occurrence:]
 
143
 
144
  # Remove any remaining JSON artifacts
145
- response = re.sub(r'\{.*?\}', '', response)
146
- response = re.sub(r'\[.*?\]', '', response)
147
 
148
- return response.strip()
149
 
150
  def create_ui(agent):
151
  with gr.Blocks(theme=gr.themes.Soft()) as demo:
@@ -207,20 +212,20 @@ Medical Records:
207
  finish_detected = True
208
 
209
  # Display intermediate response
210
- clean_intermediate = clean_response(full_response)
211
- if clean_intermediate:
212
- history[-1] = {"role": "assistant", "content": clean_intermediate}
213
  yield history, None
214
 
215
- # Final processing after all chunks are received
216
  final_cleaned = clean_response(full_response)
217
 
218
  if not final_cleaned:
219
  final_cleaned = "⚠️ No clear oversights identified or model output was invalid."
220
 
221
- # Only save report if we got a proper finish signal
222
  report_path = None
223
- if finish_detected and file_hash_value:
224
  report_path = os.path.join(report_dir, f"{file_hash_value}_report.txt")
225
  with open(report_path, "w", encoding="utf-8") as f:
226
  f.write(final_cleaned)
 
12
  import psutil
13
  import subprocess
14
 
15
+ # Persistent directory setup
16
  persistent_dir = "/data/hf_cache"
17
  os.makedirs(persistent_dir, exist_ok=True)
18
 
 
132
  return agent
133
 
134
  def clean_response(response: str) -> str:
135
+ """Enhanced response cleaner that handles duplicates and tool calls."""
136
+ # First extract the main analysis content
137
+ analysis_match = re.search(
138
+ r'(Based on the medical records provided.*?)(?=\[TOOL_CALLS\]|Based on|$)',
139
+ response,
140
+ flags=re.DOTALL
141
+ )
142
 
143
+ if analysis_match:
144
+ cleaned = analysis_match.group(1).strip()
145
+ else:
146
+ # Fallback if pattern not found
147
+ cleaned = re.sub(r'\[TOOL_CALLS\].*?$', '', response, flags=re.DOTALL).strip()
148
 
149
  # Remove any remaining JSON artifacts
150
+ cleaned = re.sub(r'\{.*?\}', '', cleaned)
151
+ cleaned = re.sub(r'\[.*?\]', '', cleaned)
152
 
153
+ return cleaned
154
 
155
  def create_ui(agent):
156
  with gr.Blocks(theme=gr.themes.Soft()) as demo:
 
212
  finish_detected = True
213
 
214
  # Display intermediate response
215
+ current_cleaned = clean_response(full_response)
216
+ if current_cleaned:
217
+ history[-1] = {"role": "assistant", "content": current_cleaned}
218
  yield history, None
219
 
220
+ # Final processing
221
  final_cleaned = clean_response(full_response)
222
 
223
  if not final_cleaned:
224
  final_cleaned = "⚠️ No clear oversights identified or model output was invalid."
225
 
226
+ # Save report
227
  report_path = None
228
+ if file_hash_value:
229
  report_path = os.path.join(report_dir, f"{file_hash_value}_report.txt")
230
  with open(report_path, "w", encoding="utf-8") as f:
231
  f.write(final_cleaned)