0

FastAPI app below responds with 500 Internal Server Error - error image.

from io import BytesIO

import numpy as np
import tensorflow as tf
import uvicorn
from PIL import Image
from fastapi import FastAPI, UploadFile
from fastapi.middleware.cors import CORSMiddleware

app = FastAPI()

origins = [
    "http://localhost",
    "http://localhost:3000",
]

app.add_middleware(
    CORSMiddleware,
    allow_origins=origins,
    allow_credentials=True,
    allow_methods=["*"],
    allow_headers=["*"],
)

MODEL = tf.keras.models.load_model("C:/Users/DELLS/Dropbox/Desktop/gnaml/traoning/models/name")

CLASS_NAMES = ["Early Blight", "Late Blight", "Healthy"]


@app.get("/ping")
async def ping():
    return "Hello, I am alive"


def read_file_as_image(data) -> np.ndarray:
    image = np.array(Image.open(BytesIO(data)))
    return image


"""@app.post("/predict")
async def predict(
        file: UploadFile = File(...)
):
    pass"""


@app.post("/uploadfile")
async def create_upload_file(file: UploadFile):
    return read_file_as_image(await file.read())


if __name__ == "__main__":
    uvicorn.run(app, host='localhost', port=8000)
Chris
  • 18,724
  • 6
  • 46
  • 80
  • Further related answers can be found [here](https://stackoverflow.com/a/71639658/17865804) and [here](https://stackoverflow.com/a/71104203/17865804), as well as [here](https://stackoverflow.com/a/71265664/17865804), [here](https://stackoverflow.com/a/71849860/17865804) and [here](https://stackoverflow.com/a/71874694/17865804) – Chris May 19 '23 at 11:32

0 Answers0