Spaces:
Sleeping
Sleeping
Commit
·
249a674
1
Parent(s):
c6811ab
add rest of the functionality
Browse files
app.py
CHANGED
@@ -1,6 +1,10 @@
|
|
1 |
from fastai.vision.all import *
|
2 |
from fastapi import FastAPI
|
3 |
from fastapi.responses import HTMLResponse
|
|
|
|
|
|
|
|
|
4 |
|
5 |
app = FastAPI()
|
6 |
|
@@ -22,48 +26,30 @@ def read_root():
|
|
22 |
html_content = "<p>This is a model inference point for the <a href='https://huggingface.co/spaces/vishalbakshi/isitadigit'>isitadigit</a> space</p>"
|
23 |
return HTMLResponse(content=html_content)
|
24 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
25 |
if __name__ == "__main__":
|
26 |
import uvicorn
|
27 |
uvicorn.run(app, host="0.0.0.0", port=7860)
|
28 |
|
29 |
|
30 |
-
# from fastapi import FastAPI, HTTPException
|
31 |
-
# from pydantic import BaseModel
|
32 |
-
# from PIL import Image
|
33 |
-
# import io
|
34 |
-
# import base64
|
35 |
-
# import uvicorn
|
36 |
-
|
37 |
-
|
38 |
-
|
39 |
-
# # Load the model
|
40 |
-
# learn = load_learner('model.pkl')
|
41 |
|
42 |
-
# app = FastAPI()
|
43 |
|
44 |
-
# class ImageData(BaseModel):
|
45 |
-
# image: str
|
46 |
|
47 |
-
# def predict_image(img):
|
48 |
-
# img = img.convert("L")
|
49 |
-
# img = img.resize((28, 28))
|
50 |
-
# img = np.array(img)
|
51 |
-
# return f"{learn.predict(img)[0][0]:.2f}"
|
52 |
-
|
53 |
-
# @app.get("/")
|
54 |
-
# def read_root():
|
55 |
-
# return {"message": "Hello World"}
|
56 |
-
|
57 |
-
# @app.post("/predict")
|
58 |
-
# async def predict(data: ImageData):
|
59 |
-
# try:
|
60 |
-
# image_data = base64.b64decode(data.image)
|
61 |
-
# img = Image.open(io.BytesIO(image_data))
|
62 |
-
# probability = predict_image(img)
|
63 |
-
# return {"probability": probability}
|
64 |
-
# except Exception as e:
|
65 |
-
# raise HTTPException(status_code=400, detail=str(e))
|
66 |
-
|
67 |
-
# if __name__ == "__main__":
|
68 |
-
# import uvicorn
|
69 |
-
# uvicorn.run(app, host="0.0.0.0", port=7860)
|
|
|
1 |
from fastai.vision.all import *
|
2 |
from fastapi import FastAPI
|
3 |
from fastapi.responses import HTMLResponse
|
4 |
+
from pydantic import BaseModel
|
5 |
+
from PIL import Image
|
6 |
+
import io
|
7 |
+
import base64
|
8 |
|
9 |
app = FastAPI()
|
10 |
|
|
|
26 |
html_content = "<p>This is a model inference point for the <a href='https://huggingface.co/spaces/vishalbakshi/isitadigit'>isitadigit</a> space</p>"
|
27 |
return HTMLResponse(content=html_content)
|
28 |
|
29 |
+
class ImageData(BaseModel):
|
30 |
+
image: str
|
31 |
+
|
32 |
+
def predict_image(img):
|
33 |
+
img = img.convert("L")
|
34 |
+
img = img.resize((28, 28))
|
35 |
+
img = np.array(img)
|
36 |
+
return f"{learn.predict(img)[0][0]:.2f}"
|
37 |
+
|
38 |
+
@app.post("/predict")
|
39 |
+
async def predict(data: ImageData):
|
40 |
+
try:
|
41 |
+
image_data = base64.b64decode(data.image)
|
42 |
+
img = Image.open(io.BytesIO(image_data))
|
43 |
+
probability = predict_image(img)
|
44 |
+
return {"probability": probability}
|
45 |
+
except Exception as e:
|
46 |
+
raise HTTPException(status_code=400, detail=str(e))
|
47 |
+
|
48 |
if __name__ == "__main__":
|
49 |
import uvicorn
|
50 |
uvicorn.run(app, host="0.0.0.0", port=7860)
|
51 |
|
52 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
53 |
|
|
|
54 |
|
|
|
|
|
55 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|