File size: 1,515 Bytes
a1dbf61
ac8cd17
fa00a76
 
 
 
ac8cd17
 
fa00a76
ac8cd17
a1dbf61
 
 
 
 
 
 
 
 
 
ac8cd17
a1dbf61
 
 
1e5b853
a1dbf61
 
 
 
ac8cd17
a1dbf61
ac8cd17
 
 
 
 
 
 
 
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
from flask import Flask, Response, request, send_file
import os, urllib.parse, requests

app = Flask(__name__)

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

@app.route("/", methods=["GET"])
def generate_or_default():
    # If no query string present, serve default.png
    if not request.query_string:
        # Make sure default.png is in the working dir (e.g. next to app.py)
        return send_file(
            "default.png",
            mimetype="image/png",
            as_attachment=False,
            download_name="default.png"
        )

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

    encoded_prompt = urllib.parse.quote(prompt, safe="")
    url = (
        URL_TEMPLATE
        .replace("[prompt]", encoded_prompt)
        .replace("[w]", width)
        .replace("[h]", height)
        .replace("[seed]", seed)
    )

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

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

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