Update app.py
Browse files
app.py
CHANGED
@@ -22,7 +22,8 @@ import PyPDF2
|
|
22 |
import mimetypes
|
23 |
from txagent.txagent import TxAgent
|
24 |
from db.mongo import get_mongo_client
|
25 |
-
|
|
|
26 |
# Logging
|
27 |
logging.basicConfig(level=logging.INFO, format="%(asctime)s - %(levelname)s - %(message)s")
|
28 |
logger = logging.getLogger("TxAgentAPI")
|
@@ -551,13 +552,13 @@ async def analyze_clinical_report(
|
|
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:
|
@@ -568,7 +569,7 @@ async def analyze_clinical_report(
|
|
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,
|
@@ -577,7 +578,7 @@ async def analyze_clinical_report(
|
|
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)
|
@@ -586,7 +587,7 @@ async def analyze_clinical_report(
|
|
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:
|
@@ -603,13 +604,14 @@ async def analyze_clinical_report(
|
|
603 |
file_content=file_content
|
604 |
)
|
605 |
|
606 |
-
|
|
|
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
|
|
|
22 |
import mimetypes
|
23 |
from txagent.txagent import TxAgent
|
24 |
from db.mongo import get_mongo_client
|
25 |
+
from fastapi.encoders import jsonable_encoder
|
26 |
+
from docx import Document
|
27 |
# Logging
|
28 |
logging.basicConfig(level=logging.INFO, format="%(asctime)s - %(levelname)s - %(message)s")
|
29 |
logger = logging.getLogger("TxAgentAPI")
|
|
|
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:
|
|
|
569 |
'text/plain',
|
570 |
'application/vnd.openxmlformats-officedocument.wordprocessingml.document'
|
571 |
]
|
572 |
+
|
573 |
if content_type not in allowed_types:
|
574 |
raise HTTPException(
|
575 |
status_code=400,
|
|
|
578 |
|
579 |
# Read file content
|
580 |
file_content = await file.read()
|
581 |
+
|
582 |
# Extract text based on file type
|
583 |
if content_type == 'application/pdf':
|
584 |
text = extract_text_from_pdf(file_content)
|
|
|
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)
|
593 |
if len(text.strip()) < 50:
|
|
|
604 |
file_content=file_content
|
605 |
)
|
606 |
|
607 |
+
# ✅ Use jsonable_encoder to serialize datetime/ObjectId
|
608 |
+
return JSONResponse(content=jsonable_encoder({
|
609 |
"status": "success",
|
610 |
"analysis": analysis,
|
611 |
"patient_id": patient_id,
|
612 |
"file_type": content_type,
|
613 |
"file_size": len(file_content)
|
614 |
+
}))
|
615 |
|
616 |
except HTTPException:
|
617 |
raise
|