ml-tooling/opyrator

launch-api does not support file upload

loongmxbt opened this issue · 2 comments

image
I've tested the launch-ui, everything works fine.
But when I go to use the api, and tried with postman, I encountered this problem:

{
    "detail": [
        {
            "loc": [
                "body"
            ],
            "msg": "value is not a valid dict",
            "type": "type_error.dict"
        }
    ]
}

I think the definition is not compatible with FastAPI? Should I change that into some Form format?

class Input(BaseModel):
    file: FileContent = Field(..., mime_type="application/x-sqlite3")
    calc_type: str = 'PF'

Append:
When change the api into http://127.0.0.1:8080/call/ the response goes into:

WARNING:  Invalid HTTP request received.
Traceback (most recent call last):
  File "/Users/dragonszy/miniconda3/lib/python3.9/site-packages/uvicorn/protocols/http/httptools_impl.py", line 132, in data_received
    self.parser.feed_data(data)
  File "httptools/parser/parser.pyx", line 212, in httptools.parser.parser.HttpParser.feed_data
httptools.parser.errors.HttpParserInvalidMethodError: Invalid method encountered
INFO:     127.0.0.1:54894 - "POST /call HTTP/1.1" 422 Unprocessable Entity
INFO:     127.0.0.1:54993 - "POST /call HTTP/1.1" 422 Unprocessable Entity

BTW, I've rewrite it into Flask if anyone has the same problem

from fastapi import FastAPI, File, UploadFile
import uvicorn
from tempfile import NamedTemporaryFile, TemporaryDirectory

@app.post("/uploadfile/")
async def create_upload_file(file: UploadFile = File(...), calc_type: str = 'PF'):
    result = {
        "filename": file.filename,
        "content-type": file.content_type,
    }

    res = await file.read()
    with NamedTemporaryFile(suffix=".db", mode="w+b") as db_file:
        db_file.write(res)
        net = pp.from_sqlite(db_file.name)

    if (calc_type == 'PF'):
        net = calcPF(net)
    if (calc_type == 'SC'):
        net = calcSC(net)
    result = export_result(net)
    return result

if __name__ == '__main__':
    uvicorn.run("ppapi:app", host="0.0.0.0", port=8090, log_level="info")

This issue is stale because it has been open 90 days with no activity. Remove stale label or comment or this will be closed in 14 days