NihalGazi commited on
Commit
5d69152
·
verified ·
1 Parent(s): b0fd046

Update app.py

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