Update app.py
Browse files
app.py
CHANGED
@@ -542,61 +542,89 @@ async def voice_chat_endpoint(
|
|
542 |
logger.error(f"Error in voice chat: {e}")
|
543 |
raise HTTPException(status_code=500, detail="Error processing voice chat")
|
544 |
|
545 |
-
@app.post("/
|
546 |
-
async def
|
547 |
-
|
548 |
-
|
|
|
|
|
549 |
):
|
550 |
-
"""
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
551 |
try:
|
552 |
-
# Validate
|
553 |
-
if
|
554 |
-
|
555 |
-
|
556 |
-
|
557 |
-
|
558 |
-
|
559 |
-
|
560 |
-
|
561 |
-
|
562 |
-
|
563 |
-
|
564 |
-
|
565 |
-
|
566 |
-
|
567 |
-
|
568 |
-
|
569 |
-
|
570 |
-
|
571 |
-
|
572 |
-
|
573 |
-
|
574 |
-
|
575 |
-
|
576 |
-
|
577 |
-
|
578 |
-
|
579 |
-
|
580 |
-
|
581 |
-
|
582 |
-
|
583 |
-
|
584 |
-
|
585 |
-
|
586 |
-
|
587 |
-
|
588 |
-
|
589 |
-
|
590 |
-
#
|
591 |
-
|
592 |
-
|
593 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
594 |
except HTTPException:
|
595 |
raise
|
596 |
except Exception as e:
|
597 |
-
logger.error(f"Error
|
598 |
-
raise HTTPException(status_code=500, detail="Error processing
|
599 |
-
|
600 |
if __name__ == "__main__":
|
601 |
import uvicorn
|
602 |
uvicorn.run(app, host="0.0.0.0", port=8000)
|
|
|
542 |
logger.error(f"Error in voice chat: {e}")
|
543 |
raise HTTPException(status_code=500, detail="Error processing voice chat")
|
544 |
|
545 |
+
@app.post("/analyze-report")
|
546 |
+
async def analyze_clinical_report(
|
547 |
+
file: Optional[UploadFile] = File(None),
|
548 |
+
text: 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 either from uploaded file or direct text input.
|
554 |
+
|
555 |
+
Parameters:
|
556 |
+
- file: Uploaded clinical report file (PDF, TXT, DOCX)
|
557 |
+
- text: Direct text input of the clinical 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 input
|
565 |
+
if not (file or text):
|
566 |
+
raise HTTPException(status_code=400, detail="Either file or text input is required")
|
567 |
+
|
568 |
+
# Extract text from file if provided
|
569 |
+
report_text = text
|
570 |
+
if file:
|
571 |
+
if file.filename.lower().endswith('.pdf'):
|
572 |
+
# PDF processing
|
573 |
+
import PyPDF2
|
574 |
+
pdf_reader = PyPDF2.PdfReader(file.file)
|
575 |
+
report_text = "\n".join([page.extract_text() for page in pdf_reader.pages])
|
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 |
+
# Clean and validate the extracted text
|
588 |
+
if not report_text or len(report_text.strip()) < 50:
|
589 |
+
raise HTTPException(status_code=400, detail="Report text is too short or empty")
|
590 |
+
|
591 |
+
# Create analysis prompt
|
592 |
+
prompt = (
|
593 |
+
"You are a senior clinical analyst. Analyze this patient report and provide:\n"
|
594 |
+
"1. SUMMARY: Concise summary of key findings\n"
|
595 |
+
"2. DIAGNOSES: List of confirmed or suspected diagnoses\n"
|
596 |
+
"3. RISK FACTORS: Important risk factors identified\n"
|
597 |
+
"4. RED FLAGS: Any urgent concerns that need attention\n"
|
598 |
+
"5. RECOMMENDATIONS: Suggested next steps for care\n\n"
|
599 |
+
f"PATIENT REPORT:\n{report_text[:15000]}" # Limit input size
|
600 |
+
)
|
601 |
+
|
602 |
+
# Get AI analysis
|
603 |
+
raw_response = agent.chat(
|
604 |
+
message=prompt,
|
605 |
+
history=[],
|
606 |
+
temperature=temperature,
|
607 |
+
max_new_tokens=max_new_tokens
|
608 |
+
)
|
609 |
+
|
610 |
+
# Structure the response
|
611 |
+
structured_response = structure_medical_response(raw_response)
|
612 |
+
|
613 |
+
# Add suicide risk assessment
|
614 |
+
risk_level, risk_score, risk_factors = detect_suicide_risk(raw_response)
|
615 |
+
structured_response["suicide_risk"] = {
|
616 |
+
"level": risk_level.value,
|
617 |
+
"score": risk_score,
|
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 analyzing clinical report: {e}")
|
627 |
+
raise HTTPException(status_code=500, detail="Error processing clinical report")
|
|
|
628 |
if __name__ == "__main__":
|
629 |
import uvicorn
|
630 |
uvicorn.run(app, host="0.0.0.0", port=8000)
|