我是 RESTful API 的新手...
所以我尝试部署两个服务器(客户端和模型)。主要思想是:
目前,我正在努力解决客户端服务器部分,如何向另一台服务器发送帖子?
# Define a flask app
app = FastAPI()
app.mount("/static", StaticFiles(directory="static"), name="static")
templates = Jinja2Templates(directory="./templates")
origins = ["*"]
methods = ["*"]
headers = ["*"]
app.add_middleware(
CORSMiddleware,
allow_origins = origins,
allow_credentials = True,
allow_methods = methods,
allow_headers = headers
)
@app.get('/', response_class=HTMLResponse)
def root(request: Request):
return templates.TemplateResponse("index.html", {"request": request})
#render_template('index.html')
#here the client will upload his image to the server
#and then encrypt it in the same button
@app.post('/encrypt', status_code=200)
async def upload_img(f: UploadFile = File(...)):
ret = {}
logger.debug(f)
#base_path = "uploads"
# filename = "predict.jpg"
f.filename = "predict.jpg"
base_path = os.path.dirname(__file__)
file_path = os.path.join(base_path, 'uploads',secure_filename(f.filename))
os.makedirs(base_path, exist_ok=True)
try:
with open(file_path, "wb") as buffer:
shutil.copyfileobj(f.file, buffer)
except Exception as e:
print("Error: {}".format(str(e)))
image_info = {"filename": f.filename, "image": f}
#preprocess image
plain_input = load_input(f)
#create a public context and drop sk
ctx, sk = create_ctx()
#encrypt
enc_input = prepare_input(ctx, plain_input)
enc_input_serialize = enc_input.serialize()
# la tu dois faire un post au serveur
return sk
class inference_param(BaseModel):
context : bytes
enc_input : bytes
@app.post("/data") #send data to the model server
async def send_data(data : inference_param):
return data