main.py 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. from fastapi import FastAPI, Form, UploadFile, File, HTTPException
  2. import uvicorn
  3. from fastapi.middleware.cors import CORSMiddleware
  4. from fastapi.middleware.httpsredirect import HTTPSRedirectMiddleware
  5. from fastapi.middleware.trustedhost import TrustedHostMiddleware
  6. from datetime import datetime
  7. from fastapi.staticfiles import StaticFiles
  8. from datetime import datetime
  9. from fastapi.responses import RedirectResponse
  10. app = FastAPI()
  11. app.add_middleware(HTTPSRedirectMiddleware)
  12. app.add_middleware(TrustedHostMiddleware, allowed_hosts=["cmm.ai"])
  13. app.add_middleware(
  14. CORSMiddleware,
  15. allow_origins=["*"],
  16. allow_credentials=True,
  17. allow_methods=["*"],
  18. allow_headers=["*"],
  19. )
  20. app.mount("/static", StaticFiles(directory="static"), name="static")
  21. # 根目錄導向docs
  22. @app.get("/")
  23. async def root():
  24. return RedirectResponse(url="/docs#")
  25. from api.tts_router import ttsRouter
  26. from api.db_router import dbRouter
  27. from api.tendent_router import tendentRouter
  28. # from api.speech2text import router
  29. # from api.tts_try import ttsTryRouter
  30. app.include_router(ttsRouter, prefix="", tags=["文字轉語音"])
  31. app.include_router(dbRouter, prefix="", tags=["supa 操作相關"])
  32. app.include_router(tendentRouter, prefix="", tags=["天燈"])
  33. # app.include_router(router, prefix='/speech2text', tags=["speech2text"])
  34. # app.include_router(ttsTryRouter, prefix='/ttsTry', tags=["測試本地端tts"])
  35. @app.get("/ad")
  36. def read_root(language :str = "ch"):
  37. message = {}
  38. if language == "ch" :
  39. message = {
  40. "type": "store",
  41. "body": {
  42. "cover_img": "https://cmm.ai:9101/static/ad_img/ad-img.png",
  43. "title": "台北101國際貴賓卡",
  44. "description":"國際旅客限定專屬三重好禮:\n●購物-品牌9折起特別優惠\n●禮遇-Welcome Pack+ NTD300現金折抵券\n●退稅-消費2000元以上提供5%快速退稅服務\n<a href='https://stage.taipei101mall.com.tw/join-member/AIsystem' class='ar-link mt-3' target='_blank'>立即申辦</a>",
  45. "date": "即日起",
  46. "price": "",
  47. "original_price": "",
  48. "website_url": "",
  49. "store_info_url": "",
  50. "included": [],
  51. "branch": [],
  52. "location" : ""
  53. },
  54. }
  55. else :
  56. message = {
  57. "type": "store",
  58. "body": {
  59. "cover_img": "https://cmm.ai:9101/static/ad_img/ad-img.png",
  60. "title": "Taipei 101 International VIP Card",
  61. "description":"Exclusive Triple Benefits for International Travelers:\n● Shopping - Special discounts starting at 10% off on selected brands\n● Welcome Gifts - Welcome Pack + NTD 300 cash voucher\n● Tax Refund - 5% instant tax refund for purchases over NTD 2000\n<a href='https://stage.taipei101mall.com.tw/join-member/AIsystem' class='ar-link mt-3' target='_blank'>立即申辦</a>",
  62. "date": "Starting from today",
  63. "price": "",
  64. "original_price": "",
  65. "website_url": "",
  66. "store_info_url": "",
  67. "included": [],
  68. "branch": [],
  69. "location" : ""
  70. },
  71. }
  72. return {"data": message}
  73. from api.image_operate import remove_background,detect_face
  74. @app.post("/image_check")
  75. async def image_check(image_file : UploadFile):
  76. currentDateAndTime = datetime.now()
  77. imgname = currentDateAndTime.strftime("%m-%d-%H-%M-%S")+ "-" + image_file.filename
  78. with open(f"/home/mia/101/static/image/{imgname}","wb") as save_img :
  79. contents = await image_file.read()
  80. save_img.write(contents)
  81. # await remove_background(f"/home/mia/101/static/image/{imgname}",f"/home/mia/101/static/image/remove/{imgname}")
  82. result = await detect_face(f"/home/mia/101/static/image/{imgname}")
  83. return result
  84. if __name__ == "__main__":
  85. uvicorn.run("main:app", host="cmm.ai", port=9101, reload=False, ssl_keyfile="/etc/letsencrypt/live/cmm.ai/privkey.pem", ssl_certfile="/etc/letsencrypt/live/cmm.ai/fullchain.pem")