Spaces:
Sleeping
Sleeping
Commit
Β·
518996e
1
Parent(s):
3a16696
Fix: Remove file upload to resolve deployment error
Browse files
app.py
CHANGED
|
@@ -45,7 +45,6 @@ def analyze_provider_notes(provider_notes, api_url, progress=gr.Progress()):
|
|
| 45 |
|
| 46 |
progress(0.9, desc="Formatting results...")
|
| 47 |
|
| 48 |
-
# Format complete response
|
| 49 |
formatted_response = format_complete_response(result)
|
| 50 |
|
| 51 |
progress(1.0, desc="Complete!")
|
|
@@ -61,105 +60,6 @@ def analyze_provider_notes(provider_notes, api_url, progress=gr.Progress()):
|
|
| 61 |
except Exception as e:
|
| 62 |
return f"β **Unexpected Error**\n\n{str(e)}"
|
| 63 |
|
| 64 |
-
def upload_file_analysis(file, api_url, progress=gr.Progress()):
|
| 65 |
-
"""Upload TXT file to backend API for analysis"""
|
| 66 |
-
if file is None:
|
| 67 |
-
return "β οΈ Please upload a TXT file first."
|
| 68 |
-
|
| 69 |
-
progress(0, desc="Reading file...")
|
| 70 |
-
|
| 71 |
-
try:
|
| 72 |
-
# Gradio 4.44+ returns file path as string
|
| 73 |
-
file_path = file.name if hasattr(file, 'name') else file
|
| 74 |
-
|
| 75 |
-
# Read file content
|
| 76 |
-
with open(file_path, 'r', encoding='utf-8') as f:
|
| 77 |
-
file_content = f.read()
|
| 78 |
-
|
| 79 |
-
filename = os.path.basename(file_path)
|
| 80 |
-
|
| 81 |
-
progress(0.3, desc="Sending file to API...")
|
| 82 |
-
|
| 83 |
-
# Prepare multipart form data
|
| 84 |
-
files = {
|
| 85 |
-
'file': (filename, file_content.encode('utf-8'), 'text/plain')
|
| 86 |
-
}
|
| 87 |
-
|
| 88 |
-
response = requests.post(
|
| 89 |
-
f"{api_url}/api/v1/upload-file",
|
| 90 |
-
files=files,
|
| 91 |
-
timeout=60
|
| 92 |
-
)
|
| 93 |
-
|
| 94 |
-
progress(0.6, desc="Processing file...")
|
| 95 |
-
|
| 96 |
-
response.raise_for_status()
|
| 97 |
-
result = response.json()
|
| 98 |
-
|
| 99 |
-
progress(0.9, desc="Formatting results...")
|
| 100 |
-
|
| 101 |
-
# Format file upload response
|
| 102 |
-
formatted_response = format_file_upload_response(result)
|
| 103 |
-
|
| 104 |
-
progress(1.0, desc="Complete!")
|
| 105 |
-
|
| 106 |
-
return formatted_response
|
| 107 |
-
|
| 108 |
-
except requests.exceptions.Timeout:
|
| 109 |
-
return "β **Request Timeout**\n\nThe API is taking too long to process the file. Please try again."
|
| 110 |
-
except requests.exceptions.HTTPError as e:
|
| 111 |
-
return f"β **HTTP Error {e.response.status_code}**\n\n{e.response.text}"
|
| 112 |
-
except requests.exceptions.RequestException as e:
|
| 113 |
-
return f"β **Request Error**\n\n{str(e)}"
|
| 114 |
-
except UnicodeDecodeError:
|
| 115 |
-
return "β **File Encoding Error**\n\nPlease ensure the file is a valid TXT file with UTF-8 encoding."
|
| 116 |
-
except FileNotFoundError:
|
| 117 |
-
return "β **File Not Found**\n\nThe uploaded file could not be found. Please try again."
|
| 118 |
-
except Exception as e:
|
| 119 |
-
return f"β **Unexpected Error**\n\n{str(e)}"
|
| 120 |
-
|
| 121 |
-
def format_file_upload_response(result):
|
| 122 |
-
"""Format file upload response"""
|
| 123 |
-
output = "---\n\n"
|
| 124 |
-
|
| 125 |
-
# File info
|
| 126 |
-
output += f"### π File Information\n\n"
|
| 127 |
-
output += f"**Filename:** {result.get('filename', 'N/A')}\n\n"
|
| 128 |
-
output += f"**Text Length:** {result.get('extracted_text_length', 0)} characters\n\n"
|
| 129 |
-
|
| 130 |
-
if result.get('pii_removed'):
|
| 131 |
-
output += f"**PII Removed:** β
Yes ({result.get('pii_count', 0)} entities removed)\n\n"
|
| 132 |
-
else:
|
| 133 |
-
output += f"**PII Removed:** β No\n\n"
|
| 134 |
-
|
| 135 |
-
output += "---\n\n"
|
| 136 |
-
|
| 137 |
-
# ICD Codes
|
| 138 |
-
output += "### π₯ ICD-10 Diagnostic Codes\n\n"
|
| 139 |
-
icd_codes = result.get("icd_codes", [])
|
| 140 |
-
|
| 141 |
-
if icd_codes:
|
| 142 |
-
for idx, code in enumerate(icd_codes, 1):
|
| 143 |
-
output += f"**{idx}.** `{code}`\n\n"
|
| 144 |
-
output += f"\n**Explanation:** {result.get('icd_explanation', 'N/A')}\n\n"
|
| 145 |
-
else:
|
| 146 |
-
output += "*No ICD-10 codes identified*\n\n"
|
| 147 |
-
|
| 148 |
-
output += "---\n\n"
|
| 149 |
-
|
| 150 |
-
# CPT Codes
|
| 151 |
-
output += "### πΌ CPT Procedure Codes\n\n"
|
| 152 |
-
cpt_codes = result.get("cpt_codes", [])
|
| 153 |
-
|
| 154 |
-
if cpt_codes:
|
| 155 |
-
for idx, code in enumerate(cpt_codes, 1):
|
| 156 |
-
output += f"**{idx}.** `{code}`\n\n"
|
| 157 |
-
output += f"\n**Explanation:** {result.get('cpt_explanation', 'N/A')}\n\n"
|
| 158 |
-
else:
|
| 159 |
-
output += "*No CPT codes identified*\n\n"
|
| 160 |
-
|
| 161 |
-
return output
|
| 162 |
-
|
| 163 |
def generate_empty_response():
|
| 164 |
"""Generate empty response message"""
|
| 165 |
return """
|
|
@@ -173,22 +73,19 @@ Please enter provider notes in the text box below and click **Send** to analyze.
|
|
| 173 |
- Be specific about treatments and prescriptions
|
| 174 |
|
| 175 |
**How to use:**
|
| 176 |
-
1. Select an example from the sidebar
|
| 177 |
-
2.
|
| 178 |
-
3.
|
| 179 |
-
4.
|
| 180 |
-
5. Review the ICD-10 and CPT codes generated
|
| 181 |
"""
|
| 182 |
|
| 183 |
def format_complete_response(result):
|
| 184 |
"""Format complete analysis response"""
|
| 185 |
output = "---\n\n"
|
| 186 |
|
| 187 |
-
# Overall Summary
|
| 188 |
if result.get("overall_summary"):
|
| 189 |
output += f"### π Overall Summary\n\n{result['overall_summary']}\n\n---\n\n"
|
| 190 |
|
| 191 |
-
# ICD-10 Codes
|
| 192 |
output += "### π₯ ICD-10 Diagnostic Codes\n\n"
|
| 193 |
icd_codes = result.get("icd_codes", [])
|
| 194 |
|
|
@@ -201,7 +98,6 @@ def format_complete_response(result):
|
|
| 201 |
|
| 202 |
output += "---\n\n"
|
| 203 |
|
| 204 |
-
# CPT Codes
|
| 205 |
output += "### πΌ CPT Procedure Codes\n\n"
|
| 206 |
cpt_codes = result.get("cpt_codes", [])
|
| 207 |
|
|
@@ -214,7 +110,7 @@ def format_complete_response(result):
|
|
| 214 |
|
| 215 |
return output
|
| 216 |
|
| 217 |
-
# ==================== Example Notes
|
| 218 |
|
| 219 |
EXAMPLES = {
|
| 220 |
"Acute Bronchitis": """Patient presents with acute bronchitis. Cough for 5 days, productive with yellow sputum. Lung exam reveals diffuse wheezing. Prescribed azithromycin 500mg.""",
|
|
@@ -244,7 +140,6 @@ def load_css():
|
|
| 244 |
|
| 245 |
with gr.Blocks(css=load_css(), theme=gr.themes.Soft(), title="Medical Coding (ICD-10) Assistant") as demo:
|
| 246 |
|
| 247 |
-
# Hidden state for API URL
|
| 248 |
api_url_state = gr.State(value="https://Distopia22-icd-cpt-coding-api.hf.space")
|
| 249 |
|
| 250 |
# Header
|
|
@@ -255,14 +150,12 @@ with gr.Blocks(css=load_css(), theme=gr.themes.Soft(), title="Medical Coding (IC
|
|
| 255 |
</div>
|
| 256 |
""")
|
| 257 |
|
| 258 |
-
# Main Layout Container
|
| 259 |
with gr.Row(elem_classes="main-container"):
|
| 260 |
|
| 261 |
-
#
|
| 262 |
with gr.Column(scale=1, min_width=280, elem_classes="sidebar"):
|
| 263 |
gr.HTML('<div class="sidebar-title">π Example Cases</div>')
|
| 264 |
|
| 265 |
-
# Example buttons (Only 3)
|
| 266 |
example_buttons = {}
|
| 267 |
for example_name in EXAMPLES.keys():
|
| 268 |
btn = gr.Button(
|
|
@@ -273,25 +166,6 @@ with gr.Blocks(css=load_css(), theme=gr.themes.Soft(), title="Medical Coding (IC
|
|
| 273 |
|
| 274 |
gr.Markdown("---")
|
| 275 |
|
| 276 |
-
# File Upload Section
|
| 277 |
-
gr.HTML('<div class="sidebar-title">π€ Upload Document</div>')
|
| 278 |
-
|
| 279 |
-
file_upload = gr.File(
|
| 280 |
-
label="Upload TXT File",
|
| 281 |
-
file_count="single",
|
| 282 |
-
file_types=[".txt"],
|
| 283 |
-
elem_classes="file-upload"
|
| 284 |
-
)
|
| 285 |
-
|
| 286 |
-
upload_btn = gr.Button(
|
| 287 |
-
"π Analyze Uploaded File",
|
| 288 |
-
elem_classes="upload-btn"
|
| 289 |
-
)
|
| 290 |
-
|
| 291 |
-
gr.Markdown("*Only .txt files are supported*", elem_classes="upload-info")
|
| 292 |
-
|
| 293 |
-
gr.Markdown("---")
|
| 294 |
-
|
| 295 |
# API Configuration
|
| 296 |
with gr.Accordion("βοΈ API Configuration", open=False, elem_classes="api-config"):
|
| 297 |
api_url_input = gr.Textbox(
|
|
@@ -304,17 +178,15 @@ with gr.Blocks(css=load_css(), theme=gr.themes.Soft(), title="Medical Coding (IC
|
|
| 304 |
check_api_btn = gr.Button("π Check API Status", elem_classes="check-api-button", size="sm")
|
| 305 |
api_status_output = gr.Textbox(label="Status", lines=3, interactive=False)
|
| 306 |
|
| 307 |
-
#
|
| 308 |
with gr.Column(scale=3, elem_classes="chat-container"):
|
| 309 |
|
| 310 |
-
# Output/Response Area
|
| 311 |
output_area = gr.Markdown(
|
| 312 |
value=generate_empty_response(),
|
| 313 |
elem_classes="output-area",
|
| 314 |
label=""
|
| 315 |
)
|
| 316 |
|
| 317 |
-
# Input Area (Prompt Box) - GPT Style
|
| 318 |
with gr.Group(elem_classes="input-container"):
|
| 319 |
with gr.Row():
|
| 320 |
provider_notes_input = gr.Textbox(
|
|
@@ -338,54 +210,40 @@ with gr.Blocks(css=load_css(), theme=gr.themes.Soft(), title="Medical Coding (IC
|
|
| 338 |
</div>
|
| 339 |
""")
|
| 340 |
|
| 341 |
-
#
|
| 342 |
-
|
| 343 |
-
# Example button clicks
|
| 344 |
for example_name, btn in example_buttons.items():
|
| 345 |
btn.click(
|
| 346 |
fn=lambda name=example_name: load_example(name),
|
| 347 |
outputs=[provider_notes_input]
|
| 348 |
)
|
| 349 |
|
| 350 |
-
# Upload button - Analyze uploaded file
|
| 351 |
-
upload_btn.click(
|
| 352 |
-
fn=upload_file_analysis,
|
| 353 |
-
inputs=[file_upload, api_url_state],
|
| 354 |
-
outputs=[output_area]
|
| 355 |
-
)
|
| 356 |
-
|
| 357 |
-
# Send button - Analyze notes
|
| 358 |
send_btn.click(
|
| 359 |
fn=analyze_provider_notes,
|
| 360 |
inputs=[provider_notes_input, api_url_state],
|
| 361 |
outputs=[output_area]
|
| 362 |
)
|
| 363 |
|
| 364 |
-
# Clear button
|
| 365 |
clear_btn.click(
|
| 366 |
fn=lambda: ("", generate_empty_response()),
|
| 367 |
outputs=[provider_notes_input, output_area]
|
| 368 |
)
|
| 369 |
|
| 370 |
-
# Check API Health
|
| 371 |
check_api_btn.click(
|
| 372 |
fn=check_api_health,
|
| 373 |
inputs=[api_url_input],
|
| 374 |
outputs=[api_status_output]
|
| 375 |
)
|
| 376 |
|
| 377 |
-
# Update API URL state when changed
|
| 378 |
api_url_input.change(
|
| 379 |
fn=lambda x: x,
|
| 380 |
inputs=[api_url_input],
|
| 381 |
outputs=[api_url_state]
|
| 382 |
)
|
| 383 |
|
| 384 |
-
# Launch
|
| 385 |
if __name__ == "__main__":
|
| 386 |
demo.launch(
|
| 387 |
server_name="0.0.0.0",
|
| 388 |
server_port=7860,
|
| 389 |
-
share=False,
|
| 390 |
show_error=True
|
| 391 |
)
|
|
|
|
| 45 |
|
| 46 |
progress(0.9, desc="Formatting results...")
|
| 47 |
|
|
|
|
| 48 |
formatted_response = format_complete_response(result)
|
| 49 |
|
| 50 |
progress(1.0, desc="Complete!")
|
|
|
|
| 60 |
except Exception as e:
|
| 61 |
return f"β **Unexpected Error**\n\n{str(e)}"
|
| 62 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 63 |
def generate_empty_response():
|
| 64 |
"""Generate empty response message"""
|
| 65 |
return """
|
|
|
|
| 73 |
- Be specific about treatments and prescriptions
|
| 74 |
|
| 75 |
**How to use:**
|
| 76 |
+
1. Select an example from the sidebar
|
| 77 |
+
2. Type your own clinical notes below
|
| 78 |
+
3. Click the **Send** button
|
| 79 |
+
4. Review the ICD-10 and CPT codes generated
|
|
|
|
| 80 |
"""
|
| 81 |
|
| 82 |
def format_complete_response(result):
|
| 83 |
"""Format complete analysis response"""
|
| 84 |
output = "---\n\n"
|
| 85 |
|
|
|
|
| 86 |
if result.get("overall_summary"):
|
| 87 |
output += f"### π Overall Summary\n\n{result['overall_summary']}\n\n---\n\n"
|
| 88 |
|
|
|
|
| 89 |
output += "### π₯ ICD-10 Diagnostic Codes\n\n"
|
| 90 |
icd_codes = result.get("icd_codes", [])
|
| 91 |
|
|
|
|
| 98 |
|
| 99 |
output += "---\n\n"
|
| 100 |
|
|
|
|
| 101 |
output += "### πΌ CPT Procedure Codes\n\n"
|
| 102 |
cpt_codes = result.get("cpt_codes", [])
|
| 103 |
|
|
|
|
| 110 |
|
| 111 |
return output
|
| 112 |
|
| 113 |
+
# ==================== Example Notes ====================
|
| 114 |
|
| 115 |
EXAMPLES = {
|
| 116 |
"Acute Bronchitis": """Patient presents with acute bronchitis. Cough for 5 days, productive with yellow sputum. Lung exam reveals diffuse wheezing. Prescribed azithromycin 500mg.""",
|
|
|
|
| 140 |
|
| 141 |
with gr.Blocks(css=load_css(), theme=gr.themes.Soft(), title="Medical Coding (ICD-10) Assistant") as demo:
|
| 142 |
|
|
|
|
| 143 |
api_url_state = gr.State(value="https://Distopia22-icd-cpt-coding-api.hf.space")
|
| 144 |
|
| 145 |
# Header
|
|
|
|
| 150 |
</div>
|
| 151 |
""")
|
| 152 |
|
|
|
|
| 153 |
with gr.Row(elem_classes="main-container"):
|
| 154 |
|
| 155 |
+
# Left Sidebar
|
| 156 |
with gr.Column(scale=1, min_width=280, elem_classes="sidebar"):
|
| 157 |
gr.HTML('<div class="sidebar-title">π Example Cases</div>')
|
| 158 |
|
|
|
|
| 159 |
example_buttons = {}
|
| 160 |
for example_name in EXAMPLES.keys():
|
| 161 |
btn = gr.Button(
|
|
|
|
| 166 |
|
| 167 |
gr.Markdown("---")
|
| 168 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 169 |
# API Configuration
|
| 170 |
with gr.Accordion("βοΈ API Configuration", open=False, elem_classes="api-config"):
|
| 171 |
api_url_input = gr.Textbox(
|
|
|
|
| 178 |
check_api_btn = gr.Button("π Check API Status", elem_classes="check-api-button", size="sm")
|
| 179 |
api_status_output = gr.Textbox(label="Status", lines=3, interactive=False)
|
| 180 |
|
| 181 |
+
# Right Chat Area
|
| 182 |
with gr.Column(scale=3, elem_classes="chat-container"):
|
| 183 |
|
|
|
|
| 184 |
output_area = gr.Markdown(
|
| 185 |
value=generate_empty_response(),
|
| 186 |
elem_classes="output-area",
|
| 187 |
label=""
|
| 188 |
)
|
| 189 |
|
|
|
|
| 190 |
with gr.Group(elem_classes="input-container"):
|
| 191 |
with gr.Row():
|
| 192 |
provider_notes_input = gr.Textbox(
|
|
|
|
| 210 |
</div>
|
| 211 |
""")
|
| 212 |
|
| 213 |
+
# Event Handlers
|
|
|
|
|
|
|
| 214 |
for example_name, btn in example_buttons.items():
|
| 215 |
btn.click(
|
| 216 |
fn=lambda name=example_name: load_example(name),
|
| 217 |
outputs=[provider_notes_input]
|
| 218 |
)
|
| 219 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 220 |
send_btn.click(
|
| 221 |
fn=analyze_provider_notes,
|
| 222 |
inputs=[provider_notes_input, api_url_state],
|
| 223 |
outputs=[output_area]
|
| 224 |
)
|
| 225 |
|
|
|
|
| 226 |
clear_btn.click(
|
| 227 |
fn=lambda: ("", generate_empty_response()),
|
| 228 |
outputs=[provider_notes_input, output_area]
|
| 229 |
)
|
| 230 |
|
|
|
|
| 231 |
check_api_btn.click(
|
| 232 |
fn=check_api_health,
|
| 233 |
inputs=[api_url_input],
|
| 234 |
outputs=[api_status_output]
|
| 235 |
)
|
| 236 |
|
|
|
|
| 237 |
api_url_input.change(
|
| 238 |
fn=lambda x: x,
|
| 239 |
inputs=[api_url_input],
|
| 240 |
outputs=[api_url_state]
|
| 241 |
)
|
| 242 |
|
| 243 |
+
# Launch
|
| 244 |
if __name__ == "__main__":
|
| 245 |
demo.launch(
|
| 246 |
server_name="0.0.0.0",
|
| 247 |
server_port=7860,
|
|
|
|
| 248 |
show_error=True
|
| 249 |
)
|