Update app.py
Browse files
app.py
CHANGED
|
@@ -1,673 +1,111 @@
|
|
| 1 |
-
from fastapi import FastAPI, WebSocket, WebSocketDisconnect
|
| 2 |
-
import uvicorn
|
| 3 |
-
import json
|
| 4 |
import os
|
| 5 |
-
import
|
| 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 |
-
"w",
|
| 40 |
-
encoding="utf-8"
|
| 41 |
-
) as f:
|
| 42 |
-
|
| 43 |
-
json.dump(
|
| 44 |
-
players_data,
|
| 45 |
-
f,
|
| 46 |
-
indent=4,
|
| 47 |
-
ensure_ascii=False
|
| 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 |
-
data = json.loads(text)
|
| 119 |
-
|
| 120 |
-
msg_type = data.get(
|
| 121 |
-
"type",
|
| 122 |
-
""
|
| 123 |
-
)
|
| 124 |
-
|
| 125 |
-
# =====================================
|
| 126 |
-
# LOGIN
|
| 127 |
-
# =====================================
|
| 128 |
-
|
| 129 |
-
if msg_type == "login":
|
| 130 |
-
|
| 131 |
-
user = str(
|
| 132 |
-
data.get(
|
| 133 |
-
"username",
|
| 134 |
-
""
|
| 135 |
-
)
|
| 136 |
-
).strip()
|
| 137 |
-
|
| 138 |
-
password = str(
|
| 139 |
-
data.get(
|
| 140 |
-
"password",
|
| 141 |
-
""
|
| 142 |
-
)
|
| 143 |
-
).strip()
|
| 144 |
-
|
| 145 |
-
if (
|
| 146 |
-
user == ""
|
| 147 |
-
or
|
| 148 |
-
password == ""
|
| 149 |
-
):
|
| 150 |
-
|
| 151 |
-
await ws.send_text(
|
| 152 |
-
json.dumps({
|
| 153 |
-
"type":"login_failed"
|
| 154 |
-
})
|
| 155 |
-
)
|
| 156 |
-
|
| 157 |
-
continue
|
| 158 |
-
|
| 159 |
-
if user not in players_data:
|
| 160 |
-
|
| 161 |
-
players_data[user] = {
|
| 162 |
-
"password":password,
|
| 163 |
-
"money":0,
|
| 164 |
-
"progress":1
|
| 165 |
-
}
|
| 166 |
-
|
| 167 |
-
save_players()
|
| 168 |
-
|
| 169 |
-
elif (
|
| 170 |
-
players_data[user]["password"]
|
| 171 |
-
!= password
|
| 172 |
-
):
|
| 173 |
-
|
| 174 |
-
await ws.send_text(
|
| 175 |
-
json.dumps({
|
| 176 |
-
"type":"login_failed"
|
| 177 |
-
})
|
| 178 |
-
)
|
| 179 |
-
|
| 180 |
-
continue
|
| 181 |
-
|
| 182 |
-
username = user
|
| 183 |
-
|
| 184 |
-
logged_users[
|
| 185 |
-
session_id
|
| 186 |
-
] = username
|
| 187 |
-
|
| 188 |
-
await ws.send_text(
|
| 189 |
-
json.dumps({
|
| 190 |
-
"type":"login_success"
|
| 191 |
-
})
|
| 192 |
-
)
|
| 193 |
-
|
| 194 |
-
await ws.send_text(
|
| 195 |
-
json.dumps({
|
| 196 |
-
"type":"player_data",
|
| 197 |
-
"money":
|
| 198 |
-
players_data[user]["money"],
|
| 199 |
-
"progress":
|
| 200 |
-
players_data[user]["progress"]
|
| 201 |
-
})
|
| 202 |
-
)
|
| 203 |
-
|
| 204 |
-
for sid in clients.keys():
|
| 205 |
-
|
| 206 |
-
if sid == session_id:
|
| 207 |
-
continue
|
| 208 |
-
|
| 209 |
-
await ws.send_text(
|
| 210 |
-
json.dumps({
|
| 211 |
-
"type":"player_join",
|
| 212 |
-
"id":sid
|
| 213 |
-
})
|
| 214 |
-
)
|
| 215 |
-
|
| 216 |
-
for sid, client in clients.items():
|
| 217 |
-
|
| 218 |
-
if sid == session_id:
|
| 219 |
-
continue
|
| 220 |
-
|
| 221 |
-
try:
|
| 222 |
-
|
| 223 |
-
await client.send_text(
|
| 224 |
-
json.dumps({
|
| 225 |
-
"type":"player_join",
|
| 226 |
-
"id":session_id
|
| 227 |
-
})
|
| 228 |
-
)
|
| 229 |
-
|
| 230 |
-
except:
|
| 231 |
-
pass
|
| 232 |
-
|
| 233 |
-
for path, owner in (
|
| 234 |
-
entreprises_data.items()
|
| 235 |
-
):
|
| 236 |
-
|
| 237 |
-
await ws.send_text(
|
| 238 |
-
json.dumps({
|
| 239 |
-
"type":"entreprise_owner",
|
| 240 |
-
"path":path,
|
| 241 |
-
"owner":owner
|
| 242 |
-
})
|
| 243 |
-
)
|
| 244 |
-
|
| 245 |
-
# =====================================
|
| 246 |
-
# POSITION
|
| 247 |
-
# =====================================
|
| 248 |
-
|
| 249 |
-
elif (
|
| 250 |
-
msg_type
|
| 251 |
-
==
|
| 252 |
-
"player_update"
|
| 253 |
-
):
|
| 254 |
-
|
| 255 |
-
player_positions[
|
| 256 |
-
session_id
|
| 257 |
-
] = {
|
| 258 |
-
|
| 259 |
-
"x":
|
| 260 |
-
data.get("x",0),
|
| 261 |
-
|
| 262 |
-
"y":
|
| 263 |
-
data.get("y",0),
|
| 264 |
-
|
| 265 |
-
"z":
|
| 266 |
-
data.get("z",0)
|
| 267 |
-
}
|
| 268 |
-
|
| 269 |
-
data["id"] = session_id
|
| 270 |
-
|
| 271 |
-
for sid, client in clients.items():
|
| 272 |
-
|
| 273 |
-
if sid == session_id:
|
| 274 |
-
continue
|
| 275 |
-
|
| 276 |
-
try:
|
| 277 |
-
|
| 278 |
-
await client.send_text(
|
| 279 |
-
json.dumps(
|
| 280 |
-
data
|
| 281 |
-
)
|
| 282 |
-
)
|
| 283 |
-
|
| 284 |
-
except:
|
| 285 |
-
pass
|
| 286 |
-
|
| 287 |
-
# =====================================
|
| 288 |
-
# MONEY
|
| 289 |
-
# =====================================
|
| 290 |
-
|
| 291 |
-
elif (
|
| 292 |
-
msg_type
|
| 293 |
-
==
|
| 294 |
-
"save_money"
|
| 295 |
-
):
|
| 296 |
-
|
| 297 |
-
if username:
|
| 298 |
-
|
| 299 |
-
players_data[
|
| 300 |
-
username
|
| 301 |
-
]["money"] = int(
|
| 302 |
-
data.get(
|
| 303 |
-
"money",
|
| 304 |
-
0
|
| 305 |
-
)
|
| 306 |
-
)
|
| 307 |
-
|
| 308 |
-
save_players()
|
| 309 |
-
|
| 310 |
-
# =====================================
|
| 311 |
-
# PROGRESS
|
| 312 |
-
# =====================================
|
| 313 |
-
|
| 314 |
-
elif (
|
| 315 |
-
msg_type
|
| 316 |
-
==
|
| 317 |
-
"save_progress"
|
| 318 |
-
):
|
| 319 |
-
|
| 320 |
-
if username:
|
| 321 |
-
|
| 322 |
-
progress = int(
|
| 323 |
-
data.get(
|
| 324 |
-
"progress",
|
| 325 |
-
1
|
| 326 |
-
)
|
| 327 |
-
)
|
| 328 |
-
|
| 329 |
-
players_data[
|
| 330 |
-
username
|
| 331 |
-
]["progress"] = progress
|
| 332 |
-
|
| 333 |
-
save_players()
|
| 334 |
-
|
| 335 |
-
broadcast = {
|
| 336 |
-
"type":"player_progress",
|
| 337 |
-
"id":session_id,
|
| 338 |
-
"progress":progress
|
| 339 |
-
}
|
| 340 |
-
|
| 341 |
-
for sid, client in clients.items():
|
| 342 |
-
|
| 343 |
-
if sid == session_id:
|
| 344 |
-
continue
|
| 345 |
-
|
| 346 |
-
try:
|
| 347 |
-
|
| 348 |
-
await client.send_text(
|
| 349 |
-
json.dumps(
|
| 350 |
-
broadcast
|
| 351 |
-
)
|
| 352 |
-
)
|
| 353 |
-
|
| 354 |
-
except:
|
| 355 |
-
pass
|
| 356 |
-
|
| 357 |
-
# =====================================
|
| 358 |
-
# ENTREPRISE
|
| 359 |
-
# =====================================
|
| 360 |
-
|
| 361 |
-
elif (
|
| 362 |
-
msg_type
|
| 363 |
-
==
|
| 364 |
-
"save_entreprise"
|
| 365 |
-
):
|
| 366 |
-
|
| 367 |
-
path = data.get(
|
| 368 |
-
"path",
|
| 369 |
-
""
|
| 370 |
-
)
|
| 371 |
-
|
| 372 |
-
owner = data.get(
|
| 373 |
-
"owner",
|
| 374 |
-
""
|
| 375 |
-
)
|
| 376 |
-
|
| 377 |
-
entreprises_data[
|
| 378 |
-
path
|
| 379 |
-
] = owner
|
| 380 |
-
|
| 381 |
-
save_entreprises()
|
| 382 |
-
|
| 383 |
-
# =====================================
|
| 384 |
-
# CHANGE OWNER
|
| 385 |
-
# =====================================
|
| 386 |
-
|
| 387 |
-
elif (
|
| 388 |
-
msg_type
|
| 389 |
-
==
|
| 390 |
-
"set_entreprise_owner"
|
| 391 |
-
):
|
| 392 |
-
|
| 393 |
-
path = data.get(
|
| 394 |
-
"path",
|
| 395 |
-
""
|
| 396 |
-
)
|
| 397 |
-
|
| 398 |
-
owner = data.get(
|
| 399 |
-
"owner",
|
| 400 |
-
""
|
| 401 |
-
)
|
| 402 |
-
|
| 403 |
-
entreprises_data[
|
| 404 |
-
path
|
| 405 |
-
] = owner
|
| 406 |
-
|
| 407 |
-
save_entreprises()
|
| 408 |
-
|
| 409 |
-
broadcast = {
|
| 410 |
-
"type":"entreprise_owner",
|
| 411 |
-
"path":path,
|
| 412 |
-
"owner":owner
|
| 413 |
-
}
|
| 414 |
-
|
| 415 |
-
for client in clients.values():
|
| 416 |
-
|
| 417 |
-
try:
|
| 418 |
-
|
| 419 |
-
await client.send_text(
|
| 420 |
-
json.dumps(
|
| 421 |
-
broadcast
|
| 422 |
-
)
|
| 423 |
-
)
|
| 424 |
-
|
| 425 |
-
except:
|
| 426 |
-
pass
|
| 427 |
-
|
| 428 |
-
# =====================================
|
| 429 |
-
# COOP INVITE
|
| 430 |
-
# =====================================
|
| 431 |
-
|
| 432 |
-
elif (
|
| 433 |
-
msg_type
|
| 434 |
-
==
|
| 435 |
-
"coop_invite"
|
| 436 |
-
):
|
| 437 |
-
|
| 438 |
-
if username:
|
| 439 |
-
|
| 440 |
-
target = str(
|
| 441 |
-
data.get(
|
| 442 |
-
"target",
|
| 443 |
-
""
|
| 444 |
-
)
|
| 445 |
-
).strip()
|
| 446 |
-
|
| 447 |
-
target_sid = None
|
| 448 |
-
|
| 449 |
-
for sid, uname in logged_users.items():
|
| 450 |
-
|
| 451 |
-
if uname == target:
|
| 452 |
-
|
| 453 |
-
target_sid = sid
|
| 454 |
-
|
| 455 |
-
break
|
| 456 |
-
|
| 457 |
-
if (
|
| 458 |
-
target_sid
|
| 459 |
-
and
|
| 460 |
-
target_sid in clients
|
| 461 |
-
):
|
| 462 |
-
|
| 463 |
-
coop_requests[
|
| 464 |
-
target_sid
|
| 465 |
-
] = session_id
|
| 466 |
-
|
| 467 |
-
try:
|
| 468 |
-
|
| 469 |
-
await clients[
|
| 470 |
-
target_sid
|
| 471 |
-
].send_text(
|
| 472 |
-
json.dumps({
|
| 473 |
-
"type":"coop_invite",
|
| 474 |
-
"sender":username
|
| 475 |
-
})
|
| 476 |
-
)
|
| 477 |
-
|
| 478 |
-
except:
|
| 479 |
-
pass
|
| 480 |
-
|
| 481 |
-
# =====================================
|
| 482 |
-
# COOP ACCEPT
|
| 483 |
-
# =====================================
|
| 484 |
-
|
| 485 |
-
elif (
|
| 486 |
-
msg_type
|
| 487 |
-
==
|
| 488 |
-
"coop_accept"
|
| 489 |
-
):
|
| 490 |
-
|
| 491 |
-
if username:
|
| 492 |
-
|
| 493 |
-
sender_name = str(
|
| 494 |
-
data.get(
|
| 495 |
-
"sender",
|
| 496 |
-
""
|
| 497 |
-
)
|
| 498 |
-
).strip()
|
| 499 |
-
|
| 500 |
-
sender_sid = None
|
| 501 |
-
|
| 502 |
-
for sid, uname in logged_users.items():
|
| 503 |
-
|
| 504 |
-
if uname == sender_name:
|
| 505 |
-
|
| 506 |
-
sender_sid = sid
|
| 507 |
-
|
| 508 |
-
break
|
| 509 |
-
|
| 510 |
-
if (
|
| 511 |
-
sender_sid
|
| 512 |
-
and
|
| 513 |
-
sender_sid in clients
|
| 514 |
-
):
|
| 515 |
-
|
| 516 |
-
if session_id in coop_requests:
|
| 517 |
-
|
| 518 |
-
del coop_requests[session_id]
|
| 519 |
-
|
| 520 |
-
try:
|
| 521 |
-
|
| 522 |
-
await clients[
|
| 523 |
-
sender_sid
|
| 524 |
-
].send_text(
|
| 525 |
-
json.dumps({
|
| 526 |
-
"type":"coop_accept",
|
| 527 |
-
"from":username
|
| 528 |
-
})
|
| 529 |
-
)
|
| 530 |
-
|
| 531 |
-
except:
|
| 532 |
-
pass
|
| 533 |
-
|
| 534 |
-
# =====================================
|
| 535 |
-
# COOP REFUSE
|
| 536 |
-
# =====================================
|
| 537 |
-
|
| 538 |
-
elif (
|
| 539 |
-
msg_type
|
| 540 |
-
==
|
| 541 |
-
"coop_refuse"
|
| 542 |
-
):
|
| 543 |
-
|
| 544 |
-
if username:
|
| 545 |
-
|
| 546 |
-
sender_name = str(
|
| 547 |
-
data.get(
|
| 548 |
-
"sender",
|
| 549 |
-
""
|
| 550 |
-
)
|
| 551 |
-
).strip()
|
| 552 |
-
|
| 553 |
-
sender_sid = None
|
| 554 |
-
|
| 555 |
-
for sid, uname in logged_users.items():
|
| 556 |
-
|
| 557 |
-
if uname == sender_name:
|
| 558 |
-
|
| 559 |
-
sender_sid = sid
|
| 560 |
-
|
| 561 |
-
break
|
| 562 |
-
|
| 563 |
-
if (
|
| 564 |
-
sender_sid
|
| 565 |
-
and
|
| 566 |
-
sender_sid in clients
|
| 567 |
-
):
|
| 568 |
-
|
| 569 |
-
if session_id in coop_requests:
|
| 570 |
-
|
| 571 |
-
del coop_requests[session_id]
|
| 572 |
-
|
| 573 |
-
try:
|
| 574 |
-
|
| 575 |
-
await clients[
|
| 576 |
-
sender_sid
|
| 577 |
-
].send_text(
|
| 578 |
-
json.dumps({
|
| 579 |
-
"type":"coop_refuse",
|
| 580 |
-
"from":username
|
| 581 |
-
})
|
| 582 |
-
)
|
| 583 |
-
|
| 584 |
-
except:
|
| 585 |
-
pass
|
| 586 |
-
|
| 587 |
-
except WebSocketDisconnect:
|
| 588 |
-
|
| 589 |
-
pass
|
| 590 |
-
|
| 591 |
-
finally:
|
| 592 |
-
|
| 593 |
-
# Libération coop
|
| 594 |
-
|
| 595 |
-
if session_id in coop_requests:
|
| 596 |
-
|
| 597 |
-
del coop_requests[session_id]
|
| 598 |
-
|
| 599 |
-
for k, v in list(
|
| 600 |
-
coop_requests.items()
|
| 601 |
-
):
|
| 602 |
-
|
| 603 |
-
if v == session_id:
|
| 604 |
-
|
| 605 |
-
del coop_requests[k]
|
| 606 |
-
|
| 607 |
-
# Libération entreprises
|
| 608 |
-
|
| 609 |
-
to_clear = []
|
| 610 |
-
|
| 611 |
-
for path, owner in entreprises_data.items():
|
| 612 |
-
|
| 613 |
-
if owner == session_id:
|
| 614 |
-
|
| 615 |
-
to_clear.append(path)
|
| 616 |
-
|
| 617 |
-
for path in to_clear:
|
| 618 |
-
|
| 619 |
-
entreprises_data[path] = ""
|
| 620 |
-
|
| 621 |
-
for client in clients.values():
|
| 622 |
-
|
| 623 |
-
try:
|
| 624 |
-
|
| 625 |
-
await client.send_text(
|
| 626 |
-
json.dumps({
|
| 627 |
-
"type":"entreprise_owner",
|
| 628 |
-
"path":path,
|
| 629 |
-
"owner":""
|
| 630 |
-
})
|
| 631 |
-
)
|
| 632 |
-
|
| 633 |
-
except:
|
| 634 |
-
pass
|
| 635 |
-
|
| 636 |
-
save_entreprises()
|
| 637 |
-
|
| 638 |
-
if session_id in clients:
|
| 639 |
-
del clients[session_id]
|
| 640 |
-
|
| 641 |
-
if session_id in logged_users:
|
| 642 |
-
del logged_users[session_id]
|
| 643 |
-
|
| 644 |
-
if session_id in player_positions:
|
| 645 |
-
del player_positions[session_id]
|
| 646 |
-
|
| 647 |
-
for client in clients.values():
|
| 648 |
-
|
| 649 |
-
try:
|
| 650 |
-
|
| 651 |
-
await client.send_text(
|
| 652 |
-
json.dumps({
|
| 653 |
-
"type":"player_leave",
|
| 654 |
-
"id":session_id
|
| 655 |
-
})
|
| 656 |
-
)
|
| 657 |
-
|
| 658 |
-
except:
|
| 659 |
-
pass
|
| 660 |
-
if __name__ == "__main__":
|
| 661 |
-
|
| 662 |
-
port = int(
|
| 663 |
-
os.environ.get(
|
| 664 |
-
"PORT",
|
| 665 |
-
7860
|
| 666 |
-
)
|
| 667 |
)
|
| 668 |
|
| 669 |
-
|
| 670 |
-
|
| 671 |
-
|
| 672 |
-
port=port
|
| 673 |
-
)
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
import os
|
| 2 |
+
import torch
|
| 3 |
+
import torch.nn as nn
|
| 4 |
+
from torchvision import transforms, models
|
| 5 |
+
from PIL import Image
|
| 6 |
+
import gradio as gr
|
| 7 |
+
|
| 8 |
+
# ==========================================
|
| 9 |
+
# 1. CONFIGURATION CHIRURGICALE
|
| 10 |
+
# ==========================================
|
| 11 |
+
DEVICE = torch.device("cuda" if torch.cuda.is_available() else "cpu")
|
| 12 |
+
MODEL_INPUT_SIZE = 224 # Taille obligatoire pour le Vision Transformer (ViT)
|
| 13 |
+
SKIN_SIZE = 128 # Taille cible HD du skin Minecraft
|
| 14 |
+
MODEL_PATH = "vit_minecraft_core.pth"
|
| 15 |
+
|
| 16 |
+
# ==========================================
|
| 17 |
+
# 2. ARCHITECTURE EXACTE : VISION TRANSFORMER
|
| 18 |
+
# ==========================================
|
| 19 |
+
class ViTSkinGenerator(nn.Module):
|
| 20 |
+
def __init__(self):
|
| 21 |
+
super().__init__()
|
| 22 |
+
# On déclare l'architecture (pretrained=False car on charge vos propres poids)
|
| 23 |
+
vit = models.vit_b_16(pretrained=False)
|
| 24 |
+
self.transformer_core = vit
|
| 25 |
+
num_features = vit.heads.head.in_features
|
| 26 |
+
|
| 27 |
+
# Décodeur de projection géométrique (Grille propre de 128x128x3)
|
| 28 |
+
self.transformer_core.heads = nn.Sequential(
|
| 29 |
+
nn.Linear(num_features, 2048),
|
| 30 |
+
nn.GELU(),
|
| 31 |
+
nn.Dropout(0.1),
|
| 32 |
+
nn.Linear(2048, 4096),
|
| 33 |
+
nn.GELU(),
|
| 34 |
+
nn.Linear(4096, 3 * SKIN_SIZE * SKIN_SIZE),
|
| 35 |
+
nn.Sigmoid()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 36 |
)
|
| 37 |
+
|
| 38 |
+
def forward(self, x):
|
| 39 |
+
out = self.transformer_core(x)
|
| 40 |
+
out = out.view(-1, 3, SKIN_SIZE, SKIN_SIZE)
|
| 41 |
+
return out
|
| 42 |
+
|
| 43 |
+
# ==========================================
|
| 44 |
+
# 3. CHARGEMENT DE VOS POIDS AU LANCEMENT
|
| 45 |
+
# ==========================================
|
| 46 |
+
model = ViTSkinGenerator().to(DEVICE)
|
| 47 |
+
if os.path.exists(MODEL_PATH):
|
| 48 |
+
print(f"💾 Chargement des poids depuis {MODEL_PATH}...")
|
| 49 |
+
model.load_state_dict(torch.load(MODEL_PATH, map_location=DEVICE))
|
| 50 |
+
else:
|
| 51 |
+
print(f"⚠️ Attention : Fichier '{MODEL_PATH}' introuvable à la racine.")
|
| 52 |
+
model.eval()
|
| 53 |
+
|
| 54 |
+
# ==========================================
|
| 55 |
+
# 4. FONCTION DE PRÉDICTION POUR GRADIO
|
| 56 |
+
# ==========================================
|
| 57 |
+
def predict_skin(input_image):
|
| 58 |
+
if input_image is None:
|
| 59 |
+
return None
|
| 60 |
+
|
| 61 |
+
# 1. Traitement de l'image d'entrée (LANCZOS pour garder la netteté)
|
| 62 |
+
img = input_image.convert("RGB").resize((MODEL_INPUT_SIZE, MODEL_INPUT_SIZE), Image.Resampling.LANCZOS)
|
| 63 |
+
|
| 64 |
+
transform = transforms.Compose([
|
| 65 |
+
transforms.ToTensor(),
|
| 66 |
+
transforms.Normalize((0.5, 0.5, 0.5), (0.5, 0.5, 0.5))
|
| 67 |
+
])
|
| 68 |
+
tensor_in = transform(img).unsqueeze(0).to(DEVICE)
|
| 69 |
+
|
| 70 |
+
# 2. Inférence de l'IA
|
| 71 |
+
with torch.no_grad():
|
| 72 |
+
tensor_out = model(tensor_in).squeeze(0).cpu()
|
| 73 |
+
|
| 74 |
+
# 3. Post-traitement strict Pixel Art (NEAREST pour éviter le flou baveux)
|
| 75 |
+
tensor_out = torch.clamp(tensor_out, 0, 1)
|
| 76 |
+
skin_pil = transforms.ToPILImage()(tensor_out)
|
| 77 |
+
skin_final = skin_pil.resize((SKIN_SIZE, SKIN_SIZE), Image.NEAREST)
|
| 78 |
+
|
| 79 |
+
return skin_final
|
| 80 |
+
|
| 81 |
+
# ==========================================
|
| 82 |
+
# 5. INTERFACE DESIGN BLOCKS (GRADIO INTERACTIVE)
|
| 83 |
+
# ==========================================
|
| 84 |
+
with gr.Blocks(theme=gr.themes.Soft(primary_hue="green", secondary_hue="slate")) as demo:
|
| 85 |
+
gr.Markdown(
|
| 86 |
+
"""
|
| 87 |
+
# 🎨 Minecraft HD Skin AI Generator
|
| 88 |
+
Déployé avec succès par **KTXStudio**. Importez un rendu 3D ou une image de personnage pour que le **Vision Transformer (ViT)** extrait intelligemment son patron de skin Minecraft en Haute Définition (128x128).
|
| 89 |
+
"""
|
| 90 |
)
|
| 91 |
+
|
| 92 |
+
with gr.Row():
|
| 93 |
+
with gr.Column():
|
| 94 |
+
input_img = gr.Image(type="pil", label="📥 Téléverse ton personnage (Rendu 3D / Image)")
|
| 95 |
+
submit_btn = gr.Button("✨ Générer le Skin HD", variant="primary")
|
| 96 |
+
|
| 97 |
+
with gr.Column():
|
| 98 |
+
output_img = gr.Image(type="pil", label="✨ Skin Minecraft HD Extrait (128x128)", image_mode="RGB")
|
| 99 |
+
|
| 100 |
+
# Lier le bouton à la fonction de prédiction
|
| 101 |
+
submit_btn.click(fn=predict_skin, inputs=input_img, outputs=output_img)
|
| 102 |
+
|
| 103 |
+
gr.Examples(
|
| 104 |
+
examples=["A.jpg"] if os.path.exists("A.jpg") else [],
|
| 105 |
+
inputs=input_img,
|
| 106 |
+
label="Exemples disponibles"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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