Upload server.py with huggingface_hub
Browse files
server.py
CHANGED
|
@@ -42,7 +42,8 @@ def health():
|
|
| 42 |
def generate():
|
| 43 |
try:
|
| 44 |
data = request.get_json(force=True)
|
| 45 |
-
|
|
|
|
| 46 |
params = data.get("parameters", {})
|
| 47 |
|
| 48 |
width = int(params.get("width", 1024))
|
|
@@ -70,11 +71,45 @@ def generate():
|
|
| 70 |
img_b64 = base64.b64encode(buf.getvalue()).decode("utf-8")
|
| 71 |
print(f"[image] Done, size: {len(buf.getvalue())} bytes", flush=True)
|
| 72 |
|
| 73 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 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()
|
|
|
|
| 42 |
def generate():
|
| 43 |
try:
|
| 44 |
data = request.get_json(force=True)
|
| 45 |
+
# Support both HF format (inputs) and backend format (prompt)
|
| 46 |
+
prompt = data.get("inputs", "") or data.get("prompt", "")
|
| 47 |
params = data.get("parameters", {})
|
| 48 |
|
| 49 |
width = int(params.get("width", 1024))
|
|
|
|
| 71 |
img_b64 = base64.b64encode(buf.getvalue()).decode("utf-8")
|
| 72 |
print(f"[image] Done, size: {len(buf.getvalue())} bytes", flush=True)
|
| 73 |
|
| 74 |
+
# Return in both formats - HF format and backend format
|
| 75 |
+
return jsonify({
|
| 76 |
+
"image": img_b64,
|
| 77 |
+
"format": "png",
|
| 78 |
+
"success": True,
|
| 79 |
+
"image_base64": img_b64,
|
| 80 |
+
"model": "glm-image",
|
| 81 |
+
})
|
| 82 |
except Exception as e:
|
| 83 |
tb = traceback.format_exc()
|
| 84 |
print(f"[image] ERROR: {e}\n{tb}", flush=True)
|
| 85 |
+
return jsonify({"error": str(e), "traceback": tb, "success": False}), 500
|
| 86 |
+
|
| 87 |
+
# Also add /api/generate route for the backend's expected format
|
| 88 |
+
@app.route("/api/generate", methods=["POST"])
|
| 89 |
+
def api_generate():
|
| 90 |
+
"""Handle the backend's /api/generate format by forwarding to generate()"""
|
| 91 |
+
return generate()
|
| 92 |
+
|
| 93 |
+
# Also add /api/analyze and /api/vision-chat for the backend
|
| 94 |
+
@app.route("/api/analyze", methods=["POST"])
|
| 95 |
+
def api_analyze():
|
| 96 |
+
"""Image analysis - return a placeholder since we don't have a vision model here"""
|
| 97 |
+
data = request.get_json(force=True)
|
| 98 |
+
return jsonify({
|
| 99 |
+
"success": True,
|
| 100 |
+
"description": "Image analysis is not available on this endpoint.",
|
| 101 |
+
"model": "none",
|
| 102 |
+
})
|
| 103 |
+
|
| 104 |
+
@app.route("/api/vision-chat", methods=["POST"])
|
| 105 |
+
def api_vision_chat():
|
| 106 |
+
"""Vision chat - return a placeholder"""
|
| 107 |
+
data = request.get_json(force=True)
|
| 108 |
+
return jsonify({
|
| 109 |
+
"success": True,
|
| 110 |
+
"answer": "Vision chat is not available on this endpoint.",
|
| 111 |
+
"model": "none",
|
| 112 |
+
})
|
| 113 |
|
| 114 |
if __name__ == "__main__":
|
| 115 |
load_model()
|