如何将信息(数据)从一个 FastAPI 服务器(客户端)发送到另一个 FastAPI(模型)
how to send information(data) from a FastAPI server(client) to another FastAPI(model)
我是RESTful的新手API...
所以我正在尝试部署两台服务器(客户端和模型)。主要思想是:
- 客户端将自己的图片上传到客户端服务器,客户端服务器(端口8000)会做一些转换
- 然后我希望客户端服务器向另一台服务器(也在端口 8008 使用 FastAPI)创建一个 post(带有转换后的数据)。
目前,我正在为客户端服务器部分而苦苦挣扎,如何对另一台服务器执行 post?
# 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
由于您的端点是异步的,因此您可以使用 aiohttp or httpx 等异步 HTTP 库来发出请求。
如果您不想让客户端服务器的客户端等到图片上传完毕,您也可以使用 Background Tasks.
我是RESTful的新手API...
所以我正在尝试部署两台服务器(客户端和模型)。主要思想是:
- 客户端将自己的图片上传到客户端服务器,客户端服务器(端口8000)会做一些转换
- 然后我希望客户端服务器向另一台服务器(也在端口 8008 使用 FastAPI)创建一个 post(带有转换后的数据)。
目前,我正在为客户端服务器部分而苦苦挣扎,如何对另一台服务器执行 post?
# 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
由于您的端点是异步的,因此您可以使用 aiohttp or httpx 等异步 HTTP 库来发出请求。
如果您不想让客户端服务器的客户端等到图片上传完毕,您也可以使用 Background Tasks.