from typing import Optional from fastapi.staticfiles import StaticFiles from fastapi import FastAPI,File, UploadFile,Request,Response import util,os, math, time,queue,threading from pydantic import BaseModel from fastapi.templating import Jinja2Templates import rpyc rootPath = '../composer/' q = queue.Queue() app = FastAPI() templates = Jinja2Templates(directory="static") app.mount("/static", StaticFiles(directory="static"), name="static") class Cast_info: sub_dict = None img_dict = None name_hash = None class updateScriptModel(BaseModel): name_hash:str scriptStr:str class name_hash_in(BaseModel): name_hash:str def tEntry(): while q.qsize!=0: ci = q.get() c = rpyc.connect("localhost", 8838) c._config['sync_request_timeout'] = None remote_svc = c.root result = remote_svc.gen_video(ci.name_hash,ci.sub_dict,ci.img_dict) t1 = threading.Thread(target=tEntry) t1.start() @app.get("/uploadAudio") def uploadAudio(request: Request, response: Response): return templates.TemplateResponse("uploadAudio.html", {"request": request, "response": response}) @app.get("/modifyScript") def modifyScript(request: Request, response: Response): return templates.TemplateResponse("modifyScript.html", {"request": request, "response": response}) @app.post("/updateScript") def updateScript(info : updateScriptModel): lines = info.scriptStr.split(',') cPath = rootPath+info.name_hash+'/' util.rewriteScript(cPath,lines) sub_dict,img_dict=filePrepare(info.name_hash) cinfo = Cast_info() cinfo.name_hash = info.name_hash cinfo.sub_dict cinfo.img_dict item = Item() q.put(item) if not t1.is_alive(): t1 = threading.Thread(target=tEntry) t1.start() return 'ok' @app.post("/uploadmp3/") async def uploadmp3(file: UploadFile = File(...)): name_hash = str(time.time()).replace('.','') cPath = rootPath+name_hash+'/' try: os.mkdir(cPath) except FileExistsError: pass with open(cPath+'speech.mp3', "wb+") as file_object: file_object.write(file.file.read()) util.transScript(cPath) return name_hash @app.post("/get_script/") async def get_script(date_in:name_hash_in): cPath = rootPath+date_in.name_hash+'/' scripts = util.get_script(cPath) return scripts