mike23415 commited on
Commit
09aa8f8
·
verified ·
1 Parent(s): 768420f

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +19 -12
app.py CHANGED
@@ -1,8 +1,8 @@
1
  import io
2
  import base64
3
  import torch
4
- from flask import Flask, request, jsonify, send_file
5
- from diffusers import Zero123PlusPipeline
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 Zero123Plus pipeline...")
17
- pipe = Zero123PlusPipeline.from_pretrained(
18
- "sudo-ai/zero123plus-v1.2",
19
- torch_dtype=torch.float32, # CPU needs 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 "Zero123Plus CPU API is running!"
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
- result = pipe(image)
56
- output_image = result.images[0]
57
- logger.info("Image processed successfully")
58
 
59
- return jsonify({"image": f"data:image/png;base64,{pil_to_base64(output_image)}"})
 
 
 
 
 
 
 
60
 
61
  except Exception as e:
62
- logger.error(f"Error generating image: {e}", exc_info=True)
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__":