feylur commited on
Commit
76d32b4
Β·
verified Β·
1 Parent(s): 6d95b71

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +44 -14
app.py CHANGED
@@ -5,6 +5,7 @@ import torch
5
  import gradio as gr
6
  from PIL import Image
7
  import gc
 
8
  from huggingface_hub import snapshot_download
9
 
10
  # Add CatVTON to path
@@ -36,7 +37,6 @@ def load_models():
36
 
37
  print(f"βœ… Models downloaded to: {repo_path}")
38
 
39
- # Create NSFW placeholder
40
  # Create NSFW placeholder in writable directory
41
  nsfw_path = "/tmp/NSFW.jpg"
42
  if not os.path.exists(nsfw_path):
@@ -47,9 +47,9 @@ def load_models():
47
  base_ckpt="booksforcharlie/stable-diffusion-inpainting",
48
  attn_ckpt=repo_path,
49
  attn_ckpt_version="mix",
50
- weight_dtype=torch.float16, # Changed from torch.float32
51
- use_tf32=True, # Changed from False
52
- device='cuda' # Changed from 'cpu'
53
  )
54
  print("βœ… Pipeline loaded!")
55
 
@@ -62,18 +62,42 @@ def load_models():
62
  print("βœ… Automasker loaded!")
63
 
64
  except Exception as e:
65
- print(f"❌ Error loading models: {e}")
66
- import traceback
67
  traceback.print_exc()
68
  raise
69
 
70
  def generate_tryon(person_img, cloth_img, progress=gr.Progress()):
71
  """Generate virtual try-on"""
72
 
 
 
 
 
 
 
73
  if person_img is None or cloth_img is None:
74
- raise gr.Error("Please upload both person and garment images!")
 
 
75
 
76
  try:
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
77
  # Load models
78
  progress(0.05, desc="Loading models...")
79
  load_models()
@@ -110,10 +134,14 @@ def generate_tryon(person_img, cloth_img, progress=gr.Progress()):
110
 
111
  progress(1.0, desc="Complete! ✨")
112
 
 
113
  return result
114
 
115
  except Exception as e:
116
- raise gr.Error(f"Error: {str(e)}")
 
 
 
117
 
118
  # Create Gradio UI
119
  with gr.Blocks(
@@ -133,12 +161,12 @@ with gr.Blocks(
133
  gr.Markdown("### πŸ“Έ Inputs")
134
  person_input = gr.Image(
135
  label="πŸ‘€ Person Image (full body, front-facing)",
136
- type="pil",
137
  height=350
138
  )
139
  cloth_input = gr.Image(
140
  label="πŸ‘• Garment Image (flat, white background)",
141
- type="pil",
142
  height=350
143
  )
144
 
@@ -157,6 +185,7 @@ with gr.Blocks(
157
  gr.Markdown("### ✨ Result")
158
  output_img = gr.Image(
159
  label="Virtual Try-On Result",
 
160
  height=700
161
  )
162
 
@@ -173,11 +202,12 @@ with gr.Blocks(
173
  - **GPU T4:** ~2-3 minutes per generation
174
  """)
175
 
176
- # Event handler
177
  submit_btn.click(
178
  fn=generate_tryon,
179
  inputs=[person_input, cloth_input],
180
- outputs=output_img
 
181
  )
182
 
183
  # Launch app
@@ -189,5 +219,5 @@ if __name__ == "__main__":
189
  except Exception as e:
190
  print(f"⚠️ Model loading will happen on first inference: {e}")
191
 
192
- # Launch with queue
193
- demo.queue().launch()
 
5
  import gradio as gr
6
  from PIL import Image
7
  import gc
8
+ import traceback
9
  from huggingface_hub import snapshot_download
10
 
11
  # Add CatVTON to path
 
37
 
38
  print(f"βœ… Models downloaded to: {repo_path}")
39
 
 
40
  # Create NSFW placeholder in writable directory
41
  nsfw_path = "/tmp/NSFW.jpg"
42
  if not os.path.exists(nsfw_path):
 
47
  base_ckpt="booksforcharlie/stable-diffusion-inpainting",
48
  attn_ckpt=repo_path,
49
  attn_ckpt_version="mix",
50
+ weight_dtype=torch.float16,
51
+ use_tf32=True,
52
+ device='cuda'
53
  )
54
  print("βœ… Pipeline loaded!")
55
 
 
62
  print("βœ… Automasker loaded!")
63
 
64
  except Exception as e:
65
+ print(f"❌ Error loading models: {e}", file=sys.stderr)
 
66
  traceback.print_exc()
67
  raise
68
 
69
  def generate_tryon(person_img, cloth_img, progress=gr.Progress()):
70
  """Generate virtual try-on"""
71
 
72
+ # ADD EXTENSIVE LOGGING FOR API DEBUGGING
73
+ print("=" * 50, file=sys.stderr)
74
+ print(f"API CALL RECEIVED", file=sys.stderr)
75
+ print(f"Person image type: {type(person_img)}", file=sys.stderr)
76
+ print(f"Cloth image type: {type(cloth_img)}", file=sys.stderr)
77
+
78
  if person_img is None or cloth_img is None:
79
+ error_msg = "Please upload both person and garment images!"
80
+ print(f"ERROR: {error_msg}", file=sys.stderr)
81
+ raise gr.Error(error_msg)
82
 
83
  try:
84
+ # HANDLE DIFFERENT INPUT TYPES (filepath or PIL)
85
+ if isinstance(person_img, str):
86
+ print(f"Converting person_img from filepath: {person_img}", file=sys.stderr)
87
+ person_img = Image.open(person_img).convert('RGB')
88
+ elif not isinstance(person_img, Image.Image):
89
+ print(f"Converting person_img from array", file=sys.stderr)
90
+ person_img = Image.fromarray(person_img).convert('RGB')
91
+
92
+ if isinstance(cloth_img, str):
93
+ print(f"Converting cloth_img from filepath: {cloth_img}", file=sys.stderr)
94
+ cloth_img = Image.open(cloth_img).convert('RGB')
95
+ elif not isinstance(cloth_img, Image.Image):
96
+ print(f"Converting cloth_img from array", file=sys.stderr)
97
+ cloth_img = Image.fromarray(cloth_img).convert('RGB')
98
+
99
+ print(f"Images loaded successfully", file=sys.stderr)
100
+
101
  # Load models
102
  progress(0.05, desc="Loading models...")
103
  load_models()
 
134
 
135
  progress(1.0, desc="Complete! ✨")
136
 
137
+ print("SUCCESS: Try-on generated successfully", file=sys.stderr)
138
  return result
139
 
140
  except Exception as e:
141
+ error_msg = f"Error during try-on: {str(e)}"
142
+ print(f"ERROR: {error_msg}", file=sys.stderr)
143
+ traceback.print_exc()
144
+ raise gr.Error(error_msg)
145
 
146
  # Create Gradio UI
147
  with gr.Blocks(
 
161
  gr.Markdown("### πŸ“Έ Inputs")
162
  person_input = gr.Image(
163
  label="πŸ‘€ Person Image (full body, front-facing)",
164
+ type="filepath", # CHANGED FROM "pil" TO "filepath"
165
  height=350
166
  )
167
  cloth_input = gr.Image(
168
  label="πŸ‘• Garment Image (flat, white background)",
169
+ type="filepath", # CHANGED FROM "pil" TO "filepath"
170
  height=350
171
  )
172
 
 
185
  gr.Markdown("### ✨ Result")
186
  output_img = gr.Image(
187
  label="Virtual Try-On Result",
188
+ type="filepath", # ADDED type
189
  height=700
190
  )
191
 
 
202
  - **GPU T4:** ~2-3 minutes per generation
203
  """)
204
 
205
+ # Event handler with API name
206
  submit_btn.click(
207
  fn=generate_tryon,
208
  inputs=[person_input, cloth_input],
209
+ outputs=output_img,
210
+ api_name="generate_tryon" # ADDED THIS - CRITICAL FOR API ACCESS
211
  )
212
 
213
  # Launch app
 
219
  except Exception as e:
220
  print(f"⚠️ Model loading will happen on first inference: {e}")
221
 
222
+ # Launch with queue and show_error for better debugging
223
+ demo.queue().launch(show_error=True) # ADDED show_error=True