api_get_article2.py 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  1. # -*- coding: utf-8 -*-
  2. from fastapi import FastAPI, Form, HTTPException, Request,status,APIRouter,Query,Path
  3. from tools.loglog import logger,log_err_e
  4. from core.respone_format import *
  5. from gpt.get_article2 import GetArticle
  6. from pydantic import BaseModel, ValidationError, conint,Field
  7. from typing import List, Optional,Literal
  8. router = APIRouter()
  9. get_article = GetArticle()
  10. class Word(BaseModel):
  11. meaning_id:int = Field(..., description="单词的词义id")
  12. word_id:int = Field(..., description="单词id")
  13. spell: str = Field(..., description="单词的拼写")
  14. meaning: str = Field(..., description="单词的意思")
  15. class ArticleRequest(BaseModel):
  16. core_words: List[Word] = Field(..., description="单词列表")
  17. extend_words: List[Word] = Field(..., description="单词列表")
  18. take_count: int = 2
  19. student_stage: Literal[1, 2, 3]
  20. demo_name: Optional[str] = "无"
  21. reading_level: int = Field(default=-1, description="阅读水平,默认值为-1")
  22. @router.post("/article/reading-comprehension")
  23. def post_article(json_data:ArticleRequest,request:Request):
  24. json_data = json_data.dict()
  25. real_ip = request.headers.get("X-Real-IP","0.0.0.0")
  26. core_words,extend_words,take_count,student_stage,demo_name = json_data["core_words"],json_data["extend_words"],json_data["take_count"],json_data["student_stage"],json_data["demo_name"]
  27. reading_level = json_data.get("reading_level",-1)
  28. try:
  29. r = get_article.submit_task(core_words=core_words,extend_words=extend_words, take_count=take_count,student_stage=student_stage,real_ip=real_ip,demo_name=demo_name)
  30. return r if not isinstance(r,str) else resp_500(message=r)
  31. except Exception as e:
  32. log_err_e(e,msg="文章2接口错误/article/reading-comprehension;")
  33. return resp_500(message=f"{type(e).__name__},{e}")