Ali2206 commited on
Commit
0f83542
·
verified ·
1 Parent(s): 9095ee0

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +55 -61
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: 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)
 
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)