Update app.py
Browse files
app.py
CHANGED
@@ -552,14 +552,11 @@ async def analyze_clinical_report(
|
|
552 |
):
|
553 |
"""
|
554 |
Analyze a clinical patient report from an uploaded file.
|
555 |
-
|
556 |
Parameters:
|
557 |
- file: Uploaded clinical report file (PDF, TXT, DOCX)
|
558 |
- patient_id: Optional patient ID to associate with this report
|
559 |
- temperature: Controls randomness of response (0.1-1.0)
|
560 |
- max_new_tokens: Maximum length of response
|
561 |
-
|
562 |
-
Returns structured analysis of the patient report.
|
563 |
"""
|
564 |
try:
|
565 |
# Validate file type
|
@@ -579,7 +576,7 @@ async def analyze_clinical_report(
|
|
579 |
# Read file content
|
580 |
file_content = await file.read()
|
581 |
|
582 |
-
# Extract text
|
583 |
if content_type == 'application/pdf':
|
584 |
text = extract_text_from_pdf(file_content)
|
585 |
elif content_type == 'text/plain':
|
@@ -587,6 +584,8 @@ async def analyze_clinical_report(
|
|
587 |
elif content_type == 'application/vnd.openxmlformats-officedocument.wordprocessingml.document':
|
588 |
doc = Document(io.BytesIO(file_content))
|
589 |
text = "\n".join([para.text for para in doc.paragraphs])
|
|
|
|
|
590 |
|
591 |
# Clean and validate text
|
592 |
text = clean_text_response(text)
|
@@ -604,7 +603,13 @@ async def analyze_clinical_report(
|
|
604 |
file_content=file_content
|
605 |
)
|
606 |
|
607 |
-
#
|
|
|
|
|
|
|
|
|
|
|
|
|
608 |
return JSONResponse(content=jsonable_encoder({
|
609 |
"status": "success",
|
610 |
"analysis": analysis,
|
@@ -621,6 +626,8 @@ async def analyze_clinical_report(
|
|
621 |
status_code=500,
|
622 |
detail=f"Failed to analyze report: {str(e)}"
|
623 |
)
|
|
|
|
|
624 |
if __name__ == "__main__":
|
625 |
import uvicorn
|
626 |
uvicorn.run(app, host="0.0.0.0", port=8000)
|
|
|
552 |
):
|
553 |
"""
|
554 |
Analyze a clinical patient report from an uploaded file.
|
|
|
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 |
try:
|
562 |
# Validate file type
|
|
|
576 |
# Read file content
|
577 |
file_content = await file.read()
|
578 |
|
579 |
+
# Extract text from file
|
580 |
if content_type == 'application/pdf':
|
581 |
text = extract_text_from_pdf(file_content)
|
582 |
elif content_type == 'text/plain':
|
|
|
584 |
elif content_type == 'application/vnd.openxmlformats-officedocument.wordprocessingml.document':
|
585 |
doc = Document(io.BytesIO(file_content))
|
586 |
text = "\n".join([para.text for para in doc.paragraphs])
|
587 |
+
else:
|
588 |
+
raise HTTPException(status_code=400, detail="Unsupported file type")
|
589 |
|
590 |
# Clean and validate text
|
591 |
text = clean_text_response(text)
|
|
|
603 |
file_content=file_content
|
604 |
)
|
605 |
|
606 |
+
# Manually convert ObjectId and timestamp if needed
|
607 |
+
if "_id" in analysis and isinstance(analysis["_id"], ObjectId):
|
608 |
+
analysis["_id"] = str(analysis["_id"])
|
609 |
+
if "timestamp" in analysis and isinstance(analysis["timestamp"], datetime):
|
610 |
+
analysis["timestamp"] = analysis["timestamp"].isoformat()
|
611 |
+
|
612 |
+
# Return response using jsonable_encoder
|
613 |
return JSONResponse(content=jsonable_encoder({
|
614 |
"status": "success",
|
615 |
"analysis": analysis,
|
|
|
626 |
status_code=500,
|
627 |
detail=f"Failed to analyze report: {str(e)}"
|
628 |
)
|
629 |
+
|
630 |
+
|
631 |
if __name__ == "__main__":
|
632 |
import uvicorn
|
633 |
uvicorn.run(app, host="0.0.0.0", port=8000)
|