from fastapi import FastAPI, Form, UploadFile, File, HTTPException import uvicorn from fastapi.middleware.cors import CORSMiddleware from fastapi.middleware.httpsredirect import HTTPSRedirectMiddleware from fastapi.middleware.trustedhost import TrustedHostMiddleware from datetime import datetime from fastapi.staticfiles import StaticFiles from datetime import datetime from fastapi.responses import RedirectResponse app = FastAPI() app.add_middleware(HTTPSRedirectMiddleware) app.add_middleware(TrustedHostMiddleware, allowed_hosts=["cmm.ai"]) app.add_middleware( CORSMiddleware, allow_origins=["*"], allow_credentials=True, allow_methods=["*"], allow_headers=["*"], ) app.mount("/static", StaticFiles(directory="static"), name="static") # 根目錄導向docs @app.get("/") async def root(): return RedirectResponse(url="/docs#") from api.tts_router import ttsRouter from api.db_router import dbRouter from api.tendent_router import tendentRouter # from api.speech2text import router # from api.tts_try import ttsTryRouter app.include_router(ttsRouter, prefix="", tags=["文字轉語音"]) app.include_router(dbRouter, prefix="", tags=["supa 操作相關"]) app.include_router(tendentRouter, prefix="", tags=["天燈"]) # app.include_router(router, prefix='/speech2text', tags=["speech2text"]) # app.include_router(ttsTryRouter, prefix='/ttsTry', tags=["測試本地端tts"]) @app.get("/ad") def read_root(): message = { "type": "store", "body": { "cover_img": "https://cmm.ai:9101/static/ad_img/ad-img.png", "title": "台北101國際貴賓卡", "description":"國際旅客限定專屬三重好禮:\n●購物-品牌9折起特別優惠\n●禮遇-Welcome Pack+ NTD300現金折抵券\n●退稅-消費2000元以上提供5%快速退稅服務\n立即申辦", "date": "即日起", "price": "", "original_price": "", "website_url": "", "store_info_url": "", "included": [], "branch": [], "location" : "" }, } return {"data": message} from api.image_operate import remove_background,detect_face @app.post("/image_check") async def image_check(image_file : UploadFile): currentDateAndTime = datetime.now() imgname = currentDateAndTime.strftime("%m-%d-%H-%M-%S")+ "-" + image_file.filename with open(f"/home/mia/101/static/image/{imgname}","wb") as save_img : contents = await image_file.read() save_img.write(contents) # await remove_background(f"/home/mia/101/static/image/{imgname}",f"/home/mia/101/static/image/remove/{imgname}") result = await detect_face(f"/home/mia/101/static/image/{imgname}") return result if __name__ == "__main__": 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")