Spaces:
Running
on
Zero
Running
on
Zero
File size: 11,270 Bytes
56a97f7 148a19e c250292 462c8e0 dd53e8c 5fe7560 50b7dca dd8ad63 46bd6e1 810d24d 5fe7560 b767bd0 46bd6e1 d08bf1f c6ad659 5fe7560 c6ad659 ed3cb4e 148a19e c6ad659 5fe7560 c6ad659 148a19e c6ad659 92d2058 72015f8 c6ad659 5fe7560 72015f8 5fe7560 148a19e c6ad659 92d2058 c6ad659 92d2058 c6ad659 92d2058 c6ad659 92d2058 c6ad659 92d2058 c6ad659 92d2058 c6ad659 92d2058 c6ad659 92d2058 c6ad659 92d2058 c6ad659 92d2058 c6ad659 92d2058 c6ad659 92d2058 c6ad659 92d2058 c6ad659 92d2058 c6ad659 92d2058 c6ad659 92d2058 c6ad659 92d2058 c6ad659 92d2058 c6ad659 92d2058 c6ad659 92d2058 c6ad659 92d2058 c6ad659 92d2058 c6ad659 92d2058 c6ad659 92d2058 c6ad659 92d2058 c6ad659 92d2058 c6ad659 92d2058 c6ad659 92d2058 c6ad659 92d2058 c6ad659 92d2058 c6ad659 92d2058 c6ad659 92d2058 c6ad659 92d2058 c6ad659 92d2058 c6ad659 92d2058 c6ad659 92d2058 c6ad659 92d2058 c6ad659 92d2058 c6ad659 92d2058 462c8e0 c6ad659 92d2058 c6ad659 92d2058 c6ad659 92d2058 c6ad659 92d2058 c6ad659 92d2058 c6ad659 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 |
from PIL import Image
import torch
import gradio as gr
import spaces
# Load models on CPU initially
model2 = torch.hub.load(
"AK391/animegan2-pytorch:main",
"generator",
pretrained=True,
device="cpu",
progress=False
)
model1 = torch.hub.load(
"AK391/animegan2-pytorch:main",
"generator",
pretrained="face_paint_512_v1",
device="cpu"
)
face2paint = torch.hub.load(
'AK391/animegan2-pytorch:main',
'face2paint',
size=512,
device="cpu",
side_by_side=False
)
@spaces.GPU
def inference(img, ver):
"""Convert portrait to anime style"""
if ver == 'Version 2':
model = model2
else:
model = model1
model = model.to('cuda')
out = face2paint(model, img, device='cuda')
return out
# Apple-inspired CSS with glassmorphism and smooth animations
custom_css = """
@import url('https://fonts.googleapis.com/css2?family=SF+Pro+Display:wght@300;400;500;600;700&display=swap');
/* Global styling with Apple aesthetics */
.gradio-container {
max-width: 1400px !important;
margin: auto !important;
font-family: -apple-system, BlinkMacSystemFont, 'SF Pro Display', 'Segoe UI', Roboto, sans-serif !important;
background: linear-gradient(135deg, #f5f7fa 0%, #c3cfe2 100%) !important;
}
/* Glassmorphism header */
#header {
text-align: center;
padding: 3rem 2rem 2rem 2rem;
background: rgba(255, 255, 255, 0.7);
backdrop-filter: blur(20px) saturate(180%);
-webkit-backdrop-filter: blur(20px) saturate(180%);
border-radius: 24px;
margin-bottom: 2rem;
border: 1px solid rgba(255, 255, 255, 0.8);
box-shadow: 0 8px 32px rgba(0, 0, 0, 0.08);
}
#header h1 {
font-size: 3.5rem;
font-weight: 700;
margin-bottom: 0.75rem;
letter-spacing: -1.5px;
background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
-webkit-background-clip: text;
-webkit-text-fill-color: transparent;
background-clip: text;
}
#header p {
font-size: 1.2rem;
color: #6b7280;
max-width: 700px;
margin: 0 auto;
line-height: 1.7;
font-weight: 400;
}
/* Main content card with Apple-style elevation */
#main-content {
background: rgba(255, 255, 255, 0.85);
backdrop-filter: blur(20px) saturate(180%);
-webkit-backdrop-filter: blur(20px) saturate(180%);
border-radius: 24px;
padding: 2.5rem;
margin-bottom: 2rem;
border: 1px solid rgba(255, 255, 255, 0.9);
box-shadow: 0 12px 48px rgba(0, 0, 0, 0.1);
}
/* Image containers with smooth corners */
.image-container {
border-radius: 16px;
overflow: hidden;
box-shadow: 0 4px 24px rgba(0, 0, 0, 0.08);
transition: all 0.3s cubic-bezier(0.4, 0, 0.2, 1);
}
.image-container:hover {
box-shadow: 0 8px 32px rgba(0, 0, 0, 0.12);
transform: translateY(-2px);
}
/* Apple-style button */
.primary-btn button {
background: linear-gradient(180deg, #667eea 0%, #5a67d8 100%) !important;
border: none !important;
font-weight: 600 !important;
font-size: 1.05rem !important;
padding: 0.875rem 2.5rem !important;
border-radius: 12px !important;
transition: all 0.3s cubic-bezier(0.4, 0, 0.2, 1) !important;
box-shadow: 0 4px 16px rgba(102, 126, 234, 0.3) !important;
letter-spacing: -0.2px !important;
}
.primary-btn button:hover {
transform: translateY(-2px) !important;
box-shadow: 0 8px 24px rgba(102, 126, 234, 0.4) !important;
background: linear-gradient(180deg, #5a67d8 0%, #4c51bf 100%) !important;
}
.primary-btn button:active {
transform: translateY(0px) !important;
box-shadow: 0 2px 8px rgba(102, 126, 234, 0.3) !important;
}
/* Radio button styling - Apple segmented control inspired */
.version-selector label {
font-weight: 600 !important;
font-size: 0.95rem !important;
margin-bottom: 0.75rem !important;
color: #374151 !important;
letter-spacing: -0.2px !important;
}
.version-selector .wrap {
background: rgba(243, 244, 246, 0.8) !important;
border-radius: 12px !important;
padding: 0.5rem !important;
}
/* Examples section */
#examples {
margin-top: 2.5rem;
background: rgba(255, 255, 255, 0.7);
backdrop-filter: blur(20px) saturate(180%);
-webkit-backdrop-filter: blur(20px) saturate(180%);
border-radius: 20px;
padding: 2rem;
border: 1px solid rgba(255, 255, 255, 0.8);
box-shadow: 0 8px 32px rgba(0, 0, 0, 0.06);
}
#examples h3 {
font-weight: 600;
letter-spacing: -0.5px;
color: #1f2937;
margin-bottom: 1.5rem;
}
/* Example items */
.gradio-examples {
gap: 1rem !important;
}
.gradio-examples button {
border-radius: 12px !important;
transition: all 0.3s cubic-bezier(0.4, 0, 0.2, 1) !important;
border: 1px solid rgba(229, 231, 235, 0.8) !important;
}
.gradio-examples button:hover {
transform: scale(1.02) !important;
box-shadow: 0 4px 16px rgba(0, 0, 0, 0.1) !important;
}
/* Footer with subtle styling */
#footer {
text-align: center;
padding: 2.5rem 1rem;
margin-top: 2rem;
background: rgba(255, 255, 255, 0.5);
backdrop-filter: blur(20px) saturate(180%);
-webkit-backdrop-filter: blur(20px) saturate(180%);
border-radius: 20px;
border: 1px solid rgba(255, 255, 255, 0.7);
}
#footer a {
color: #667eea;
text-decoration: none;
font-weight: 600;
transition: all 0.2s ease;
letter-spacing: -0.2px;
}
#footer a:hover {
color: #5a67d8;
text-decoration: underline;
}
/* Input/Output labels */
.block-label {
font-weight: 600 !important;
font-size: 0.95rem !important;
color: #374151 !important;
letter-spacing: -0.2px !important;
margin-bottom: 0.75rem !important;
}
/* Smooth scrollbar */
::-webkit-scrollbar {
width: 10px;
}
::-webkit-scrollbar-track {
background: rgba(243, 244, 246, 0.5);
border-radius: 10px;
}
::-webkit-scrollbar-thumb {
background: rgba(156, 163, 175, 0.5);
border-radius: 10px;
transition: all 0.3s ease;
}
::-webkit-scrollbar-thumb:hover {
background: rgba(107, 114, 128, 0.7);
}
/* Mobile responsiveness */
@media (max-width: 768px) {
#header h1 {
font-size: 2.5rem;
}
#header p {
font-size: 1rem;
}
#main-content {
padding: 1.5rem;
}
.primary-btn button {
width: 100% !important;
}
}
/* Smooth transitions for all interactive elements */
* {
transition: background-color 0.3s cubic-bezier(0.4, 0, 0.2, 1),
border-color 0.3s cubic-bezier(0.4, 0, 0.2, 1),
color 0.3s cubic-bezier(0.4, 0, 0.2, 1);
}
"""
# Create the Gradio interface
with gr.Blocks(fill_height=True) as demo:
# Header
with gr.Column(elem_id="header"):
gr.Markdown("# ⚡ AnimeGAN v2")
gr.Markdown("Transform your portraits into stunning anime-style artwork with AI")
# Main content
with gr.Column(elem_id="main-content"):
with gr.Row():
with gr.Column(scale=1):
input_image = gr.Image(
label="📸 Upload Your Portrait",
type="pil",
sources=["upload", "clipboard"],
height=400,
elem_classes="image-container"
)
version_selector = gr.Radio(
choices=[
'Version 1',
'Version 2'
],
value='Version 2',
label="🎨 Style Version",
info="Version 1: More stylized | Version 2: More robust",
elem_classes="version-selector"
)
with gr.Row():
submit_btn = gr.Button(
"✨ Generate Anime Style",
variant="primary",
size="lg",
elem_classes="primary-btn"
)
with gr.Column(scale=1):
output_image = gr.Image(
label="🎨 Anime Result",
type="pil",
height=400,
interactive=False,
elem_classes="image-container"
)
# Examples section
with gr.Column(elem_id="examples"):
gr.Markdown("### 💫 Try These Examples")
gr.Examples(
examples=[
['groot.jpeg', 'Version 2'],
['bill.png', 'Version 1'],
['tony.png', 'Version 1'],
['elon.png', 'Version 2'],
['IU.png', 'Version 1'],
['billie.png', 'Version 2'],
['will.png', 'Version 2'],
['beyonce.png', 'Version 1'],
['gongyoo.jpeg', 'Version 1']
],
inputs=[input_image, version_selector],
outputs=output_image,
fn=inference,
cache_examples=False,
examples_per_page=9
)
# Footer
with gr.Column(elem_id="footer"):
gr.Markdown(
"""
<div style='display: flex; justify-content: center; align-items: center; gap: 1.5rem; flex-wrap: wrap;'>
<a href='https://github.com/bryandlee/animegan2-pytorch' target='_blank'>
📚 GitHub Repository
</a>
<span style='color: rgba(107, 114, 128, 0.3);'>•</span>
<a href='https://huggingface.co/spaces/akhaliq/anycoder' target='_blank'>
🚀 Built with anycoder
</a>
</div>
<p style='margin-top: 1.5rem; font-size: 0.95rem; color: #6b7280;'>
💡 <strong>Tip:</strong> Use a cropped portrait photo for best results
</p>
<p style='margin-top: 0.75rem; font-size: 0.9rem; color: #9ca3af;'>
⚡ Powered by Zero GPU - efficient GPU usage on Hugging Face Spaces
</p>
"""
)
# Event handlers
submit_btn.click(
fn=inference,
inputs=[input_image, version_selector],
outputs=output_image,
api_name="generate"
)
input_image.change(
fn=inference,
inputs=[input_image, version_selector],
outputs=output_image,
show_progress="minimal"
)
# Launch with Apple-inspired clean theme
demo.launch(
theme=gr.themes.Soft(
primary_hue="blue",
secondary_hue="slate",
neutral_hue="slate",
font=gr.themes.GoogleFont("Inter"),
text_size="md",
spacing_size="lg",
radius_size="lg"
).set(
button_primary_background_fill="linear-gradient(180deg, #667eea 0%, #5a67d8 100%)",
button_primary_background_fill_hover="linear-gradient(180deg, #5a67d8 0%, #4c51bf 100%)",
block_title_text_weight="600",
block_label_text_weight="600",
block_background_fill="rgba(255, 255, 255, 0.7)",
input_background_fill="rgba(255, 255, 255, 0.9)",
),
css=custom_css,
footer_links=[
{"label": "Built with anycoder", "url": "https://huggingface.co/spaces/akhaliq/anycoder"}
]
) |