为什么我的 FastAPI 或 Uvicorn 会被关闭?

6 浏览
0 Comments

为什么我的 FastAPI 或 Uvicorn 会被关闭?

我正在尝试运行一个使用简单变换器(simple transformers)Roberta模型进行分类的服务。当我单独测试推理脚本/函数时,它按预期工作。但是,当我将其与FastAPI结合使用时,服务器就会关闭。

uvicorn==0.11.8
fastapi==0.61.1
simpletransformers==0.51.6
cmd : uvicorn --host 0.0.0.0 --port 5000 src.main:app


@app.get("/article_classify")
def classification(text:str):
    """使用深度学习模型对文章进行分类的函数。
    返回:
        [type]: [description]
    """
    _,_,result = inference(text)
    return result

错误:

INFO:     启动服务器进程 [8262]
INFO:     等待应用程序启动。
INFO:     应用程序启动完成。
INFO:     Uvicorn运行在 http://0.0.0.0:5000(按CTRL+C退出)
INFO:     127.0.0.1:36454 - "GET / HTTP/1.1" 200 OK
INFO:     127.0.0.1:36454 - "GET /favicon.ico HTTP/1.1" 404 Not Found
INFO:     127.0.0.1:36454 - "GET /docs HTTP/1.1" 200 OK
INFO:     127.0.0.1:36454 - "GET /openapi.json HTTP/1.1" 200 OK
before
100%|████████████████████████████████████████████████████████████████████████████| 1/1 [00:00<00:00, 17.85it/s]
INFO:     关闭中
INFO:     完成服务器进程 [8262]

推理脚本:

model_name = "checkpoint-3380-epoch-20"
model = MultiLabelClassificationModel("roberta","src/outputs/"+model_name)
def inference(input_text,model_name="checkpoint-3380-epoch-20"):
    """对单个文本样本进行推理的函数"""
    #model = MultiLabelClassificationModel("roberta","src/outputs/"+model_name)
    all_tags =[]
    if isinstance(input_text,str):
        print("before")
        result ,output = model.predict([input_text])
        print(result)
        tags=[]
        for idx,each in enumerate(result[0]):
            if each==1:
                tags.append(classes[idx])
        all_tags.append(tags)
    elif isinstance(input_text,list):
        result ,output = model.predict(input_text)
        tags=[]
        for res in result : 
            for idx,each in enumerate(res):
                if each==1:
                    tags.append(classes[idx])
            all_tags.append(tags)
    return result,output,all_tags

更新:尝试使用Flask,服务正常工作,但是当在Flask之上添加uvicorn时,它陷入重启循环中。

0