Spaces:
Sleeping
Sleeping
Commit
·
d67a120
1
Parent(s):
7ac93aa
initial commit
Browse files- app.py +30 -0
- model.pkl +3 -0
- requirements.txt +5 -0
app.py
ADDED
|
@@ -0,0 +1,30 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from fastai.vision.all import *
|
| 2 |
+
from fastapi import FastAPI, HTTPException
|
| 3 |
+
from pydantic import BaseModel
|
| 4 |
+
from PIL import Image
|
| 5 |
+
import io
|
| 6 |
+
import base64
|
| 7 |
+
|
| 8 |
+
# Load the model
|
| 9 |
+
learn = load_learner('model.pkl')
|
| 10 |
+
|
| 11 |
+
app = FastAPI()
|
| 12 |
+
|
| 13 |
+
class ImageData(BaseModel):
|
| 14 |
+
image: str
|
| 15 |
+
|
| 16 |
+
def predict_image(img):
|
| 17 |
+
img = img.convert("L")
|
| 18 |
+
img = img.resize((28, 28))
|
| 19 |
+
img = np.array(img)
|
| 20 |
+
return f"{learn.predict(img)[0][0]:.2f}"
|
| 21 |
+
|
| 22 |
+
@app.post("/predict")
|
| 23 |
+
async def predict(data: ImageData):
|
| 24 |
+
try:
|
| 25 |
+
image_data = base64.b64decode(data.image)
|
| 26 |
+
img = Image.open(io.BytesIO(image_data))
|
| 27 |
+
probability = predict_image(img)
|
| 28 |
+
return {"probability": probability}
|
| 29 |
+
except Exception as e:
|
| 30 |
+
raise HTTPException(status_code=400, detail=str(e))
|
model.pkl
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:04a212a1709f207d4f3c33a3ccd17d891df6917fe1ec1d7b0c1c918579b67df3
|
| 3 |
+
size 89750534
|
requirements.txt
ADDED
|
@@ -0,0 +1,5 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
fastai==2.7.16
|
| 2 |
+
fastapi
|
| 3 |
+
pillow
|
| 4 |
+
pydantic
|
| 5 |
+
uvicorn
|