# encoding:utf-8 from fastapi import FastAPI, BackgroundTasks from pydantic import BaseModel import uvicorn from lib.service.baidu import check_baidu from lib.service.local import check_local, replace_local from lib.service.wechat import msg_sec_check app = FastAPI() class Item(BaseModel): content: str def begin_audit_task(task_id: str): print('begin audit task: %s' % (task_id)) @app.post("/check_wx") async def check_with_wx(item: Item): res_wechat = await msg_sec_check(item.content) return res_wechat @app.post("/check_bd") async def check_with_bd(item: Item): res_baidu = await check_baidu(item.content) return res_baidu @app.post("/check_local") async def check_with_local(item: Item): res_local = check_local(item.content) return res_local @app.get("/check_local/{content}") async def check_with_local(content: str): res_local = check_local(content) return res_local @app.post("/replace_local") async def replace_with_local(item: Item): res_local = replace_local(item.content) return res_local @app.get("/replace_local/{content}") async def replace_with_local(content: str): res_local = replace_local(content) return res_local # @app.post("/begin_task") # async def begin_task(item: Item, background_tasks: BackgroundTasks): # res_local = check_local(item.content) # background_tasks.add_task(begin_audit_task, item.content) # return res_local @app.post("/check_all") async def check_content(item: Item): res_wechat = await msg_sec_check(item.content) result = {'errcode': 1} if res_wechat['errcode'] == 0: result['errcode'] = 0 result['wx'] = res_wechat['risk'] result['reason_wx'] = res_wechat['reason'] res_baidu = await check_baidu(item.content) if res_baidu['errcode'] == 0: result['errcode'] = 0 result['bd'] = res_baidu['risk'] result['reason_bd'] = res_baidu['reason'] res_local = check_local(item.content) if res_local['errcode'] == 0: result['errcode'] = 0 result['local'] = res_local['risk'] result['reason_local'] = res_local['reason'] return result if __name__ == "__main__": uvicorn.run(app, host="0.0.0.0", port=8019)