Spaces:
Sleeping
Sleeping
fixe
Browse files- .dockerignore +0 -0
- Dockerfile +20 -0
- app.py +79 -0
- gradient_boosting_model.pkl +3 -0
- requirements.txt +6 -0
.dockerignore
ADDED
File without changes
|
Dockerfile
ADDED
@@ -0,0 +1,20 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
# Use an official Python image
|
2 |
+
FROM python:3.9-slim
|
3 |
+
|
4 |
+
# Set the working directory in the container
|
5 |
+
WORKDIR /app
|
6 |
+
|
7 |
+
# Copy requirements.txt into the container
|
8 |
+
COPY requirements.txt .
|
9 |
+
|
10 |
+
# Install dependencies
|
11 |
+
RUN pip install --no-cache-dir -r requirements.txt
|
12 |
+
|
13 |
+
# Copy the entire application into the container
|
14 |
+
COPY . .
|
15 |
+
|
16 |
+
# Expose the port the app runs on
|
17 |
+
EXPOSE 7860
|
18 |
+
|
19 |
+
# Run the FastAPI application with Uvicorn
|
20 |
+
CMD ["uvicorn", "app:app", "--host", "0.0.0.0", "--port", "7860", "--reload"]
|
app.py
ADDED
@@ -0,0 +1,79 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from fastapi import FastAPI
|
2 |
+
from pydantic import BaseModel
|
3 |
+
import joblib
|
4 |
+
import numpy as np
|
5 |
+
|
6 |
+
# Initialize FastAPI app
|
7 |
+
app = FastAPI()
|
8 |
+
|
9 |
+
# Load the trained Gradient Boosting model
|
10 |
+
model = joblib.load('gradient_boosting_model.pkl')
|
11 |
+
|
12 |
+
# Define the input data schema
|
13 |
+
class PredictionInput(BaseModel):
|
14 |
+
age: int
|
15 |
+
job: str
|
16 |
+
marital: str
|
17 |
+
education: str
|
18 |
+
default: str
|
19 |
+
balance: float
|
20 |
+
housing: str
|
21 |
+
loan: str
|
22 |
+
contact: str
|
23 |
+
day: int
|
24 |
+
month: str
|
25 |
+
duration: float
|
26 |
+
campaign: int
|
27 |
+
pdays: int
|
28 |
+
previous: int
|
29 |
+
poutcome: str
|
30 |
+
|
31 |
+
# Define the mapping for categorical variables (if encoded)
|
32 |
+
categorical_mapping = {
|
33 |
+
"job": ['admin.', 'technician', 'blue-collar', 'management', 'retired', 'services', 'self-employed', 'entrepreneur', 'unemployed', 'housemaid', 'student', 'unknown'],
|
34 |
+
"marital": ['married', 'single', 'divorced'],
|
35 |
+
"education": ['secondary', 'tertiary', 'primary', 'unknown'],
|
36 |
+
"default": ['no', 'yes'],
|
37 |
+
"housing": ['no', 'yes'],
|
38 |
+
"loan": ['no', 'yes'],
|
39 |
+
"contact": ['unknown', 'telephone', 'cellular'],
|
40 |
+
"month": ['jan', 'feb', 'mar', 'apr', 'may', 'jun', 'jul', 'aug', 'sep', 'oct', 'nov', 'dec'],
|
41 |
+
"poutcome": ['unknown', 'other', 'failure', 'success']
|
42 |
+
}
|
43 |
+
|
44 |
+
# Utility function to preprocess the input data
|
45 |
+
def preprocess_input(data: PredictionInput):
|
46 |
+
# Convert input data to a feature array
|
47 |
+
features = [
|
48 |
+
data.age,
|
49 |
+
categorical_mapping['job'].index(data.job),
|
50 |
+
categorical_mapping['marital'].index(data.marital),
|
51 |
+
categorical_mapping['education'].index(data.education),
|
52 |
+
categorical_mapping['default'].index(data.default),
|
53 |
+
data.balance,
|
54 |
+
categorical_mapping['housing'].index(data.housing),
|
55 |
+
categorical_mapping['loan'].index(data.loan),
|
56 |
+
categorical_mapping['contact'].index(data.contact),
|
57 |
+
data.day,
|
58 |
+
categorical_mapping['month'].index(data.month),
|
59 |
+
data.duration,
|
60 |
+
data.campaign,
|
61 |
+
data.pdays,
|
62 |
+
data.previous,
|
63 |
+
categorical_mapping['poutcome'].index(data.poutcome),
|
64 |
+
]
|
65 |
+
return np.array([features])
|
66 |
+
|
67 |
+
# Define a POST endpoint for predictions
|
68 |
+
@app.post("/predict")
|
69 |
+
async def predict(data: PredictionInput):
|
70 |
+
# Preprocess the input
|
71 |
+
input_data = preprocess_input(data)
|
72 |
+
|
73 |
+
# Make a prediction
|
74 |
+
prediction = model.predict(input_data)
|
75 |
+
|
76 |
+
# Convert prediction to "yes"/"no"
|
77 |
+
response = "yes" if prediction[0] == 1 else "no"
|
78 |
+
|
79 |
+
return {"prediction": response}
|
gradient_boosting_model.pkl
ADDED
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
1 |
+
version https://git-lfs.github.com/spec/v1
|
2 |
+
oid sha256:b39cd8c31dfbb53a3936302ace105f40b5f5462cc78fe7007ae7eaf07b1c9611
|
3 |
+
size 1287324
|
requirements.txt
ADDED
@@ -0,0 +1,6 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
fastapi==0.95.2
|
2 |
+
uvicorn==0.22.0
|
3 |
+
scikit-learn==1.3.0
|
4 |
+
joblib==1.3.2
|
5 |
+
numpy==1.24.3
|
6 |
+
pandas==2.0.3
|