如何将文件(docx、doc、pdf 或 json)发送到 fastapi 并在没有 UI(即 HTML)的情况下对其进行预测?
How to send a file (docx, doc, pdf or json) to fastapi and predict on it without UI (i.e., HTML)?
如果您知道如何将文件发送到 FastAPI 服务器并在 /predict 端点访问它以使用我的模型进行预测,请帮助我。
我已经使用 /predict 端点部署了模型并完成了 uvicorn main:app
并且它已经部署,但唯一的输入是文档在我的本地电脑中,所以我如何将它发送到 FastAPI?
我已经阅读了 FastAPI 的文档,并且在那里找到了这个示例代码,但挑战在于,这段代码创建了一个 UI 用于上传文件,这不是我正在寻找的。
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 代码
这是您的 javascript 代码(假设您使用 javascript 上传文件)
form = new FormData();
form.append("file", myFile);
let response = await fetch('/file', {
method: 'POST',
body: form
});
let result = await response.json();
编辑:Python 文件上传
我正在使用 httpx
,但从技术上讲它应该与 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 的 httpx
的官方文档中查看更多 configurations/examples。
同样,我没有测试代码,因为我现在时间很紧。
编辑结束
请注意,文件的参数名称必须与用于发送文件的名称相匹配。
以防万一,我还有另一个关于如何使用 POSTMAN 进行测试的答案。参见
注意
我没有测试代码,因为我现在没有时间。以防万一,还有一个 link 我以前的答案有效(除非 FASTAPI 引入了重大更改)。
如果您知道如何将文件发送到 FastAPI 服务器并在 /predict 端点访问它以使用我的模型进行预测,请帮助我。
我已经使用 /predict 端点部署了模型并完成了 uvicorn main:app
并且它已经部署,但唯一的输入是文档在我的本地电脑中,所以我如何将它发送到 FastAPI?
我已经阅读了 FastAPI 的文档,并且在那里找到了这个示例代码,但挑战在于,这段代码创建了一个 UI 用于上传文件,这不是我正在寻找的。
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 代码
这是您的 javascript 代码(假设您使用 javascript 上传文件)
form = new FormData();
form.append("file", myFile);
let response = await fetch('/file', {
method: 'POST',
body: form
});
let result = await response.json();
编辑:Python 文件上传
我正在使用 httpx
,但从技术上讲它应该与 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 的 httpx
的官方文档中查看更多 configurations/examples。
同样,我没有测试代码,因为我现在时间很紧。
编辑结束
请注意,文件的参数名称必须与用于发送文件的名称相匹配。
以防万一,我还有另一个关于如何使用 POSTMAN 进行测试的答案。参见
注意
我没有测试代码,因为我现在没有时间。以防万一,还有一个 link 我以前的答案有效(除非 FASTAPI 引入了重大更改)。