Barath05 commited on
Commit
325970c
Β·
verified Β·
1 Parent(s): 03bb86c

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +73 -54
app.py CHANGED
@@ -5,80 +5,102 @@ import cv2
5
  import os
6
  import tempfile
7
  import time
8
- from gradio_client import Client, file
9
 
10
- # --- CONFIGURATION ---
11
- # We switched to "hysts/Shap-E" (OpenAI's model).
12
- # It is extremely reliable and rarely crashes compared to TripoSR/InstantMesh.
13
- REMOTE_MODEL_ID = "hysts/Shap-E"
 
 
 
 
 
 
14
 
15
  def photo_to_sketch(image):
16
- """Instant local sketch – always returns PIL Image"""
17
- print("-> Starting Sketch Generation...")
18
- if image is None:
19
- return None
20
-
21
  if isinstance(image, np.ndarray):
22
  image = Image.fromarray(image.astype('uint8'))
23
-
24
  gray = image.convert("L")
25
  img_array = np.array(gray)
26
-
27
  blurred = cv2.GaussianBlur(img_array, (5, 5), 0)
28
  edges = cv2.Canny(blurred, 60, 150)
29
-
30
  sketch_np = 255 - edges
31
- sketch_pil = Image.fromarray(sketch_np)
32
-
33
- return sketch_pil.convert("RGB")
34
 
35
  def generate_3d_avatar(sketch_image, height, weight, muscle, gender, breast):
36
- """Generate 3D model using OpenAI Shap-E (Reliable Fallback)"""
37
- print(f"-> Starting 3D Generation using {REMOTE_MODEL_ID}...")
38
 
39
  if sketch_image is None:
40
  raise gr.Error("Please upload an image first!")
41
 
42
- # 1. Save Sketch to a temporary file
43
  if isinstance(sketch_image, np.ndarray):
44
  sketch_image = Image.fromarray(sketch_image.astype('uint8'))
45
-
46
  temp_dir = tempfile.gettempdir()
47
  sketch_path = os.path.join(temp_dir, f"sketch_{int(time.time())}.png")
48
  sketch_image.save(sketch_path)
49
-
50
- # 2. Connect to Shap-E
51
- try:
52
- print(f"-> Connecting to {REMOTE_MODEL_ID}...")
53
- client = Client(REMOTE_MODEL_ID)
54
-
55
- # 3. Send request (Shap-E API parameters)
56
- print("-> Sending request to Shap-E...")
57
-
58
- # Shap-E expects: [image, prompt_text, seed, guidance_scale, num_inference_steps]
59
- # We leave prompt empty to force Image-to-3D mode
60
- result = client.predict(
61
- file(sketch_path), # Input Image
62
- "", # Text Prompt (Empty for Img-to-3D)
63
- 0, # Seed
64
- 15, # Guidance Scale
65
- 64, # Steps (64 is fast, 128 is better)
66
- api_name="/image-to-3d"
67
- )
68
-
69
- # Result is just the file path string
70
- print(f"-> Success! Received model: {result}")
71
- return result, result
72
 
73
- except Exception as e:
74
- print(f"-> Connection Error: {e}")
75
- # If even Shap-E fails, we raise a clear error
76
- raise gr.Error(f"All 3D models are currently busy. Error: {e}")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
77
 
78
  # =============== UI ===============
79
  with gr.Blocks(title="SketchToLife") as demo:
80
- gr.Markdown("# SketchToLife – Photo β†’ Sketch β†’ 3D Avatar")
81
- gr.Markdown(f"**Status:** Connected to `OpenAI Shap-E` (High Stability Backup).")
82
 
83
  with gr.Row():
84
  with gr.Column():
@@ -87,8 +109,7 @@ with gr.Blocks(title="SketchToLife") as demo:
87
  out_sketch = gr.Image(label="Your Sketch", height=420, type="pil")
88
 
89
  with gr.Column():
90
- gr.Markdown("### Customize 3D Body")
91
- # Placeholders to prevent argument errors
92
  h = gr.Dropdown(["short", "average", "tall", "giant"], value="average", label="Height")
93
  w = gr.Dropdown(["slim", "average", "curvy", "heavy"], value="average", label="Weight")
94
  m = gr.Dropdown(["slim", "fit", "muscular", "bodybuilder"], value="fit", label="Muscle")
@@ -98,12 +119,10 @@ with gr.Blocks(title="SketchToLife") as demo:
98
  btn2 = gr.Button("Generate 3D Model", variant="primary", size="lg")
99
 
100
  with gr.Row():
101
- view3d = gr.Model3D(label="Your 3D Model – Rotate & Zoom!", height=520, interactive=True)
102
  download = gr.File(label="Download .GLB")
103
 
104
  btn1.click(photo_to_sketch, inputs=inp, outputs=out_sketch)
105
-
106
- # 6 inputs -> 6 arguments
107
  btn2.click(generate_3d_avatar, inputs=[out_sketch, h, w, m, g, b], outputs=[view3d, download])
108
 
109
  if __name__ == "__main__":
 
5
  import os
6
  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
 
21
  def photo_to_sketch(image):
22
+ """Instant local sketch"""
23
+ if image is None: return None
 
 
 
24
  if isinstance(image, np.ndarray):
25
  image = Image.fromarray(image.astype('uint8'))
 
26
  gray = image.convert("L")
27
  img_array = np.array(gray)
 
28
  blurred = cv2.GaussianBlur(img_array, (5, 5), 0)
29
  edges = cv2.Canny(blurred, 60, 150)
 
30
  sketch_np = 255 - edges
31
+ return Image.fromarray(sketch_np).convert("RGB")
 
 
32
 
33
  def generate_3d_avatar(sketch_image, height, weight, muscle, gender, breast):
34
+ """Try multiple remote models until one succeeds"""
35
+ print(f"-> Starting 3D Generation Process...")
36
 
37
  if sketch_image is None:
38
  raise gr.Error("Please upload an image first!")
39
 
40
+ # Save temp file
41
  if isinstance(sketch_image, np.ndarray):
42
  sketch_image = Image.fromarray(sketch_image.astype('uint8'))
 
43
  temp_dir = tempfile.gettempdir()
44
  sketch_path = os.path.join(temp_dir, f"sketch_{int(time.time())}.png")
45
  sketch_image.save(sketch_path)
46
+ print(f"-> Saved input to {sketch_path}")
47
+
48
+ last_error = ""
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
49
 
50
+ # --- RETRY LOOP ---
51
+ for model in MODELS:
52
+ try:
53
+ model_id = model["id"]
54
+ print(f"------------------------------------------")
55
+ print(f"-> Attempting Connection to: {model_id}...")
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
78
+ api_name=model["api"]
79
+ )
80
+
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}")
94
+ last_error = str(e)
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 (TripoSR β†’ Mirror β†’ Shap-E)")
104
 
105
  with gr.Row():
106
  with gr.Column():
 
109
  out_sketch = gr.Image(label="Your Sketch", height=420, type="pil")
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")
 
119
  btn2 = gr.Button("Generate 3D Model", variant="primary", size="lg")
120
 
121
  with gr.Row():
122
+ view3d = gr.Model3D(label="3D Result", height=520, interactive=True)
123
  download = gr.File(label="Download .GLB")
124
 
125
  btn1.click(photo_to_sketch, inputs=inp, outputs=out_sketch)
 
 
126
  btn2.click(generate_3d_avatar, inputs=[out_sketch, h, w, m, g, b], outputs=[view3d, download])
127
 
128
  if __name__ == "__main__":