akhaliq HF Staff commited on
Commit
c6ad659
Β·
verified Β·
1 Parent(s): 1618f85

🎨 Redesign from AnyCoder (#14)

Browse files

- 🎨 Redesign from AnyCoder (a84f1413bfbc5f9ba7220f0930b18e46de97c089)

Files changed (1) hide show
  1. app.py +253 -13
app.py CHANGED
@@ -2,8 +2,7 @@ from PIL import Image
2
  import torch
3
  import gradio as gr
4
 
5
-
6
-
7
  model2 = torch.hub.load(
8
  "AK391/animegan2-pytorch:main",
9
  "generator",
@@ -12,22 +11,263 @@ model2 = torch.hub.load(
12
  progress=False
13
  )
14
 
 
 
 
 
 
 
15
 
16
- model1 = torch.hub.load("AK391/animegan2-pytorch:main", "generator", pretrained="face_paint_512_v1", device="cuda")
17
  face2paint = torch.hub.load(
18
- 'AK391/animegan2-pytorch:main', 'face2paint',
19
- size=512, device="cuda",side_by_side=False
 
 
 
20
  )
 
21
  def inference(img, ver):
22
- if ver == 'version 2 (πŸ”Ί robustness,πŸ”» stylization)':
 
23
  out = face2paint(model2, img)
24
  else:
25
  out = face2paint(model1, img)
26
  return out
27
-
28
- title = "AnimeGANv2"
29
- description = "Gradio Demo for AnimeGanv2 Face Portrait. To use it, simply upload your image, or click one of the examples to load them. Read more at the links below. Please use a cropped portrait picture for best results similar to the examples below."
30
- article = "<p style='text-align: center'><a href='https://github.com/bryandlee/animegan2-pytorch' target='_blank'>Github Repo Pytorch</a></p> <center><img src='https://visitor-badge.glitch.me/badge?page_id=akhaliq_animegan' alt='visitor badge'></center></p>"
31
- examples=[['groot.jpeg','version 2 (πŸ”Ί robustness,πŸ”» stylization)'],['bill.png','version 1 (πŸ”Ί stylization, πŸ”» robustness)'],['tony.png','version 1 (πŸ”Ί stylization, πŸ”» robustness)'],['elon.png','version 2 (πŸ”Ί robustness,πŸ”» stylization)'],['IU.png','version 1 (πŸ”Ί stylization, πŸ”» robustness)'],['billie.png','version 2 (πŸ”Ί robustness,πŸ”» stylization)'],['will.png','version 2 (πŸ”Ί robustness,πŸ”» stylization)'],['beyonce.png','version 1 (πŸ”Ί stylization, πŸ”» robustness)'],['gongyoo.jpeg','version 1 (πŸ”Ί stylization, πŸ”» robustness)']]
32
- gr.Interface(inference, [gr.inputs.Image(type="pil"),gr.inputs.Radio(['version 1 (πŸ”Ί stylization, πŸ”» robustness)','version 2 (πŸ”Ί robustness,πŸ”» stylization)'], type="value", default='version 2 (πŸ”Ί robustness,πŸ”» stylization)', label='version')
33
- ], gr.outputs.Image(type="pil"),title=title,description=description,article=article,examples=examples,allow_flagging=False,allow_screenshot=False).launch()
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2
  import torch
3
  import gradio as gr
4
 
5
+ # Load models
 
6
  model2 = torch.hub.load(
7
  "AK391/animegan2-pytorch:main",
8
  "generator",
 
11
  progress=False
12
  )
13
 
14
+ model1 = torch.hub.load(
15
+ "AK391/animegan2-pytorch:main",
16
+ "generator",
17
+ pretrained="face_paint_512_v1",
18
+ device="cuda"
19
+ )
20
 
 
21
  face2paint = torch.hub.load(
22
+ 'AK391/animegan2-pytorch:main',
23
+ 'face2paint',
24
+ size=512,
25
+ device="cuda",
26
+ side_by_side=False
27
  )
28
+
29
  def inference(img, ver):
30
+ """Convert portrait to anime style"""
31
+ if ver == 'Version 2':
32
  out = face2paint(model2, img)
33
  else:
34
  out = face2paint(model1, img)
35
  return out
36
+
37
+ # Custom CSS for modern, mobile-friendly design
38
+ custom_css = """
39
+ /* Container styling */
40
+ .gradio-container {
41
+ max-width: 1200px !important;
42
+ margin: auto !important;
43
+ }
44
+
45
+ /* Header styling */
46
+ #header {
47
+ text-align: center;
48
+ padding: 2rem 1rem 1rem 1rem;
49
+ background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
50
+ border-radius: 16px;
51
+ margin-bottom: 2rem;
52
+ color: white;
53
+ }
54
+
55
+ #header h1 {
56
+ font-size: 2.5rem;
57
+ font-weight: 800;
58
+ margin-bottom: 0.5rem;
59
+ letter-spacing: -0.5px;
60
+ }
61
+
62
+ #header p {
63
+ font-size: 1.1rem;
64
+ opacity: 0.95;
65
+ max-width: 600px;
66
+ margin: 0 auto;
67
+ line-height: 1.6;
68
+ }
69
+
70
+ /* Main content area */
71
+ #main-content {
72
+ background: rgba(255, 255, 255, 0.05);
73
+ border-radius: 16px;
74
+ padding: 2rem;
75
+ margin-bottom: 2rem;
76
+ }
77
+
78
+ /* Image containers */
79
+ .image-container {
80
+ border-radius: 12px;
81
+ overflow: hidden;
82
+ box-shadow: 0 4px 6px rgba(0, 0, 0, 0.1);
83
+ }
84
+
85
+ /* Button styling */
86
+ .primary-btn button {
87
+ background: linear-gradient(135deg, #667eea 0%, #764ba2 100%) !important;
88
+ border: none !important;
89
+ font-weight: 600 !important;
90
+ font-size: 1.1rem !important;
91
+ padding: 0.75rem 2rem !important;
92
+ border-radius: 12px !important;
93
+ transition: transform 0.2s ease !important;
94
+ }
95
+
96
+ .primary-btn button:hover {
97
+ transform: translateY(-2px) !important;
98
+ box-shadow: 0 8px 16px rgba(102, 126, 234, 0.3) !important;
99
+ }
100
+
101
+ /* Radio button styling */
102
+ .version-selector label {
103
+ font-weight: 600 !important;
104
+ font-size: 1rem !important;
105
+ margin-bottom: 0.75rem !important;
106
+ }
107
+
108
+ /* Examples styling */
109
+ #examples {
110
+ margin-top: 2rem;
111
+ }
112
+
113
+ /* Footer */
114
+ #footer {
115
+ text-align: center;
116
+ padding: 2rem 1rem;
117
+ margin-top: 2rem;
118
+ border-top: 1px solid rgba(255, 255, 255, 0.1);
119
+ }
120
+
121
+ #footer a {
122
+ color: #667eea;
123
+ text-decoration: none;
124
+ font-weight: 600;
125
+ transition: color 0.2s ease;
126
+ }
127
+
128
+ #footer a:hover {
129
+ color: #764ba2;
130
+ }
131
+
132
+ /* Mobile responsiveness */
133
+ @media (max-width: 768px) {
134
+ #header h1 {
135
+ font-size: 2rem;
136
+ }
137
+
138
+ #header p {
139
+ font-size: 1rem;
140
+ }
141
+
142
+ #main-content {
143
+ padding: 1rem;
144
+ }
145
+ }
146
+ """
147
+
148
+ # Create the Gradio interface
149
+ with gr.Blocks(fill_height=True) as demo:
150
+
151
+ # Header
152
+ with gr.Column(elem_id="header"):
153
+ gr.Markdown("# ⚑ AnimeGAN v2")
154
+ gr.Markdown("Transform your portraits into stunning anime-style artwork")
155
+
156
+ # Main content
157
+ with gr.Column(elem_id="main-content"):
158
+ with gr.Row():
159
+ with gr.Column(scale=1):
160
+ input_image = gr.Image(
161
+ label="πŸ“Έ Upload Your Portrait",
162
+ type="pil",
163
+ sources=["upload", "clipboard"],
164
+ height=400,
165
+ elem_classes="image-container"
166
+ )
167
+
168
+ version_selector = gr.Radio(
169
+ choices=[
170
+ 'Version 1',
171
+ 'Version 2'
172
+ ],
173
+ value='Version 2',
174
+ label="🎨 Style Version",
175
+ info="Version 1: More stylized | Version 2: More robust",
176
+ elem_classes="version-selector"
177
+ )
178
+
179
+ with gr.Row():
180
+ submit_btn = gr.Button(
181
+ "✨ Generate Anime Style",
182
+ variant="primary",
183
+ size="lg",
184
+ elem_classes="primary-btn"
185
+ )
186
+
187
+ with gr.Column(scale=1):
188
+ output_image = gr.Image(
189
+ label="🎨 Anime Result",
190
+ type="pil",
191
+ height=400,
192
+ interactive=False,
193
+ elem_classes="image-container"
194
+ )
195
+
196
+ # Examples section
197
+ with gr.Column(elem_id="examples"):
198
+ gr.Markdown("### πŸ’« Try These Examples")
199
+ gr.Examples(
200
+ examples=[
201
+ ['groot.jpeg', 'Version 2'],
202
+ ['bill.png', 'Version 1'],
203
+ ['tony.png', 'Version 1'],
204
+ ['elon.png', 'Version 2'],
205
+ ['IU.png', 'Version 1'],
206
+ ['billie.png', 'Version 2'],
207
+ ['will.png', 'Version 2'],
208
+ ['beyonce.png', 'Version 1'],
209
+ ['gongyoo.jpeg', 'Version 1']
210
+ ],
211
+ inputs=[input_image, version_selector],
212
+ outputs=output_image,
213
+ fn=inference,
214
+ cache_examples=False,
215
+ examples_per_page=9
216
+ )
217
+
218
+ # Footer
219
+ with gr.Column(elem_id="footer"):
220
+ gr.Markdown(
221
+ """
222
+ <div style='display: flex; justify-content: center; align-items: center; gap: 1rem; flex-wrap: wrap;'>
223
+ <a href='https://github.com/bryandlee/animegan2-pytorch' target='_blank'>
224
+ πŸ“š GitHub Repository
225
+ </a>
226
+ <span style='color: rgba(255,255,255,0.3);'>β€’</span>
227
+ <a href='https://huggingface.co/spaces/akhaliq/anycoder' target='_blank'>
228
+ πŸš€ Built with anycoder
229
+ </a>
230
+ </div>
231
+
232
+ <p style='margin-top: 1rem; font-size: 0.9rem; color: rgba(255,255,255,0.6);'>
233
+ πŸ’‘ <strong>Tip:</strong> Use a cropped portrait photo for best results
234
+ </p>
235
+ """
236
+ )
237
+
238
+ # Event handlers
239
+ submit_btn.click(
240
+ fn=inference,
241
+ inputs=[input_image, version_selector],
242
+ outputs=output_image,
243
+ api_name="generate"
244
+ )
245
+
246
+ input_image.change(
247
+ fn=inference,
248
+ inputs=[input_image, version_selector],
249
+ outputs=output_image,
250
+ show_progress="minimal"
251
+ )
252
+
253
+ # Launch with modern theme
254
+ demo.launch(
255
+ theme=gr.themes.Soft(
256
+ primary_hue="indigo",
257
+ secondary_hue="purple",
258
+ neutral_hue="slate",
259
+ font=gr.themes.GoogleFont("Inter"),
260
+ text_size="lg",
261
+ spacing_size="lg",
262
+ radius_size="lg"
263
+ ).set(
264
+ button_primary_background_fill="*primary_600",
265
+ button_primary_background_fill_hover="*primary_700",
266
+ block_title_text_weight="600",
267
+ block_label_text_weight="600",
268
+ ),
269
+ css=custom_css,
270
+ footer_links=[
271
+ {"label": "Built with anycoder", "url": "https://huggingface.co/spaces/akhaliq/anycoder"}
272
+ ]
273
+ )