I’m trying to use fastapi e uvicorn to make a prediction. I’ve already trained the model on my Pycharm project. It works. Next step is to deploy via FastAPI. Should be kinda straightforward: in the url/docs i should load the pickle file with the weights and the csv with the data. Then i should retrieve the output with the results.
I wrote this:
import io
from fastapi import FastAPI, UploadFile, File
import pandas as pd
import joblib
import uvicorn
# instance
app = FastAPI(title="Breast cancer ML API", description="API for classification of breast cancer", version="1.0", predix="asd", tags=["items"])
@app.post("/predict/")
async def predict(file: UploadFile = File(...), weights: UploadFile = File(...)):
content = await weights.read()
model = joblib.load(content)
test = await file.read()
df = pd.read_csv(io.StringIO(test.decode('utf-8')))
preprocessing = model.named_steps("preprocessing")
preprocessed_data = preprocessing.transform(df)
# prediction
classifier = model.named_steps("classifier")
prediction = classifier.predict(preprocessed_data)
prediction_df = pd.DataFrame(prediction, index=df.index, columns=["diagnosis"])
result = prediction_df.to_csv()
return len(df)
if __name__ == "__main__":
uvicorn.run(app, host="0.0.0.0", port=8000)
but i have UniCodeDecodeError. So, how do i upload and read the csv as pandas dataframe, in order to be preprocessed and classified as my model?
And, how do i read the pickle correctly?
Thanks for answers!