|
@@ -1,13 +1,13 @@
|
|
|
-from fastapi import FastAPI,Cookie, Depends, FastAPI, Query, WebSocket, status, WebSocketDisconnect,File, UploadFile
|
|
|
-from os import listdir
|
|
|
+from fastapi import FastAPI,Cookie, Depends, Query, WebSocket, status, WebSocketDisconnect,File, UploadFile,Request,Response,HTTPException
|
|
|
+from fastapi.templating import Jinja2Templates
|
|
|
+from fastapi.responses import HTMLResponse, RedirectResponse, JSONResponse
|
|
|
+from typing import List, Optional
|
|
|
from os.path import isfile, isdir, join
|
|
|
import threading
|
|
|
import zhtts
|
|
|
import os
|
|
|
import urllib
|
|
|
-from typing import List
|
|
|
import requests
|
|
|
-from pydantic import BaseModel
|
|
|
from bs4 import BeautifulSoup
|
|
|
from PIL import Image,ImageDraw,ImageFont
|
|
|
import pyttsx3
|
|
@@ -23,7 +23,7 @@ from fastapi.responses import FileResponse
|
|
|
from websocket import create_connection
|
|
|
from fastapi.middleware.cors import CORSMiddleware
|
|
|
import dataset
|
|
|
-from datetime import datetime
|
|
|
+from datetime import datetime, timedelta
|
|
|
from util.swap_face import swap_face
|
|
|
from fastapi.staticfiles import StaticFiles
|
|
|
import shutil
|
|
@@ -33,21 +33,16 @@ from passlib.context import CryptContext
|
|
|
from jose import JWTError, jwt
|
|
|
from fastapi_jwt_auth import AuthJWT
|
|
|
from fastapi_jwt_auth.exceptions import AuthJWTException
|
|
|
+from fastapi.security import OAuth2PasswordBearer, OAuth2PasswordRequestForm
|
|
|
+import models
|
|
|
import pymysql
|
|
|
pymysql.install_as_MySQLdb()
|
|
|
|
|
|
app = FastAPI()
|
|
|
|
|
|
-origins = [
|
|
|
- "https://hhh.com.tw"
|
|
|
- "http://172.105.205.52",
|
|
|
- "http://172.105.205.52:8001",
|
|
|
- "http://172.104.93.163",
|
|
|
-]
|
|
|
|
|
|
app.add_middleware(
|
|
|
CORSMiddleware,
|
|
|
- # allow_origins=origins,
|
|
|
allow_origins=["*"],
|
|
|
allow_credentials=True,
|
|
|
allow_methods=["*"],
|
|
@@ -74,42 +69,7 @@ img_upload_folder = '/var/www/html/'+tmp_img_sub_folder
|
|
|
video_dest = '/var/www/html/'+video_sub_folder
|
|
|
avatar_dest = '/var/www/html/'+avatar_sub_folder
|
|
|
|
|
|
-class Token(BaseModel):
|
|
|
- access_token: str
|
|
|
- token_type: str
|
|
|
-
|
|
|
-class swap_req(BaseModel):
|
|
|
- imgurl: str
|
|
|
-
|
|
|
-class request(BaseModel):
|
|
|
- name: str
|
|
|
- text_content: List[str]
|
|
|
- image_urls: List[str]
|
|
|
- avatar: str
|
|
|
- client_id :str
|
|
|
-
|
|
|
-class request_eng(BaseModel):
|
|
|
- name: str
|
|
|
- text_content: List[str]
|
|
|
- image_urls: List[str]
|
|
|
- sub_titles: List[str]
|
|
|
- avatar: str
|
|
|
- client_id :str
|
|
|
-# AuthJWT setting
|
|
|
-class Settings(BaseModel):
|
|
|
- authjwt_secret_key: str = SECRET_KEY
|
|
|
- authjwt_token_location: set = {"cookies"}
|
|
|
- authjwt_cookie_secure: bool = False
|
|
|
- authjwt_cookie_csrf_protect: bool = True
|
|
|
-
|
|
|
-class User(BaseModel):
|
|
|
- username: str
|
|
|
- email: str
|
|
|
- password: str
|
|
|
- token: Optional[str] = None
|
|
|
-
|
|
|
-class TokenData(BaseModel):
|
|
|
- username: Optional[str] = None
|
|
|
+
|
|
|
|
|
|
@app.get("/index2")
|
|
|
async def index2():
|
|
@@ -176,7 +136,7 @@ async def access_token(form_data: OAuth2PasswordRequestForm = Depends(), Authori
|
|
|
|
|
|
@app.post("/register")
|
|
|
async def register(request: Request):
|
|
|
- user = User(**await request.form())
|
|
|
+ user = models.User(**await request.form())
|
|
|
user_register(user)
|
|
|
return templates.TemplateResponse("login.html", {'request': request,"success": True}, status_code=status.HTTP_302_FOUND)
|
|
|
|
|
@@ -197,7 +157,7 @@ async def avatar():
|
|
|
return FileResponse('static/gen_avatar.html')
|
|
|
|
|
|
@app.post("/swapFace")
|
|
|
-async def swapFace(req:swap_req):
|
|
|
+async def swapFace(req:models.swap_req):
|
|
|
if 'http' not in req.imgurl:
|
|
|
req.imgurl= 'http://'+req.imgurl
|
|
|
try:
|
|
@@ -224,7 +184,7 @@ async def create_upload_file(file: UploadFile = File(...)):
|
|
|
return {"msg": 'www.choozmo.com:8168/'+tmp_img_sub_folder+img_name+'.jpg'}
|
|
|
|
|
|
@app.post("/make_anchor_video_v2")
|
|
|
-async def make_anchor_video_v2(req:request):
|
|
|
+async def make_anchor_video_v2(req:models.request):
|
|
|
if len(req.image_urls) != len(req.text_content):
|
|
|
return {'msg':'副標題數量、圖片(影片)數量以及台詞數量必須一致'}
|
|
|
for idx in range(len(req.image_urls)):
|
|
@@ -249,7 +209,7 @@ async def make_anchor_video_v2(req:request):
|
|
|
return {"msg":"製作影片需要時間,請您耐心等候,成果會傳送至LINE群組中"}
|
|
|
|
|
|
@app.post("/make_anchor_video_eng")
|
|
|
-async def make_anchor_video_eng(req:request_eng):
|
|
|
+async def make_anchor_video_eng(req:models.request_eng):
|
|
|
if len(req.image_urls) != len(req.sub_titles) or len(req.sub_titles) != len(req.text_content):
|
|
|
return {'msg':'副標題數量、圖片(影片)數量以及台詞數量必須一致'}
|
|
|
for idx in range(len(req.image_urls)):
|
|
@@ -282,7 +242,7 @@ async def history_input():
|
|
|
|
|
|
@AuthJWT.load_config
|
|
|
def get_config():
|
|
|
- return Settings()
|
|
|
+ return models.Settings()
|
|
|
|
|
|
@app.exception_handler(AuthJWTException)
|
|
|
def authjwt_exception_handler(request: Request, exc: AuthJWTException):
|
|
@@ -338,10 +298,11 @@ def get_password_hash(password):
|
|
|
def verify_password(plain_password, hashed_password):
|
|
|
return pwd_context.verify(plain_password, hashed_password)
|
|
|
def authenticate_user(username: str, password: str):
|
|
|
+ db = dataset.connect('mysql://choozmo:pAssw0rd@db.ptt.cx:3306/AI_anchor?charset=utf8mb4')
|
|
|
if not check_user_exists(username): # if user don't exist
|
|
|
return False
|
|
|
user_dict = next(iter(db.query('SELECT * FROM AI_anchor.users where username ="'+username+'"')))
|
|
|
- user = User(**user_dict)
|
|
|
+ user = models.User(**user_dict)
|
|
|
if not verify_password(password, user.password):
|
|
|
return False
|
|
|
return user
|