|
import os |
|
import urllib.parse |
|
import requests |
|
from flask import Flask, Response, request |
|
|
|
app = Flask(__name__) |
|
|
|
|
|
URL_TEMPLATE = os.environ.get("FLUX") |
|
if URL_TEMPLATE is None: |
|
raise RuntimeError("ERR-SCRT") |
|
|
|
@app.route("/generate", methods=["GET"]) |
|
def generate_image(): |
|
""" |
|
Query params: |
|
- prompt (string, required) |
|
- width (int, optional, default=512) |
|
- height (int, optional, default=512) |
|
- seed (int, optional, default=0) |
|
""" |
|
prompt = request.args.get("prompt", "").strip() |
|
if not prompt: |
|
return Response("Error: 'prompt' is required", status=400) |
|
|
|
|
|
width = request.args.get("width", "512") |
|
height = request.args.get("height", "512") |
|
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 (status {resp.status_code})", status=502) |
|
|
|
|
|
content_type = resp.headers.get("Content-Type", "application/octet-stream") |
|
return Response(resp.content, content_type=content_type) |
|
|
|
if __name__ == "__main__": |
|
|
|
app.run(host="0.0.0.0", port=7860) |