Spaces:
Running
on
Zero
Running
on
Zero
Update app.py
Browse files
app.py
CHANGED
|
@@ -71,53 +71,66 @@ img_transforms = transforms.Compose([
|
|
| 71 |
transforms.Normalize(means,stds)
|
| 72 |
])
|
| 73 |
|
| 74 |
-
# Load models globally
|
| 75 |
-
|
| 76 |
-
|
| 77 |
-
|
| 78 |
|
| 79 |
@spaces.GPU
|
| 80 |
-
def proc_pil_img(input_image,
|
| 81 |
"""GPU-accelerated image processing"""
|
| 82 |
-
#
|
| 83 |
-
|
| 84 |
-
t_means = torch.tensor(means).cuda().half()[:,None,None]
|
| 85 |
|
| 86 |
-
#
|
| 87 |
-
|
|
|
|
| 88 |
|
| 89 |
-
|
|
|
|
| 90 |
|
| 91 |
with torch.no_grad():
|
| 92 |
result_image = model(transformed_image)[0]
|
| 93 |
-
output_image = result_image.mul(t_stds).add(t_means).mul(255.).clamp(0,255).permute(1,2,0)
|
| 94 |
-
output_image = output_image.
|
| 95 |
output_image = PIL.Image.fromarray(output_image)
|
| 96 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 97 |
return output_image
|
| 98 |
|
| 99 |
@spaces.GPU
|
| 100 |
def process(im, version):
|
| 101 |
"""Main processing function with GPU acceleration"""
|
| 102 |
if im is None:
|
| 103 |
-
|
| 104 |
-
|
| 105 |
-
# Ensure image is PIL Image
|
| 106 |
-
if not isinstance(im, Image.Image):
|
| 107 |
-
im = Image.fromarray(im)
|
| 108 |
-
|
| 109 |
-
# Scale image (CPU operation)
|
| 110 |
-
im = scale_by_face_size(im, target_face=256, max_res=1_500_000, max_upscale=1)
|
| 111 |
|
| 112 |
-
|
| 113 |
-
|
| 114 |
-
|
| 115 |
-
|
| 116 |
-
|
| 117 |
-
|
| 118 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 119 |
|
| 120 |
-
|
|
|
|
| 121 |
|
| 122 |
# Custom theme
|
| 123 |
custom_theme = gr.themes.Soft(
|
|
@@ -287,21 +300,16 @@ with gr.Blocks() as demo:
|
|
| 287 |
buttons=["download", "share"]
|
| 288 |
)
|
| 289 |
|
| 290 |
-
#
|
| 291 |
-
gr.Markdown("### 🖼️ Try These Examples", elem_id="examples-header")
|
| 292 |
with gr.Row():
|
| 293 |
-
gr.
|
| 294 |
-
|
| 295 |
-
|
| 296 |
-
|
| 297 |
-
|
| 298 |
-
|
| 299 |
-
|
| 300 |
-
|
| 301 |
-
fn=process,
|
| 302 |
-
cache_examples=False,
|
| 303 |
-
label="Example Images",
|
| 304 |
-
examples_per_page=3
|
| 305 |
)
|
| 306 |
|
| 307 |
# Footer
|
|
@@ -315,6 +323,11 @@ with gr.Blocks() as demo:
|
|
| 315 |
|
| 316 |
**⚡ Zero-GPU Optimization**: This Space uses Hugging Face's Zero-GPU infrastructure for efficient GPU allocation.
|
| 317 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 318 |
<div style='margin-top: 1rem;'>
|
| 319 |
<img src='https://visitor-badge.glitch.me/badge?page_id=akhaliq_arcanegan' alt='visitor badge'>
|
| 320 |
</div>
|
|
|
|
| 71 |
transforms.Normalize(means,stds)
|
| 72 |
])
|
| 73 |
|
| 74 |
+
# Load models globally on CPU
|
| 75 |
+
modelv4_cpu = torch.jit.load(modelarcanev4, map_location='cpu').eval()
|
| 76 |
+
modelv3_cpu = torch.jit.load(modelarcanev3, map_location='cpu').eval()
|
| 77 |
+
modelv2_cpu = torch.jit.load(modelarcanev2, map_location='cpu').eval()
|
| 78 |
|
| 79 |
@spaces.GPU
|
| 80 |
+
def proc_pil_img(input_image, model_path):
|
| 81 |
"""GPU-accelerated image processing"""
|
| 82 |
+
# Load model fresh on GPU to avoid device mismatch
|
| 83 |
+
model = torch.jit.load(model_path, map_location='cuda').eval()
|
|
|
|
| 84 |
|
| 85 |
+
# Create tensors on GPU
|
| 86 |
+
t_stds = torch.tensor(stds).cuda().view(3, 1, 1)
|
| 87 |
+
t_means = torch.tensor(means).cuda().view(3, 1, 1)
|
| 88 |
|
| 89 |
+
# Transform and move to GPU
|
| 90 |
+
transformed_image = img_transforms(input_image).unsqueeze(0).cuda()
|
| 91 |
|
| 92 |
with torch.no_grad():
|
| 93 |
result_image = model(transformed_image)[0]
|
| 94 |
+
output_image = result_image.mul(t_stds).add(t_means).mul(255.).clamp(0, 255).permute(1, 2, 0)
|
| 95 |
+
output_image = output_image.cpu().numpy().astype('uint8')
|
| 96 |
output_image = PIL.Image.fromarray(output_image)
|
| 97 |
|
| 98 |
+
# Clean up
|
| 99 |
+
del model
|
| 100 |
+
torch.cuda.empty_cache()
|
| 101 |
+
|
| 102 |
return output_image
|
| 103 |
|
| 104 |
@spaces.GPU
|
| 105 |
def process(im, version):
|
| 106 |
"""Main processing function with GPU acceleration"""
|
| 107 |
if im is None:
|
| 108 |
+
raise gr.Error("Please upload an image first!")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 109 |
|
| 110 |
+
try:
|
| 111 |
+
# Ensure image is PIL Image
|
| 112 |
+
if not isinstance(im, Image.Image):
|
| 113 |
+
im = Image.fromarray(im)
|
| 114 |
+
|
| 115 |
+
# Convert to RGB if needed
|
| 116 |
+
if im.mode != 'RGB':
|
| 117 |
+
im = im.convert('RGB')
|
| 118 |
+
|
| 119 |
+
# Scale image (CPU operation)
|
| 120 |
+
im = scale_by_face_size(im, target_face=256, max_res=1_500_000, max_upscale=1)
|
| 121 |
+
|
| 122 |
+
# Select model based on version
|
| 123 |
+
if version == 'v0.4 (Recommended)':
|
| 124 |
+
res = proc_pil_img(im, modelarcanev4)
|
| 125 |
+
elif version == 'v0.3':
|
| 126 |
+
res = proc_pil_img(im, modelarcanev3)
|
| 127 |
+
else:
|
| 128 |
+
res = proc_pil_img(im, modelarcanev2)
|
| 129 |
+
|
| 130 |
+
return res
|
| 131 |
|
| 132 |
+
except Exception as e:
|
| 133 |
+
raise gr.Error(f"Error processing image: {str(e)}")
|
| 134 |
|
| 135 |
# Custom theme
|
| 136 |
custom_theme = gr.themes.Soft(
|
|
|
|
| 300 |
buttons=["download", "share"]
|
| 301 |
)
|
| 302 |
|
| 303 |
+
# Tips section
|
|
|
|
| 304 |
with gr.Row():
|
| 305 |
+
gr.Markdown(
|
| 306 |
+
"""
|
| 307 |
+
### 💡 Tips for Best Results
|
| 308 |
+
- Use clear, well-lit portrait photos
|
| 309 |
+
- Face should be clearly visible and not too small
|
| 310 |
+
- Works best with frontal or slightly angled faces
|
| 311 |
+
- Try different model versions for varied artistic styles
|
| 312 |
+
"""
|
|
|
|
|
|
|
|
|
|
|
|
|
| 313 |
)
|
| 314 |
|
| 315 |
# Footer
|
|
|
|
| 323 |
|
| 324 |
**⚡ Zero-GPU Optimization**: This Space uses Hugging Face's Zero-GPU infrastructure for efficient GPU allocation.
|
| 325 |
|
| 326 |
+
**Model Versions:**
|
| 327 |
+
- **v0.4**: Latest and recommended - best quality and style accuracy
|
| 328 |
+
- **v0.3**: Alternative style interpretation
|
| 329 |
+
- **v0.2**: Original version with unique characteristics
|
| 330 |
+
|
| 331 |
<div style='margin-top: 1rem;'>
|
| 332 |
<img src='https://visitor-badge.glitch.me/badge?page_id=akhaliq_arcanegan' alt='visitor badge'>
|
| 333 |
</div>
|