Nafiul Huda commited on
Update app.py
Browse files
app.py
CHANGED
|
@@ -2,7 +2,7 @@
|
|
| 2 |
"""
|
| 3 |
AI VRM Companion β Gradio App
|
| 4 |
Single-file Gradio application designed for Python 3.12, Gradio 6.17.3, and Hugging Face ZeroGPU.
|
| 5 |
-
Runs local Edge TTS (XiaoyiNeural) and
|
| 6 |
"""
|
| 7 |
|
| 8 |
import os
|
|
@@ -14,8 +14,6 @@ import base64
|
|
| 14 |
import threading
|
| 15 |
import zipfile
|
| 16 |
import gradio as gr
|
| 17 |
-
from fastapi import FastAPI
|
| 18 |
-
from fastapi.responses import FileResponse, Response
|
| 19 |
|
| 20 |
# Setup logger
|
| 21 |
logging.basicConfig(level=logging.INFO)
|
|
@@ -31,6 +29,9 @@ ANIM_DIR = os.path.join(BASE_DIR, "animation")
|
|
| 31 |
os.makedirs(MODEL_DIR, exist_ok=True)
|
| 32 |
os.makedirs(ANIM_DIR, exist_ok=True)
|
| 33 |
|
|
|
|
|
|
|
|
|
|
| 34 |
# ---------------------------------------------------------------------------
|
| 35 |
# Asset Discovery & System Parameters
|
| 36 |
# ---------------------------------------------------------------------------
|
|
@@ -513,54 +514,70 @@ const gltfLoader = new GLTFLoader();
|
|
| 513 |
gltfLoader.register(p => new VRMLoaderPlugin(p));
|
| 514 |
gltfLoader.register(p => new VRMAnimationLoaderPlugin(p));
|
| 515 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 516 |
/* βββββββββββββ LOADERS βββββββββββββ */
|
| 517 |
-
async function loadVrmFromUrl(
|
| 518 |
setLoading(true, 'Loading character...');
|
| 519 |
-
|
| 520 |
-
|
| 521 |
-
|
| 522 |
-
|
| 523 |
-
|
| 524 |
-
|
| 525 |
-
|
| 526 |
-
|
| 527 |
-
|
| 528 |
-
|
| 529 |
-
|
| 530 |
-
|
| 531 |
-
|
| 532 |
-
|
| 533 |
-
|
| 534 |
-
|
| 535 |
-
|
| 536 |
-
|
| 537 |
-
|
| 538 |
-
|
| 539 |
-
|
| 540 |
-
|
| 541 |
-
|
| 542 |
-
|
| 543 |
-
|
| 544 |
-
|
| 545 |
-
|
| 546 |
-
|
| 547 |
-
|
| 548 |
-
|
| 549 |
-
|
| 550 |
-
|
| 551 |
-
|
| 552 |
-
|
| 553 |
-
|
| 554 |
-
|
| 555 |
-
}
|
| 556 |
-
},
|
| 557 |
-
err => {
|
| 558 |
-
console.error(err);
|
| 559 |
-
showErr('Failed to load local VRM model');
|
| 560 |
-
setLoading(false);
|
| 561 |
-
reject(err);
|
| 562 |
});
|
| 563 |
-
})
|
|
|
|
|
|
|
|
|
|
|
|
|
| 564 |
}
|
| 565 |
|
| 566 |
function groundVrm(){
|
|
@@ -846,8 +863,7 @@ async function autoLoadAnimations(){
|
|
| 846 |
setLoading(true, 'Loading animations...');
|
| 847 |
try {
|
| 848 |
const Zip = await ensureJSZip();
|
| 849 |
-
const res = await
|
| 850 |
-
if(!res.ok) throw new Error('Zip missing on server');
|
| 851 |
const blob = await res.blob();
|
| 852 |
const zip = await Zip.loadAsync(blob);
|
| 853 |
|
|
@@ -1092,12 +1108,7 @@ function animate(){
|
|
| 1092 |
|
| 1093 |
/* βββββββββββββ SYSTEM ENTRYPOINT βββββββββββββ */
|
| 1094 |
async function init(){
|
| 1095 |
-
|
| 1096 |
-
await loadVrmFromUrl('/model/Ani.vrm');
|
| 1097 |
-
} catch(e){
|
| 1098 |
-
console.warn('Ani.vrm not found in /model directory, awaiting mounting...');
|
| 1099 |
-
showInfo('Place Ani.vrm model in the model/ folder');
|
| 1100 |
-
}
|
| 1101 |
await autoLoadAnimations();
|
| 1102 |
setupInputWatchers();
|
| 1103 |
animate();
|
|
@@ -1110,7 +1121,8 @@ init();
|
|
| 1110 |
# ---------------------------------------------------------------------------
|
| 1111 |
# Gradio Application Definition
|
| 1112 |
# ---------------------------------------------------------------------------
|
| 1113 |
-
|
|
|
|
| 1114 |
# 98% Custom HTML Canvas container
|
| 1115 |
gr.HTML(HTML_PAGE, elem_id="custom_html_overlay")
|
| 1116 |
|
|
@@ -1149,30 +1161,12 @@ with gr.Blocks(css=CSS_STYLES, title="AI VRM Companion") as demo:
|
|
| 1149 |
)
|
| 1150 |
|
| 1151 |
# ---------------------------------------------------------------------------
|
| 1152 |
-
#
|
| 1153 |
# ---------------------------------------------------------------------------
|
| 1154 |
-
fastapi_app = FastAPI()
|
| 1155 |
-
|
| 1156 |
-
# Mount local assets so the web application can access files correctly
|
| 1157 |
-
@fastapi_app.get("/model/{filename}")
|
| 1158 |
-
def serve_vrm_model(filename: str):
|
| 1159 |
-
path = os.path.join(MODEL_DIR, filename)
|
| 1160 |
-
if os.path.exists(path):
|
| 1161 |
-
return FileResponse(path)
|
| 1162 |
-
return Response("Model file not found", status_code=404)
|
| 1163 |
-
|
| 1164 |
-
@fastapi_app.get("/animation/{filename}")
|
| 1165 |
-
def serve_vrm_animation(filename: str):
|
| 1166 |
-
path = os.path.join(ANIM_DIR, filename)
|
| 1167 |
-
if os.path.exists(path):
|
| 1168 |
-
return FileResponse(path)
|
| 1169 |
-
return Response("Animation file not found", status_code=404)
|
| 1170 |
-
|
| 1171 |
-
# Mount the fully configured Gradio application context onto FastAPI
|
| 1172 |
-
gr.mount_gradio_app(fastapi_app, demo, path="/")
|
| 1173 |
-
|
| 1174 |
if __name__ == "__main__":
|
| 1175 |
-
|
| 1176 |
-
|
| 1177 |
-
|
| 1178 |
-
|
|
|
|
|
|
|
|
|
| 2 |
"""
|
| 3 |
AI VRM Companion β Gradio App
|
| 4 |
Single-file Gradio application designed for Python 3.12, Gradio 6.17.3, and Hugging Face ZeroGPU.
|
| 5 |
+
Runs local Edge TTS (XiaoyiNeural) and uses native Gradio static paths for file asset delivery.
|
| 6 |
"""
|
| 7 |
|
| 8 |
import os
|
|
|
|
| 14 |
import threading
|
| 15 |
import zipfile
|
| 16 |
import gradio as gr
|
|
|
|
|
|
|
| 17 |
|
| 18 |
# Setup logger
|
| 19 |
logging.basicConfig(level=logging.INFO)
|
|
|
|
| 29 |
os.makedirs(MODEL_DIR, exist_ok=True)
|
| 30 |
os.makedirs(ANIM_DIR, exist_ok=True)
|
| 31 |
|
| 32 |
+
# Register static paths with Gradio for direct browser resolution
|
| 33 |
+
gr.set_static_paths(paths=[MODEL_DIR, ANIM_DIR])
|
| 34 |
+
|
| 35 |
# ---------------------------------------------------------------------------
|
| 36 |
# Asset Discovery & System Parameters
|
| 37 |
# ---------------------------------------------------------------------------
|
|
|
|
| 514 |
gltfLoader.register(p => new VRMLoaderPlugin(p));
|
| 515 |
gltfLoader.register(p => new VRMAnimationLoaderPlugin(p));
|
| 516 |
|
| 517 |
+
/* βββββββββββββ FETCH FAILSAFE FOR GRADIO SPACES βββββββββββββ */
|
| 518 |
+
async function fetchWithFallback(filename, type) {
|
| 519 |
+
const relativePath = type === 'model' ? `model/${filename}` : `animation/${filename}`;
|
| 520 |
+
// Iterates across multiple endpoints to robustly capture the local asset context
|
| 521 |
+
const urls = [
|
| 522 |
+
`/gradio_api/file=${relativePath}`,
|
| 523 |
+
`/file=${relativePath}`,
|
| 524 |
+
`/${relativePath}`
|
| 525 |
+
];
|
| 526 |
+
for (const url of urls) {
|
| 527 |
+
try {
|
| 528 |
+
const res = await fetch(url);
|
| 529 |
+
if (res.ok) return res;
|
| 530 |
+
} catch(e) {}
|
| 531 |
+
}
|
| 532 |
+
throw new Error(`Asset fetch error for ${filename}`);
|
| 533 |
+
}
|
| 534 |
+
|
| 535 |
/* βββββββββββββ LOADERS βββββββββββββ */
|
| 536 |
+
async function loadVrmFromUrl() {
|
| 537 |
setLoading(true, 'Loading character...');
|
| 538 |
+
try {
|
| 539 |
+
const res = await fetchWithFallback('Ani.vrm', 'model');
|
| 540 |
+
const blob = await res.blob();
|
| 541 |
+
const url = URL.createObjectURL(blob);
|
| 542 |
+
return new Promise((resolve, reject) => {
|
| 543 |
+
gltfLoader.load(url, gltf => {
|
| 544 |
+
URL.revokeObjectURL(url);
|
| 545 |
+
const newVrm = gltf.userData.vrm;
|
| 546 |
+
if(!newVrm){ showErr('Invalid VRM'); setLoading(false); reject(); return; }
|
| 547 |
+
try { VRMUtils.rotateVRM0(newVrm); } catch(e){}
|
| 548 |
+
|
| 549 |
+
newVrm.scene.traverse(o => {
|
| 550 |
+
if(o.isMesh){
|
| 551 |
+
o.frustumCulled = false;
|
| 552 |
+
o.castShadow = true;
|
| 553 |
+
o.receiveShadow = true;
|
| 554 |
+
}
|
| 555 |
+
});
|
| 556 |
+
|
| 557 |
+
if(vrm) { scene.remove(vrm.scene); }
|
| 558 |
+
if(mixer){ mixer.stopAllAction(); mixer = null; vrmaAction = null; }
|
| 559 |
+
|
| 560 |
+
vrm = newVrm;
|
| 561 |
+
scene.add(vrm.scene);
|
| 562 |
+
groundVrm();
|
| 563 |
+
buildExprAlias();
|
| 564 |
+
|
| 565 |
+
for(let i=0; i<6; i++) applyIdle(performance.now()*0.001, 0.95);
|
| 566 |
+
vrm.update(0.016);
|
| 567 |
+
renderer.compile(scene, camera);
|
| 568 |
+
reframe();
|
| 569 |
+
|
| 570 |
+
vrmLoadedOk = true;
|
| 571 |
+
setLoading(false);
|
| 572 |
+
showInfo('Model loaded ready.');
|
| 573 |
+
resolve();
|
| 574 |
+
}, undefined, reject);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 575 |
});
|
| 576 |
+
} catch(e) {
|
| 577 |
+
console.error(e);
|
| 578 |
+
showErr('Failed to load local VRM model: ' + e.message);
|
| 579 |
+
setLoading(false);
|
| 580 |
+
}
|
| 581 |
}
|
| 582 |
|
| 583 |
function groundVrm(){
|
|
|
|
| 863 |
setLoading(true, 'Loading animations...');
|
| 864 |
try {
|
| 865 |
const Zip = await ensureJSZip();
|
| 866 |
+
const res = await fetchWithFallback('all_vrma.zip', 'animation');
|
|
|
|
| 867 |
const blob = await res.blob();
|
| 868 |
const zip = await Zip.loadAsync(blob);
|
| 869 |
|
|
|
|
| 1108 |
|
| 1109 |
/* βββββββββββββ SYSTEM ENTRYPOINT βββββββββββββ */
|
| 1110 |
async function init(){
|
| 1111 |
+
await loadVrmFromUrl();
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1112 |
await autoLoadAnimations();
|
| 1113 |
setupInputWatchers();
|
| 1114 |
animate();
|
|
|
|
| 1121 |
# ---------------------------------------------------------------------------
|
| 1122 |
# Gradio Application Definition
|
| 1123 |
# ---------------------------------------------------------------------------
|
| 1124 |
+
# Title parameter retained; Blocks level custom styles refactored into demo.launch()
|
| 1125 |
+
with gr.Blocks(title="AI VRM Companion") as demo:
|
| 1126 |
# 98% Custom HTML Canvas container
|
| 1127 |
gr.HTML(HTML_PAGE, elem_id="custom_html_overlay")
|
| 1128 |
|
|
|
|
| 1161 |
)
|
| 1162 |
|
| 1163 |
# ---------------------------------------------------------------------------
|
| 1164 |
+
# Gradio Launch Execution (Port binding and routing managed automatically)
|
| 1165 |
# ---------------------------------------------------------------------------
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1166 |
if __name__ == "__main__":
|
| 1167 |
+
demo.launch(
|
| 1168 |
+
server_name="0.0.0.0",
|
| 1169 |
+
server_port=7860,
|
| 1170 |
+
css=CSS_STYLES, # Custom CSS rules applied here directly
|
| 1171 |
+
allowed_paths=[MODEL_DIR, ANIM_DIR] # Access rights given dynamically to Gradio's internal engine
|
| 1172 |
+
)
|