Update app.py
Browse files
app.py
CHANGED
@@ -544,87 +544,81 @@ async def voice_chat_endpoint(
|
|
544 |
|
545 |
@app.post("/analyze-report")
|
546 |
async def analyze_clinical_report(
|
547 |
-
file:
|
548 |
-
|
549 |
temperature: float = Form(0.5),
|
550 |
max_new_tokens: int = Form(1024)
|
551 |
):
|
552 |
"""
|
553 |
-
Analyze a clinical patient report
|
554 |
|
555 |
Parameters:
|
556 |
- file: Uploaded clinical report file (PDF, TXT, DOCX)
|
557 |
-
-
|
558 |
- temperature: Controls randomness of response (0.1-1.0)
|
559 |
- max_new_tokens: Maximum length of response
|
560 |
|
561 |
Returns structured analysis of the patient report.
|
562 |
"""
|
563 |
try:
|
564 |
-
# Validate
|
565 |
-
|
566 |
-
|
|
|
|
|
|
|
|
|
567 |
|
568 |
-
|
569 |
-
|
570 |
-
|
571 |
-
|
572 |
-
|
573 |
-
|
574 |
-
|
575 |
-
|
576 |
-
elif file.filename.lower().endswith(('.txt', '.md')):
|
577 |
-
# Plain text
|
578 |
-
report_text = (await file.read()).decode('utf-8')
|
579 |
-
elif file.filename.lower().endswith(('.docx', '.doc')):
|
580 |
-
# Word document
|
581 |
-
from docx import Document
|
582 |
-
doc = Document(io.BytesIO(await file.read()))
|
583 |
-
report_text = "\n".join([para.text for para in doc.paragraphs])
|
584 |
-
else:
|
585 |
-
raise HTTPException(status_code=400, detail="Unsupported file format")
|
586 |
|
587 |
-
#
|
588 |
-
if
|
589 |
-
|
|
|
|
|
|
|
|
|
|
|
590 |
|
591 |
-
#
|
592 |
-
|
593 |
-
|
594 |
-
|
595 |
-
|
596 |
-
|
597 |
-
|
598 |
-
|
599 |
-
|
600 |
-
|
601 |
-
|
602 |
-
|
603 |
-
|
604 |
-
|
605 |
-
history=[],
|
606 |
-
temperature=temperature,
|
607 |
-
max_new_tokens=max_new_tokens
|
608 |
)
|
609 |
-
|
610 |
-
|
611 |
-
|
612 |
-
|
613 |
-
|
614 |
-
|
615 |
-
|
616 |
-
|
617 |
-
|
618 |
-
"factors": risk_factors
|
619 |
-
}
|
620 |
-
|
621 |
-
return JSONResponse(content=structured_response)
|
622 |
-
|
623 |
except HTTPException:
|
624 |
raise
|
625 |
except Exception as e:
|
626 |
-
logger.error(f"Error
|
627 |
-
raise HTTPException(
|
|
|
|
|
|
|
628 |
if __name__ == "__main__":
|
629 |
import uvicorn
|
630 |
uvicorn.run(app, host="0.0.0.0", port=8000)
|
|
|
544 |
|
545 |
@app.post("/analyze-report")
|
546 |
async def analyze_clinical_report(
|
547 |
+
file: UploadFile = File(...),
|
548 |
+
patient_id: Optional[str] = Form(None),
|
549 |
temperature: float = Form(0.5),
|
550 |
max_new_tokens: int = Form(1024)
|
551 |
):
|
552 |
"""
|
553 |
+
Analyze a clinical patient report from an uploaded file.
|
554 |
|
555 |
Parameters:
|
556 |
- file: Uploaded clinical report file (PDF, TXT, DOCX)
|
557 |
+
- patient_id: Optional patient ID to associate with this report
|
558 |
- temperature: Controls randomness of response (0.1-1.0)
|
559 |
- max_new_tokens: Maximum length of response
|
560 |
|
561 |
Returns structured analysis of the patient report.
|
562 |
"""
|
563 |
try:
|
564 |
+
# Validate file type
|
565 |
+
content_type = file.content_type
|
566 |
+
allowed_types = [
|
567 |
+
'application/pdf',
|
568 |
+
'text/plain',
|
569 |
+
'application/vnd.openxmlformats-officedocument.wordprocessingml.document'
|
570 |
+
]
|
571 |
|
572 |
+
if content_type not in allowed_types:
|
573 |
+
raise HTTPException(
|
574 |
+
status_code=400,
|
575 |
+
detail=f"Unsupported file type: {content_type}. Supported types: PDF, TXT, DOCX"
|
576 |
+
)
|
577 |
+
|
578 |
+
# Read file content
|
579 |
+
file_content = await file.read()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
580 |
|
581 |
+
# Extract text based on file type
|
582 |
+
if content_type == 'application/pdf':
|
583 |
+
text = extract_text_from_pdf(file_content)
|
584 |
+
elif content_type == 'text/plain':
|
585 |
+
text = file_content.decode('utf-8')
|
586 |
+
elif content_type == 'application/vnd.openxmlformats-officedocument.wordprocessingml.document':
|
587 |
+
doc = Document(io.BytesIO(file_content))
|
588 |
+
text = "\n".join([para.text for para in doc.paragraphs])
|
589 |
|
590 |
+
# Clean and validate text
|
591 |
+
text = clean_text_response(text)
|
592 |
+
if len(text.strip()) < 50:
|
593 |
+
raise HTTPException(
|
594 |
+
status_code=400,
|
595 |
+
detail="Extracted text is too short (minimum 50 characters required)"
|
596 |
+
)
|
597 |
+
|
598 |
+
# Analyze the report
|
599 |
+
analysis = await analyze_patient_report(
|
600 |
+
patient_id=patient_id,
|
601 |
+
report_content=text,
|
602 |
+
file_type=content_type,
|
603 |
+
file_content=file_content
|
|
|
|
|
|
|
604 |
)
|
605 |
+
|
606 |
+
return JSONResponse(content={
|
607 |
+
"status": "success",
|
608 |
+
"analysis": analysis,
|
609 |
+
"patient_id": patient_id,
|
610 |
+
"file_type": content_type,
|
611 |
+
"file_size": len(file_content)
|
612 |
+
})
|
613 |
+
|
|
|
|
|
|
|
|
|
|
|
614 |
except HTTPException:
|
615 |
raise
|
616 |
except Exception as e:
|
617 |
+
logger.error(f"Error in report analysis: {str(e)}")
|
618 |
+
raise HTTPException(
|
619 |
+
status_code=500,
|
620 |
+
detail=f"Failed to analyze report: {str(e)}"
|
621 |
+
)
|
622 |
if __name__ == "__main__":
|
623 |
import uvicorn
|
624 |
uvicorn.run(app, host="0.0.0.0", port=8000)
|