Spaces:
Sleeping
Sleeping
Upload 3 files
Browse files- Dockerfile +24 -0
- app.py +66 -0
- requirements.txt +6 -0
Dockerfile
ADDED
|
@@ -0,0 +1,24 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# Use a lightweight and secure Python base image
|
| 2 |
+
FROM python:3.10-slim
|
| 3 |
+
|
| 4 |
+
# Set the working directory inside the container
|
| 5 |
+
WORKDIR /app
|
| 6 |
+
|
| 7 |
+
# Create a non-root user for better security
|
| 8 |
+
RUN addgroup --system app && adduser --system --group app
|
| 9 |
+
|
| 10 |
+
# Copy the requirements file and install dependencies
|
| 11 |
+
COPY --chown=app:app ./requirements.txt /app/requirements.txt
|
| 12 |
+
RUN pip install --no-cache-dir --upgrade -r /app/requirements.txt
|
| 13 |
+
|
| 14 |
+
# Copy the rest of your application code
|
| 15 |
+
COPY --chown=app:app . /app
|
| 16 |
+
|
| 17 |
+
# Switch to the non-root user
|
| 18 |
+
USER app
|
| 19 |
+
|
| 20 |
+
# Expose the standard port for Hugging Face Spaces
|
| 21 |
+
EXPOSE 7860
|
| 22 |
+
|
| 23 |
+
# Command to run the application using the standard HF port
|
| 24 |
+
CMD ["uvicorn", "app:app", "--host", "0.0.0.0", "--port", "7860"]
|
app.py
ADDED
|
@@ -0,0 +1,66 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# backend/app.py
|
| 2 |
+
|
| 3 |
+
from fastapi import FastAPI
|
| 4 |
+
from fastapi.middleware.cors import CORSMiddleware
|
| 5 |
+
from pydantic import BaseModel
|
| 6 |
+
import joblib
|
| 7 |
+
import pandas as pd
|
| 8 |
+
from huggingface_hub import hf_hub_download
|
| 9 |
+
import os
|
| 10 |
+
|
| 11 |
+
app = FastAPI(title="PulmoProbe AI API")
|
| 12 |
+
|
| 13 |
+
# Add CORS middleware to allow your Vercel app to call the API
|
| 14 |
+
app.add_middleware(
|
| 15 |
+
CORSMiddleware,
|
| 16 |
+
allow_origins=["*"],
|
| 17 |
+
allow_credentials=True,
|
| 18 |
+
allow_methods=["*"],
|
| 19 |
+
allow_headers=["*"],
|
| 20 |
+
)
|
| 21 |
+
|
| 22 |
+
# --- Download and Load Model from Hugging Face Hub ---
|
| 23 |
+
# This points to your model repository
|
| 24 |
+
MODEL_REPO_ID = "costaspinto/PulmoProbe"
|
| 25 |
+
MODEL_FILENAME = "best_model.joblib"
|
| 26 |
+
|
| 27 |
+
print("Downloading model from Hugging Face Hub...")
|
| 28 |
+
model_path = hf_hub_download(repo_id=MODEL_REPO_ID, filename=MODEL_FILENAME)
|
| 29 |
+
model = joblib.load(model_path)
|
| 30 |
+
print("Model loaded successfully.")
|
| 31 |
+
|
| 32 |
+
# --- Define Input Data Model ---
|
| 33 |
+
class PatientData(BaseModel):
|
| 34 |
+
age: float
|
| 35 |
+
gender: str
|
| 36 |
+
country: str
|
| 37 |
+
cancer_stage: str
|
| 38 |
+
family_history: int
|
| 39 |
+
smoking_status: str
|
| 40 |
+
bmi: float
|
| 41 |
+
cholesterol_level: float
|
| 42 |
+
hypertension: int
|
| 43 |
+
asthma: int
|
| 44 |
+
cirrhosis: int
|
| 45 |
+
other_cancer: int
|
| 46 |
+
treatment_type: str
|
| 47 |
+
|
| 48 |
+
# --- Define API Endpoints ---
|
| 49 |
+
@app.get("/")
|
| 50 |
+
def read_root():
|
| 51 |
+
return {"message": "Welcome to the PulmoProbe AI API"}
|
| 52 |
+
|
| 53 |
+
@app.post("/predict")
|
| 54 |
+
def predict(data: PatientData):
|
| 55 |
+
try:
|
| 56 |
+
input_df = pd.DataFrame([data.dict()])
|
| 57 |
+
probabilities = model.predict_proba(input_df)[0]
|
| 58 |
+
confidence_high_risk = probabilities[0]
|
| 59 |
+
risk_level = "High Risk of Non-Survival" if confidence_high_risk > 0.5 else "Low Risk of Non-Survival"
|
| 60 |
+
|
| 61 |
+
return {
|
| 62 |
+
"risk": risk_level,
|
| 63 |
+
"confidence": f"{confidence_high_risk * 100:.1f}"
|
| 64 |
+
}
|
| 65 |
+
except Exception as e:
|
| 66 |
+
return {"error": str(e)}
|
requirements.txt
ADDED
|
@@ -0,0 +1,6 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
fastapi
|
| 2 |
+
uvicorn[standard]
|
| 3 |
+
scikit-learn
|
| 4 |
+
pandas
|
| 5 |
+
joblib
|
| 6 |
+
huggingface_hub
|