Upload app.py
Browse files
app.py
ADDED
@@ -0,0 +1,40 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from fastai.vision import *
|
2 |
+
from starlette.applications import Starlette
|
3 |
+
from starlette.responses import JSONResponse
|
4 |
+
from starlette.middleware.cors import CORSMiddleware
|
5 |
+
import uvicorn
|
6 |
+
import aiohttp
|
7 |
+
import asyncio
|
8 |
+
import keras
|
9 |
+
import numpy as np
|
10 |
+
from tensorflow.keras.preprocessing import image
|
11 |
+
|
12 |
+
app = Starlette()
|
13 |
+
app.add_middleware(CORSMiddleware, allow_origins=["*"], allow_headers=["*"], allow_methods=["*"])
|
14 |
+
|
15 |
+
model = keras.models.load_model("Detection_Covid_19.h5")
|
16 |
+
|
17 |
+
async def save_file(request):
|
18 |
+
try:
|
19 |
+
form = await request.form()
|
20 |
+
file = form['file']
|
21 |
+
file_bytes = await file.read()
|
22 |
+
file_name = "test.jpg"
|
23 |
+
with open(file_name, 'wb') as f:
|
24 |
+
f.write(file_bytes)
|
25 |
+
|
26 |
+
# Preprocess the image before feeding it to the model
|
27 |
+
img = image.load_img(file_name, target_size=(224, 224))
|
28 |
+
img = image.img_to_array(img)
|
29 |
+
img = np.expand_dims(img, axis=0)
|
30 |
+
img = img / 255.0 # Normalize the pixel values
|
31 |
+
|
32 |
+
pred = model.predict(img)
|
33 |
+
prediction = "1" if pred[0][0] <= 0.5 else "0"
|
34 |
+
|
35 |
+
return JSONResponse({"prediction": prediction})
|
36 |
+
|
37 |
+
except Exception as e:
|
38 |
+
return JSONResponse(content={"error": str(e)}, status_code=500)
|
39 |
+
|
40 |
+
app.add_route("/", save_file, methods=['POST'])
|