mac9087 commited on
Commit
2ed8fe6
·
verified ·
1 Parent(s): 02f669e

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +17 -17
app.py CHANGED
@@ -14,7 +14,7 @@ from huggingface_hub import snapshot_download
14
  from flask_cors import CORS
15
  import numpy as np
16
  import trimesh
17
- from instantmesh import InstantMesh
18
 
19
  # Force CPU usage
20
  os.environ["CUDA_VISIBLE_DEVICES"] = ""
@@ -46,12 +46,12 @@ app.config['MAX_CONTENT_LENGTH'] = 16 * 1024 * 1024
46
  processing_jobs = {}
47
 
48
  # Global model variables
49
- instant_mesh = None
50
  model_loaded = False
51
  model_loading = False
52
 
53
  # Configuration for processing
54
- TIMEOUT_SECONDS = 300 # 5 minutes max for InstantMesh on CPU
55
  MAX_DIMENSION = 256
56
 
57
  # TimeoutError for handling timeouts
@@ -106,21 +106,21 @@ def preprocess_image(image_path):
106
  return img
107
 
108
  def load_model():
109
- global instant_mesh, model_loaded, model_loading
110
 
111
  if model_loaded:
112
- return instant_mesh
113
 
114
  if model_loading:
115
  while model_loading and not model_loaded:
116
  time.sleep(0.5)
117
- return instant_mesh
118
 
119
  try:
120
  model_loading = True
121
  print("Starting model loading...")
122
 
123
- model_name = "BAAI/InstantMesh"
124
 
125
  # Download model with retry mechanism
126
  max_retries = 3
@@ -141,8 +141,8 @@ def load_model():
141
  else:
142
  raise
143
 
144
- # Load InstantMesh
145
- instant_mesh = InstantMesh.from_pretrained(
146
  model_name,
147
  use_safetensors=True,
148
  torch_dtype=torch.float16,
@@ -152,7 +152,7 @@ def load_model():
152
 
153
  model_loaded = True
154
  print("Model loaded successfully on CPU")
155
- return instant_mesh
156
 
157
  except Exception as e:
158
  print(f"Error loading model: {str(e)}")
@@ -165,7 +165,7 @@ def load_model():
165
  def health_check():
166
  return jsonify({
167
  "status": "healthy",
168
- "model": "InstantMesh 3D Generator",
169
  "device": "cpu"
170
  }), 200
171
 
@@ -263,17 +263,17 @@ def convert_image_to_3d():
263
  try:
264
  def generate_3d():
265
  # Adjust settings based on detail level
266
- steps = {'low': 50, 'medium': 75, 'high': 100}
267
- resolution = {'low': 128, 'medium': 256, 'high': 512}
268
 
269
- # Convert PIL image to numpy for InstantMesh
270
  img_array = np.array(image)
271
 
272
  # Generate mesh
273
  mesh = model(
274
  image=img_array,
275
  num_steps=steps[detail_level],
276
- resolution=resolution[detail_level],
277
  seed=12345
278
  )
279
  return mesh
@@ -414,7 +414,7 @@ def model_info(job_id):
414
  @app.route('/', methods=['GET'])
415
  def index():
416
  return jsonify({
417
- "message": "Image to 3D API (InstantMesh)",
418
  "endpoints": [
419
  "/convert",
420
  "/progress/<job_id>",
@@ -426,7 +426,7 @@ def index():
426
  "output_format": "glb",
427
  "detail_level": "low, medium, or high - controls mesh detail"
428
  },
429
- "description": "This API creates full 3D models from 2D images using InstantMesh"
430
  }), 200
431
 
432
  if __name__ == '__main__':
 
14
  from flask_cors import CORS
15
  import numpy as np
16
  import trimesh
17
+ from sf3d.system import StableFast3D
18
 
19
  # Force CPU usage
20
  os.environ["CUDA_VISIBLE_DEVICES"] = ""
 
46
  processing_jobs = {}
47
 
48
  # Global model variables
49
+ sf3d = None
50
  model_loaded = False
51
  model_loading = False
52
 
53
  # Configuration for processing
54
+ TIMEOUT_SECONDS = 180 # 3 minutes max for SF3D on CPU
55
  MAX_DIMENSION = 256
56
 
57
  # TimeoutError for handling timeouts
 
106
  return img
107
 
108
  def load_model():
109
+ global sf3d, model_loaded, model_loading
110
 
111
  if model_loaded:
112
+ return sf3d
113
 
114
  if model_loading:
115
  while model_loading and not model_loaded:
116
  time.sleep(0.5)
117
+ return sf3d
118
 
119
  try:
120
  model_loading = True
121
  print("Starting model loading...")
122
 
123
+ model_name = "stabilityai/stable-fast-3d"
124
 
125
  # Download model with retry mechanism
126
  max_retries = 3
 
141
  else:
142
  raise
143
 
144
+ # Load Stable Fast 3D
145
+ sf3d = StableFast3D.from_pretrained(
146
  model_name,
147
  use_safetensors=True,
148
  torch_dtype=torch.float16,
 
152
 
153
  model_loaded = True
154
  print("Model loaded successfully on CPU")
155
+ return sf3d
156
 
157
  except Exception as e:
158
  print(f"Error loading model: {str(e)}")
 
165
  def health_check():
166
  return jsonify({
167
  "status": "healthy",
168
+ "model": "Stable Fast 3D Generator",
169
  "device": "cpu"
170
  }), 200
171
 
 
263
  try:
264
  def generate_3d():
265
  # Adjust settings based on detail level
266
+ steps = {'low': 30, 'medium': 50, 'high': 75}
267
+ texture_res = {'low': 512, 'medium': 1024, 'high': 2048}
268
 
269
+ # Convert PIL image to numpy
270
  img_array = np.array(image)
271
 
272
  # Generate mesh
273
  mesh = model(
274
  image=img_array,
275
  num_steps=steps[detail_level],
276
+ texture_resolution=texture_res[detail_level],
277
  seed=12345
278
  )
279
  return mesh
 
414
  @app.route('/', methods=['GET'])
415
  def index():
416
  return jsonify({
417
+ "message": "Image to 3D API (Stable Fast 3D)",
418
  "endpoints": [
419
  "/convert",
420
  "/progress/<job_id>",
 
426
  "output_format": "glb",
427
  "detail_level": "low, medium, or high - controls mesh detail"
428
  },
429
+ "description": "This API creates full 3D models from 2D images using Stable Fast 3D"
430
  }), 200
431
 
432
  if __name__ == '__main__':