Spaces:
Sleeping
Sleeping
phonefern
commited on
Commit
·
8fc0265
1
Parent(s):
2adac0e
'1
Browse files- Dockerfile +33 -0
- app.py +53 -0
- model.pkl +3 -0
- requirements.txt +6 -0
Dockerfile
ADDED
|
@@ -0,0 +1,33 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# Use an official Python runtime as a parent image
|
| 2 |
+
FROM python:3.9-slim
|
| 3 |
+
|
| 4 |
+
# Set environment variables
|
| 5 |
+
ENV PYTHONDONTWRITEBYTECODE=1 \
|
| 6 |
+
PYTHONUNBUFFERED=1
|
| 7 |
+
|
| 8 |
+
# Create a non-root user
|
| 9 |
+
RUN useradd --create-home user
|
| 10 |
+
# Set the working directory to the user's home directory
|
| 11 |
+
WORKDIR /home/user
|
| 12 |
+
|
| 13 |
+
# Copy and install dependencies
|
| 14 |
+
COPY --chown=user:user requirements.txt ./
|
| 15 |
+
RUN pip install --no-cache-dir --upgrade pip && \
|
| 16 |
+
pip install --no-cache-dir -r requirements.txt
|
| 17 |
+
|
| 18 |
+
# Copy the application code
|
| 19 |
+
COPY --chown=user:user app.py ./
|
| 20 |
+
COPY --chown=user:user rf_model.pkl ./
|
| 21 |
+
|
| 22 |
+
# Switch to the non-root user
|
| 23 |
+
USER user
|
| 24 |
+
|
| 25 |
+
# Set environment variables for the application
|
| 26 |
+
ENV HOME=/home/user \
|
| 27 |
+
PATH=/home/user/.local/bin:$PATH
|
| 28 |
+
|
| 29 |
+
# Expose the port uvicorn will run on
|
| 30 |
+
EXPOSE 8501
|
| 31 |
+
|
| 32 |
+
# Define the default command to run the application
|
| 33 |
+
CMD ["uvicorn", "app:app", "--host", "0.0.0.0", "--port", "8501"]
|
app.py
ADDED
|
@@ -0,0 +1,53 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import json
|
| 2 |
+
import numpy as np
|
| 3 |
+
import joblib
|
| 4 |
+
from fastapi import FastAPI, HTTPException
|
| 5 |
+
from pydantic import BaseModel
|
| 6 |
+
import uvicorn
|
| 7 |
+
|
| 8 |
+
# Load model from the local storage (ensure the model file is in the same directory)
|
| 9 |
+
model_path = "rf_model.pkl"
|
| 10 |
+
gb_model_loaded = joblib.load(model_path)
|
| 11 |
+
|
| 12 |
+
# Create FastAPI app
|
| 13 |
+
app = FastAPI()
|
| 14 |
+
|
| 15 |
+
# Define class labels
|
| 16 |
+
class_names = [
|
| 17 |
+
'Emergency & Accident Unit', 'Heart Clinic',
|
| 18 |
+
'Neuro Med Center', 'OPD:EYE', 'Dental',
|
| 19 |
+
'OPD:MED', 'OPD:ENT', 'OPD:OBG',
|
| 20 |
+
'OPD:Surgery + Uro.', 'Orthopedic Surgery',
|
| 21 |
+
'GI Clinic', 'Breast Clinic', 'Skin & Dermatology'
|
| 22 |
+
]
|
| 23 |
+
|
| 24 |
+
# Define the input format for FastAPI using Pydantic BaseModel
|
| 25 |
+
class InputData(BaseModel):
|
| 26 |
+
features: list[float] # List of 32 feature inputs
|
| 27 |
+
|
| 28 |
+
@app.post("/predict")
|
| 29 |
+
def predict(data: InputData):
|
| 30 |
+
try:
|
| 31 |
+
# Validate input length
|
| 32 |
+
if len(data.features) != 32:
|
| 33 |
+
raise HTTPException(status_code=400, detail=f"Expected 32 features, but got {len(data.features)}")
|
| 34 |
+
|
| 35 |
+
# Convert list to numpy array and reshape
|
| 36 |
+
input_array = np.array(data.features).reshape(1, -1)
|
| 37 |
+
|
| 38 |
+
# Get predictions
|
| 39 |
+
prediction = gb_model_loaded.predict_proba(input_array)
|
| 40 |
+
|
| 41 |
+
# Convert probabilities to percentage and format
|
| 42 |
+
probabilities = (prediction[0] * 100).round(2)
|
| 43 |
+
result_pro = {class_name: f"{prob:.2f}%" for class_name, prob in zip(class_names, probabilities)}
|
| 44 |
+
|
| 45 |
+
# Return result as JSON
|
| 46 |
+
return {'result': result_pro}
|
| 47 |
+
|
| 48 |
+
except Exception as e:
|
| 49 |
+
raise HTTPException(status_code=500, detail=str(e))
|
| 50 |
+
|
| 51 |
+
# Run the application with the following command if needed
|
| 52 |
+
# if __name__ == "__main__":
|
| 53 |
+
# uvicorn.run(app, host="0.0.0.0", port=8501)
|
model.pkl
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:eefc4721e5f6d31845e925a604e34ca3e42107762c8d8ea7ffb68e5c5f3fb108
|
| 3 |
+
size 173541345
|
requirements.txt
ADDED
|
@@ -0,0 +1,6 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
fastapi
|
| 2 |
+
uvicorn
|
| 3 |
+
numpy
|
| 4 |
+
joblib
|
| 5 |
+
pydantic
|
| 6 |
+
requests
|