File size: 1,509 Bytes
5d69152
 
 
 
fa00a76
 
 
5d69152
fa00a76
ac8cd17
 
fa00a76
5d69152
 
ac8cd17
a1dbf61
5d69152
a1dbf61
5d69152
 
 
 
ac8cd17
5d69152
a1dbf61
 
5d69152
a1dbf61
 
 
 
ac8cd17
4031b1e
81ab4f9
5d69152
 
ac8cd17
 
5d69152
ac8cd17
 
 
 
 
a1dbf61
 
 
5d69152
a1dbf61
 
 
 
ac8cd17
a1dbf61
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
import os
import urllib.parse
import requests
from flask import Flask, Response, request, send_file, abort

app = Flask(__name__)

# URL template from env
URL_TEMPLATE = os.environ.get("FLUX")
if not URL_TEMPLATE:
    raise RuntimeError("Missing FLUX env variable")

DEFAULT_PATH = os.path.join(os.getcwd(), "default.png")

@app.route("/", methods=["GET"])
def generate_or_default():
    # No query => serve default.png
    if not request.query_string:
        if os.path.exists(DEFAULT_PATH):
            return send_file(DEFAULT_PATH, mimetype="image/png")
        else:
            abort(404, description="default.png not found in container")

    # Otherwise, generate
    prompt = request.args.get(
        "prompt",
        "a glowing board with the word 'KindSynapse'"
    )
    width  = request.args.get("width", "1024")
    height = request.args.get("height", "1024")
    seed   = request.args.get("seed", "0")

    

    # URL-encode and build
    encoded = urllib.parse.quote(prompt, safe="")
    url = (
        URL_TEMPLATE
        .replace("[prompt]", encoded)
        .replace("[w]", width)
        .replace("[h]", height)
        .replace("[seed]", seed)
    )

    resp = requests.get(url, stream=True)
    if resp.status_code != 200:
        return Response(
            f"Upstream error: {resp.status_code}",
            status=502
        )

    return Response(resp.content, content_type=resp.headers.get("Content-Type"))

if __name__ == "__main__":
    app.run(host="0.0.0.0", port=7860)