api_get_article.py 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. # -*- coding: utf-8 -*-
  2. from fastapi import FastAPI, Form, HTTPException, Request,status,APIRouter,Query,Path
  3. from tools.loglog import logger
  4. from gpt.get_article import GetArticle
  5. from gpt.query_oss_file import query_file_content
  6. from core.respone_format import *
  7. from pydantic import BaseModel, ValidationError, conint
  8. from typing import List, Optional
  9. router = APIRouter()
  10. get_article = GetArticle()
  11. class ArticleRequest(BaseModel):
  12. meaning_ids: List[conint(ge=1)]
  13. callback_url: Optional[str] = None
  14. demo_name: Optional[str] = "无"
  15. student_stage: Optional[int] = 1
  16. vocabulary: Optional[int] = 500
  17. class_id :Optional[int]
  18. @router.post("/article")
  19. def post_article(json_data:ArticleRequest,request:Request):
  20. real_ip = request.headers.get("X-Real-IP","localhost")
  21. words_meaning_ids: list = json_data.meaning_ids
  22. callback_url = json_data.callback_url
  23. demo_name = json_data.demo_name
  24. student_stage = json_data.student_stage
  25. vocabulary = json_data.vocabulary
  26. class_id = json_data.class_id
  27. try:
  28. if not words_meaning_ids:
  29. return resp_404(message="没有词义id")
  30. r = get_article.submit_task(words_meaning_ids=words_meaning_ids,callback_url=callback_url,
  31. real_ip=real_ip,demo_name=demo_name,
  32. student_stage=student_stage,vocabulary=vocabulary,class_id=class_id)
  33. return r if not isinstance(r,str) else resp_500(message=r)
  34. except Exception as e:
  35. logger.error(f"{type(e).__name__},{e}")
  36. return resp_500(message=f"{type(e).__name__},{e}")
  37. @router.post("/query_oss_file")
  38. def query_oss_file(json_data:dict,request:Request):
  39. oss_key = json_data.get("key")
  40. if not oss_key:
  41. return resp_500(message="请提供key值")
  42. j = query_file_content(key=oss_key)
  43. if j == 0:
  44. return resp_500(message="错误:没有这个文件")
  45. return JSONResponse(j)