NihalGazi commited on
Commit
ac8cd17
·
verified ·
1 Parent(s): 70118e6

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +30 -44
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 is None:
11
- raise RuntimeError("ERR-SCRT")
12
 
13
- @app.route("/generate", methods=["GET"])
14
  def generate_image():
15
- """
16
- Query params:
17
- - prompt (string, required)
18
- - width (int, optional, default=512)
19
- - height (int, optional, default=512)
20
- - seed (int, optional, default=0)
21
- """
22
- prompt = request.args.get("prompt", "").strip()
23
- if not prompt:
24
- return Response("Error: 'prompt' is required", status=400)
25
-
26
- # Defaults
27
- width = request.args.get("width", "512")
28
- height = request.args.get("height", "512")
29
- seed = request.args.get("seed", "0")
30
-
31
- # URL‐encode the prompt
32
- encoded_prompt = urllib.parse.quote(prompt, safe="")
33
-
34
- # Build the actual Pollinations URL
35
- url = URL_TEMPLATE.replace("[prompt]", encoded_prompt) \
36
- .replace("[w]", width) \
37
- .replace("[h]", height) \
38
- .replace("[seed]", seed)
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"))