Spaces:
				
			
			
	
			
			
		Sleeping
		
	
	
	
			
			
	
	
	
	
		
		
		Sleeping
		
	Upload 5 files
Browse files- Dockerfile +25 -0
- app/__init__.py +0 -0
- app/main.py +97 -0
- models/diabetes_model.pkl +3 -0
- requirements.txt +23 -0
    	
        Dockerfile
    ADDED
    
    | @@ -0,0 +1,25 @@ | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | 
|  | |
| 1 | 
            +
            # Use Python 3.13.5 slim image
         | 
| 2 | 
            +
            # The slim image keeps our container small
         | 
| 3 | 
            +
            FROM python:3.13.5-slim
         | 
| 4 | 
            +
             | 
| 5 | 
            +
            # Set working directory
         | 
| 6 | 
            +
            WORKDIR /app
         | 
| 7 | 
            +
             | 
| 8 | 
            +
            # Install system dependencies (if needed)
         | 
| 9 | 
            +
            RUN apt-get update && apt-get install -y \
         | 
| 10 | 
            +
                && rm -rf /var/lib/apt/lists/*
         | 
| 11 | 
            +
             | 
| 12 | 
            +
            # Copy requirements and install Python dependencies
         | 
| 13 | 
            +
            # `--no-cache-dir` prevents pip from storing cached packages, further reducing size
         | 
| 14 | 
            +
            COPY requirements.txt .
         | 
| 15 | 
            +
            RUN pip install --no-cache-dir -r requirements.txt
         | 
| 16 | 
            +
             | 
| 17 | 
            +
            # Copy application code
         | 
| 18 | 
            +
            COPY app/ ./app/
         | 
| 19 | 
            +
            COPY models/ ./models/
         | 
| 20 | 
            +
             | 
| 21 | 
            +
            # Expose port
         | 
| 22 | 
            +
            EXPOSE 8000
         | 
| 23 | 
            +
             | 
| 24 | 
            +
            # Run the application
         | 
| 25 | 
            +
            CMD [ "uvicorn", "app.main:app", "--host", "0.0.0.0", "--port", "8000" ]
         | 
    	
        app/__init__.py
    ADDED
    
    | 
            File without changes
         | 
    	
        app/main.py
    ADDED
    
    | @@ -0,0 +1,97 @@ | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | 
|  | |
| 1 | 
            +
            from fastapi import FastAPI
         | 
| 2 | 
            +
            from pydantic import BaseModel
         | 
| 3 | 
            +
            import pickle
         | 
| 4 | 
            +
            import numpy as np
         | 
| 5 | 
            +
            import os
         | 
| 6 | 
            +
             | 
| 7 | 
            +
            # ---DEFINE INPUT DATA SCHEMA---
         | 
| 8 | 
            +
            # FastAPI uses Pydantic for request validation. 
         | 
| 9 | 
            +
            # Meaning it automatically validates incoming data and provides clear error messages if something’s wrong.
         | 
| 10 | 
            +
            class PatientData(BaseModel): 
         | 
| 11 | 
            +
                age: float
         | 
| 12 | 
            +
                sex: float
         | 
| 13 | 
            +
                bmi: float
         | 
| 14 | 
            +
                bp: float  # blood pressure
         | 
| 15 | 
            +
                s1: float  # serum measurement 1
         | 
| 16 | 
            +
                s2: float  # serum measurement 2
         | 
| 17 | 
            +
                s3: float  # serum measurement 3
         | 
| 18 | 
            +
                s4: float  # serum measurement 4
         | 
| 19 | 
            +
                s5: float  # serum measurement 5
         | 
| 20 | 
            +
                s6: float  # serum measurement 6 
         | 
| 21 | 
            +
             | 
| 22 | 
            +
            # The example values help API users understand the expected input format. 
         | 
| 23 | 
            +
            # Note that the diabetes dataset features are already normalized.
         | 
| 24 | 
            +
                class Config: 
         | 
| 25 | 
            +
                    schema_extra = {
         | 
| 26 | 
            +
                        "example": {
         | 
| 27 | 
            +
                            "age": 0.05,
         | 
| 28 | 
            +
                            "sex": 0.05,
         | 
| 29 | 
            +
                            "bmi": 0.06,
         | 
| 30 | 
            +
                            "bp": 0.02,
         | 
| 31 | 
            +
                            "s1": -0.04,
         | 
| 32 | 
            +
                            "s2": -0.04,
         | 
| 33 | 
            +
                            "s3": -0.02,
         | 
| 34 | 
            +
                            "s4": -0.01,
         | 
| 35 | 
            +
                            "s5": 0.01,
         | 
| 36 | 
            +
                            "s6": 0.02                
         | 
| 37 | 
            +
                        }
         | 
| 38 | 
            +
                    }
         | 
| 39 | 
            +
             | 
| 40 | 
            +
            # ---INITIALIZE FASTAPI APP---
         | 
| 41 | 
            +
            app = FastAPI(
         | 
| 42 | 
            +
                title="Diabetes Progression Predictor",
         | 
| 43 | 
            +
                description="Predicts diabetes progression score from physiological features",
         | 
| 44 | 
            +
                version="1.0.0"
         | 
| 45 | 
            +
            )
         | 
| 46 | 
            +
             | 
| 47 | 
            +
            # ---LOAD THE TRAINED MODEL---
         | 
| 48 | 
            +
            model_path = os.path.join("models", "diabetes_model.pkl")
         | 
| 49 | 
            +
            with open(model_path, "rb") as f: 
         | 
| 50 | 
            +
                model = pickle.load(f)
         | 
| 51 | 
            +
             | 
| 52 | 
            +
            # ---CREATE ENDPOINT---
         | 
| 53 | 
            +
            @app.post("/predict")
         | 
| 54 | 
            +
            def predict_progression(patient: PatientData): 
         | 
| 55 | 
            +
                """
         | 
| 56 | 
            +
                Predict diabetes progression score. 
         | 
| 57 | 
            +
                """
         | 
| 58 | 
            +
                # Convert input to numpy array
         | 
| 59 | 
            +
                features = np.array([[
         | 
| 60 | 
            +
                    patient.age, 
         | 
| 61 | 
            +
                    patient.sex, 
         | 
| 62 | 
            +
                    patient.bmi, 
         | 
| 63 | 
            +
                    patient.bp,
         | 
| 64 | 
            +
                    patient.s1,
         | 
| 65 | 
            +
                    patient.s2,
         | 
| 66 | 
            +
                    patient.s3,
         | 
| 67 | 
            +
                    patient.s4,
         | 
| 68 | 
            +
                    patient.s5,
         | 
| 69 | 
            +
                    patient.s6,
         | 
| 70 | 
            +
                ]])
         | 
| 71 | 
            +
             | 
| 72 | 
            +
                # Make prediction
         | 
| 73 | 
            +
                prediction = model.predict(features)[0]
         | 
| 74 | 
            +
             | 
| 75 | 
            +
                # Return result with additional context
         | 
| 76 | 
            +
                return {
         | 
| 77 | 
            +
                    "predicted_progression_score": round(prediction, 2),
         | 
| 78 | 
            +
                    "interpretation": get_interpretation(prediction)
         | 
| 79 | 
            +
                }
         | 
| 80 | 
            +
             | 
| 81 | 
            +
            def get_interpretation(score): 
         | 
| 82 | 
            +
                """
         | 
| 83 | 
            +
                Provide human-readdable interpretation of the score.
         | 
| 84 | 
            +
                """
         | 
| 85 | 
            +
                if score < 100: 
         | 
| 86 | 
            +
                    return "Below average progression"
         | 
| 87 | 
            +
                elif score < 150: 
         | 
| 88 | 
            +
                    return "Average progression"
         | 
| 89 | 
            +
                else: 
         | 
| 90 | 
            +
                    return "Above average progression"
         | 
| 91 | 
            +
                
         | 
| 92 | 
            +
            @app.get("/")
         | 
| 93 | 
            +
            def health_check(): 
         | 
| 94 | 
            +
                return {
         | 
| 95 | 
            +
                    "status": "healthy",
         | 
| 96 | 
            +
                    "model": "diabetes_progression_v1"
         | 
| 97 | 
            +
                }
         | 
    	
        models/diabetes_model.pkl
    ADDED
    
    | @@ -0,0 +1,3 @@ | |
|  | |
|  | |
|  | 
|  | |
| 1 | 
            +
            version https://git-lfs.github.com/spec/v1
         | 
| 2 | 
            +
            oid sha256:3866b799fa4ca1901363061898e8902f6d21262aff5f221e9ff11701cc705933
         | 
| 3 | 
            +
            size 2312740
         | 
    	
        requirements.txt
    ADDED
    
    | @@ -0,0 +1,23 @@ | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | 
|  | |
| 1 | 
            +
            annotated-types==0.7.0
         | 
| 2 | 
            +
            anyio==4.9.0
         | 
| 3 | 
            +
            click==8.2.1
         | 
| 4 | 
            +
            fastapi==0.115.12
         | 
| 5 | 
            +
            h11==0.16.0
         | 
| 6 | 
            +
            idna==3.10
         | 
| 7 | 
            +
            joblib==1.5.1
         | 
| 8 | 
            +
            numpy==2.3.0
         | 
| 9 | 
            +
            pandas==2.3.0
         | 
| 10 | 
            +
            pydantic==2.11.7
         | 
| 11 | 
            +
            pydantic_core==2.33.2
         | 
| 12 | 
            +
            python-dateutil==2.9.0.post0
         | 
| 13 | 
            +
            pytz==2025.2
         | 
| 14 | 
            +
            scikit-learn==1.7.0
         | 
| 15 | 
            +
            scipy==1.15.3
         | 
| 16 | 
            +
            six==1.17.0
         | 
| 17 | 
            +
            sniffio==1.3.1
         | 
| 18 | 
            +
            starlette==0.46.2
         | 
| 19 | 
            +
            threadpoolctl==3.6.0
         | 
| 20 | 
            +
            typing-inspection==0.4.1
         | 
| 21 | 
            +
            typing_extensions==4.14.0
         | 
| 22 | 
            +
            tzdata==2025.2
         | 
| 23 | 
            +
            uvicorn==0.34.3
         | 
