main.py 2.3 KB

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