-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
34 lines (29 loc) · 934 Bytes
/
Copy pathmain.py
File metadata and controls
34 lines (29 loc) · 934 Bytes
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
from fastapi import FastAPI
from fastapi.staticfiles import StaticFiles
from fastapi.middleware.cors import CORSMiddleware
from pydantic import BaseModel
from typing import List
import onnx
import onnxruntime
import numpy as np
class Item(BaseModel):
array: List[float]
app = FastAPI()
app.add_middleware(
CORSMiddleware,
allow_origins=["*"],
allow_credentials=True,
allow_methods=["*"],
allow_headers=["*"],
)
@app.post("/lenet")
async def lenet(item: Item):
array = item.array
image = np.array(array, dtype=np.double)
image = np.reshape(image, (1,1,32,32)).astype(np.float32)
onnx_model = onnx.load('cnn.onnx')
session = onnxruntime.InferenceSession(onnx_model.SerializeToString())
output = session.run(None, {'in': image})
predictions = output[0][0].tolist()
return {"predictions": predictions}
app.mount("/", StaticFiles(directory="static",html = True), name="static")