api.py 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. from fastapi import FastAPI, Request, status
  2. from fastapi.exceptions import RequestValidationError
  3. from fastapi.responses import JSONResponse
  4. import json
  5. from pydantic import BaseModel
  6. from utils import Service
  7. import uvicorn
  8. app = FastAPI()
  9. # 添加全局异常处理器
  10. @app.exception_handler(RequestValidationError)
  11. async def validation_exception_handler(request: Request, exc: RequestValidationError):
  12. return JSONResponse(
  13. status_code=status.HTTP_400_BAD_REQUEST,
  14. content={
  15. "code": 400,
  16. "msg": "请求参数错误",
  17. "data": {
  18. "detail": exc.errors(),
  19. "body": exc.body
  20. }
  21. },
  22. )
  23. # 定义请求体
  24. class KeyWordRequest(BaseModel):
  25. brand_name: str # 判定品牌名称
  26. shop_detail_info_brand_name: str # 商品详情页的品牌名称
  27. title: str # 商品名称
  28. class LicenseRequest(BaseModel):
  29. brand_name: str # 判定品牌名称
  30. title: str # 商品名称
  31. class LogoRequest(BaseModel):
  32. brand_name: str # 判定品牌名称
  33. image_url: str # 图片链接
  34. @app.post("/brandanalysis/api/v1/keyword")
  35. async def brand_key_word_judgement(request: KeyWordRequest):
  36. """关键词引流判定api"""
  37. if request.shop_detail_info_brand_name not in request.title:
  38. title = request.shop_detail_info_brand_name + request.title
  39. else:
  40. title = request.title
  41. judgement_res = Service.agent.brand_key_word_judgement(request.brand_name, title)
  42. result = json.loads(judgement_res)
  43. return {"code": 200, "message": "success", "data": {"judgement_info": result}}
  44. @app.post("/brandanalysis/api/v1/license")
  45. async def license_judgement(request: LicenseRequest):
  46. license_list = Service.get_license_list(request.brand_name)
  47. license_judgement = json.loads(Service.agent.license_product_judgement(request.title, license_list))
  48. return {"code": 200, "message": "success", "data": {"judgement_info": license_judgement}}
  49. @app.post("/brandanalysis/api/v1/logo")
  50. async def logo_judgement(request: LogoRequest):
  51. logo_path_map = {
  52. "李宁": "./logo/lining.jpg"
  53. }
  54. logo_path = logo_path_map[request.brand_name]
  55. logo_judgement_res = json.loads(Service.agent.image_logo_judgement(logo_path, request.image_url))
  56. return {"code": 200, "message": "success", "data": {"judgement_info": logo_judgement_res}}
  57. if __name__ == "__main__":
  58. uvicorn.run(app, host="0.0.0.0", port=7860)