Plana-Archive commited on
Commit
784c10e
Β·
verified Β·
1 Parent(s): 959b41c

Migrated content from Space: Plana-Archive/full-body-anime-gan

Browse files
Mutsumi-Chan/full-body-anime-gan/Dockerfile ADDED
@@ -0,0 +1,18 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ FROM python:3.11-slim
2
+
3
+ RUN apt-get update && apt-get install -y \
4
+ git git-lfs ffmpeg libsm6 libxext6 cmake rsync libgl1 \
5
+ && rm -rf /var/lib/apt/lists/* \
6
+ && git lfs install
7
+
8
+ WORKDIR /app
9
+
10
+ COPY requirements.txt .
11
+ RUN pip install --no-cache-dir -r requirements.txt
12
+
13
+ COPY . .
14
+
15
+ ENV GRADIO_SERVER_PORT=7860
16
+ ENV GRADIO_SERVER_NAME="0.0.0.0"
17
+
18
+ CMD ["python", "app.py"]
Mutsumi-Chan/full-body-anime-gan/README.md ADDED
@@ -0,0 +1,13 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ ---
2
+ title: FAST! Generate Image Anime
3
+ emoji: 🎨
4
+ colorFrom: gray
5
+ colorTo: pink
6
+ sdk: gradio
7
+ sdk_version: 5.20.0
8
+ python_version: '3.11'
9
+ app_file: app.py
10
+ pinned: false
11
+ license: apache-2.0
12
+ short_description: Generate Image Anime πŸ–Ό
13
+ ---
Mutsumi-Chan/full-body-anime-gan/app.py ADDED
@@ -0,0 +1,296 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # ============================================================
2
+ # PATCH 1: HfFolder hilang di huggingface_hub >= 0.30
3
+ # ============================================================
4
+ import huggingface_hub
5
+ if not hasattr(huggingface_hub, 'HfFolder'):
6
+ class HfFolder:
7
+ @staticmethod
8
+ def get_token():
9
+ return None
10
+ @staticmethod
11
+ def save_token(token):
12
+ pass
13
+ @staticmethod
14
+ def delete_token():
15
+ pass
16
+ huggingface_hub.HfFolder = HfFolder
17
+
18
+ # ============================================================
19
+ # PATCH 2: Bug TypeError di gradio_client >= 1.4
20
+ # Patch fungsi get_type agar tidak crash saat schema adalah bool
21
+ # ============================================================
22
+ try:
23
+ import gradio_client.utils as _gcu
24
+
25
+ _original_get_type = _gcu.get_type
26
+
27
+ def _patched_get_type(schema):
28
+ if not isinstance(schema, dict):
29
+ return "Any"
30
+ return _original_get_type(schema)
31
+
32
+ _gcu.get_type = _patched_get_type
33
+
34
+ _original_json_schema = _gcu._json_schema_to_python_type
35
+
36
+ def _patched_json_schema(schema, defs=None):
37
+ if not isinstance(schema, dict):
38
+ return "Any"
39
+ try:
40
+ return _original_json_schema(schema, defs)
41
+ except TypeError:
42
+ return "Any"
43
+
44
+ _gcu._json_schema_to_python_type = _patched_json_schema
45
+
46
+ except Exception as e:
47
+ print(f"[WARN] gradio_client patch failed: {e}")
48
+
49
+ import random
50
+ import gradio as gr
51
+ import numpy as np
52
+ import onnxruntime as rt
53
+ from numpy.random import RandomState
54
+ from skimage import transform
55
+
56
+ # --- CORE FUNCTIONS ---
57
+ def get_inter(r1, r2):
58
+ h_inter = max(min(r1[3], r2[3]) - max(r1[1], r2[1]), 0)
59
+ w_inter = max(min(r1[2], r2[2]) - max(r1[0], r2[0]), 0)
60
+ return h_inter * w_inter
61
+
62
+
63
+ def iou(r1, r2):
64
+ s1 = (r1[2] - r1[0]) * (r1[3] - r1[1])
65
+ s2 = (r2[2] - r2[0]) * (r2[3] - r2[1])
66
+ i = get_inter(r1, r2)
67
+ return i / (s1 + s2 - i)
68
+
69
+
70
+ def letterbox(im, new_shape=(640, 640), color=(0.5, 0.5, 0.5), stride=32):
71
+ shape = im.shape[:2]
72
+ r = min(new_shape[0] / shape[0], new_shape[1] / shape[1])
73
+ new_unpad = int(round(shape[1] * r)), int(round(shape[0] * r))
74
+ dw, dh = new_shape[1] - new_unpad[0], new_shape[0] - new_unpad[1]
75
+ dw, dh = np.mod(dw, stride), np.mod(dh, stride)
76
+ dw /= 2
77
+ dh /= 2
78
+ if shape != new_unpad:
79
+ im = transform.resize(im, (new_unpad[1], new_unpad[0]))
80
+ top, bottom = int(round(dh - 0.1)), int(round(dh + 0.1))
81
+ left, right = int(round(dw - 0.1)), int(round(dw + 0.1))
82
+ im_new = np.full((new_unpad[1] + top + bottom, new_unpad[0] + left + right, 3), color, dtype=np.float32)
83
+ im_new[top:new_unpad[1] + top, left:new_unpad[0] + left] = im
84
+ return im_new
85
+
86
+
87
+ def nms(pred, conf_thres, iou_thres, max_instance=20):
88
+ nc = pred.shape[1] - 5
89
+ candidates = [list() for _ in range(nc)]
90
+ for x in pred:
91
+ if x[4] < conf_thres:
92
+ continue
93
+ cls = np.argmax(x[5:])
94
+ p = x[4] * x[5 + cls]
95
+ if conf_thres <= p:
96
+ box = (x[0] - x[2] / 2, x[1] - x[3] / 2, x[0] + x[2] / 2, x[1] + x[3] / 2)
97
+ candidates[cls].append([p, box])
98
+ result = [list() for _ in range(nc)]
99
+ for i, candidate in enumerate(candidates):
100
+ candidate = sorted(candidate, key=lambda a: a[0], reverse=True)
101
+ candidate = candidate[:max_instance]
102
+ for x in candidate:
103
+ ok = True
104
+ for r in result[i]:
105
+ if iou(r[1], x[1]) > iou_thres:
106
+ ok = False
107
+ break
108
+ if ok:
109
+ result[i].append(x)
110
+ return result
111
+
112
+
113
+ class Model:
114
+ def __init__(self):
115
+ self.detector = None
116
+ self.g_synthesis = None
117
+ self.g_mapping = None
118
+ self.detector_stride = None
119
+ self.detector_imgsz = None
120
+ self.detector_class_names = None
121
+ self.w_avg = None
122
+ self.load_models()
123
+
124
+ def load_models(self):
125
+ g_mapping_path = huggingface_hub.hf_hub_download("skytnt/fbanime-gan", "g_mapping.onnx")
126
+ g_synthesis_path = huggingface_hub.hf_hub_download("skytnt/fbanime-gan", "g_synthesis.onnx")
127
+ detector_path = huggingface_hub.hf_hub_download("skytnt/fbanime-gan", "waifu_dect.onnx")
128
+
129
+ providers = ['CPUExecutionProvider']
130
+ import onnx
131
+ g_mapping_onnx = onnx.load(g_mapping_path)
132
+ w_avg = [x for x in g_mapping_onnx.graph.initializer if x.name == "w_avg"][0]
133
+ w_avg = np.frombuffer(w_avg.raw_data, dtype=np.float32)[np.newaxis, :]
134
+ w_avg = w_avg.repeat(16, axis=0)[np.newaxis, :]
135
+ self.w_avg = w_avg
136
+
137
+ self.g_mapping = rt.InferenceSession(g_mapping_path, providers=providers)
138
+ self.g_synthesis = rt.InferenceSession(g_synthesis_path, providers=providers)
139
+ self.detector = rt.InferenceSession(detector_path, providers=providers)
140
+ detector_meta = self.detector.get_modelmeta().custom_metadata_map
141
+ self.detector_stride = int(detector_meta['stride'])
142
+ self.detector_imgsz = 1088
143
+ self.detector_class_names = eval(detector_meta['names'])
144
+
145
+ def get_img(self, w, noise=0):
146
+ img = self.g_synthesis.run(None, {'w': w.astype(np.float32), "noise": np.asarray([noise], dtype=np.float32)})[0]
147
+ return (img.transpose(0, 2, 3, 1) * 127.5 + 128).clip(0, 255).astype(np.uint8)[0]
148
+
149
+ def get_w(self, z, psi1, psi2):
150
+ return self.g_mapping.run(None, {'z': z.astype(np.float32), 'psi': np.asarray([psi1, psi2], dtype=np.float32)})[0]
151
+
152
+
153
+ model = Model()
154
+
155
+ # --- GRADIO APP UTILS ---
156
+ def gen_fn(seed, random_seed, psi1, psi2, noise):
157
+ if random_seed:
158
+ seed = random.randint(0, 2 ** 32 - 1)
159
+ z = RandomState(int(seed)).randn(1, 1024)
160
+ w = model.get_w(z.astype(dtype=np.float32), psi1, psi2)
161
+ img_out = model.get_img(w, noise)
162
+ return img_out, seed
163
+
164
+
165
+ # --- UI STYLE (BABY GREEN THEME MATCHING APP.PY) ---
166
+ css = """
167
+ :root {
168
+ --primary-600: #a8e6cf !important;
169
+ --accent-600: #a8e6cf !important;
170
+ }
171
+ .gradio-container, .gradio-container * {
172
+ --loader-color: #dcedc1 !important;
173
+ }
174
+ .ba-header-container {
175
+ border: 1.5px solid #e1f0e5;
176
+ border-radius: 12px;
177
+ margin-bottom: 12px;
178
+ background: white;
179
+ overflow: hidden;
180
+ line-height: 0;
181
+ }
182
+ .ba-header-container img {
183
+ width: 100%;
184
+ height: auto;
185
+ }
186
+ .status-container {
187
+ border: 1.5px solid #e1f0e5;
188
+ border-radius: 12px;
189
+ padding: 15px 22px;
190
+ margin-bottom: 20px;
191
+ background: white;
192
+ }
193
+ .status-title {
194
+ color: #88d498 !important;
195
+ font-weight: 800;
196
+ font-size: 16px;
197
+ margin-bottom: 8px;
198
+ }
199
+ .text-green-bold { color: #88d498 !important; font-weight: 900 !important; }
200
+
201
+ /* Pulsing Dot Effect */
202
+ .dot {
203
+ height: 8px;
204
+ width: 8px;
205
+ background-color: #88d498;
206
+ border-radius: 50%;
207
+ display: inline-block;
208
+ margin-right: 5px;
209
+ box-shadow: 0 0 0 0 rgba(136, 212, 152, 1);
210
+ animation: pulse-green 2s infinite;
211
+ }
212
+ @keyframes pulse-green {
213
+ 0% { transform: scale(0.95); box-shadow: 0 0 0 0 rgba(136, 212, 152, 0.7); }
214
+ 70% { transform: scale(1); box-shadow: 0 0 0 10px rgba(136, 212, 152, 0); }
215
+ 100% { transform: scale(0.95); box-shadow: 0 0 0 0 rgba(136, 212, 152, 0); }
216
+ }
217
+
218
+ .slim-card { max-width: 480px; margin: 0 auto; background: transparent; padding: 10px; }
219
+ .tabs > .tab-nav { display: flex !important; overflow-x: auto !important; white-space: nowrap !important; }
220
+ .warning-card { background: #fffcf0; border: 2px dashed #ffd3b6; border-radius: 10px; padding: 12px; margin-bottom: 15px; text-align: center; }
221
+ .gen-btn { background: #a8e6cf !important; color: #4a5568 !important; font-weight: 700 !important; border-radius: 12px !important; height: 45px !important; width: 100%; border: none !important; cursor: pointer; }
222
+ .info-header-custom { background: #dcedc1 !important; color: #4a5568 !important; border: none !important; border-radius: 8px 8px 0 0 !important; padding: 12px 15px !important; width: 100% !important; cursor: pointer; font-weight: 800 !important; margin-top: 15px; }
223
+ .credit-footer { margin-top: 25px; padding: 15px; background: white; border-radius: 12px; text-align: center; border-bottom: 4px solid #a8e6cf; color: #94a3b8; font-weight: 700; font-size: 12px; letter-spacing: 2px; }
224
+ .gif-container { display: flex; justify-content: center; margin-top: 15px; }
225
+ .gif-container img { border-radius: 12px; max-width: 100%; height: auto; border: 1px solid #e1f0e5; }
226
+ """
227
+
228
+ with gr.Blocks(css=css, title="Full-Body Anime GAN") as app:
229
+ with gr.Column(elem_classes="slim-card"):
230
+ gr.HTML("""
231
+ <div class="ba-header-container">
232
+ <img src="https://huggingface.co/spaces/Plana-Archive/MOE-TTS/resolve/main/Mutsumi.jpg" alt="Mutsumi Banner">
233
+ </div>
234
+ <div class="status-container">
235
+ <div class="status-title">GAN Engine Status</div>
236
+ <div class="status-item"><span style="color:#4a5568">Model :</span> <span class="text-green-bold">&nbsp;FBANIME-GAN LOADED βœ…</span></div>
237
+ <div class="status-item"><span style="color:#4a5568">System :</span> <span style="color:#88d498; font-weight:700;"><span class="dot"></span>Online</span></div>
238
+ </div>
239
+ """)
240
+
241
+ with gr.Tabs(elem_classes="tabs"):
242
+ with gr.Tab("🎨 Generate Image"):
243
+ gr.Markdown("### 🎲 Generate New Character")
244
+
245
+ with gr.Row():
246
+ gen_input1 = gr.Slider(minimum=0, maximum=2 ** 32 - 1, step=1, value=0, label="Seed")
247
+ gen_input2 = gr.Checkbox(label="Random Seed", value=True)
248
+
249
+ gen_input3 = gr.Slider(minimum=0, maximum=1, step=0.01, value=0.7, label="Truncation Psi 1")
250
+ gen_input4 = gr.Slider(minimum=0, maximum=1, step=0.01, value=1, label="Truncation Psi 2")
251
+ gen_input5 = gr.Slider(minimum=0, maximum=1, step=0.01, value=1, label="Noise Strength")
252
+
253
+ gr.HTML("""
254
+ <div class="warning-card" style="margin-top: 10px;">
255
+ <div style="color:#855d1a;font-size:11px;font-weight:600;">
256
+ Atur parameter Psi dan Noise untuk menyesuaikan variasi gaya visual anime. ✨
257
+ </div>
258
+ </div>
259
+ """)
260
+
261
+ gen_submit = gr.Button("🎨 GENERATE CHARACTER 🎨", elem_classes="gen-btn")
262
+ gen_output1 = gr.Image(label="Generated Image")
263
+
264
+ # --- Interactive Information Area ---
265
+ gr.HTML("""
266
+ <div class="gif-container">
267
+ <img src="https://huggingface.co/spaces/Plana-Archive/MOE-TTS/resolve/main/kurumi-tokisaki.gif" alt="Kurumi GIF">
268
+ </div>
269
+ <button onclick="document.getElementById('info-gan-content').style.display = (document.getElementById('info-gan-content').style.display === 'none') ? 'block' : 'none';" class="info-header-custom">
270
+ πŸ“‘ Model Architecture Info πŸ“‘
271
+ </button>
272
+ <div id="info-gan-content" style="display: none; padding: 15px; background: white; border: 1px solid #e1f0e5; border-top: none; border-radius: 0 0 10px 10px; font-size: 12px; color: #4a5568; line-height: 1.6;">
273
+ <strong>Repository Sources:</strong><br>
274
+ β€’ GAN Backbone: skytnt/fbanime-gan<br>
275
+ β€’ Model Type: StyleGAN Architecture for Full-body Anime Generation<br><br>
276
+ <strong>Pipeline Process:</strong><br>
277
+ Generates a clean full-body anime character using mapping networks (Z to W space Conversion) with real-time truncation tricks.
278
+ </div>
279
+ """)
280
+
281
+ gr.HTML("""<div class="credit-footer">🍏 CREATED BY MUTSUMI 🍏</div>""")
282
+
283
+ # --- CONTROLLER CONNECTIONS ---
284
+ gen_submit.click(
285
+ fn=gen_fn,
286
+ inputs=[gen_input1, gen_input2, gen_input3, gen_input4, gen_input5],
287
+ outputs=[gen_output1, gen_input1]
288
+ )
289
+
290
+ # --- LAUNCH ---
291
+ if __name__ == "__main__":
292
+ app.launch(
293
+ server_name="0.0.0.0",
294
+ server_port=7860,
295
+ ssr_mode=False
296
+ )
Mutsumi-Chan/full-body-anime-gan/examples/01.jpg ADDED
Mutsumi-Chan/full-body-anime-gan/examples/02.jpg ADDED
Mutsumi-Chan/full-body-anime-gan/examples/03.jpg ADDED
Mutsumi-Chan/full-body-anime-gan/examples/04.jpg ADDED
Mutsumi-Chan/full-body-anime-gan/requirements.txt ADDED
@@ -0,0 +1,7 @@
 
 
 
 
 
 
 
 
1
+ gradio
2
+ gradio_client
3
+ onnx==1.16.0
4
+ onnxruntime==1.18.1
5
+ scikit-image==0.23.2
6
+ numpy==1.26.4
7
+ huggingface-hub>=0.25.1,<0.30.0