Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,25 +1,35 @@
|
|
| 1 |
-
from flask import Flask, Response, request
|
| 2 |
import os, urllib.parse, requests
|
| 3 |
|
| 4 |
app = Flask(__name__)
|
| 5 |
|
| 6 |
-
#
|
| 7 |
URL_TEMPLATE = os.environ.get("FLUX")
|
| 8 |
if not URL_TEMPLATE:
|
| 9 |
raise RuntimeError("Missing FLUX env variable")
|
| 10 |
|
| 11 |
@app.route("/", methods=["GET"])
|
| 12 |
-
def
|
| 13 |
-
#
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 18 |
|
| 19 |
-
#
|
| 20 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 21 |
|
| 22 |
-
|
| 23 |
url = (
|
| 24 |
URL_TEMPLATE
|
| 25 |
.replace("[prompt]", encoded_prompt)
|
|
@@ -28,10 +38,15 @@ def generate_image():
|
|
| 28 |
.replace("[seed]", seed)
|
| 29 |
)
|
| 30 |
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 35 |
|
| 36 |
-
|
| 37 |
-
|
|
|
|
|
|
| 1 |
+
from flask import Flask, Response, request, send_file
|
| 2 |
import os, urllib.parse, requests
|
| 3 |
|
| 4 |
app = Flask(__name__)
|
| 5 |
|
| 6 |
+
# Pollinations URL template from env
|
| 7 |
URL_TEMPLATE = os.environ.get("FLUX")
|
| 8 |
if not URL_TEMPLATE:
|
| 9 |
raise RuntimeError("Missing FLUX env variable")
|
| 10 |
|
| 11 |
@app.route("/", methods=["GET"])
|
| 12 |
+
def generate_or_default():
|
| 13 |
+
# If no query string present, serve default.png
|
| 14 |
+
if not request.query_string:
|
| 15 |
+
# Make sure default.png is in the working dir (e.g. next to app.py)
|
| 16 |
+
return send_file(
|
| 17 |
+
"default.png",
|
| 18 |
+
mimetype="image/png",
|
| 19 |
+
as_attachment=False,
|
| 20 |
+
download_name="default.png"
|
| 21 |
+
)
|
| 22 |
|
| 23 |
+
# Otherwise, generate via Pollinations
|
| 24 |
+
prompt = request.args.get(
|
| 25 |
+
"prompt",
|
| 26 |
+
"a glowing board with the word 'KindSynapse'"
|
| 27 |
+
)
|
| 28 |
+
width = request.args.get("width", "1024")
|
| 29 |
+
height = request.args.get("height", "1024")
|
| 30 |
+
seed = request.args.get("seed", "0")
|
| 31 |
|
| 32 |
+
encoded_prompt = urllib.parse.quote(prompt, safe="")
|
| 33 |
url = (
|
| 34 |
URL_TEMPLATE
|
| 35 |
.replace("[prompt]", encoded_prompt)
|
|
|
|
| 38 |
.replace("[seed]", seed)
|
| 39 |
)
|
| 40 |
|
| 41 |
+
resp = requests.get(url, stream=True)
|
| 42 |
+
if resp.status_code != 200:
|
| 43 |
+
return Response(
|
| 44 |
+
f"Failed to fetch image. Upstream status: {resp.status_code}",
|
| 45 |
+
status=502
|
| 46 |
+
)
|
| 47 |
+
|
| 48 |
+
return Response(resp.content, content_type=resp.headers.get("Content-Type"))
|
| 49 |
|
| 50 |
+
if __name__ == "__main__":
|
| 51 |
+
# Dev server
|
| 52 |
+
app.run(host="0.0.0.0", port=7860)
|