Spaces:
Sleeping
Sleeping
File size: 1,649 Bytes
b5ebd06 4d88116 2208db5 6a9b1ed 249a674 d67a120 ce31921 c6811ab aad5d7e c6811ab aad5d7e c6811ab aad5d7e c6811ab b5ebd06 ab9d8b6 b5ebd06 84c2ff1 ce31921 2208db5 84c2ff1 249a674 d6e7dc7 249a674 4d88116 b5ebd06 4d88116 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 |
from fastai.vision.all import *
from fastapi import FastAPI
from fastapi.responses import HTMLResponse
from fastapi.middleware.cors import CORSMiddleware
from pydantic import BaseModel
from PIL import Image
import io
import base64
app = FastAPI()
app.add_middleware(
CORSMiddleware,
allow_origins=["*"],
allow_credentials=True,
allow_methods=["*"],
allow_headers=["*"],
)
def get_x(i):
# Convert NumPy array to a single-channel PIL image with inverted colors
return PILImageBW.create(all_noise[i])
def get_y(i):
return all_thresh[i].astype(np.float32)
def get_items(_):
return range(len(all_noise))
# Load the model
#learn = load_learner('model.pkl')
learn = load_learner('model2.pkl')
@app.get("/")
def read_root():
html_content = "<p>This is a model inference point for the <a href='https://huggingface.co/spaces/vishalbakshi/isitadigit' target='_blank'>isitadigit</a> space</p>"
return HTMLResponse(content=html_content)
class ImageData(BaseModel):
image: str
def predict_image(img):
img = img.convert("L")
img = img.resize((28, 28))
img = np.array(img)
pred = np.clip(learn.predict(img)[0][0], 0.0, 1.0)
return f"{pred:.2f}"
@app.post("/predict")
async def predict(data: ImageData):
try:
image_data = base64.b64decode(data.image)
img = Image.open(io.BytesIO(image_data))
probability = predict_image(img)
return {"probability": probability}
except Exception as e:
raise HTTPException(status_code=400, detail=str(e))
if __name__ == "__main__":
import uvicorn
uvicorn.run(app, host="0.0.0.0", port=7860)
|