Upload server.py with huggingface_hub
Browse files
server.py
ADDED
|
@@ -0,0 +1,73 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import os
|
| 2 |
+
import sys
|
| 3 |
+
import subprocess
|
| 4 |
+
import base64
|
| 5 |
+
import io
|
| 6 |
+
import json
|
| 7 |
+
|
| 8 |
+
# Install diffusers from source (supports GLM-Image)
|
| 9 |
+
subprocess.check_call([sys.executable, "-m", "pip", "install", "-q", "git+https://github.com/huggingface/diffusers.git"])
|
| 10 |
+
subprocess.check_call([sys.executable, "-m", "pip", "install", "-q", "accelerate", "sentencepiece", "protobuf"])
|
| 11 |
+
|
| 12 |
+
import torch
|
| 13 |
+
from flask import Flask, request, jsonify
|
| 14 |
+
from diffusers.pipelines.glm_image import GlmImagePipeline
|
| 15 |
+
|
| 16 |
+
app = Flask(__name__)
|
| 17 |
+
pipe = None
|
| 18 |
+
|
| 19 |
+
def load_model():
|
| 20 |
+
global pipe
|
| 21 |
+
print("[image] Loading GLM-Image pipeline...", flush=True)
|
| 22 |
+
pipe = GlmImagePipeline.from_pretrained(
|
| 23 |
+
"zai-org/GLM-Image",
|
| 24 |
+
torch_dtype=torch.bfloat16,
|
| 25 |
+
device_map="cuda",
|
| 26 |
+
enable_model_cpu_offload=True,
|
| 27 |
+
)
|
| 28 |
+
print("[image] Model loaded successfully!", flush=True)
|
| 29 |
+
|
| 30 |
+
@app.route("/health", methods=["GET"])
|
| 31 |
+
def health():
|
| 32 |
+
if pipe is not None:
|
| 33 |
+
return jsonify({"status": "healthy"}), 200
|
| 34 |
+
return jsonify({"status": "loading"}), 503
|
| 35 |
+
|
| 36 |
+
@app.route("/", methods=["POST"])
|
| 37 |
+
def generate():
|
| 38 |
+
try:
|
| 39 |
+
data = request.get_json()
|
| 40 |
+
prompt = data.get("inputs", "")
|
| 41 |
+
params = data.get("parameters", {})
|
| 42 |
+
|
| 43 |
+
width = int(params.get("width", 1024))
|
| 44 |
+
height = int(params.get("height", 1024))
|
| 45 |
+
steps = int(params.get("num_inference_steps", 50))
|
| 46 |
+
guidance = float(params.get("guidance_scale", 1.5))
|
| 47 |
+
|
| 48 |
+
# GLM-Image requires dimensions divisible by 32
|
| 49 |
+
width = (width // 32) * 32
|
| 50 |
+
height = (height // 32) * 32
|
| 51 |
+
|
| 52 |
+
print(f"[image] Generating: {prompt[:100]}...", flush=True)
|
| 53 |
+
image = pipe(
|
| 54 |
+
prompt=prompt,
|
| 55 |
+
height=height,
|
| 56 |
+
width=width,
|
| 57 |
+
num_inference_steps=steps,
|
| 58 |
+
guidance_scale=guidance,
|
| 59 |
+
).images[0]
|
| 60 |
+
|
| 61 |
+
buf = io.BytesIO()
|
| 62 |
+
image.save(buf, format="PNG")
|
| 63 |
+
img_b64 = base64.b64encode(buf.getvalue()).decode("utf-8")
|
| 64 |
+
print(f"[image] Done, image size: {len(buf.getvalue())} bytes", flush=True)
|
| 65 |
+
|
| 66 |
+
return jsonify({"image": img_b64, "format": "png"})
|
| 67 |
+
except Exception as e:
|
| 68 |
+
print(f"[image] Error: {e}", flush=True)
|
| 69 |
+
return jsonify({"error": str(e)}), 500
|
| 70 |
+
|
| 71 |
+
if __name__ == "__main__":
|
| 72 |
+
load_model()
|
| 73 |
+
app.run(host="0.0.0.0", port=8000)
|