Spaces:
Sleeping
Sleeping
File size: 2,335 Bytes
2e2dac7 388cf5c 4e31b1a 09aa8f8 31cc64d 2e2dac7 81914fc 2e2dac7 1087492 2e2dac7 388cf5c 31cc64d 2e2dac7 31cc64d 09aa8f8 31cc64d 09aa8f8 c012aad 31cc64d 2e2dac7 c012aad 2e2dac7 0a74686 2e2dac7 31cc64d 388cf5c 2e2dac7 583e56c 388cf5c 2e2dac7 388cf5c 2e2dac7 388cf5c 2e2dac7 388cf5c c012aad 31cc64d 0a74686 09aa8f8 583e56c 388cf5c 09aa8f8 388cf5c 1087492 48056a7 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 |
import io
import base64
import torch
import os
from flask import Flask, request, jsonify
from diffusers import StableDiffusionPipeline # Placeholder; adjust based on InstantMesh docs
from PIL import Image
import logging
logging.basicConfig(level=logging.INFO)
logger = logging.getLogger(__name__)
app = Flask(__name__)
# Load the model once at startup (on CPU) without token (test only)
try:
logger.info("Loading TencentARC InstantMesh pipeline...")
pipe = StableDiffusionPipeline.from_pretrained(
"TencentARC/InstantMesh",
torch_dtype=torch.float32,
cache_dir="/tmp/hf_home",
# token=token, # Comment out or remove for test
)
pipe.to("cpu")
logger.info("=== Application Startup at CPU mode =====")
except Exception as e:
logger.error(f"Error loading model: {e}", exc_info=True)
pipe = None
def pil_to_base64(image):
buffer = io.BytesIO()
image.save(buffer, format="PNG")
return base64.b64encode(buffer.getvalue()).decode("utf-8")
@app.route("/")
def home():
return "TencentARC InstantMesh CPU API is running!"
@app.route("/generate", methods=["POST"])
def generate():
if pipe is None:
return jsonify({"error": "Model not loaded"}), 500
try:
data = request.get_json()
image_data = data.get("image")
if not image_data:
return jsonify({"error": "No image provided"}), 400
if image_data.startswith("data:image"):
image_data = image_data.split(",")[1]
image = Image.open(io.BytesIO(base64.b64decode(image_data))).convert("RGB")
logger.info("Processing image with pipeline...")
result = pipe(image) # Adjust based on InstantMesh documentation
output_mesh = result.mesh # Hypothetical; check InstantMesh output format
output_path = "/tmp/output.glb"
output_mesh.save(output_path)
with open(output_path, "rb") as f:
mesh_data = base64.b64encode(f.read()).decode("utf-8")
logger.info("Mesh processed successfully")
return jsonify({"mesh": f"data:model/gltf-binary;base64,{mesh_data}"})
except Exception as e:
logger.error(f"Error generating mesh: {e}", exc_info=True)
return jsonify({"error": str(e)}), 500
if __name__ == "__main__":
app.run(host="0.0.0.0", port=7860) |