ArxAlfa commited on
Commit ·
57ee6b1
1
Parent(s): 508270a
Update model input size and add OneHotEncoder
Browse files4 unmodified lines567891016 unmodified lines272829303132333435363738394041424344454647484950515253545556571 unmodified line596061626364654 unmodified linesfrom fastapi import FastAPI, UploadFile, Filefrom sklearn.model_selection import KFoldfrom sklearn.metrics import mean_squared_errorimport csvimport io16 unmodified lines# Load the modelmodel = DNN(input_size=4, hidden_size=64, output_size=1)# Create a new FastAPI app instanceapp = FastAPI(docs_url="/", redoc_url="/new_redoc")# Create a POST endpoint@app.get("/generate/{squareFeet}/{bedrooms}/{bathrooms}/{yearBuilt}")def generate(squareFeet: float,bedrooms: float,bathrooms: float,yearBuilt: float,):global modelinput_data = torch.tensor([[squareFeet, bedrooms, bathrooms, yearBuilt]], dtype=torch.float32)prediction = model(input_data)return {"output": prediction.item()}@app.post("/train")async def train(file: UploadFile = File(...)):global modelcontents = await file.read()data = list(csv.reader(io.StringIO(contents.decode("utf-8"))))1 unmodified linedata_np = np.array(data[1:], dtype=object)# Delete the fourth columndata_np = np.delete(data_np, 3, axis=1)data_np = np.array(data_np, dtype=float)# All columns except the last4 unmodified lines56789101116 unmodified lines282930313233343536373839404142434445464748495051525354555657585960616263646566671 unmodified line69707172737475764 unmodified linesfrom fastapi import FastAPI, UploadFile, Filefrom sklearn.model_selection import KFoldfrom sklearn.metrics import mean_squared_errorfrom sklearn.preprocessing import OneHotEncoderimport csvimport io16 unmodified lines# Load the modelmodel = DNN(input_size=5, hidden_size=64, output_size=1)# Initialize the OneHotEncoderencoder = OneHotEncoder(handle_unknown="ignore")# Create a new FastAPI app instanceapp = FastAPI(docs_url="/", redoc_url="/new_redoc")# Create a POST endpoint@app.get("/generate/{squareFeet}/{bedrooms}/{bathrooms}/{neighborhood}/{yearBuilt}")def generate(squareFeet: float,bedrooms: float,bathrooms: float,neighborhood: str,yearBuilt: float,):global model, encoder# Apply the encoder to the neighborhood inputneighborhood_encoded = encoder.transform([[neighborhood]]).toarray()[0]# Combine all inputsinput_data = [squareFeet, bedrooms, bathrooms, *neighborhood_encoded, yearBuilt]input_data = torch.tensor([input_data], dtype=torch.float32)prediction = model(input_data)return {"output": prediction.item()}@app.post("/train")async def train(file: UploadFile = File(...)):global model, encodercontents = await file.read()data = list(csv.reader(io.StringIO(contents.decode("utf-8"))))1 unmodified linedata_np = np.array(data[1:], dtype=object)# Delete the fourth column# data_np = np.delete(data_np, 3, axis=1)data_np[:, 3] = encoder.fit_transform(data_np[:, 3]).toarray()data_np = np.array(data_np, dtype=float)# All columns except the last
app.py+19-8