ArxAlfa commited on
Commit ·
a650593
1
Parent(s): 9d63bd4
Add model.joblib, update Dockerfile and
Browse files- Dockerfile +1 -1
- app.py +64 -5
- dataset/housing_price_dataset.csv +0 -0
- model.joblib +0 -0
- requirements.txt +30 -6
Dockerfile
CHANGED
|
@@ -1,5 +1,5 @@
|
|
| 1 |
# Use the official Python 3.9 image
|
| 2 |
-
FROM python:3.9
|
| 3 |
|
| 4 |
# Set the working directory to /code
|
| 5 |
WORKDIR /code
|
|
|
|
| 1 |
# Use the official Python 3.9 image
|
| 2 |
+
FROM python:3.9-slim
|
| 3 |
|
| 4 |
# Set the working directory to /code
|
| 5 |
WORKDIR /code
|
app.py
CHANGED
|
@@ -1,11 +1,70 @@
|
|
| 1 |
-
from fastapi import FastAPI
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 2 |
|
| 3 |
# Create a new FastAPI app instance
|
| 4 |
app = FastAPI(docs_url="/", redoc_url="/new_redoc")
|
| 5 |
|
| 6 |
|
| 7 |
# Create a POST endpoint
|
| 8 |
-
@app.get("/generate")
|
| 9 |
-
def generate(
|
| 10 |
-
|
| 11 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from fastapi import FastAPI, UploadFile, File
|
| 2 |
+
import numpy as np
|
| 3 |
+
from sklearn.neural_network import MLPRegressor
|
| 4 |
+
from sklearn.model_selection import KFold
|
| 5 |
+
from sklearn.metrics import mean_squared_error
|
| 6 |
+
import csv
|
| 7 |
+
import io
|
| 8 |
+
|
| 9 |
+
from joblib import load, dump
|
| 10 |
+
|
| 11 |
+
# Load the model
|
| 12 |
+
model = load("model.joblib")
|
| 13 |
|
| 14 |
# Create a new FastAPI app instance
|
| 15 |
app = FastAPI(docs_url="/", redoc_url="/new_redoc")
|
| 16 |
|
| 17 |
|
| 18 |
# Create a POST endpoint
|
| 19 |
+
@app.get("/generate/{squareFeet}/{bedrooms}/{bathrooms}/{yearBuilt}")
|
| 20 |
+
def generate(
|
| 21 |
+
squareFeet: float,
|
| 22 |
+
bedrooms: float,
|
| 23 |
+
bathrooms: float,
|
| 24 |
+
yearBuilt: float,
|
| 25 |
+
):
|
| 26 |
+
global model
|
| 27 |
+
prediction = model.predict([[squareFeet, bedrooms, bathrooms, yearBuilt]])
|
| 28 |
+
return {"output": prediction[0]}
|
| 29 |
+
|
| 30 |
+
|
| 31 |
+
@app.post("/train")
|
| 32 |
+
async def train(file: UploadFile = File(...)):
|
| 33 |
+
global model
|
| 34 |
+
|
| 35 |
+
contents = await file.read()
|
| 36 |
+
data = list(csv.reader(io.StringIO(contents.decode("utf-8"))))
|
| 37 |
+
|
| 38 |
+
data_np = np.array(data[1:], dtype=object)
|
| 39 |
+
|
| 40 |
+
# Delete the fourth column
|
| 41 |
+
data_np = np.delete(data_np, 3, axis=1)
|
| 42 |
+
data_np = np.array(data_np, dtype=float)
|
| 43 |
+
|
| 44 |
+
# All columns except the last
|
| 45 |
+
X = data_np[:, :-1]
|
| 46 |
+
|
| 47 |
+
# Only the last column
|
| 48 |
+
y = data_np[:, -1]
|
| 49 |
+
y = np.ravel(y)
|
| 50 |
+
|
| 51 |
+
# Fit the model
|
| 52 |
+
kf = KFold(n_splits=4)
|
| 53 |
+
accuracies = []
|
| 54 |
+
|
| 55 |
+
for train_index, test_index in kf.split(X):
|
| 56 |
+
X_train, X_test = X[train_index], X[test_index]
|
| 57 |
+
y_train, y_test = y[train_index], y[test_index]
|
| 58 |
+
|
| 59 |
+
model.fit(X_train, y_train)
|
| 60 |
+
|
| 61 |
+
predictions = model.predict(X_test)
|
| 62 |
+
rmse = np.sqrt(mean_squared_error(y_test, predictions))
|
| 63 |
+
accuracies.append(rmse)
|
| 64 |
+
|
| 65 |
+
average_rmse = sum(accuracies) / len(accuracies)
|
| 66 |
+
print(f"Average RMSE: {average_rmse}")
|
| 67 |
+
|
| 68 |
+
dump(model, "model.joblib")
|
| 69 |
+
|
| 70 |
+
return {"filename": file.filename, "average_rmse": average_rmse}
|
dataset/housing_price_dataset.csv
ADDED
|
The diff for this file is too large to render.
See raw diff
|
|
|
model.joblib
ADDED
|
Binary file (8.19 kB). View file
|
|
|
requirements.txt
CHANGED
|
@@ -1,6 +1,30 @@
|
|
| 1 |
-
|
| 2 |
-
|
| 3 |
-
|
| 4 |
-
|
| 5 |
-
|
| 6 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# requirements.txt
|
| 2 |
+
|
| 3 |
+
anyio==3.7.1
|
| 4 |
+
asgiref==3.7.2
|
| 5 |
+
certifi==2023.11.17
|
| 6 |
+
charset-normalizer==2.0.12
|
| 7 |
+
click==8.1.7
|
| 8 |
+
fastapi==0.74.1
|
| 9 |
+
h11==0.14.0
|
| 10 |
+
httptools==0.6.1
|
| 11 |
+
idna==3.6
|
| 12 |
+
joblib==1.3.2
|
| 13 |
+
numpy==1.26.2
|
| 14 |
+
pydantic==1.10.13
|
| 15 |
+
python-dotenv==1.0.0
|
| 16 |
+
python-multipart==0.0.6
|
| 17 |
+
PyYAML==6.0.1
|
| 18 |
+
requests==2.27.1
|
| 19 |
+
scikit-learn==1.3.2
|
| 20 |
+
scipy==1.11.4
|
| 21 |
+
sentencepiece==0.1.99
|
| 22 |
+
sniffio==1.3.0
|
| 23 |
+
starlette==0.17.1
|
| 24 |
+
threadpoolctl==3.2.0
|
| 25 |
+
typing_extensions==4.8.0
|
| 26 |
+
urllib3==1.26.18
|
| 27 |
+
uvicorn==0.17.6
|
| 28 |
+
uvloop==0.19.0
|
| 29 |
+
watchgod==0.8.2
|
| 30 |
+
websockets==12.0
|