Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,51 +1,37 @@
|
|
| 1 |
-
import os
|
| 2 |
-
import urllib.parse
|
| 3 |
-
import requests
|
| 4 |
from flask import Flask, Response, request
|
|
|
|
| 5 |
|
| 6 |
app = Flask(__name__)
|
| 7 |
|
| 8 |
-
|
| 9 |
URL_TEMPLATE = os.environ.get("FLUX")
|
| 10 |
-
if URL_TEMPLATE
|
| 11 |
-
raise RuntimeError("
|
| 12 |
|
| 13 |
-
@app.route("/
|
| 14 |
def generate_image():
|
| 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 |
-
# Fetch the image bytes
|
| 41 |
-
resp = requests.get(url, stream=True)
|
| 42 |
-
if resp.status_code != 200:
|
| 43 |
-
return Response(f"Failed to fetch image (status {resp.status_code})", status=502)
|
| 44 |
-
|
| 45 |
-
# Forward the content‐type header (likely "image/png" or "image/jpeg")
|
| 46 |
-
content_type = resp.headers.get("Content-Type", "application/octet-stream")
|
| 47 |
-
return Response(resp.content, content_type=content_type)
|
| 48 |
-
|
| 49 |
-
if __name__ == "__main__":
|
| 50 |
-
# If you run `python app.py` locally, this will start Flask’s dev server.
|
| 51 |
-
app.run(host="0.0.0.0", port=7860)
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
from flask import Flask, Response, request
|
| 2 |
+
import os, urllib.parse, requests
|
| 3 |
|
| 4 |
app = Flask(__name__)
|
| 5 |
|
| 6 |
+
# Get URL template from env (set in HF secrets)
|
| 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_image():
|
| 13 |
+
# Default parameters
|
| 14 |
+
prompt = request.args.get("prompt", "a glowing board with the word 'KindSynapse'")
|
| 15 |
+
width = request.args.get("width", "1024")
|
| 16 |
+
height = request.args.get("height", "1024")
|
| 17 |
+
seed = request.args.get("seed", "0")
|
| 18 |
+
|
| 19 |
+
# Sanitize prompt
|
| 20 |
+
encoded_prompt = urllib.parse.quote(prompt)
|
| 21 |
+
|
| 22 |
+
# Fill in URL
|
| 23 |
+
url = (
|
| 24 |
+
URL_TEMPLATE
|
| 25 |
+
.replace("[prompt]", encoded_prompt)
|
| 26 |
+
.replace("[w]", width)
|
| 27 |
+
.replace("[h]", height)
|
| 28 |
+
.replace("[seed]", seed)
|
| 29 |
+
)
|
| 30 |
+
|
| 31 |
+
# Request image
|
| 32 |
+
response = requests.get(url, stream=True)
|
| 33 |
+
if response.status_code != 200:
|
| 34 |
+
return Response(f"Failed to fetch image. Upstream status: {response.status_code}", status=502)
|
| 35 |
+
|
| 36 |
+
# Serve image
|
| 37 |
+
return Response(response.content, content_type=response.headers.get("Content-Type"))
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|