ArxAlfa commited on
Commit ·
a650593
1
Parent(s): 9d63bd4
Add model.joblib, update Dockerfile and
Browse filesFiles changed (5)
- Dockerfile+1-1
- app.py+64-5
- dataset/housing_price_dataset.csvAdded
- model.joblibAdded
- requirements.txt+30-6
122345# Use the official Python 3.9 imageFROM python:3.9FROM python:3.9-slim
# Set the working directory to /codeWORKDIR /code112345678910111213141516171889101119202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970from fastapi import FastAPIfrom fastapi import FastAPI, UploadFile, Fileimport numpy as npfrom sklearn.neural_network import MLPRegressorfrom sklearn.model_selection import KFoldfrom sklearn.metrics import mean_squared_errorimport csvimport io
from joblib import load, dump
# Load the modelmodel = load("model.joblib")
# Create a new FastAPI app instanceapp = FastAPI(docs_url="/", redoc_url="/new_redoc")
# Create a POST endpoint@app.get("/generate")def generate(text: str): """Traduce del castellano al inglés""" return {"output": text}@app.get("/generate/{squareFeet}/{bedrooms}/{bathrooms}/{yearBuilt}")def generate( squareFeet: float, bedrooms: float, bathrooms: float, yearBuilt: float,): global model prediction = model.predict([[squareFeet, bedrooms, bathrooms, yearBuilt]]) return {"output": prediction[0]}
@app.post("/train")async def train(file: UploadFile = File(...)): global model
contents = await file.read() data = list(csv.reader(io.StringIO(contents.decode("utf-8"))))
data_np = np.array(data[1:], dtype=object)
# Delete the fourth column data_np = np.delete(data_np, 3, axis=1) data_np = np.array(data_np, dtype=float)
# All columns except the last X = data_np[:, :-1]
# Only the last column y = data_np[:, -1] y = np.ravel(y)
# Fit the model kf = KFold(n_splits=4) accuracies = []
for train_index, test_index in kf.split(X): X_train, X_test = X[train_index], X[test_index] y_train, y_test = y[train_index], y[test_index]
model.fit(X_train, y_train)
predictions = model.predict(X_test) rmse = np.sqrt(mean_squared_error(y_test, predictions)) accuracies.append(rmse)
average_rmse = sum(accuracies) / len(accuracies) print(f"Average RMSE: {average_rmse}")
dump(model, "model.joblib")
return {"filename": file.filename, "average_rmse": average_rmse}123456123456789101112131415161718192021222324252627282930fastapi==0.74.*requests==2.27.*uvicorn[standard]==0.17.*sentencepiece==0.1.*torch==1.11.*#transformers==4.*# requirements.txt
anyio==3.7.1asgiref==3.7.2certifi==2023.11.17charset-normalizer==2.0.12click==8.1.7fastapi==0.74.1h11==0.14.0httptools==0.6.1idna==3.6joblib==1.3.2numpy==1.26.2pydantic==1.10.13python-dotenv==1.0.0python-multipart==0.0.6PyYAML==6.0.1requests==2.27.1scikit-learn==1.3.2scipy==1.11.4sentencepiece==0.1.99sniffio==1.3.0starlette==0.17.1threadpoolctl==3.2.0typing_extensions==4.8.0urllib3==1.26.18uvicorn==0.17.6uvloop==0.19.0watchgod==0.8.2websockets==12.0Dockerfile+1-1
app.py+64-5
dataset/housing_price_dataset.csvAdded
model.joblibAdded
requirements.txt+30-6