You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

app.py 3.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  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. choices = glob("static/media/{}/*.*".format(topic))
  64. titles = [os.path.basename(c) for c in choices]
  65. try:
  66. descdir = json.load(open("static/media/{}.json".format(topic)))
  67. except FileNotFoundError:
  68. descdir = {}
  69. return {
  70. "type": "string",
  71. "enum": choices,
  72. "options": {
  73. "enum_titles": [descdir.get(t, t.rsplit(".", 1)[0]) for t in titles]
  74. }
  75. }
  76. abort(404)
  77. @application.get("/editor")
  78. def editor():
  79. return render_template("editor.html")
  80. @application.route("/img", methods=['GET', 'POST'])
  81. def image():
  82. if request.method=='GET':
  83. src = "static/img/marble-question-mark.png"
  84. alt = "A question mark"
  85. prompt = ""
  86. else:
  87. prompt = request.form["prompt"]
  88. alt = prompt
  89. response = openai.Image.create(prompt=prompt, n=1, size="1024x1024")
  90. src = response['data'][0]['url']
  91. return render_template("image.html", src=src, alt=alt, prompt=prompt)
  92. @application.route("/favicon.ico")
  93. def favicon():
  94. return send_from_directory(os.path.join(application.root_path, "static"),
  95. "favicon.ico", mimetype="image/vnd.microsoft.icon")