问题描述
如果您知道如何将文件发送到FastAPI服务器并使用我的模型在/predict端点中进行访问以进行预测,请帮帮我.
If you know how to send a file to FastAPI server and access it in /predict endpoint for prediction using my models please help me out.
我已经使用/predict端点部署了模型并完成了 uvicorn main:app
,并且已经部署了该模型,但是唯一的输入是文档在我的本地PC中,所以我如何将其发送到FastAPI?
I have deployed the model using /predict endpoint and done uvicorn main:app
and it's deployed but the only thing is input that is a document is in my local pc so how can I sent it to FastAPI?
我已经阅读了FastAPI的文档,并在其中找到了此示例代码,但是挑战在于该代码创建了一个用于上传文件的UI,这不是我想要的.
I have went through the documentation of FastAPI and I have found this example code there, but the challenge is that this code creates an UI for uploading file which is not what I"m looking for.
from typing import Optional
from fastapi import FastAPI
from fastapi import FastAPI, File, UploadFile
from pydantic import BaseModel
from typing import List
from fastapi.responses import HTMLResponse
app = FastAPI()
class User(BaseModel):
user_name: dict
@app.post("/files/")
async def create_files(files: List[bytes] = File(...)):
return {"file_sizes": [len(file) for file in files]}
@app.post("/uploadfiles/")
async def create_upload_files(files: List[UploadFile] = File(...)):
return {"filenames": [file.filename for file in files]}
@app.get("/")
async def main():
content = """
<body>
<form action="/files/" enctype="multipart/form-data" method="post">
<input name="files" type="file" multiple>
<input type="submit">
</form>
</body>
"""
return HTMLResponse(content=content)
推荐答案
FASTAPI代码
这将是您的终点.
from fastapi import FastAPI, UploadFile, File
app = FastAPI()
@app.post("/file")
async def upload_file(file: UploadFile = File(...)):
# Do here your stuff with the file
return {"filename": file.filename}
JAVASCRIPT CODE
JAVASCRIPT CODE
这是您的javascript代码(假设您使用的是javascript来上传文件)
This is your javascript code (assuming you are using javascript for uploading the file)
form = new FormData();
form.append("file", myFile);
let response = await fetch('/file', {
method: 'POST',
body: form
});
let result = await response.json();
上传Python文件
我正在使用 httpx
,但是从技术上讲,它应该与 requests
完全兼容.
I'm using httpx
, but technically it should be fully compatible with requests
.
import httpx
# Create a dict with a key that has the same name as your file parameter and the file in binary form (the "b" in "rb")
f = {'file': open('foo.png', 'rb')}
r = httpx.post("your_url/file", files=f)
您可以在 https://www.python-httpx.org/quickstart/#sending-multipart-file-uploads .
同样,由于目前时间紧迫,我没有测试代码.
Again, I did not test the code, since I'm tight with time at the moment.
编辑结束
请注意,文件的参数名称必须与用于发送文件的参数名称匹配.
以防万一,我还有另一个关于如何使用POSTMAN测试它的答案.请参见如何使用邮递员将文件发送到fastapi端点
In case, there is also another answer from me on how to test it with POSTMAN. See How to send file to fastapi endpoint using postman
注意
我没有测试代码,因为我现在没有时间.以防万一,还有一个与我以前的答案有效的链接(除非FASTAPI引入了重大更改).
I did not test the code as I don't have time right now. In case, there is also a link with a previous answer of mine that works (unless FASTAPI introduced breaking changes).
这篇关于如何将文件(docx,doc,pdf或json)发送到fastapi并在没有UI(即HTML)的情况下对其进行预测?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!