main.py 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361
  1. from fastapi import FastAPI,Cookie, Depends, FastAPI, Query, WebSocket, status, WebSocketDisconnect,File, UploadFile
  2. from os import listdir
  3. from os.path import isfile, isdir, join
  4. import threading
  5. import zhtts
  6. import os
  7. import urllib
  8. from typing import List
  9. import requests
  10. from pydantic import BaseModel
  11. from bs4 import BeautifulSoup
  12. from PIL import Image,ImageDraw,ImageFont
  13. import pyttsx3
  14. import rpyc
  15. import random
  16. import time
  17. import math
  18. import hashlib
  19. import re
  20. import asyncio
  21. import urllib.request
  22. from fastapi.responses import FileResponse
  23. from websocket import create_connection
  24. from fastapi.middleware.cors import CORSMiddleware
  25. import dataset
  26. from datetime import datetime
  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. import traceback
  33. import logging
  34. #test
  35. app = FastAPI()
  36. origins = [
  37. "https://hhh.com.tw"
  38. "http://172.105.205.52",
  39. "http://172.105.205.52:8001",
  40. "http://172.104.93.163",
  41. ]
  42. app.add_middleware(
  43. CORSMiddleware,
  44. # allow_origins=origins,
  45. allow_origins=["*"],
  46. allow_credentials=True,
  47. allow_methods=["*"],
  48. allow_headers=["*"],
  49. )
  50. app.mount("/static", StaticFiles(directory="static"), name="static")
  51. app.mount("/static/img", StaticFiles(directory="static/img"), name="static/img")
  52. tmp_video_dir = '../OpenshotService/tmp_video/'
  53. tmp_avatar_dir = '../../face_swap/tmp_avatar/' #change source face path here
  54. video_sub_folder = 'ai_anchor_video/'
  55. avatar_sub_folder = 'swap_save/'
  56. tmp_img_sub_folder = 'tmp_img/'
  57. img_upload_folder = '/var/www/html/'+tmp_img_sub_folder
  58. video_dest = '/var/www/html/'+video_sub_folder
  59. avatar_dest = '/var/www/html/'+avatar_sub_folder
  60. class swap_req(BaseModel):
  61. imgurl: str
  62. class request(BaseModel):
  63. name: str
  64. text_content: List[str]
  65. image_urls: List[str]
  66. avatar: str
  67. client_id :str
  68. class request_eng(BaseModel):
  69. name: str
  70. text_content: List[str]
  71. image_urls: List[str]
  72. sub_titles: List[str]
  73. avatar: str
  74. client_id :str
  75. @app.get("/index2")
  76. async def index2():
  77. return FileResponse('static/index2.html')
  78. @app.get("/index_eng")
  79. async def index2():
  80. return FileResponse('static/index_eng.html')
  81. @app.get("/gen_avatar")
  82. async def avatar():
  83. return FileResponse('static/gen_avatar.html')
  84. @app.post("/swapFace")
  85. async def swapFace(req:swap_req):
  86. '''
  87. sf = swap_face(req.imgurl)
  88. result = sf.run()
  89. #notify_group(result)hi
  90. '''
  91. if 'http' not in req.imgurl:
  92. req.imgurl= 'http://'+req.imgurl
  93. try:
  94. im = Image.open(requests.get(req.imgurl, stream=True).raw)
  95. im= im.convert("RGB")
  96. except:
  97. return {'msg':"無法辨別圖片網址"+req.imgurl}
  98. name_hash = str(time.time()).replace('.','')
  99. x = threading.Thread(target=gen_avatar, args=(name_hash,req.imgurl))
  100. x.start()
  101. return {'msg':'人物生成中,請稍候'}
  102. @app.post("/uploadfile/")
  103. async def create_upload_file(file: UploadFile = File(...)):
  104. img_name = str(time.time()).replace('.','')
  105. try:
  106. if file.content_type=='video/mp4':
  107. async with aiofiles.open(img_upload_folder+img_name+'.mp4', 'wb') as out_file:
  108. content = await file.read()
  109. await out_file.write(content)
  110. else:
  111. contents = await file.read()
  112. image = Image.open(io.BytesIO(contents))
  113. image= image.convert("RGB")
  114. image.save(img_upload_folder+img_name+'.jpg')
  115. except Exception as e:
  116. logging.error(traceback.format_exc())
  117. return {'msg':'檔案無法使用'}
  118. return {"msg": 'www.choozmo.com:8168/'+tmp_img_sub_folder+img_name+'.jpg'}
  119. @app.post("/make_anchor_video_v2")
  120. async def make_anchor_video_v2(req:request):
  121. if len(req.image_urls) != len(req.text_content):
  122. return {'msg':'副標題數量、圖片(影片)數量以及台詞數量必須一致'}
  123. for idx in range(len(req.image_urls)):
  124. if 'http' not in req.image_urls[idx]:
  125. req.image_urls[idx] = 'http://'+req.image_urls[idx]
  126. for txt in req.text_content:
  127. if re.search('[a-zA-Z]', txt) !=None:
  128. return {'msg':'輸入字串不能包含英文字!'}
  129. name_hash = str(time.time()).replace('.','')
  130. for imgu in req.image_urls:
  131. try:
  132. if get_url_type(imgu) =='video/mp4':
  133. r=requests.get(imgu)
  134. else:
  135. im = Image.open(requests.get(imgu, stream=True).raw)
  136. im= im.convert("RGB")
  137. except:
  138. return {'msg':"無法辨別圖片網址"+imgu}
  139. save_history(req,name_hash)
  140. x = threading.Thread(target=gen_video_queue, args=(name_hash,req.name, req.text_content, req.image_urls,int(req.avatar)))
  141. x.start()
  142. return {"msg":"製作影片需要時間,請您耐心等候,成果會傳送至LINE群組中"}
  143. @app.post("/make_anchor_video_eng")
  144. async def make_anchor_video_eng(req:request_eng):
  145. if len(req.image_urls) != len(req.sub_titles) or len(req.sub_titles) != len(req.text_content):
  146. return {'msg':'副標題數量、圖片(影片)數量以及台詞數量必須一致'}
  147. for idx in range(len(req.image_urls)):
  148. if 'http' not in req.image_urls[idx]:
  149. req.image_urls[idx] = 'http://'+req.image_urls[idx]
  150. name_hash = str(time.time()).replace('.','')
  151. for imgu in req.image_urls:
  152. try:
  153. if get_url_type(imgu) =='video/mp4':
  154. r=requests.get(imgu)
  155. else:
  156. im = Image.open(requests.get(imgu, stream=True).raw)
  157. im= im.convert("RGB")
  158. except:
  159. return {'msg':"無法辨別圖片網址"+imgu}
  160. save_history(req,name_hash)
  161. 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)))
  162. x.start()
  163. return {"msg":"製作影片需要時間,請您耐心等候,成果會傳送至LINE群組中"}
  164. @app.get("/history_input")
  165. async def history_input():
  166. db = dataset.connect('mysql://choozmo:pAssw0rd@db.ptt.cx:3306/AI_anchor?charset=utf8mb4')
  167. statement = 'SELECT * FROM history_input ORDER BY timestamp DESC LIMIT 50'
  168. statement = 'SELECT * FROM history_input ORDER BY timestamp DESC'
  169. logs = []
  170. for row in db.query(statement):
  171. logs.append({'id':row['id'],'name':row['name'],'text_content':row['text_content'].split(','),'link':row['link'],'image_urls':row['image_urls'].split(',')})
  172. return logs
  173. def save_history(req,name_hash):
  174. db = dataset.connect('mysql://choozmo:pAssw0rd@db.ptt.cx:3306/AI_anchor?charset=utf8mb4')
  175. log_table = db['history_input']
  176. txt_content_seperate_by_dot = ''
  177. for txt in req.text_content:
  178. txt_content_seperate_by_dot += txt+","
  179. txt_content_seperate_by_dot = txt_content_seperate_by_dot[:-1]
  180. img_urls_seperate_by_dot = ''
  181. for iurl in req.image_urls:
  182. img_urls_seperate_by_dot += iurl+","
  183. img_urls_seperate_by_dot = img_urls_seperate_by_dot[:-1]
  184. time_stamp = datetime.fromtimestamp(time.time())
  185. time_stamp = time_stamp.strftime("%Y-%m-%d %H:%M:%S")
  186. pk = log_table.insert({'name':req.name,'text_content':txt_content_seperate_by_dot,'image_urls':img_urls_seperate_by_dot,'link':'www.choozmo.com:8168/'+video_sub_folder+name_hash+'.mp4','timestamp':time_stamp})
  187. def get_url_type(url):
  188. req = urllib.request.Request(url, method='HEAD', headers={'User-Agent': 'Mozilla/5.0'})
  189. r = urllib.request.urlopen(req)
  190. contentType = r.getheader('Content-Type')
  191. return contentType
  192. def notify_group(msg):
  193. glist=['WekCRfnAirSiSxALiD6gcm0B56EejsoK89zFbIaiZQD']
  194. for gid in glist:
  195. headers = {
  196. "Authorization": "Bearer " + gid,
  197. "Content-Type": "application/x-www-form-urlencoded"
  198. }
  199. params = {"message": msg}
  200. r = requests.post("https://notify-api.line.me/api/notify",headers=headers, params=params)
  201. def gen_video(name_hash,name,text_content, image_urls,avatar):
  202. c = rpyc.connect("localhost", 8878)
  203. c._config['sync_request_timeout'] = None
  204. remote_svc = c.root
  205. my_answer = remote_svc.call_video(name_hash,name,text_content, image_urls,avatar) # method call
  206. shutil.copy(tmp_video_dir+name_hash+'.mp4',video_dest+name_hash+'.mp4')
  207. os.remove(tmp_video_dir+name_hash+'.mp4')
  208. def gen_video_eng(name_hash,name,text_content, image_urls,sub_titles,avatar):
  209. c = rpyc.connect("localhost", 8878)
  210. c._config['sync_request_timeout'] = None
  211. remote_svc = c.root
  212. my_answer = remote_svc.call_video_eng(name_hash,name,text_content, image_urls,sub_titles,avatar) # method call
  213. shutil.copy(tmp_video_dir+name_hash+'.mp4',video_dest+name_hash+'.mp4')
  214. os.remove(tmp_video_dir+name_hash+'.mp4')
  215. def gen_video_queue(name_hash,name,text_content, image_urls,avatar):
  216. db = dataset.connect('mysql://choozmo:pAssw0rd@db.ptt.cx:3306/AI_anchor?charset=utf8mb4')
  217. time_stamp = datetime.fromtimestamp(time.time()).strftime("%Y-%m-%d %H:%M:%S")
  218. txt_content_seperate_by_dot = ''
  219. for txt in text_content:
  220. txt_content_seperate_by_dot += txt+","
  221. txt_content_seperate_by_dot = txt_content_seperate_by_dot[:-1]
  222. img_urls_seperate_by_dot = ''
  223. for iurl in image_urls:
  224. img_urls_seperate_by_dot += iurl+","
  225. img_urls_seperate_by_dot = img_urls_seperate_by_dot[:-1]
  226. db['video_queue'].insert({'name_hash':name_hash,'name':name,'text_content':txt_content_seperate_by_dot,'image_urls':img_urls_seperate_by_dot,'avatar':avatar,'timestamp':time_stamp})
  227. while True:
  228. if first(db.query('SELECT * FROM video_queue_status'))['status'] == 1:#only one row in this table, which is the id 1 one
  229. print('another process running, leave loop')#1 means already running
  230. break
  231. if first(db.query('SELECT COUNT(1) FROM video_queue'))['COUNT(1)'] == 0:
  232. print('all finish, leave loop')
  233. break
  234. top1 = first(db.query('SELECT * FROM video_queue'))
  235. try:
  236. #if True:
  237. db.query('UPDATE video_queue_status SET status = 1;')
  238. c = rpyc.connect("localhost", 8878)
  239. c._config['sync_request_timeout'] = None
  240. remote_svc = c.root
  241. my_answer = remote_svc.call_video(top1['name_hash'],top1['name'],top1['text_content'].split(','), top1['image_urls'].split(','),top1['avatar']) # method call
  242. shutil.copy(tmp_video_dir+top1['name_hash']+'.mp4',video_dest+top1['name_hash']+'.mp4')
  243. os.remove(tmp_video_dir+top1['name_hash']+'.mp4')
  244. except Exception as e:
  245. logging.error(traceback.format_exc())
  246. print('video generation error')
  247. notify_group('影片錯誤')
  248. db['video_queue'].delete(id=top1['id'])
  249. db.query('UPDATE video_queue_status SET status = 0')
  250. def gen_video_queue_eng(name_hash,name,text_content, image_urls,sub_titles,avatar):
  251. db = dataset.connect('mysql://choozmo:pAssw0rd@db.ptt.cx:3306/AI_anchor?charset=utf8mb4')
  252. time_stamp = datetime.fromtimestamp(time.time()).strftime("%Y-%m-%d %H:%M:%S")
  253. txt_content_seperate_by_dot = ''
  254. for txt in text_content:
  255. txt_content_seperate_by_dot += txt+",%"
  256. txt_content_seperate_by_dot = txt_content_seperate_by_dot[:-2]
  257. img_urls_seperate_by_dot = ''
  258. for iurl in image_urls:
  259. img_urls_seperate_by_dot += iurl+","
  260. img_urls_seperate_by_dot = img_urls_seperate_by_dot[:-1]
  261. subtitles_seperate_by_dot = ''
  262. for sub in sub_titles:
  263. subtitles_seperate_by_dot += sub+",%"
  264. subtitles_seperate_by_dot = subtitles_seperate_by_dot[:-2]
  265. 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})
  266. while True:
  267. if first(db.query('SELECT * FROM video_queue_status'))['status'] == 1:#only one row in this table, which is the id 1 one
  268. print('another process running, leave loop')
  269. break
  270. if first(db.query('SELECT COUNT(1) FROM video_queue'))['COUNT(1)'] == 0:
  271. print('all finish, leave loop')
  272. break
  273. top1 = first(db.query('SELECT * FROM video_queue'))
  274. try:
  275. db.query('UPDATE video_queue_status SET status = 1;')
  276. c = rpyc.connect("localhost", 8878)
  277. c._config['sync_request_timeout'] = None
  278. remote_svc = c.root
  279. 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
  280. shutil.copy(tmp_video_dir+top1['name_hash']+'.mp4',video_dest+top1['name_hash']+'.mp4')
  281. os.remove(tmp_video_dir+top1['name_hash']+'.mp4')
  282. except Exception as e:
  283. logging.error(traceback.format_exc())
  284. print('video generation error')
  285. notify_group('影片錯誤')
  286. db['video_queue'].delete(id=top1['id'])
  287. db.query('UPDATE video_queue_status SET status = 0')
  288. def gen_avatar(name_hash, imgurl):
  289. db = dataset.connect('mysql://choozmo:pAssw0rd@db.ptt.cx:3306/AI_anchor?charset=utf8mb4')
  290. db['avatar_queue'].insert({'name_hash':name_hash,'imgurl':imgurl})
  291. while True:
  292. statement = 'SELECT * FROM avatar_service_status'#only one row in this table, which is the id 1 one
  293. status = -1
  294. for row in db.query(statement):
  295. status = row['status']
  296. if status == 1:
  297. print('leave process loop')
  298. break
  299. statement = 'SELECT * FROM avatar_queue'
  300. works = []
  301. for row in db.query(statement):
  302. works.append({'id':row['id'],'name_hash':row['name_hash'],'imgurl':row['imgurl']})
  303. if len(works)==0:
  304. print('leave process loop')
  305. break
  306. try:
  307. statement = 'UPDATE avatar_service_status SET status = 1 WHERE id=1;'
  308. db.query(statement)
  309. name_hash = works[0]['name_hash']
  310. imgurl = works[0]['imgurl']
  311. c = rpyc.connect("localhost", 8868)
  312. c._config['sync_request_timeout'] = None
  313. remote_svc = c.root
  314. my_answer = remote_svc.call_avatar(name_hash,imgurl) # method call
  315. shutil.copy(tmp_avatar_dir+name_hash+'.mp4',avatar_dest+name_hash+'.mp4')
  316. os.remove(tmp_avatar_dir+name_hash+'.mp4')
  317. except:
  318. print('gen error')
  319. notify_group('無法辨識人臉')
  320. db['avatar_queue'].delete(id=works[0]['id'])
  321. statement = 'UPDATE avatar_service_status SET status = 0 WHERE id=1;' #only one row in this table, which id 1 one
  322. db.query(statement)