main.py 32 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711
  1. from fastapi import FastAPI,Cookie, Depends, Query, status,File, UploadFile,Request,Response,HTTPException
  2. from fastapi.templating import Jinja2Templates
  3. from fastapi.responses import HTMLResponse, RedirectResponse, JSONResponse
  4. from pydantic import BaseModel
  5. from typing import List, Optional
  6. from os.path import isfile, isdir, join
  7. import threading
  8. import zhtts
  9. import os
  10. import urllib
  11. import requests
  12. from bs4 import BeautifulSoup
  13. from PIL import Image,ImageDraw,ImageFont
  14. import pyttsx3
  15. import rpyc
  16. import random
  17. import time
  18. import math
  19. import hashlib
  20. import re
  21. import asyncio
  22. import urllib.request
  23. from fastapi.responses import FileResponse
  24. from fastapi.middleware.cors import CORSMiddleware
  25. import dataset
  26. from datetime import datetime, timedelta
  27. from util.swap_face import swap_face
  28. from fastapi.staticfiles import StaticFiles
  29. import shutil
  30. import io
  31. from first import first
  32. from passlib.context import CryptContext
  33. from jose import JWTError, jwt
  34. from fastapi_jwt_auth import AuthJWT
  35. from fastapi_jwt_auth.exceptions import AuthJWTException
  36. from fastapi.security import OAuth2PasswordBearer, OAuth2PasswordRequestForm
  37. import models
  38. import pymysql
  39. from first import first
  40. import mailer
  41. from moviepy.editor import VideoFileClip
  42. import traceback
  43. import logging
  44. import gSlide
  45. import aiofiles
  46. import json
  47. pymysql.install_as_MySQLdb()
  48. app = FastAPI()
  49. app.add_middleware(
  50. CORSMiddleware,
  51. allow_origins=["*"],
  52. allow_credentials=True,
  53. allow_methods=["*"],
  54. allow_headers=["*"],
  55. )
  56. SECRET_KEY = "df2f77bd544240801a048bd4293afd8eeb7fff3cb7050e42c791db4b83ebadcd"
  57. ALGORITHM = "HS256"
  58. ACCESS_TOKEN_EXPIRE_MINUTES = 3000
  59. pwd_context = CryptContext(schemes=["bcrypt"], deprecated="auto")
  60. app.mount("/static", StaticFiles(directory="static"), name="static")
  61. app.mount("/static/img", StaticFiles(directory="static/img"), name="static/img")
  62. app.mount("/html", StaticFiles(directory="templates"), name="templates")
  63. templates = Jinja2Templates(directory="templates")
  64. oauth2_scheme = OAuth2PasswordBearer(tokenUrl="token")
  65. tmp_video_dir = '../OpenshotService/tmp_video/'
  66. tmp_avatar_dir = '../../face_swap/tmp_avatar/' #change source face path here
  67. video_sub_folder = 'ai_anchor_video/'
  68. avatar_sub_folder = 'swap_save/'
  69. tmp_img_sub_folder = 'tmp_img/'
  70. img_upload_folder = '/var/www/html/'+tmp_img_sub_folder
  71. video_dest = '/var/www/html/'+video_sub_folder
  72. avatar_dest = '/var/www/html/'+avatar_sub_folder
  73. # @app.get("/index2")
  74. # async def index2():
  75. # return FileResponse('static/index2.html')
  76. @app.get("/index_eng")
  77. async def index2():
  78. return FileResponse('static/index_eng.html')
  79. # home page
  80. @app.get("/index", response_class=HTMLResponse)
  81. async def get_home_page(request: Request, response: Response):
  82. return FileResponse('../html/index.html')
  83. return templates.TemplateResponse("index.html", {"request": request, "response": response})
  84. @app.get("/", response_class=HTMLResponse)
  85. async def get_home_page(request: Request, response: Response):
  86. return templates.TemplateResponse("index.html", {"request": request, "response": response})
  87. @app.get("/make_video", response_class=HTMLResponse)
  88. async def get_home_page(request: Request, response: Response, Authorize: AuthJWT = Depends()):
  89. print('make_video'); # test
  90. try:
  91. Authorize.jwt_required()
  92. except Exception as e:
  93. print(e)
  94. return '請先登入帳號'
  95. current_user = Authorize.get_jwt_subject()
  96. return templates.TemplateResponse("make_video.html", {"request": request, "response": response})
  97. @app.get("/make_video_long", response_class=HTMLResponse)
  98. async def get_home_page(request: Request, response: Response, Authorize: AuthJWT = Depends()):
  99. try:
  100. Authorize.jwt_required()
  101. except Exception as e:
  102. print(e)
  103. return '請先登入帳號'
  104. current_user = Authorize.get_jwt_subject()
  105. return templates.TemplateResponse("make_video_long.html", {"request": request, "response": response})
  106. @app.get("/make_video_slide", response_class=HTMLResponse)
  107. async def make_video_slide(request: Request, response: Response, Authorize: AuthJWT = Depends()):
  108. try:
  109. Authorize.jwt_required()
  110. except Exception as e:
  111. print(e)
  112. return '請先登入帳號'
  113. current_user = Authorize.get_jwt_subject()
  114. return templates.TemplateResponse("make_video_slide.html", {"request": request, "response": response})
  115. @app.get('/user_profile', response_class=HTMLResponse)
  116. def protected(request: Request, Authorize: AuthJWT = Depends()):
  117. db = dataset.connect('mysql://choozmo:pAssw0rd@db.ptt.cx:3306/AI_anchor?charset=utf8mb4')
  118. Authorize.jwt_required()
  119. current_user = Authorize.get_jwt_subject()
  120. user_obj = first(db.query('SELECT * FROM users where username ="'+str(current_user)+'"'))
  121. video_num = str(first(db.query('SELECT COUNT(*) FROM history_input WHERE user_id ='+str(user_obj['id'])))['COUNT(*)'])
  122. total_sec = str(first(db.query('SELECT SUM(duration) FROM history_input where user_id='+str(user_obj['id'])))['SUM(duration)'])
  123. left_sec = user_obj['left_time']
  124. video_info_list = []
  125. statement = 'SELECT * FROM history_input WHERE user_id='+str(user_obj['id'])
  126. for row in db.query(statement):
  127. video_info_list.append({'id':row['id'],'title':row['name'],'duration':row['duration'],'url':row['url']})
  128. dic_return = {'user_info':{'userName':current_user,'email':user_obj['email'],'video_num':video_num,'total_sec':total_sec,'left_sec':user_obj['left_time']},'video_info':video_info_list}
  129. str_return = json.dumps(dic_return)
  130. return str_return
  131. # login & register page
  132. @app.get("/login", response_class=HTMLResponse)
  133. async def get_login_and_register_page(request: Request):
  134. return templates.TemplateResponse("login.html", {"request": request})
  135. @app.post("/login")
  136. async def login_for_access_token(request: Request, form_data: OAuth2PasswordRequestForm = Depends(), Authorize: AuthJWT = Depends()):
  137. db = dataset.connect('mysql://choozmo:pAssw0rd@db.ptt.cx:3306/AI_anchor?charset=utf8mb4')
  138. user = authenticate_user(form_data.username, form_data.password)
  139. if not user:
  140. raise HTTPException(
  141. status_code=status.HTTP_401_UNAUTHORIZED,
  142. detail="Incorrect username or password",
  143. headers={"WWW-Authenticate": "Bearer"},
  144. )
  145. access_token_expires = timedelta(minutes=ACCESS_TOKEN_EXPIRE_MINUTES)
  146. access_token = create_access_token(
  147. data={"sub": user.username}, expires_delta=access_token_expires
  148. )
  149. table = db['users']
  150. user.token = access_token
  151. table.update(dict(user), ['username'])
  152. access_token = Authorize.create_access_token(subject=user.username)
  153. refresh_token = Authorize.create_refresh_token(subject=user.username)
  154. Authorize.set_access_cookies(access_token)
  155. Authorize.set_refresh_cookies(refresh_token)
  156. #return templates.TemplateResponse("index.html", {"request": request, "msg": 'Login'})
  157. return {"access_token": access_token, "token_type": "bearer", "username": form_data.username}
  158. @app.post("/token")
  159. async def access_token(form_data: OAuth2PasswordRequestForm = Depends(), Authorize: AuthJWT = Depends()):
  160. db = dataset.connect('mysql://choozmo:pAssw0rd@db.ptt.cx:3306/AI_anchor?charset=utf8mb4')
  161. user = authenticate_user(form_data.username, form_data.password)
  162. if not user:
  163. raise HTTPException(
  164. status_code=status.HTTP_401_UNAUTHORIZED,
  165. detail="Incorrect username or password",
  166. headers={"WWW-Authenticate": "Bearer"},
  167. )
  168. access_token_expires = timedelta(minutes=ACCESS_TOKEN_EXPIRE_MINUTES)
  169. access_token = create_access_token(
  170. data={"sub": user.username}, expires_delta=access_token_expires
  171. )
  172. return {"access_token": access_token, "token_type": "bearer"}
  173. @app.post("/register")
  174. async def register(request: Request):
  175. user = models.User(**await request.form())
  176. user_obj = first(db.query('SELECT * FROM users where username ="'+str(user.username)+'"'))
  177. if user_obj != None:
  178. user_register(user)
  179. return templates.TemplateResponse("login.html", {'request': request,"success": True}, status_code=status.HTTP_302_FOUND)
  180. else:
  181. return {'msg':user.username+'重複,請更改'}
  182. @app.get('/logout')
  183. def logout(request: Request, Authorize: AuthJWT = Depends()):
  184. Authorize.jwt_required()
  185. Authorize.unset_jwt_cookies()
  186. return {"msg": "Successfully logout"}
  187. @app.get("/gen_avatar")
  188. async def avatar():
  189. return FileResponse('static/gen_avatar.html')
  190. @app.post("/swapFace")
  191. async def swapFace(req:models.swap_req):
  192. if 'http' not in req.imgurl:
  193. req.imgurl= 'http://'+req.imgurl
  194. try:
  195. im = Image.open(requests.get(req.imgurl, stream=True).raw)
  196. im= im.convert("RGB")
  197. except:
  198. return {'msg':"無法辨別圖片網址"+req.imgurl}
  199. name_hash = str(time.time()).replace('.','')
  200. x = threading.Thread(target=gen_avatar, args=(name_hash,req.imgurl))
  201. x.start()
  202. return {'msg':'人物生成中,請稍候'}
  203. @app.post("/uploadfile/")
  204. async def create_upload_file(file: UploadFile = File(...)):
  205. img_name = str(time.time()).replace('.','')
  206. try:
  207. if file.content_type=='video/mp4':
  208. async with aiofiles.open(img_upload_folder+img_name+'.mp4', 'wb') as out_file:
  209. content = await file.read()
  210. await out_file.write(content)
  211. return {"msg": 'www.choozmo.com:8168/'+tmp_img_sub_folder+img_name+'.mp4'}
  212. else:
  213. contents = await file.read()
  214. image = Image.open(io.BytesIO(contents))
  215. image= image.convert("RGB")
  216. image.save(img_upload_folder+img_name+'.jpg')
  217. return {"msg": 'www.choozmo.com:8168/'+tmp_img_sub_folder+img_name+'.jpg'}
  218. except Exception as e:
  219. logging.error(traceback.format_exc())
  220. return {'msg':'檔案無法使用'}
  221. @app.post("/make_anchor_video_gSlide")
  222. async def make_anchor_video_gSlide(req:models.gSlide_req,token: str = Depends(oauth2_scheme)):
  223. name, text_content, image_urls = gSlide.parse_slide_url(req.slide_url,eng=False)
  224. if len(image_urls) != len(text_content):
  225. return {'msg':'副標題數量、圖片(影片)數量以及台詞數量必須一致'}
  226. for idx in range(len(image_urls)):
  227. if 'http' not in image_urls[idx]:
  228. image_urls[idx] = 'http://'+image_urls[idx]
  229. if req.multiLang==0:
  230. for txt in text_content:
  231. if re.search('[a-zA-Z]', txt) !=None:
  232. print('語言錯誤')
  233. return {'msg':'輸入字串不能包含英文字!'}
  234. name_hash = str(time.time()).replace('.','')
  235. for imgu in image_urls:
  236. try:
  237. if get_url_type(imgu) =='video/mp4':
  238. r=requests.get(imgu)
  239. else:
  240. im = Image.open(requests.get(imgu, stream=True).raw)
  241. im= im.convert("RGB")
  242. except:
  243. return {'msg':"無法辨別圖片網址"+imgu}
  244. user_id = get_user_id(token)
  245. proto_req = models.request_normal()
  246. proto_req.text_content = text_content
  247. proto_req.name = name
  248. proto_req.image_urls = image_urls
  249. proto_req.avatar = req.avatar
  250. proto_req.multiLang = req.multiLang
  251. video_id = save_history(proto_req,name_hash,user_id)
  252. x = threading.Thread(target=gen_video_queue, args=(name_hash,name, text_content, image_urls,int(req.avatar),req.multiLang,video_id,user_id))
  253. x.start()
  254. return {"msg":"製作影片需要時間,請您耐心等候,成果會傳送至LINE群組中"}
  255. @app.post("/make_anchor_video_long")
  256. async def make_anchor_video_long(req:models.request,token: str = Depends(oauth2_scheme)):
  257. left_tag = [m.start() for m in re.finditer('{', req.text_content[0])]
  258. if len(req.image_urls) != len(left_tag):
  259. return {'msg':'副標題數量、圖片(影片)數量以及台詞數量必須一致'}
  260. for idx in range(len(req.image_urls)):
  261. if 'http' not in req.image_urls[idx]:
  262. req.image_urls[idx] = 'http://'+req.image_urls[idx]
  263. if req.multiLang==0:
  264. for txt in req.text_content:
  265. if re.search('[a-zA-Z]', txt) !=None:
  266. print('語言錯誤')
  267. return {'msg':'輸入字串不能包含英文字!'}
  268. name_hash = str(time.time()).replace('.','')
  269. for imgu in req.image_urls:
  270. try:
  271. if get_url_type(imgu) =='video/mp4':
  272. r=requests.get(imgu)
  273. else:
  274. im = Image.open(requests.get(imgu, stream=True).raw)
  275. im= im.convert("RGB")
  276. except:
  277. return {'msg':"無法辨別圖片網址"+imgu}
  278. user_id = get_user_id(token)
  279. video_id = save_history(req,name_hash,user_id)
  280. x = threading.Thread(target=gen_video_long_queue, args=(name_hash,req.name, req.text_content, req.image_urls,int(req.avatar),req.multiLang,video_id,user_id))
  281. x.start()
  282. return {"msg":"ok"}
  283. @app.post("/make_anchor_video")
  284. async def make_anchor_video(req:models.request,token: str = Depends(oauth2_scheme)):
  285. if len(req.image_urls) != len(req.text_content):
  286. return {'msg':'副標題數量、圖片(影片)數量以及台詞數量必須一致'}
  287. for idx in range(len(req.image_urls)):
  288. if 'http' not in req.image_urls[idx]:
  289. req.image_urls[idx] = 'http://'+req.image_urls[idx]
  290. if req.multiLang==0:
  291. for txt in req.text_content:
  292. if re.search('[a-zA-Z]', txt) !=None:
  293. print('語言錯誤')
  294. return {'msg':'輸入字串不能包含英文字!'}
  295. name_hash = str(time.time()).replace('.','')
  296. for imgu in req.image_urls:
  297. try:
  298. if get_url_type(imgu) =='video/mp4':
  299. r=requests.get(imgu)
  300. else:
  301. im = Image.open(requests.get(imgu, stream=True).raw)
  302. im= im.convert("RGB")
  303. except:
  304. return {'msg':"無法辨別圖片網址"+imgu}
  305. user_id = get_user_id(token)
  306. video_id = save_history(req,name_hash,user_id)
  307. x = threading.Thread(target=gen_video_queue, args=(name_hash,req.name, req.text_content, req.image_urls,int(req.avatar),req.multiLang,video_id,user_id))
  308. x.start()
  309. return {'msg':'ok'}
  310. @app.post("/make_anchor_video_eng")
  311. async def make_anchor_video_eng(req:models.request_eng):
  312. if len(req.image_urls) != len(req.sub_titles) or len(req.sub_titles) != len(req.text_content):
  313. return {'msg':'副標題數量、圖片(影片)數量以及台詞數量必須一致'}
  314. for idx in range(len(req.image_urls)):
  315. if 'http' not in req.image_urls[idx]:
  316. req.image_urls[idx] = 'http://'+req.image_urls[idx]
  317. name_hash = str(time.time()).replace('.','')
  318. for imgu in req.image_urls:
  319. try:
  320. if get_url_type(imgu) =='video/mp4':
  321. r=requests.get(imgu)
  322. else:
  323. im = Image.open(requests.get(imgu, stream=True).raw)
  324. im= im.convert("RGB")
  325. except:
  326. return {'msg':"無法辨別圖片網址"+imgu}
  327. video_id = save_history(req,name_hash)
  328. x = threading.Thread(target=gen_video_queue_eng, args=(name_hash,req.name, req.text_content, req.image_urls,req.sub_titles,int(req.avatar),video_id))
  329. x.start()
  330. return {"msg":"ok"}
  331. @app.get("/history_input")
  332. async def history_input(request: Request, Authorize: AuthJWT = Depends()):
  333. Authorize.jwt_required()
  334. current_user = Authorize.get_jwt_subject()
  335. db = dataset.connect('mysql://choozmo:pAssw0rd@db.ptt.cx:3306/AI_anchor?charset=utf8mb4')
  336. user_id = first(db.query('SELECT * FROM users where username="' + current_user +'"'))['id']
  337. statement = 'SELECT * FROM history_input WHERE user_id="'+str(user_id)+'" ORDER BY timestamp DESC LIMIT 50'
  338. logs = []
  339. for row in db.query(statement):
  340. logs.append({'id':row['id'],'name':row['name'],'text_content':row['text_content'].split(','),'link':row['link'],'image_urls':row['image_urls'].split(',')})
  341. return logs
  342. @AuthJWT.load_config
  343. def get_config():
  344. return models.Settings()
  345. @app.exception_handler(AuthJWTException)
  346. def authjwt_exception_handler(request: Request, exc: AuthJWTException):
  347. return JSONResponse(
  348. status_code=exc.status_code,
  349. content={"detail": exc.message}
  350. )
  351. def get_user_id(token):
  352. db = dataset.connect('mysql://choozmo:pAssw0rd@db.ptt.cx:3306/AI_anchor?charset=utf8mb4')
  353. credentials_exception = HTTPException(
  354. status_code=status.HTTP_401_UNAUTHORIZED,
  355. detail="Could not validate credentials",
  356. headers={"WWW-Authenticate": "Bearer"},
  357. )
  358. try:
  359. payload = jwt.decode(token, SECRET_KEY, algorithms=[ALGORITHM])
  360. username: str = payload.get("sub")
  361. if username is None:
  362. raise credentials_exception
  363. token_data = models.TokenData(username=username)
  364. except JWTError:
  365. raise credentials_exception
  366. user = get_user(username=token_data.username)
  367. if user is None:
  368. raise credentials_exception
  369. user_id = first(db.query('SELECT * FROM users where username="' + user.username+'"'))['id']
  370. return user_id
  371. def check_user_exists(username):
  372. db = dataset.connect('mysql://choozmo:pAssw0rd@db.ptt.cx:3306/AI_anchor?charset=utf8mb4')
  373. if int(next(iter(db.query('SELECT COUNT(*) FROM AI_anchor.users WHERE username = "'+username+'"')))['COUNT(*)']) > 0:
  374. return True
  375. else:
  376. return False
  377. def get_user(username: str):
  378. db = dataset.connect('mysql://choozmo:pAssw0rd@db.ptt.cx:3306/AI_anchor?charset=utf8mb4')
  379. if not check_user_exists(username): # if user don't exist
  380. return False
  381. user_dict = next(
  382. iter(db.query('SELECT * FROM AI_anchor.users where username ="'+username+'"')))
  383. user = models.User(**user_dict)
  384. return user
  385. def user_register(user):
  386. db = dataset.connect('mysql://choozmo:pAssw0rd@db.ptt.cx:3306/AI_anchor?charset=utf8mb4')
  387. table = db['users']
  388. user.password = get_password_hash(user.password)
  389. table.insert(dict(user))
  390. def get_password_hash(password):
  391. return pwd_context.hash(password)
  392. def verify_password(plain_password, hashed_password):
  393. return pwd_context.verify(plain_password, hashed_password)
  394. def authenticate_user(username: str, password: str):
  395. db = dataset.connect('mysql://choozmo:pAssw0rd@db.ptt.cx:3306/AI_anchor?charset=utf8mb4')
  396. if not check_user_exists(username): # if user don't exist
  397. return False
  398. user_dict = next(iter(db.query('SELECT * FROM AI_anchor.users where username ="'+username+'"')))
  399. user = models.User(**user_dict)
  400. if not verify_password(password, user.password):
  401. return False
  402. return user
  403. def create_access_token(data: dict, expires_delta: Optional[timedelta] = None):
  404. to_encode = data.copy()
  405. if expires_delta:
  406. expire = datetime.utcnow() + expires_delta
  407. else:
  408. expire = datetime.utcnow() + timedelta(minutes=15)
  409. to_encode.update({"exp": expire})
  410. encoded_jwt = jwt.encode(to_encode, SECRET_KEY, algorithm=ALGORITHM)
  411. return encoded_jwt
  412. def save_history(req,name_hash,user_id):
  413. db = dataset.connect('mysql://choozmo:pAssw0rd@db.ptt.cx:3306/AI_anchor?charset=utf8mb4')
  414. log_table = db['history_input']
  415. txt_content_seperate_by_dot = ''
  416. for txt in req.text_content:
  417. txt_content_seperate_by_dot += txt+","
  418. txt_content_seperate_by_dot = txt_content_seperate_by_dot[:-1]
  419. img_urls_seperate_by_dot = ''
  420. for iurl in req.image_urls:
  421. img_urls_seperate_by_dot += iurl+","
  422. img_urls_seperate_by_dot = img_urls_seperate_by_dot[:-1]
  423. time_stamp = datetime.fromtimestamp(time.time())
  424. time_stamp = time_stamp.strftime("%Y-%m-%d %H:%M:%S")
  425. pk = log_table.insert({'name':req.name,'text_content':txt_content_seperate_by_dot,'image_urls':img_urls_seperate_by_dot
  426. ,'user_id':user_id,'link':'www.choozmo.com:8168/'+video_sub_folder+name_hash+'.mp4','timestamp':time_stamp})
  427. return pk
  428. def get_url_type(url):
  429. req = urllib.request.Request(url, method='HEAD', headers={'User-Agent': 'Mozilla/5.0'})
  430. r = urllib.request.urlopen(req)
  431. contentType = r.getheader('Content-Type')
  432. return contentType
  433. def notify_group(msg):
  434. glist=['7vilzohcyQMPLfAMRloUawiTV4vtusZhxv8Czo7AJX8','WekCRfnAirSiSxALiD6gcm0B56EejsoK89zFbIaiZQD','1dbtJHbWVbrooXmQqc4r8OyRWDryjD4TMJ6DiDsdgsX','HOB1kVNgIb81tTB4Ort1BfhVp9GFo6NlToMQg88vEhh']
  435. for gid in glist:
  436. headers = {"Authorization": "Bearer " + gid,"Content-Type": "application/x-www-form-urlencoded"}
  437. r = requests.post("https://notify-api.line.me/api/notify",headers=headers, params={"message": msg})
  438. def gen_video_long_queue(name_hash,name,text_content, image_urls,avatar,multiLang,video_id,user_id):
  439. db = dataset.connect('mysql://choozmo:pAssw0rd@db.ptt.cx:3306/AI_anchor?charset=utf8mb4')
  440. time_stamp = datetime.fromtimestamp(time.time()).strftime("%Y-%m-%d %H:%M:%S")
  441. txt_content_seperate_by_dot = ''
  442. for txt in text_content:
  443. txt_content_seperate_by_dot += txt+","
  444. txt_content_seperate_by_dot = txt_content_seperate_by_dot[:-1]
  445. img_urls_seperate_by_dot = ''
  446. for iurl in image_urls:
  447. img_urls_seperate_by_dot += iurl+","
  448. img_urls_seperate_by_dot = img_urls_seperate_by_dot[:-1]
  449. db['video_queue'].insert({'name_hash':name_hash,'name':name,'text_content':txt_content_seperate_by_dot,'image_urls':img_urls_seperate_by_dot,'multiLang':multiLang,'avatar':avatar,'timestamp':time_stamp})
  450. while True:
  451. if first(db.query('SELECT * FROM video_queue_status'))['status'] == 1:#only one row in this table, which is the id 1 one
  452. print('another process running, leave loop')#1 means already running
  453. break
  454. if first(db.query('SELECT COUNT(1) FROM video_queue'))['COUNT(1)'] == 0:
  455. print('all finish, leave loop')
  456. break
  457. top1 = first(db.query('SELECT * FROM video_queue'))
  458. try:
  459. # if True:
  460. db.query('UPDATE video_queue_status SET status = 1;')
  461. c = rpyc.connect("localhost", 8858)
  462. c._config['sync_request_timeout'] = None
  463. remote_svc = c.root
  464. my_answer = remote_svc.call_video_gen(top1['name_hash'],top1['name'],top1['text_content'].split(','), top1['image_urls'].split(','),top1['multiLang'],top1['avatar']) # method call
  465. shutil.copy(tmp_video_dir+top1['name_hash']+'.mp4',video_dest+top1['name_hash']+'.mp4')
  466. os.remove(tmp_video_dir+top1['name_hash']+'.mp4')
  467. vid_duration = VideoFileClip(video_dest+top1['name_hash']+'.mp4').duration
  468. user_obj = first(db.query('SELECT * FROM users where id ="'+str(user_id)+'"'))
  469. line_token = user_obj['line_token'] # aa
  470. left_time = user_obj['left_time']
  471. email = user_obj['email']
  472. print('left_time is '+str(left_time))
  473. db.query('UPDATE history_input SET duration ='+str(vid_duration)+' WHERE id='+str(video_id)+';')
  474. if left_time is None:
  475. left_time = 5*60
  476. if left_time < vid_duration:
  477. msg = '您本月額度剩下'+str(left_time)+'秒,此部影片有'+str(vid_duration)+'秒, 若要繼續產生影片請至 192.168.1.106:8887/confirm_add_value?name_hash='+name_hash+' 加值'
  478. print(msg)
  479. msg =msg.encode(encoding='utf-8')
  480. mailer.send_left_not_enough(msg, email)
  481. #notify_line_user(msg, line_token)
  482. notify_group(name+":帳號餘額不足,請至email查看詳細資訊")
  483. else:
  484. left_time = left_time - vid_duration
  485. db.query('UPDATE users SET left_time ='+str(left_time)+' WHERE id='+str(user_id)+';')
  486. notify_group(name+"的影片已經產生完成囉! www.choozmo.com:8168/"+video_sub_folder+name_hash+".mp4")
  487. #notify_line_user(name+"的影片已經產生完成囉! www.choozmo.com:8168/"+video_sub_folder+name_hash+".mp4", line_token)
  488. except Exception as e:
  489. logging.error(traceback.format_exc())
  490. print('video generation error')
  491. #notify_group('長影片錯誤-測試')
  492. db['video_queue'].delete(id=top1['id'])
  493. db.query('UPDATE video_queue_status SET status = 0')
  494. def gen_video_queue(name_hash,name,text_content, image_urls,avatar,multiLang,video_id,user_id):
  495. db = dataset.connect('mysql://choozmo:pAssw0rd@db.ptt.cx:3306/AI_anchor?charset=utf8mb4')
  496. time_stamp = datetime.fromtimestamp(time.time()).strftime("%Y-%m-%d %H:%M:%S")
  497. txt_content_seperate_by_dot = ''
  498. for txt in text_content:
  499. txt_content_seperate_by_dot += txt+","
  500. txt_content_seperate_by_dot = txt_content_seperate_by_dot[:-1]
  501. img_urls_seperate_by_dot = ''
  502. for iurl in image_urls:
  503. img_urls_seperate_by_dot += iurl+","
  504. img_urls_seperate_by_dot = img_urls_seperate_by_dot[:-1]
  505. db['video_queue'].insert({'name_hash':name_hash,'name':name,'text_content':txt_content_seperate_by_dot,'image_urls':img_urls_seperate_by_dot,'multiLang':multiLang,'avatar':avatar,'timestamp':time_stamp})
  506. while True:
  507. if first(db.query('SELECT * FROM video_queue_status'))['status'] == 1:#only one row in this table, which is the id 1 one
  508. print('another process running, leave loop')#1 means already running
  509. break
  510. if first(db.query('SELECT COUNT(1) FROM video_queue'))['COUNT(1)'] == 0:
  511. print('all finish, leave loop')
  512. break
  513. top1 = first(db.query('SELECT * FROM video_queue'))
  514. try:
  515. # if True:
  516. db.query('UPDATE video_queue_status SET status = 1;')
  517. c = rpyc.connect("localhost", 8858)
  518. c._config['sync_request_timeout'] = None
  519. remote_svc = c.root
  520. my_answer = remote_svc.call_video(top1['name_hash'],top1['name'],top1['text_content'].split(','), top1['image_urls'].split(','),top1['multiLang'],top1['avatar']) # method call
  521. shutil.copy(tmp_video_dir+top1['name_hash']+'.mp4',video_dest+top1['name_hash']+'.mp4')
  522. os.remove(tmp_video_dir+top1['name_hash']+'.mp4')
  523. vid_duration = VideoFileClip(video_dest+top1['name_hash']+'.mp4').duration
  524. user_obj = first(db.query('SELECT * FROM users where id ="'+str(user_id)+'"'))
  525. line_token = user_obj['line_token'] # aa
  526. left_time = user_obj['left_time']
  527. email = user_obj['email']
  528. print('left_time is '+str(left_time))
  529. db.query('UPDATE history_input SET duration ='+str(vid_duration)+' WHERE id='+str(video_id)+';')
  530. if left_time is None:
  531. left_time = 5*60
  532. if left_time < vid_duration:
  533. msg = '您本月額度剩下'+str(left_time)+'秒,此部影片有'+str(vid_duration)+'秒, 若要繼續產生影片請至 192.168.1.106:8887/confirm_add_value?name_hash='+name_hash+' 加值'
  534. print(msg)
  535. msg =msg.encode(encoding='utf-8')
  536. mailer.send_left_not_enough(msg, email)
  537. notify_group(msg)
  538. #notify_line_user(msg, line_token)
  539. else:
  540. left_time = left_time - vid_duration
  541. db.query('UPDATE users SET left_time ='+str(left_time)+' WHERE id='+str(user_id)+';')
  542. notify_group(name+"的影片已經產生完成囉! www.choozmo.com:8168/"+video_sub_folder+name_hash+".mp4")
  543. #notify_line_user(name+"的影片已經產生完成囉! www.choozmo.com:8168/"+video_sub_folder+name_hash+".mp4", line_token)
  544. except Exception as e:
  545. logging.error(traceback.format_exc())
  546. print('video generation error')
  547. notify_group('影片錯誤')
  548. db['video_queue'].delete(id=top1['id'])
  549. db.query('UPDATE video_queue_status SET status = 0')
  550. def gen_video_queue_eng(name_hash,name,text_content, image_urls,sub_titles,avatar,video_id):
  551. db = dataset.connect('mysql://choozmo:pAssw0rd@db.ptt.cx:3306/AI_anchor?charset=utf8mb4')
  552. time_stamp = datetime.fromtimestamp(time.time()).strftime("%Y-%m-%d %H:%M:%S")
  553. txt_content_seperate_by_dot = ''
  554. for txt in text_content:
  555. txt_content_seperate_by_dot += txt+","
  556. txt_content_seperate_by_dot = txt_content_seperate_by_dot[:-1]
  557. img_urls_seperate_by_dot = ''
  558. for iurl in image_urls:
  559. img_urls_seperate_by_dot += iurl+","
  560. img_urls_seperate_by_dot = img_urls_seperate_by_dot[:-1]
  561. subtitles_seperate_by_dot = ''
  562. for sub in sub_titles:
  563. subtitles_seperate_by_dot += sub+","
  564. subtitles_seperate_by_dot = subtitles_seperate_by_dot[:-1]
  565. db['video_queue'].insert({'name_hash':name_hash,'name':name,'text_content':txt_content_seperate_by_dot,'image_urls':img_urls_seperate_by_dot,'subtitles':subtitles_seperate_by_dot,'avatar':avatar,'timestamp':time_stamp})
  566. while True:
  567. if first(db.query('SELECT * FROM video_queue_status'))['status'] == 1:#only one row in this table, which is the id 1 one
  568. print('another process running, leave loop')
  569. break
  570. if first(db.query('SELECT COUNT(1) FROM video_queue'))['COUNT(1)'] == 0:
  571. print('all finish, leave loop')
  572. break
  573. top1 = first(db.query('SELECT * FROM video_queue'))
  574. try:
  575. db.query('UPDATE video_queue_status SET status = 1;')
  576. c = rpyc.connect("localhost", 8858)
  577. c._config['sync_request_timeout'] = None
  578. remote_svc = c.root
  579. my_answer = remote_svc.call_video_eng(top1['name_hash'],top1['name'],top1['text_content'].split(','), top1['image_urls'].split(','),top1['subtitles'].split(','),top1['avatar']) # method call
  580. shutil.copy(tmp_video_dir+top1['name_hash']+'.mp4',video_dest+top1['name_hash']+'.mp4')
  581. os.remove(tmp_video_dir+top1['name_hash']+'.mp4')
  582. notify_group(name+"(ENG)的影片已經產生完成囉! www.choozmo.com:8168/"+video_sub_folder+name_hash+".mp4")
  583. except:
  584. print('video generation error')
  585. notify_group('影片錯誤')
  586. db['video_queue'].delete(id=top1['id'])
  587. db.query('UPDATE video_queue_status SET status = 0')
  588. def gen_avatar(name_hash, imgurl):
  589. db = dataset.connect('mysql://choozmo:pAssw0rd@db.ptt.cx:3306/AI_anchor?charset=utf8mb4')
  590. db['avatar_queue'].insert({'name_hash':name_hash,'imgurl':imgurl})
  591. while True:
  592. statement = 'SELECT * FROM avatar_service_status'#only one row in this table, which is the id 1 one
  593. status = -1
  594. for row in db.query(statement):
  595. status = row['status']
  596. if status == 1:
  597. print('leave process loop')
  598. break
  599. statement = 'SELECT * FROM avatar_queue'
  600. works = []
  601. for row in db.query(statement):
  602. works.append({'id':row['id'],'name_hash':row['name_hash'],'imgurl':row['imgurl']})
  603. if len(works)==0:
  604. print('leave process loop')
  605. break
  606. try:
  607. statement = 'UPDATE avatar_service_status SET status = 1 WHERE id=1;'
  608. db.query(statement)
  609. name_hash = works[0]['name_hash']
  610. imgurl = works[0]['imgurl']
  611. c = rpyc.connect("localhost", 8868)
  612. c._config['sync_request_timeout'] = None
  613. remote_svc = c.root
  614. my_answer = remote_svc.call_avatar(name_hash,imgurl) # method call
  615. shutil.copy(tmp_avatar_dir+name_hash+'.mp4',avatar_dest+name_hash+'.mp4')
  616. os.remove(tmp_avatar_dir+name_hash+'.mp4')
  617. except:
  618. print('gen error')
  619. notify_group('無法辨識人臉')
  620. db['avatar_queue'].delete(id=works[0]['id'])
  621. statement = 'UPDATE avatar_service_status SET status = 0 WHERE id=1;' #only one row in this table, which id 1 one
  622. db.query(statement)
  623. def call_voice(text):
  624. print(text)
  625. print(len(text))
  626. print(type(text))
  627. c = rpyc.connect("localhost", 8858)
  628. c._config['sync_request_timeout'] = None
  629. remote_svc = c.root
  630. my_answer = remote_svc.make_speech(text) # method call
  631. src_path = '/home/ming/AI_Anchor/OpenshotService/speech.mp3'
  632. shutil.copy(src_path,'/home/ming/speech.mp3')
  633. os.remove(src_path)
  634. class text_in(BaseModel):
  635. text: str
  636. @app.post("/make_voice")
  637. async def make_voice(in_text:text_in):
  638. x = threading.Thread(target=call_voice, args=(in_text.text,))
  639. x.start()