Spaces:
Sleeping
Sleeping
Create generate.py
Browse files- generate.py +23 -39
generate.py
CHANGED
|
@@ -1,3 +1,4 @@
|
|
|
|
|
| 1 |
import os
|
| 2 |
import requests
|
| 3 |
from flask import Flask, request, send_file, jsonify
|
|
@@ -5,54 +6,37 @@ from io import BytesIO
|
|
| 5 |
|
| 6 |
app = Flask(__name__)
|
| 7 |
|
| 8 |
-
#
|
| 9 |
HF_API_KEY = os.environ.get("HF_API_KEY")
|
| 10 |
if not HF_API_KEY:
|
| 11 |
-
raise ValueError("HF_API_KEY
|
| 12 |
-
|
| 13 |
-
# Default model
|
| 14 |
-
DEFAULT_MODEL = "stabilityai/stable-diffusion-xl-base-1.0"
|
| 15 |
|
| 16 |
@app.route("/generate", methods=["POST"])
|
| 17 |
def generate():
|
| 18 |
-
data = request.json
|
| 19 |
prompt = data.get("prompt", "")
|
| 20 |
-
|
| 21 |
-
model = data.get("model", DEFAULT_MODEL)
|
| 22 |
-
width = min(1024, int(data.get("width", 512))) # Keep sizes safe
|
| 23 |
-
height = min(1024, int(data.get("height", 512)))
|
| 24 |
|
| 25 |
-
if not prompt:
|
| 26 |
-
return jsonify({"error": "Prompt
|
| 27 |
|
| 28 |
headers = {"Authorization": f"Bearer {HF_API_KEY}"}
|
| 29 |
-
payload = {
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
"
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
# If API returns JSON, it's likely an error
|
| 47 |
-
content_type = response.headers.get("content-type", "")
|
| 48 |
-
if "application/json" in content_type:
|
| 49 |
-
try:
|
| 50 |
-
data = response.json()
|
| 51 |
-
return jsonify({"error": data.get("error", str(data))}), 400
|
| 52 |
-
except:
|
| 53 |
-
return jsonify({"error": response.text}), 400
|
| 54 |
-
|
| 55 |
-
# Return image as PNG
|
| 56 |
return send_file(BytesIO(response.content), mimetype="image/png")
|
| 57 |
|
| 58 |
if __name__ == "__main__":
|
|
|
|
| 1 |
+
# generate.py
|
| 2 |
import os
|
| 3 |
import requests
|
| 4 |
from flask import Flask, request, send_file, jsonify
|
|
|
|
| 6 |
|
| 7 |
app = Flask(__name__)
|
| 8 |
|
| 9 |
+
# Make sure you set your HF API key in Secrets (HF_API_KEY)
|
| 10 |
HF_API_KEY = os.environ.get("HF_API_KEY")
|
| 11 |
if not HF_API_KEY:
|
| 12 |
+
raise ValueError("HF_API_KEY not set in environment variables")
|
|
|
|
|
|
|
|
|
|
| 13 |
|
| 14 |
@app.route("/generate", methods=["POST"])
|
| 15 |
def generate():
|
| 16 |
+
data = request.json
|
| 17 |
prompt = data.get("prompt", "")
|
| 18 |
+
model = data.get("model", "stabilityai/stable-diffusion-xl-base-1.0")
|
|
|
|
|
|
|
|
|
|
| 19 |
|
| 20 |
+
if not prompt.strip():
|
| 21 |
+
return jsonify({"error": "Prompt cannot be empty"}), 400
|
| 22 |
|
| 23 |
headers = {"Authorization": f"Bearer {HF_API_KEY}"}
|
| 24 |
+
payload = {"inputs": prompt}
|
| 25 |
+
|
| 26 |
+
try:
|
| 27 |
+
response = requests.post(
|
| 28 |
+
f"https://api-inference.huggingface.co/models/{model}",
|
| 29 |
+
headers=headers,
|
| 30 |
+
json=payload,
|
| 31 |
+
timeout=120
|
| 32 |
+
)
|
| 33 |
+
except requests.exceptions.RequestException as e:
|
| 34 |
+
return jsonify({"error": str(e)}), 500
|
| 35 |
+
|
| 36 |
+
if response.status_code != 200:
|
| 37 |
+
return jsonify({"error": response.text}), response.status_code
|
| 38 |
+
|
| 39 |
+
# Return the raw bytes as PNG
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 40 |
return send_file(BytesIO(response.content), mimetype="image/png")
|
| 41 |
|
| 42 |
if __name__ == "__main__":
|