Spaces:
Sleeping
title: AI-Powered Health Risk Profiler
emoji: 🩺
colorFrom: blue
colorTo: green
sdk: docker
app_file: Dockerfile
pinned: false
AI-Powered Health Risk Profiler
A FastAPI-based service that analyzes lifestyle survey responses from JSON or image inputs using OCR, extracts risk factors, classifies health risk levels, and provides actionable recommendations.
Architecture
The application follows a modular architecture with the following components:
- FastAPI App: Main application with CORS middleware and endpoints.
- Schemas: Pydantic models for input validation and response formatting.
- Services: Business logic for OCR parsing, factor extraction, risk classification, and recommendations.
- Simulator: HTML frontend for testing the API.
Workflow
- Input Processing: Accepts JSON survey data or image files.
- OCR/Text Parsing: Extracts key fields (age, smoker, exercise, diet) from images using EasyOCR.
- Factor Extraction: Converts answers into risk factors (smoking, poor diet, low exercise).
- Risk Classification: Calculates risk score and level based on factors.
- Recommendations: Generates personalized health recommendations.
Setup Instructions
Prerequisites
- Python 3.8+
- pip
Installation
Local Development
Clone the repository:
git clone <repository-url> cd health-risk-profilesCreate a virtual environment:
python -m venv venv venv\Scripts\activate # On WindowsInstall dependencies:
pip install -r requirements.txtRun the application:
uvicorn app.main:app --reloadAccess the simulator at
http://127.0.0.1:8000/API documentation at
http://127.0.0.1:8000/docs
Docker Deployment
Build the Docker image:
docker build -t health-risk-profiler .Run the container:
docker run -p 10000:10000 health-risk-profilerAccess the simulator at
http://localhost:10000/API documentation at
http://localhost:10000/docs
API Usage
Endpoint: POST /analyze
Analyzes health survey data and returns risk profile and recommendations.
Request
- Content-Type:
application/jsonfor JSON input ormultipart/form-datafor image input.
JSON Input Example
{
"age": 42,
"smoker": true,
"exercise": "rarely",
"diet": "high sugar"
}
Image Input
Upload an image file with survey text in key-value format (e.g., "Age: 42\nSmoker: yes").
Sample Curl Requests
JSON Input:
curl -X POST "http://127.0.0.1:8000/analyze" \
-H "Content-Type: application/json" \
-d '{"age":42,"smoker":true,"exercise":"rarely","diet":"high sugar"}'
Image Input:
curl -X POST "http://127.0.0.1:8000/analyze" \
-F "file=@survey_image.jpg"
Incomplete Profile (JSON with missing fields):
curl -X POST "http://127.0.0.1:8000/analyze" \
-H "Content-Type: application/json" \
-d '{"age":42,"smoker":true}'
Response
Successful Analysis:
{
"risk_level": "high",
"factors": ["smoking", "poor diet", "low exercise"],
"recommendations": ["Quit smoking", "Reduce sugar", "Walk 30 mins daily"],
"status": "ok",
"confidence": 0.92
}
Incomplete Profile:
{
"status": "incomplete_profile",
"reason": ">50% fields missing. Missing: exercise, diet"
}
Features
- Dual Input Support: JSON and image (OCR) inputs.
- Guardrails: Handles incomplete profiles with >50% missing fields.
- Error Handling: Validates inputs and provides meaningful error messages.
- Modular Design: Separates concerns for easy maintenance and extension.
- CORS Enabled: Supports cross-origin requests for web integration.
Technologies Used
- FastAPI: Web framework for building APIs.
- EasyOCR: OCR library for text extraction from images.
- Pillow: Image processing.
- Pydantic: Data validation and serialization.
- Uvicorn: ASGI server.
Evaluation Notes
- Correctness: API responses adhere to defined schemas.
- OCR Handling: Robust parsing with logging and error handling.
- Guardrails: Incomplete data detection and appropriate responses.
- Code Quality: Organized, documented, and reusable code.
- AI Integration: Uses OCR for input processing and rule-based logic for analysis.
For demo purposes, run locally and use ngrok for public access if needed.