Upload server.py with huggingface_hub
Browse files
server.py
CHANGED
|
@@ -3,27 +3,34 @@ import sys
|
|
| 3 |
import subprocess
|
| 4 |
import base64
|
| 5 |
import io
|
| 6 |
-
import
|
| 7 |
|
| 8 |
-
# Install
|
| 9 |
-
subprocess.check_call([sys.executable, "-m", "pip", "install", "-q", "--upgrade",
|
|
|
|
| 10 |
|
| 11 |
import torch
|
| 12 |
from flask import Flask, request, jsonify
|
| 13 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 14 |
|
| 15 |
app = Flask(__name__)
|
| 16 |
pipe = None
|
| 17 |
|
| 18 |
def load_model():
|
| 19 |
global pipe
|
|
|
|
| 20 |
print("[image] Loading GLM-Image pipeline...", flush=True)
|
| 21 |
pipe = GlmImagePipeline.from_pretrained(
|
| 22 |
"zai-org/GLM-Image",
|
| 23 |
torch_dtype=torch.bfloat16,
|
| 24 |
)
|
| 25 |
-
pipe.
|
| 26 |
-
print("[image] Model loaded
|
| 27 |
|
| 28 |
@app.route("/health", methods=["GET"])
|
| 29 |
def health():
|
|
@@ -34,20 +41,22 @@ def health():
|
|
| 34 |
@app.route("/", methods=["POST"])
|
| 35 |
def generate():
|
| 36 |
try:
|
| 37 |
-
data = request.get_json()
|
| 38 |
prompt = data.get("inputs", "")
|
| 39 |
params = data.get("parameters", {})
|
| 40 |
|
| 41 |
width = int(params.get("width", 1024))
|
| 42 |
height = int(params.get("height", 1024))
|
| 43 |
-
steps = int(params.get("num_inference_steps",
|
| 44 |
guidance = float(params.get("guidance_scale", 1.5))
|
| 45 |
|
| 46 |
# GLM-Image requires dimensions divisible by 32
|
| 47 |
width = (width // 32) * 32
|
| 48 |
height = (height // 32) * 32
|
| 49 |
|
| 50 |
-
print(f"[image] Generating: {prompt[:100]}
|
|
|
|
|
|
|
| 51 |
image = pipe(
|
| 52 |
prompt=prompt,
|
| 53 |
height=height,
|
|
@@ -59,12 +68,13 @@ def generate():
|
|
| 59 |
buf = io.BytesIO()
|
| 60 |
image.save(buf, format="PNG")
|
| 61 |
img_b64 = base64.b64encode(buf.getvalue()).decode("utf-8")
|
| 62 |
-
print(f"[image] Done,
|
| 63 |
|
| 64 |
return jsonify({"image": img_b64, "format": "png"})
|
| 65 |
except Exception as e:
|
| 66 |
-
|
| 67 |
-
|
|
|
|
| 68 |
|
| 69 |
if __name__ == "__main__":
|
| 70 |
load_model()
|
|
|
|
| 3 |
import subprocess
|
| 4 |
import base64
|
| 5 |
import io
|
| 6 |
+
import traceback
|
| 7 |
|
| 8 |
+
# Install latest diffusers (supports GLM-Image) + dependencies
|
| 9 |
+
subprocess.check_call([sys.executable, "-m", "pip", "install", "-q", "--upgrade",
|
| 10 |
+
"diffusers", "flask", "accelerate", "sentencepiece", "protobuf", "transformers", "huggingface_hub"])
|
| 11 |
|
| 12 |
import torch
|
| 13 |
from flask import Flask, request, jsonify
|
| 14 |
+
|
| 15 |
+
print(f"[image] torch version: {torch.__version__}", flush=True)
|
| 16 |
+
print(f"[image] CUDA available: {torch.cuda.is_available()}", flush=True)
|
| 17 |
+
if torch.cuda.is_available():
|
| 18 |
+
print(f"[image] GPU: {torch.cuda.get_device_name(0)}", flush=True)
|
| 19 |
+
print(f"[image] VRAM: {torch.cuda.get_device_properties(0).total_mem / 1e9:.1f} GB", flush=True)
|
| 20 |
|
| 21 |
app = Flask(__name__)
|
| 22 |
pipe = None
|
| 23 |
|
| 24 |
def load_model():
|
| 25 |
global pipe
|
| 26 |
+
from diffusers.pipelines.glm_image import GlmImagePipeline
|
| 27 |
print("[image] Loading GLM-Image pipeline...", flush=True)
|
| 28 |
pipe = GlmImagePipeline.from_pretrained(
|
| 29 |
"zai-org/GLM-Image",
|
| 30 |
torch_dtype=torch.bfloat16,
|
| 31 |
)
|
| 32 |
+
pipe.to("cuda")
|
| 33 |
+
print("[image] Model loaded on CUDA!", flush=True)
|
| 34 |
|
| 35 |
@app.route("/health", methods=["GET"])
|
| 36 |
def health():
|
|
|
|
| 41 |
@app.route("/", methods=["POST"])
|
| 42 |
def generate():
|
| 43 |
try:
|
| 44 |
+
data = request.get_json(force=True)
|
| 45 |
prompt = data.get("inputs", "")
|
| 46 |
params = data.get("parameters", {})
|
| 47 |
|
| 48 |
width = int(params.get("width", 1024))
|
| 49 |
height = int(params.get("height", 1024))
|
| 50 |
+
steps = int(params.get("num_inference_steps", 30))
|
| 51 |
guidance = float(params.get("guidance_scale", 1.5))
|
| 52 |
|
| 53 |
# GLM-Image requires dimensions divisible by 32
|
| 54 |
width = (width // 32) * 32
|
| 55 |
height = (height // 32) * 32
|
| 56 |
|
| 57 |
+
print(f"[image] Generating: {prompt[:100]}", flush=True)
|
| 58 |
+
print(f"[image] Params: {width}x{height}, steps={steps}, guidance={guidance}", flush=True)
|
| 59 |
+
|
| 60 |
image = pipe(
|
| 61 |
prompt=prompt,
|
| 62 |
height=height,
|
|
|
|
| 68 |
buf = io.BytesIO()
|
| 69 |
image.save(buf, format="PNG")
|
| 70 |
img_b64 = base64.b64encode(buf.getvalue()).decode("utf-8")
|
| 71 |
+
print(f"[image] Done, size: {len(buf.getvalue())} bytes", flush=True)
|
| 72 |
|
| 73 |
return jsonify({"image": img_b64, "format": "png"})
|
| 74 |
except Exception as e:
|
| 75 |
+
tb = traceback.format_exc()
|
| 76 |
+
print(f"[image] ERROR: {e}\n{tb}", flush=True)
|
| 77 |
+
return jsonify({"error": str(e), "traceback": tb}), 500
|
| 78 |
|
| 79 |
if __name__ == "__main__":
|
| 80 |
load_model()
|