from fastai.vision import * from starlette.applications import Starlette from starlette.responses import JSONResponse from starlette.middleware.cors import CORSMiddleware import uvicorn import aiohttp import asyncio import keras import numpy as np from tensorflow.keras.preprocessing import image app = Starlette() app.add_middleware(CORSMiddleware, allow_origins=["*"], allow_headers=["*"], allow_methods=["*"]) model = keras.models.load_model("Detection_Covid_19.h5") async def save_file(request): try: form = await request.form() file = form['file'] file_bytes = await file.read() file_name = "test.jpg" with open(file_name, 'wb') as f: f.write(file_bytes) # Preprocess the image before feeding it to the model img = image.load_img(file_name, target_size=(224, 224)) img = image.img_to_array(img) img = np.expand_dims(img, axis=0) img = img / 255.0 # Normalize the pixel values pred = model.predict(img) prediction = "1" if pred[0][0] <= 0.5 else "0" return JSONResponse({"prediction": prediction}) except Exception as e: return JSONResponse(content={"error": str(e)}, status_code=500) app.add_route("/", save_file, methods=['POST'])