Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
@@ -1,8 +1,8 @@
|
|
1 |
import io
|
2 |
import base64
|
3 |
import torch
|
4 |
-
from flask import Flask, request, jsonify
|
5 |
-
from diffusers import
|
6 |
from PIL import Image
|
7 |
import logging
|
8 |
|
@@ -13,10 +13,10 @@ app = Flask(__name__)
|
|
13 |
|
14 |
# Load the model once at startup (on CPU)
|
15 |
try:
|
16 |
-
logger.info("Loading
|
17 |
-
pipe =
|
18 |
-
"
|
19 |
-
torch_dtype=torch.float32,
|
20 |
cache_dir="/tmp/hf_home",
|
21 |
)
|
22 |
pipe.to("cpu")
|
@@ -32,7 +32,7 @@ def pil_to_base64(image):
|
|
32 |
|
33 |
@app.route("/")
|
34 |
def home():
|
35 |
-
return "
|
36 |
|
37 |
@app.route("/generate", methods=["POST"])
|
38 |
def generate():
|
@@ -52,14 +52,21 @@ def generate():
|
|
52 |
image = Image.open(io.BytesIO(base64.b64decode(image_data))).convert("RGB")
|
53 |
|
54 |
logger.info("Processing image with pipeline...")
|
55 |
-
|
56 |
-
|
57 |
-
|
58 |
|
59 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
60 |
|
61 |
except Exception as e:
|
62 |
-
logger.error(f"Error generating
|
63 |
return jsonify({"error": str(e)}), 500
|
64 |
|
65 |
if __name__ == "__main__":
|
|
|
1 |
import io
|
2 |
import base64
|
3 |
import torch
|
4 |
+
from flask import Flask, request, jsonify
|
5 |
+
from diffusers import StableDiffusionPipeline # Placeholder; SF3D uses a custom setup
|
6 |
from PIL import Image
|
7 |
import logging
|
8 |
|
|
|
13 |
|
14 |
# Load the model once at startup (on CPU)
|
15 |
try:
|
16 |
+
logger.info("Loading Stable Fast 3D pipeline...")
|
17 |
+
pipe = StableDiffusionPipeline.from_pretrained(
|
18 |
+
"stabilityai/stable-fast-3d",
|
19 |
+
torch_dtype=torch.float32,
|
20 |
cache_dir="/tmp/hf_home",
|
21 |
)
|
22 |
pipe.to("cpu")
|
|
|
32 |
|
33 |
@app.route("/")
|
34 |
def home():
|
35 |
+
return "Stable Fast 3D CPU API is running!"
|
36 |
|
37 |
@app.route("/generate", methods=["POST"])
|
38 |
def generate():
|
|
|
52 |
image = Image.open(io.BytesIO(base64.b64decode(image_data))).convert("RGB")
|
53 |
|
54 |
logger.info("Processing image with pipeline...")
|
55 |
+
# SF3D-specific generation; adjust based on documentation
|
56 |
+
result = pipe(image) # Placeholder; check SF3D repo for exact method
|
57 |
+
output_mesh = result.mesh # Hypothetical output; may be .glb or .obj
|
58 |
|
59 |
+
# Save mesh to temporary file and encode
|
60 |
+
output_path = "/tmp/output.glb"
|
61 |
+
output_mesh.save(output_path)
|
62 |
+
with open(output_path, "rb") as f:
|
63 |
+
mesh_data = base64.b64encode(f.read()).decode("utf-8")
|
64 |
+
|
65 |
+
logger.info("Mesh processed successfully")
|
66 |
+
return jsonify({"mesh": f"data:model/gltf-binary;base64,{mesh_data}"})
|
67 |
|
68 |
except Exception as e:
|
69 |
+
logger.error(f"Error generating mesh: {e}", exc_info=True)
|
70 |
return jsonify({"error": str(e)}), 500
|
71 |
|
72 |
if __name__ == "__main__":
|