Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -273,6 +273,91 @@ Return ONLY valid JSON with keys: assistant_reply, patientDetails, conversationS
|
|
273 |
|
274 |
return jsonify(response_payload)
|
275 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
276 |
@app.route("/ping", methods=["GET"])
|
277 |
def ping():
|
278 |
return jsonify({"status": "ok"})
|
|
|
273 |
|
274 |
return jsonify(response_payload)
|
275 |
|
276 |
+
@app.route("/upload_reports", methods=["POST"])
|
277 |
+
def upload_reports():
|
278 |
+
"""
|
279 |
+
Upload one or more files for a patient.
|
280 |
+
|
281 |
+
Expects multipart/form-data with:
|
282 |
+
- patient_id (form field)
|
283 |
+
- files (one or multiple files; use the same field name 'files' for each file)
|
284 |
+
|
285 |
+
Example curl:
|
286 |
+
curl -X POST http://localhost:7860/upload_reports \
|
287 |
+
-F "patient_id=12345" \
|
288 |
+
-F "files[]=@/path/to/report1.pdf" \
|
289 |
+
-F "files[]=@/path/to/report2.pdf"
|
290 |
+
"""
|
291 |
+
try:
|
292 |
+
# patient id can be in form or args (for convenience)
|
293 |
+
patient_id = request.form.get("patient_id") or request.args.get("patient_id")
|
294 |
+
if not patient_id:
|
295 |
+
return jsonify({"error": "patient_id form field required"}), 400
|
296 |
+
|
297 |
+
# get uploaded files (support both files and files[] naming)
|
298 |
+
uploaded_files = request.files.getlist("files")
|
299 |
+
if not uploaded_files:
|
300 |
+
# fallback: single file under name 'file'
|
301 |
+
single = request.files.get("file")
|
302 |
+
if single:
|
303 |
+
uploaded_files = [single]
|
304 |
+
|
305 |
+
if not uploaded_files:
|
306 |
+
return jsonify({"error": "no files uploaded (use form field 'files')"}), 400
|
307 |
+
|
308 |
+
# create patient folder under REPORTS_ROOT/<patient_id>
|
309 |
+
patient_folder = REPORTS_ROOT / str(patient_id)
|
310 |
+
patient_folder.mkdir(parents=True, exist_ok=True)
|
311 |
+
|
312 |
+
saved = []
|
313 |
+
skipped = []
|
314 |
+
|
315 |
+
for file_storage in uploaded_files:
|
316 |
+
orig_name = getattr(file_storage, "filename", "") or ""
|
317 |
+
filename = secure_filename(orig_name)
|
318 |
+
if not filename:
|
319 |
+
skipped.append({"filename": orig_name, "reason": "invalid filename"})
|
320 |
+
continue
|
321 |
+
|
322 |
+
# extension check
|
323 |
+
ext = filename.rsplit(".", 1)[1].lower() if "." in filename else ""
|
324 |
+
if ext not in ALLOWED_EXTENSIONS:
|
325 |
+
skipped.append({"filename": filename, "reason": f"extension '{ext}' not allowed"})
|
326 |
+
continue
|
327 |
+
|
328 |
+
# avoid overwriting: if collision, add numeric suffix
|
329 |
+
dest = patient_folder / filename
|
330 |
+
if dest.exists():
|
331 |
+
base, dot, extension = filename.rpartition(".")
|
332 |
+
# if no base (e.g. ".bashrc") fallback
|
333 |
+
base = base or filename
|
334 |
+
i = 1
|
335 |
+
while True:
|
336 |
+
candidate = f"{base}__{i}.{extension}" if extension else f"{base}__{i}"
|
337 |
+
dest = patient_folder / candidate
|
338 |
+
if not dest.exists():
|
339 |
+
filename = candidate
|
340 |
+
break
|
341 |
+
i += 1
|
342 |
+
|
343 |
+
try:
|
344 |
+
file_storage.save(str(dest))
|
345 |
+
saved.append(filename)
|
346 |
+
except Exception as e:
|
347 |
+
logger.exception("Failed to save uploaded file %s: %s", filename, e)
|
348 |
+
skipped.append({"filename": filename, "reason": f"save failed: {e}"})
|
349 |
+
|
350 |
+
return jsonify({
|
351 |
+
"patient_id": str(patient_id),
|
352 |
+
"saved": saved,
|
353 |
+
"skipped": skipped,
|
354 |
+
"patient_folder": str(patient_folder)
|
355 |
+
}), 200
|
356 |
+
|
357 |
+
except Exception as exc:
|
358 |
+
logger.exception("Upload failed: %s", exc)
|
359 |
+
return jsonify({"error": "upload failed", "detail": str(exc)}), 500
|
360 |
+
|
361 |
@app.route("/ping", methods=["GET"])
|
362 |
def ping():
|
363 |
return jsonify({"status": "ok"})
|