main.py 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. from typing import Optional
  2. from fastapi.staticfiles import StaticFiles
  3. from fastapi import FastAPI,File, UploadFile,Request,Response
  4. import util,os, math, time,queue,threading
  5. from pydantic import BaseModel
  6. from fastapi.templating import Jinja2Templates
  7. import rpyc
  8. q = queue.Queue()
  9. app = FastAPI()
  10. templates = Jinja2Templates(directory="static")
  11. app.mount("/static", StaticFiles(directory="static"), name="static")
  12. class Cast_info:
  13. sub_dict = None
  14. img_dict = None
  15. name_hash = None
  16. class updateScriptModel(BaseModel):
  17. name_hash:str
  18. scriptStr:str
  19. def tEntry():
  20. while q.qsize!=0:
  21. ci = q.get()
  22. c = rpyc.connect("localhost", 8838)
  23. c._config['sync_request_timeout'] = None
  24. remote_svc = c.root
  25. result = remote_svc.gen_video(ci.name_hash,ci.sub_dict,ci.img_dict)
  26. t1 = threading.Thread(target=tEntry)
  27. t1.start()
  28. rootPath = '../composer/'
  29. @app.get("/uploadAudio")
  30. def uploadAudio(request: Request, response: Response):
  31. return templates.TemplateResponse("uploadmp3.html", {"request": request, "response": response})
  32. @app.get("/modifyScript")
  33. def modifyScript(request: Request, response: Response):
  34. return templates.TemplateResponse("modifyScript.html", {"request": request, "response": response})
  35. @app.post("/updateScript")
  36. def read_item(info : updateScriptModel):
  37. lines = info.scriptStr.split(',')
  38. cPath = rootPath+info.name_hash+'/'
  39. util.rewriteScript(cPath,lines)
  40. sub_dict,img_dict=filePrepare(info.name_hash)
  41. cinfo = Cast_info()
  42. cinfo.name_hash = info.name_hash
  43. cinfo.sub_dict
  44. cinfo.img_dict
  45. item = Item()
  46. q.put(item)
  47. if not t1.is_alive():
  48. t1 = threading.Thread(target=tEntry)
  49. t1.start()
  50. return 'ok'
  51. @app.post("/uploadmp3/")
  52. async def uploadmp3(file: UploadFile = File(...)):
  53. name_hash = str(time.time()).replace('.','')
  54. cPath = rootPath+name_hash+'/'
  55. try:
  56. os.mkdir(cPath)
  57. except FileExistsError:
  58. pass
  59. with open(cPath+'speech.mp3', "wb+") as file_object:
  60. file_object.write(file.file.read())
  61. util.transScript(cPath)
  62. scripts = util.get_script(cPath)
  63. return name_hash