Barath05 commited on
Commit
933118b
·
verified ·
1 Parent(s): 325970c

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +28 -17
app.py CHANGED
@@ -7,14 +7,14 @@ import tempfile
7
  import time
8
  from gradio_client import Client, handle_file
9
 
10
- # --- CONFIGURATION: PRIORITY LIST ---
11
- # We will try these models in order until one works.
12
- # 1. Official TripoSR (Best speed)
13
- # 2. Community Mirror (Backup)
14
- # 3. OpenAI Shap-E (Old reliable)
15
  MODELS = [
 
16
  {"id": "stabilityai/TripoSR", "api": "/generate", "type": "tripo"},
17
- {"id": "virattt/TripoSR", "api": "/generate", "type": "tripo"},
18
  {"id": "hysts/Shap-E", "api": "/image-to-3d", "type": "shape"}
19
  ]
20
 
@@ -56,22 +56,31 @@ def generate_3d_avatar(sketch_image, height, weight, muscle, gender, breast):
56
 
57
  client = Client(model_id)
58
 
59
- if model["type"] == "tripo":
 
 
 
 
 
 
 
 
 
60
  # TripoSR Parameters
61
  print("-> Sending request (TripoSR format)...")
62
  result = client.predict(
63
- handle_file(sketch_path), # New handle_file method
64
- False, # Do not remove background
65
  0.85, # Foreground ratio
66
  api_name=model["api"]
67
  )
68
 
69
  elif model["type"] == "shape":
70
- # Shap-E Parameters (Corrected inputs)
71
  print("-> Sending request (Shap-E format)...")
72
  result = client.predict(
73
  handle_file(sketch_path), # Input Image
74
- "high quality 3d model", # Prompt (Fixes empty string error)
75
  0, # Seed
76
  15, # Guidance Scale
77
  64, # Steps
@@ -81,13 +90,14 @@ def generate_3d_avatar(sketch_image, height, weight, muscle, gender, breast):
81
  # If we get here, it worked!
82
  print(f"-> SUCCESS! Model generated by {model_id}")
83
 
84
- # Handle different return types (tuple vs string)
85
  if isinstance(result, (list, tuple)):
86
- final_glb = result[0]
 
87
  else:
88
- final_glb = result
89
 
90
- return final_glb, final_glb
91
 
92
  except Exception as e:
93
  print(f"-> FAILED: {model_id} | Error: {e}")
@@ -95,12 +105,12 @@ def generate_3d_avatar(sketch_image, height, weight, muscle, gender, breast):
95
  continue # Try next model
96
 
97
  # If loop finishes without success
98
- raise gr.Error(f"All 3 backup models failed. Last error: {last_error}")
99
 
100
  # =============== UI ===============
101
  with gr.Blocks(title="SketchToLife") as demo:
102
  gr.Markdown("# SketchToLife – Robust 3D Generator")
103
- gr.Markdown("**Status:** Using Multi-Model Fallback (TripoSRMirror → Shap-E)")
104
 
105
  with gr.Row():
106
  with gr.Column():
@@ -110,6 +120,7 @@ with gr.Blocks(title="SketchToLife") as demo:
110
 
111
  with gr.Column():
112
  gr.Markdown("### Customize Body")
 
113
  h = gr.Dropdown(["short", "average", "tall", "giant"], value="average", label="Height")
114
  w = gr.Dropdown(["slim", "average", "curvy", "heavy"], value="average", label="Weight")
115
  m = gr.Dropdown(["slim", "fit", "muscular", "bodybuilder"], value="fit", label="Muscle")
 
7
  import time
8
  from gradio_client import Client, handle_file
9
 
10
+ # --- CONFIGURATION: ROBUST PRIORITY LIST ---
11
+ # We try completely different architectures to avoid shared server outages.
12
+ # 1. CRM (Zhengyi/CRM) - High quality, separate infrastructure
13
+ # 2. TripoSR (Official) - Fast, but currently flaky
14
+ # 3. Shap-E (OpenAI) - Old reliable fallback
15
  MODELS = [
16
+ {"id": "Zhengyi/CRM", "api": "/generate", "type": "crm"},
17
  {"id": "stabilityai/TripoSR", "api": "/generate", "type": "tripo"},
 
18
  {"id": "hysts/Shap-E", "api": "/image-to-3d", "type": "shape"}
19
  ]
20
 
 
56
 
57
  client = Client(model_id)
58
 
59
+ if model["type"] == "crm":
60
+ # CRM Parameters: [Image, Scale, Steps, Seed]
61
+ print("-> Sending request (CRM format)...")
62
+ # Note: CRM sometimes returns a tuple of (model, video). We handle both.
63
+ result = client.predict(
64
+ handle_file(sketch_path), # Input image
65
+ api_name=model["api"]
66
+ )
67
+
68
+ elif model["type"] == "tripo":
69
  # TripoSR Parameters
70
  print("-> Sending request (TripoSR format)...")
71
  result = client.predict(
72
+ handle_file(sketch_path), # Input image
73
+ False, # Remove background?
74
  0.85, # Foreground ratio
75
  api_name=model["api"]
76
  )
77
 
78
  elif model["type"] == "shape":
79
+ # Shap-E Parameters
80
  print("-> Sending request (Shap-E format)...")
81
  result = client.predict(
82
  handle_file(sketch_path), # Input Image
83
+ "high quality 3d model", # Prompt (Required)
84
  0, # Seed
85
  15, # Guidance Scale
86
  64, # Steps
 
90
  # If we get here, it worked!
91
  print(f"-> SUCCESS! Model generated by {model_id}")
92
 
93
+ # Handle different return types (some spaces return [path, video], others just path)
94
  if isinstance(result, (list, tuple)):
95
+ # Look for the .glb or .obj file in the list
96
+ final_model = next((item for item in result if isinstance(item, str) and item.endswith(('.glb', '.obj', '.gltf'))), result[0])
97
  else:
98
+ final_model = result
99
 
100
+ return final_model, final_model
101
 
102
  except Exception as e:
103
  print(f"-> FAILED: {model_id} | Error: {e}")
 
105
  continue # Try next model
106
 
107
  # If loop finishes without success
108
+ raise gr.Error(f"All backup models failed. The Hugging Face inference cloud is experiencing a widespread outage. Last Error: {last_error}")
109
 
110
  # =============== UI ===============
111
  with gr.Blocks(title="SketchToLife") as demo:
112
  gr.Markdown("# SketchToLife – Robust 3D Generator")
113
+ gr.Markdown("**Status:** Using Multi-Model Fallback (CRMTripoSR → Shap-E)")
114
 
115
  with gr.Row():
116
  with gr.Column():
 
120
 
121
  with gr.Column():
122
  gr.Markdown("### Customize Body")
123
+ # Placeholders for UI consistency
124
  h = gr.Dropdown(["short", "average", "tall", "giant"], value="average", label="Height")
125
  w = gr.Dropdown(["slim", "average", "curvy", "heavy"], value="average", label="Weight")
126
  m = gr.Dropdown(["slim", "fit", "muscular", "bodybuilder"], value="fit", label="Muscle")