main.py 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. from fastapi import FastAPI, Form
  2. from fastapi import Depends, FastAPI, HTTPException, status, Request, Form, Cookie, Response, Header
  3. from fastapi.responses import HTMLResponse, RedirectResponse, JSONResponse
  4. from fastapi.templating import Jinja2Templates
  5. from fastapi.staticfiles import StaticFiles
  6. from pydantic import BaseModel
  7. import iCulture_semantic_search
  8. import iCulture_wordcloud
  9. import json
  10. import tqdm
  11. import uvicorn
  12. app = FastAPI()
  13. app.mount("/static", StaticFiles(directory="static"), name="static")
  14. templates = Jinja2Templates(directory="templates")
  15. # input model
  16. class Query(BaseModel):
  17. query: str
  18. top_k: int
  19. similarity: float
  20. start_date: str
  21. end_date: str
  22. # output model
  23. class Semantic_search(BaseModel):
  24. semantic_search: str
  25. class Tag_list(BaseModel):
  26. tag_list: str
  27. class Wordcloud(BaseModel):
  28. wordcloud: str
  29. @app.get("/", response_class=HTMLResponse)
  30. async def root(request: Request, response: Response):
  31. return templates.TemplateResponse("index.html", {"request": request, "response": response})
  32. @app.post("/semantic_search", response_model=Semantic_search)
  33. async def semantic_search(query: Query):
  34. print('-'*50,'\n')
  35. print('【Request】')
  36. print(query,'\n')
  37. print('-'*50)
  38. query = query.dict()
  39. return Semantic_search(
  40. semantic_search=json.dumps(
  41. iCulture_semantic_search.search_event(query['query'], query['top_k'], query['similarity'], query['start_date'], query['end_date']))
  42. )
  43. @app.post("/tag_list", response_model=Tag_list)
  44. async def tag_list(query: Query):
  45. query = query.dict()
  46. return Tag_list(
  47. tag_list=json.dumps(
  48. iCulture_semantic_search.search_event_for_tag_list(
  49. query['query'], query['top_k'], query['similarity'], query['start_date'], query['end_date']
  50. ))
  51. )
  52. @app.post("/wordcloud", response_model=Wordcloud)
  53. async def wordcloud(query: Query):
  54. query = query.dict()
  55. return Wordcloud(
  56. wordcloud=json.dumps(iCulture_wordcloud.to_wordcloud(
  57. query['query'], query['top_k'], query['similarity'], query['start_date'], query['end_date']))
  58. )
  59. @app.post("/add_search")
  60. async def add_search(query: Query):
  61. query = query.dict()
  62. keywords = query['query'].split()
  63. ### return these three
  64. ret_keywords = []
  65. ret_labels = []
  66. ret_names = []
  67. #print("###############keywords:", keywords)
  68. '''
  69. with open("static/data.json", "r") as f:
  70. return_keywords = json.load(f)
  71. for keyword in keywords:
  72. if ret := return_keywords.get(keyword):
  73. return_keywords = ret
  74. else:
  75. ret = {}
  76. break
  77. ret_keywords = list(ret.keys())
  78. print("##########return", ret_keywords)
  79. '''
  80. ### write here and ret is list of recommend keywords
  81. ret_keywords = ["abc", "def", "ghi", "jkl", "nmo", "pqr"]
  82. ret_labels = ["1", "2", "3", "4", "5", "6"]
  83. ret_names = ["Tomoya", "Jared", "Doris", "Wizer", "Nina", "Morrison"]
  84. ###
  85. return {"add_keywords":ret_keywords, "add_labels":ret_labels, "add_names":ret_names}
  86. # if __name__ == "__main__":
  87. # print('123')
  88. # uvicorn.run("setup:app", host="0.0.0.0", port=12345, reload=True)