Spaces:
Running on Zero
Running on Zero
File size: 7,122 Bytes
56a97f7 148a19e c250292 dd53e8c c6ad659 50b7dca dd8ad63 46bd6e1 810d24d a9636e0 b767bd0 46bd6e1 d08bf1f c6ad659 ed3cb4e 148a19e c6ad659 148a19e c6ad659 72015f8 c6ad659 72015f8 148a19e 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 | from PIL import Image
import torch
import gradio as gr
# Load models
model2 = torch.hub.load(
"AK391/animegan2-pytorch:main",
"generator",
pretrained=True,
device="cuda",
progress=False
)
model1 = torch.hub.load(
"AK391/animegan2-pytorch:main",
"generator",
pretrained="face_paint_512_v1",
device="cuda"
)
face2paint = torch.hub.load(
'AK391/animegan2-pytorch:main',
'face2paint',
size=512,
device="cuda",
side_by_side=False
)
def inference(img, ver):
"""Convert portrait to anime style"""
if ver == 'Version 2':
out = face2paint(model2, img)
else:
out = face2paint(model1, img)
return out
# Custom CSS for modern, mobile-friendly design
custom_css = """
/* Container styling */
.gradio-container {
max-width: 1200px !important;
margin: auto !important;
}
/* Header styling */
#header {
text-align: center;
padding: 2rem 1rem 1rem 1rem;
background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
border-radius: 16px;
margin-bottom: 2rem;
color: white;
}
#header h1 {
font-size: 2.5rem;
font-weight: 800;
margin-bottom: 0.5rem;
letter-spacing: -0.5px;
}
#header p {
font-size: 1.1rem;
opacity: 0.95;
max-width: 600px;
margin: 0 auto;
line-height: 1.6;
}
/* Main content area */
#main-content {
background: rgba(255, 255, 255, 0.05);
border-radius: 16px;
padding: 2rem;
margin-bottom: 2rem;
}
/* Image containers */
.image-container {
border-radius: 12px;
overflow: hidden;
box-shadow: 0 4px 6px rgba(0, 0, 0, 0.1);
}
/* Button styling */
.primary-btn button {
background: linear-gradient(135deg, #667eea 0%, #764ba2 100%) !important;
border: none !important;
font-weight: 600 !important;
font-size: 1.1rem !important;
padding: 0.75rem 2rem !important;
border-radius: 12px !important;
transition: transform 0.2s ease !important;
}
.primary-btn button:hover {
transform: translateY(-2px) !important;
box-shadow: 0 8px 16px rgba(102, 126, 234, 0.3) !important;
}
/* Radio button styling */
.version-selector label {
font-weight: 600 !important;
font-size: 1rem !important;
margin-bottom: 0.75rem !important;
}
/* Examples styling */
#examples {
margin-top: 2rem;
}
/* Footer */
#footer {
text-align: center;
padding: 2rem 1rem;
margin-top: 2rem;
border-top: 1px solid rgba(255, 255, 255, 0.1);
}
#footer a {
color: #667eea;
text-decoration: none;
font-weight: 600;
transition: color 0.2s ease;
}
#footer a:hover {
color: #764ba2;
}
/* Mobile responsiveness */
@media (max-width: 768px) {
#header h1 {
font-size: 2rem;
}
#header p {
font-size: 1rem;
}
#main-content {
padding: 1rem;
}
}
"""
# 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")
# 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: 1rem; flex-wrap: wrap;'>
<a href='https://github.com/bryandlee/animegan2-pytorch' target='_blank'>
📚 GitHub Repository
</a>
<span style='color: rgba(255,255,255,0.3);'>•</span>
<a href='https://huggingface.co/spaces/akhaliq/anycoder' target='_blank'>
🚀 Built with anycoder
</a>
</div>
<p style='margin-top: 1rem; font-size: 0.9rem; color: rgba(255,255,255,0.6);'>
💡 <strong>Tip:</strong> Use a cropped portrait photo for best results
</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 modern theme
demo.launch(
theme=gr.themes.Soft(
primary_hue="indigo",
secondary_hue="purple",
neutral_hue="slate",
font=gr.themes.GoogleFont("Inter"),
text_size="lg",
spacing_size="lg",
radius_size="lg"
).set(
button_primary_background_fill="*primary_600",
button_primary_background_fill_hover="*primary_700",
block_title_text_weight="600",
block_label_text_weight="600",
),
css=custom_css,
footer_links=[
{"label": "Built with anycoder", "url": "https://huggingface.co/spaces/akhaliq/anycoder"}
]
) |