我是 RESTful API 的新手...
所以我正在嘗試部署兩臺服務器(客戶端和模型)。主要思想是:
- 客戶端上傳他的圖片到客戶端服務器,客戶端服務器(8000埠)會做一些轉換
- 然后我希望客戶端服務器向另一臺服務器(也在埠 8008 上使用 FastAPI)發布一個帖子(帶有轉換后的資料)。
目前,我正在為客戶端服務器部分苦苦掙扎,如何發布到另一臺服務器?
# 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
uj5u.com熱心網友回復:
由于您的端點是異步的,您可以使用異步 HTTP 庫(如aiohttp或httpx)來發出請求。
如果您不希望客戶端服務器的客戶端等到影像上傳,您也可以使用Background Tasks。
轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/464820.html
