Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,37 +1,40 @@
|
|
| 1 |
-
|
| 2 |
-
import
|
|
|
|
|
|
|
| 3 |
|
| 4 |
app = Flask(__name__)
|
| 5 |
|
|
|
|
| 6 |
URL_TEMPLATE = os.environ.get("FLUX")
|
| 7 |
if not URL_TEMPLATE:
|
| 8 |
raise RuntimeError("Missing FLUX env variable")
|
| 9 |
|
|
|
|
|
|
|
| 10 |
@app.route("/", methods=["GET"])
|
| 11 |
def generate_or_default():
|
| 12 |
-
#
|
| 13 |
if not request.query_string:
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
as_attachment=False,
|
| 19 |
-
download_name="default.png"
|
| 20 |
-
)
|
| 21 |
|
| 22 |
-
# Otherwise, generate
|
| 23 |
prompt = request.args.get(
|
| 24 |
"prompt",
|
| 25 |
-
"a glowing board with the word '
|
| 26 |
)
|
| 27 |
width = request.args.get("width", "1024")
|
| 28 |
height = request.args.get("height", "1024")
|
| 29 |
seed = request.args.get("seed", "0")
|
| 30 |
|
| 31 |
-
|
|
|
|
| 32 |
url = (
|
| 33 |
URL_TEMPLATE
|
| 34 |
-
.replace("[prompt]",
|
| 35 |
.replace("[w]", width)
|
| 36 |
.replace("[h]", height)
|
| 37 |
.replace("[seed]", seed)
|
|
@@ -40,12 +43,11 @@ def generate_or_default():
|
|
| 40 |
resp = requests.get(url, stream=True)
|
| 41 |
if resp.status_code != 200:
|
| 42 |
return Response(
|
| 43 |
-
f"
|
| 44 |
status=502
|
| 45 |
)
|
| 46 |
|
| 47 |
return Response(resp.content, content_type=resp.headers.get("Content-Type"))
|
| 48 |
|
| 49 |
if __name__ == "__main__":
|
| 50 |
-
# Dev server
|
| 51 |
app.run(host="0.0.0.0", port=7860)
|
|
|
|
| 1 |
+
import os
|
| 2 |
+
import urllib.parse
|
| 3 |
+
import requests
|
| 4 |
+
from flask import Flask, Response, request, send_file, abort
|
| 5 |
|
| 6 |
app = Flask(__name__)
|
| 7 |
|
| 8 |
+
# URL template from env
|
| 9 |
URL_TEMPLATE = os.environ.get("FLUX")
|
| 10 |
if not URL_TEMPLATE:
|
| 11 |
raise RuntimeError("Missing FLUX env variable")
|
| 12 |
|
| 13 |
+
DEFAULT_PATH = os.path.join(os.getcwd(), "default.png")
|
| 14 |
+
|
| 15 |
@app.route("/", methods=["GET"])
|
| 16 |
def generate_or_default():
|
| 17 |
+
# No query => serve default.png
|
| 18 |
if not request.query_string:
|
| 19 |
+
if os.path.exists(DEFAULT_PATH):
|
| 20 |
+
return send_file(DEFAULT_PATH, mimetype="image/png")
|
| 21 |
+
else:
|
| 22 |
+
abort(404, description="default.png not found in container")
|
|
|
|
|
|
|
|
|
|
| 23 |
|
| 24 |
+
# Otherwise, generate
|
| 25 |
prompt = request.args.get(
|
| 26 |
"prompt",
|
| 27 |
+
"a glowing board with the word 'KindSynapse'"
|
| 28 |
)
|
| 29 |
width = request.args.get("width", "1024")
|
| 30 |
height = request.args.get("height", "1024")
|
| 31 |
seed = request.args.get("seed", "0")
|
| 32 |
|
| 33 |
+
# URL-encode and build
|
| 34 |
+
encoded = urllib.parse.quote(prompt, safe="")
|
| 35 |
url = (
|
| 36 |
URL_TEMPLATE
|
| 37 |
+
.replace("[prompt]", encoded)
|
| 38 |
.replace("[w]", width)
|
| 39 |
.replace("[h]", height)
|
| 40 |
.replace("[seed]", seed)
|
|
|
|
| 43 |
resp = requests.get(url, stream=True)
|
| 44 |
if resp.status_code != 200:
|
| 45 |
return Response(
|
| 46 |
+
f"Upstream error: {resp.status_code}",
|
| 47 |
status=502
|
| 48 |
)
|
| 49 |
|
| 50 |
return Response(resp.content, content_type=resp.headers.get("Content-Type"))
|
| 51 |
|
| 52 |
if __name__ == "__main__":
|
|
|
|
| 53 |
app.run(host="0.0.0.0", port=7860)
|