vishalbakshi commited on
Commit
4d88116
·
1 Parent(s): 84c2ff1

basic app for testing

Browse files
Files changed (1) hide show
  1. app.py +54 -37
app.py CHANGED
@@ -1,44 +1,61 @@
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
- def get_x(i):
9
- # Convert NumPy array to a single-channel PIL image with inverted colors
10
- return PILImageBW.create(all_noise[i])
11
-
12
- def get_y(i):
13
- return all_thresh[i].astype(np.float32)
14
-
15
- def get_items(_):
16
- return range(len(all_noise))
17
-
18
- # Load the model
19
- learn = load_learner('model.pkl')
20
 
21
  app = FastAPI()
22
 
23
- class ImageData(BaseModel):
24
- image: str
25
-
26
- def predict_image(img):
27
- img = img.convert("L")
28
- img = img.resize((28, 28))
29
- img = np.array(img)
30
- return f"{learn.predict(img)[0][0]:.2f}"
31
-
32
  @app.get("/")
33
  def read_root():
34
  return {"message": "Hello World"}
35
 
36
- @app.post("/predict")
37
- async def predict(data: ImageData):
38
- try:
39
- image_data = base64.b64decode(data.image)
40
- img = Image.open(io.BytesIO(image_data))
41
- probability = predict_image(img)
42
- return {"probability": probability}
43
- except Exception as e:
44
- raise HTTPException(status_code=400, detail=str(e))
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from fastapi import FastAPI
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2
 
3
  app = FastAPI()
4
 
 
 
 
 
 
 
 
 
 
5
  @app.get("/")
6
  def read_root():
7
  return {"message": "Hello World"}
8
 
9
+ if __name__ == "__main__":
10
+ import uvicorn
11
+ uvicorn.run(app, host="0.0.0.0", port=7860)
12
+
13
+ # from fastai.vision.all import *
14
+ # from fastapi import FastAPI, HTTPException
15
+ # from pydantic import BaseModel
16
+ # from PIL import Image
17
+ # import io
18
+ # import base64
19
+ # import uvicorn
20
+
21
+ # def get_x(i):
22
+ # # Convert NumPy array to a single-channel PIL image with inverted colors
23
+ # return PILImageBW.create(all_noise[i])
24
+
25
+ # def get_y(i):
26
+ # return all_thresh[i].astype(np.float32)
27
+
28
+ # def get_items(_):
29
+ # return range(len(all_noise))
30
+
31
+ # # Load the model
32
+ # learn = load_learner('model.pkl')
33
+
34
+ # app = FastAPI()
35
+
36
+ # class ImageData(BaseModel):
37
+ # image: str
38
+
39
+ # def predict_image(img):
40
+ # img = img.convert("L")
41
+ # img = img.resize((28, 28))
42
+ # img = np.array(img)
43
+ # return f"{learn.predict(img)[0][0]:.2f}"
44
+
45
+ # @app.get("/")
46
+ # def read_root():
47
+ # return {"message": "Hello World"}
48
+
49
+ # @app.post("/predict")
50
+ # async def predict(data: ImageData):
51
+ # try:
52
+ # image_data = base64.b64decode(data.image)
53
+ # img = Image.open(io.BytesIO(image_data))
54
+ # probability = predict_image(img)
55
+ # return {"probability": probability}
56
+ # except Exception as e:
57
+ # raise HTTPException(status_code=400, detail=str(e))
58
+
59
+ # if __name__ == "__main__":
60
+ # import uvicorn
61
+ # uvicorn.run(app, host="0.0.0.0", port=7860)