Spaces:
Sleeping
Sleeping
Upload 7 files
Browse files- .dockerignore +4 -4
- Dockerfile +17 -15
- README.md +69 -69
- app.py +83 -85
- requirements.txt +4 -6
.dockerignore
CHANGED
|
@@ -1,4 +1,4 @@
|
|
| 1 |
-
|
| 2 |
-
|
| 3 |
-
|
| 4 |
-
|
|
|
|
| 1 |
+
.git
|
| 2 |
+
.gitattributes
|
| 3 |
+
README.md
|
| 4 |
+
model-card.md
|
Dockerfile
CHANGED
|
@@ -1,16 +1,18 @@
|
|
| 1 |
-
FROM python:3.10-slim
|
| 2 |
-
|
| 3 |
-
WORKDIR /app
|
| 4 |
-
|
| 5 |
-
# Create a non-root user
|
| 6 |
-
RUN useradd -m -u 1000 user
|
| 7 |
-
USER user
|
| 8 |
-
ENV HOME=/home/user \
|
| 9 |
-
PATH=/home/user/.local/bin:$PATH
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
RUN pip install --no-cache-dir --user
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
|
|
|
|
|
|
| 16 |
CMD ["uvicorn", "app:app", "--host", "0.0.0.0", "--port", "7860"]
|
|
|
|
| 1 |
+
FROM python:3.10-slim
|
| 2 |
+
|
| 3 |
+
WORKDIR /app
|
| 4 |
+
|
| 5 |
+
# Create a non-root user
|
| 6 |
+
RUN useradd -m -u 1000 user
|
| 7 |
+
USER user
|
| 8 |
+
ENV HOME=/home/user \
|
| 9 |
+
PATH=/home/user/.local/bin:$PATH
|
| 10 |
+
|
| 11 |
+
# Install required packages directly
|
| 12 |
+
RUN pip install --no-cache-dir --user fastapi==0.95.2 uvicorn==0.23.2 numpy==1.24.3 joblib==1.3.2
|
| 13 |
+
|
| 14 |
+
# Copy application files
|
| 15 |
+
COPY --chown=user:user . .
|
| 16 |
+
|
| 17 |
+
# Run the application
|
| 18 |
CMD ["uvicorn", "app:app", "--host", "0.0.0.0", "--port", "7860"]
|
README.md
CHANGED
|
@@ -1,69 +1,69 @@
|
|
| 1 |
-
---
|
| 2 |
-
title: Stroke Prediction Model
|
| 3 |
-
emoji: 🧠
|
| 4 |
-
colorFrom: red
|
| 5 |
-
colorTo: blue
|
| 6 |
-
sdk: docker
|
| 7 |
-
app_file: app.py
|
| 8 |
-
pinned: false
|
| 9 |
-
---
|
| 10 |
-
|
| 11 |
-
# Stroke Prediction Model
|
| 12 |
-
|
| 13 |
-
This model predicts the risk of stroke based on demographic and health-related features.
|
| 14 |
-
|
| 15 |
-
## Model Details
|
| 16 |
-
|
| 17 |
-
- **Model Type**: Random Forest Classifier
|
| 18 |
-
- **Training Data**: Healthcare data including age, gender, various diseases, and lifestyle factors
|
| 19 |
-
- **Features**: Age, gender, hypertension, heart disease, marital status, work type, residence type, glucose level, BMI, smoking status
|
| 20 |
-
- **Output**: Probability of stroke risk (0-1) and risk category
|
| 21 |
-
|
| 22 |
-
## Usage
|
| 23 |
-
|
| 24 |
-
You can use this model through the Hugging Face Inference API:
|
| 25 |
-
|
| 26 |
-
```python
|
| 27 |
-
import requests
|
| 28 |
-
|
| 29 |
-
API_URL = "https://abdullah1211-ml-stroke.hf.space"
|
| 30 |
-
headers = {"Content-Type": "application/json"}
|
| 31 |
-
|
| 32 |
-
def query(payload):
|
| 33 |
-
response = requests.post(API_URL, headers=headers, json=payload)
|
| 34 |
-
return response.json()
|
| 35 |
-
|
| 36 |
-
data = {
|
| 37 |
-
"gender": "Male",
|
| 38 |
-
"age": 67,
|
| 39 |
-
"hypertension": 1,
|
| 40 |
-
"heart_disease": 0,
|
| 41 |
-
"ever_married": "Yes",
|
| 42 |
-
"work_type": "Private",
|
| 43 |
-
"Residence_type": "Urban",
|
| 44 |
-
"avg_glucose_level": 228.69,
|
| 45 |
-
"bmi": 36.6,
|
| 46 |
-
"smoking_status": "formerly smoked"
|
| 47 |
-
}
|
| 48 |
-
|
| 49 |
-
output = query(data)
|
| 50 |
-
print(output)
|
| 51 |
-
```
|
| 52 |
-
|
| 53 |
-
## Response Format
|
| 54 |
-
|
| 55 |
-
```json
|
| 56 |
-
{
|
| 57 |
-
"probability": 0.72,
|
| 58 |
-
"prediction": "High Risk",
|
| 59 |
-
"stroke_prediction": 1
|
| 60 |
-
}
|
| 61 |
-
```
|
| 62 |
-
|
| 63 |
-
## Risk Categories
|
| 64 |
-
|
| 65 |
-
- Very Low Risk: probability < 0.2
|
| 66 |
-
- Low Risk: probability between 0.2 and 0.4
|
| 67 |
-
- Moderate Risk: probability between 0.4 and 0.6
|
| 68 |
-
- High Risk: probability between 0.6 and 0.8
|
| 69 |
-
- Very High Risk: probability > 0.8
|
|
|
|
| 1 |
+
---
|
| 2 |
+
title: Stroke Prediction Model
|
| 3 |
+
emoji: 🧠
|
| 4 |
+
colorFrom: red
|
| 5 |
+
colorTo: blue
|
| 6 |
+
sdk: docker
|
| 7 |
+
app_file: app.py
|
| 8 |
+
pinned: false
|
| 9 |
+
---
|
| 10 |
+
|
| 11 |
+
# Stroke Prediction Model
|
| 12 |
+
|
| 13 |
+
This model predicts the risk of stroke based on demographic and health-related features.
|
| 14 |
+
|
| 15 |
+
## Model Details
|
| 16 |
+
|
| 17 |
+
- **Model Type**: Random Forest Classifier
|
| 18 |
+
- **Training Data**: Healthcare data including age, gender, various diseases, and lifestyle factors
|
| 19 |
+
- **Features**: Age, gender, hypertension, heart disease, marital status, work type, residence type, glucose level, BMI, smoking status
|
| 20 |
+
- **Output**: Probability of stroke risk (0-1) and risk category
|
| 21 |
+
|
| 22 |
+
## Usage
|
| 23 |
+
|
| 24 |
+
You can use this model through the Hugging Face Inference API:
|
| 25 |
+
|
| 26 |
+
```python
|
| 27 |
+
import requests
|
| 28 |
+
|
| 29 |
+
API_URL = "https://abdullah1211-ml-stroke.hf.space"
|
| 30 |
+
headers = {"Content-Type": "application/json"}
|
| 31 |
+
|
| 32 |
+
def query(payload):
|
| 33 |
+
response = requests.post(API_URL, headers=headers, json=payload)
|
| 34 |
+
return response.json()
|
| 35 |
+
|
| 36 |
+
data = {
|
| 37 |
+
"gender": "Male",
|
| 38 |
+
"age": 67,
|
| 39 |
+
"hypertension": 1,
|
| 40 |
+
"heart_disease": 0,
|
| 41 |
+
"ever_married": "Yes",
|
| 42 |
+
"work_type": "Private",
|
| 43 |
+
"Residence_type": "Urban",
|
| 44 |
+
"avg_glucose_level": 228.69,
|
| 45 |
+
"bmi": 36.6,
|
| 46 |
+
"smoking_status": "formerly smoked"
|
| 47 |
+
}
|
| 48 |
+
|
| 49 |
+
output = query(data)
|
| 50 |
+
print(output)
|
| 51 |
+
```
|
| 52 |
+
|
| 53 |
+
## Response Format
|
| 54 |
+
|
| 55 |
+
```json
|
| 56 |
+
{
|
| 57 |
+
"probability": 0.72,
|
| 58 |
+
"prediction": "High Risk",
|
| 59 |
+
"stroke_prediction": 1
|
| 60 |
+
}
|
| 61 |
+
```
|
| 62 |
+
|
| 63 |
+
## Risk Categories
|
| 64 |
+
|
| 65 |
+
- Very Low Risk: probability < 0.2
|
| 66 |
+
- Low Risk: probability between 0.2 and 0.4
|
| 67 |
+
- Moderate Risk: probability between 0.4 and 0.6
|
| 68 |
+
- High Risk: probability between 0.6 and 0.8
|
| 69 |
+
- Very High Risk: probability > 0.8
|
app.py
CHANGED
|
@@ -1,85 +1,83 @@
|
|
| 1 |
-
from fastapi import FastAPI, Request
|
| 2 |
-
import
|
| 3 |
-
import joblib
|
| 4 |
-
|
| 5 |
-
|
| 6 |
-
|
| 7 |
-
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
|
| 62 |
-
|
| 63 |
-
|
| 64 |
-
|
| 65 |
-
|
| 66 |
-
|
| 67 |
-
|
| 68 |
-
|
| 69 |
-
|
| 70 |
-
|
| 71 |
-
|
| 72 |
-
|
| 73 |
-
|
| 74 |
-
|
| 75 |
-
|
| 76 |
-
|
| 77 |
-
|
| 78 |
-
|
| 79 |
-
|
| 80 |
-
|
| 81 |
-
|
| 82 |
-
|
| 83 |
-
"
|
| 84 |
-
"stroke_prediction": int(prediction_proba > 0.5)
|
| 85 |
-
}
|
|
|
|
| 1 |
+
from fastapi import FastAPI, Request, HTTPException
|
| 2 |
+
import numpy as np
|
| 3 |
+
import joblib
|
| 4 |
+
|
| 5 |
+
app = FastAPI()
|
| 6 |
+
|
| 7 |
+
# Simple risk level function
|
| 8 |
+
def get_risk_level(probability):
|
| 9 |
+
if probability < 0.2:
|
| 10 |
+
return "Very Low Risk"
|
| 11 |
+
elif probability < 0.4:
|
| 12 |
+
return "Low Risk"
|
| 13 |
+
elif probability < 0.6:
|
| 14 |
+
return "Moderate Risk"
|
| 15 |
+
elif probability < 0.8:
|
| 16 |
+
return "High Risk"
|
| 17 |
+
else:
|
| 18 |
+
return "Very High Risk"
|
| 19 |
+
|
| 20 |
+
# Fallback prediction
|
| 21 |
+
def predict_risk(data):
|
| 22 |
+
# Count risk factors
|
| 23 |
+
risk_factors = 0
|
| 24 |
+
|
| 25 |
+
if data.get('hypertension', 0) == 1:
|
| 26 |
+
risk_factors += 1
|
| 27 |
+
if data.get('heart_disease', 0) == 1:
|
| 28 |
+
risk_factors += 1
|
| 29 |
+
if data.get('age', 0) > 65:
|
| 30 |
+
risk_factors += 1
|
| 31 |
+
if data.get('smoking_status', '') == 'smokes':
|
| 32 |
+
risk_factors += 1
|
| 33 |
+
if data.get('avg_glucose_level', 0) > 140:
|
| 34 |
+
risk_factors += 1
|
| 35 |
+
if data.get('bmi', 0) > 30:
|
| 36 |
+
risk_factors += 1
|
| 37 |
+
|
| 38 |
+
# Simple logic based on risk factor count
|
| 39 |
+
if risk_factors == 0:
|
| 40 |
+
probability = 0.05
|
| 41 |
+
elif risk_factors == 1:
|
| 42 |
+
probability = 0.15
|
| 43 |
+
elif risk_factors == 2:
|
| 44 |
+
probability = 0.30
|
| 45 |
+
elif risk_factors == 3:
|
| 46 |
+
probability = 0.60
|
| 47 |
+
else:
|
| 48 |
+
probability = 0.80
|
| 49 |
+
|
| 50 |
+
return probability, get_risk_level(probability)
|
| 51 |
+
|
| 52 |
+
@app.get("/")
|
| 53 |
+
async def root():
|
| 54 |
+
return {
|
| 55 |
+
"message": "Stroke Prediction API is running",
|
| 56 |
+
"usage": "Send a POST request to / with patient data",
|
| 57 |
+
"example": {
|
| 58 |
+
"gender": "Male",
|
| 59 |
+
"age": 67,
|
| 60 |
+
"hypertension": 1,
|
| 61 |
+
"heart_disease": 0,
|
| 62 |
+
"avg_glucose_level": 228.69,
|
| 63 |
+
"bmi": 36.6,
|
| 64 |
+
"smoking_status": "formerly smoked"
|
| 65 |
+
}
|
| 66 |
+
}
|
| 67 |
+
|
| 68 |
+
@app.post("/")
|
| 69 |
+
async def predict(request: Request):
|
| 70 |
+
try:
|
| 71 |
+
data = await request.json()
|
| 72 |
+
|
| 73 |
+
# Use fallback prediction
|
| 74 |
+
probability, risk_level = predict_risk(data)
|
| 75 |
+
|
| 76 |
+
return {
|
| 77 |
+
"probability": float(probability),
|
| 78 |
+
"prediction": risk_level,
|
| 79 |
+
"stroke_prediction": int(probability > 0.5)
|
| 80 |
+
}
|
| 81 |
+
|
| 82 |
+
except Exception as e:
|
| 83 |
+
raise HTTPException(status_code=400, detail=f"Invalid input: {str(e)}")
|
|
|
|
|
|
requirements.txt
CHANGED
|
@@ -1,6 +1,4 @@
|
|
| 1 |
-
|
| 2 |
-
|
| 3 |
-
|
| 4 |
-
|
| 5 |
-
pydantic>=2.0.0
|
| 6 |
-
uvicorn>=0.23.0
|
|
|
|
| 1 |
+
fastapi==0.95.2
|
| 2 |
+
uvicorn==0.23.2
|
| 3 |
+
numpy==1.24.3
|
| 4 |
+
joblib==1.3.2
|
|
|
|
|
|