server.py 947 B

1234567891011121314151617181920212223242526272829303132333435363738
  1. from pydantic import BaseModel, field_validator, model_validator, Field
  2. from typing import List, Optional, Generic, TypeVar
  3. from utils import base64_to_cv2
  4. from fastapi import FastAPI
  5. app = FastAPI()
  6. T = TypeVar("T")
  7. class APIRequest(BaseModel):
  8. imageData : str
  9. text : str
  10. class APIResponse(BaseModel, Generic[T]):
  11. success: bool
  12. data: Optional[List[T]]
  13. msg: Optional[List[str]]
  14. @app.post("/llm/detect")
  15. @app.post("/llm/detect/")
  16. async def detect(item: APIRequest):
  17. # illegal 为 0 代表没有违章
  18. # illegal 为 1 代表有违章
  19. response = {
  20. "sucess": "OK",
  21. "data": {"illegal" : 0},
  22. "msg": ""
  23. }
  24. image = base64_to_cv2(item.imageData)
  25. if not image.size:
  26. response["sucess"] = "FAILED"
  27. response["msg"] = "Decode Image Error"
  28. return response
  29. # 大模型检测后如果有违章
  30. # response["data"]["illegal"] = 1
  31. return response