Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

app.py 3.3KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. from flask import Flask, render_template, request, abort, send_from_directory
  2. import openai
  3. from dotenv import load_dotenv
  4. from markdown import markdown
  5. from glob import glob
  6. import json
  7. import os
  8. import sys
  9. import time
  10. import shutil
  11. import re
  12. RE_VID = re.compile("""\[video ["']([^['"]*?)["']\]""")
  13. TEMPLATE_VID = """<video controls class="centered"><source src="{}" type="video/{}"></video>"""
  14. def preprocess_content(defs):
  15. md = defs.get("markdown", "")
  16. if md:
  17. content = markdown(md)
  18. content = RE_VID.sub(lambda m: TEMPLATE_VID.format(m.group(1), m.group(1).split('.')[-1]), content)
  19. else:
  20. content = ""
  21. img = defs.get("image", {})
  22. match img.get("placement", "none"):
  23. case "above":
  24. content = """<img src="{source}" alt="{alt}" class="centered w-{width}"/><br/>\n""".format(**img)+content
  25. case "left":
  26. content = """<img src="{source}" alt="{alt}" class="float-left w-{width}"/><br/>\n""".format(**img)+content
  27. case "right":
  28. content = """<img src="{source}" alt="{alt}" class="float-right w-{width}"/><br/>\n""".format(**img)+content
  29. case "below":
  30. content = content+"""<br/>\n<img src="{source}" alt="{alt}" class="centered w-{width}"/><br/>\n""".format(**img)
  31. if defs.get('soundtrack'):
  32. content = """<audio loop data-autoplay><source src="{}" type="audio/{}"></audio>\n""".format(
  33. defs["soundtrack"], defs["soundtrack"].split(".")[-1])+content
  34. return content
  35. def preprocess_payload(payload):
  36. for column in payload.get("columns", []):
  37. column["content"] = preprocess_content(column)
  38. for slide in column.get("slides", []):
  39. slide["content"] = preprocess_content(slide)
  40. load_dotenv()
  41. openai.organization = "org-GFWgNyt7NSKpCv6GhzXYZTpi"
  42. application = Flask(__name__)
  43. @application.get("/")
  44. def home():
  45. payload = json.load(open("static/slides.json"))
  46. preprocess_payload(payload)
  47. return render_template("slides.html", generate_indices=False, **payload)
  48. @application.post("/update")
  49. def update():
  50. shutil.copy(
  51. "static/slides.json",
  52. time.strftime(
  53. "archive/slides-%Y-%m-%d-%H.%M.%S.json",
  54. time.localtime()))
  55. payload = request.get_json()
  56. print(type(payload))
  57. json.dump(payload, sys.stdout, indent=4)
  58. json.dump(payload, open("static/slides.json", "w"), indent=4)
  59. return {"status": "success"}
  60. @application.get("/enum/<topic>")
  61. def choices(topic):
  62. if topic in ["img", "bg", "bg-video", "audio"]:
  63. return {"type": "string", "enum": glob("static/{}/*.*".format(topic))}
  64. abort(404)
  65. @application.get("/editor")
  66. def editor():
  67. return render_template("editor.html")
  68. @application.route("/img", methods=['GET', 'POST'])
  69. def image():
  70. if request.method=='GET':
  71. src = "static/img/marble-question-mark.png"
  72. alt = "A question mark"
  73. prompt = ""
  74. else:
  75. prompt = request.form["prompt"]
  76. alt = prompt
  77. response = openai.Image.create(prompt=prompt, n=1, size="1024x1024")
  78. src = response['data'][0]['url']
  79. return render_template("image.html", src=src, alt=alt, prompt=prompt)
  80. @application.route("/favicon.ico")
  81. def favicon():
  82. return send_from_directory(os.path.join(application.root_path, "static"),
  83. "favicon.ico", mimetype="image/vnd.microsoft.icon")