Nafiul Huda commited on
Update app.py
Browse files
app.py
CHANGED
|
@@ -2,6 +2,7 @@ import os
|
|
| 2 |
import json
|
| 3 |
import asyncio
|
| 4 |
import zipfile
|
|
|
|
| 5 |
import torch
|
| 6 |
import gradio as gr
|
| 7 |
import edge_tts
|
|
@@ -9,51 +10,55 @@ from transformers import AutoModelForCausalLM, AutoTokenizer, pipeline
|
|
| 9 |
import uuid
|
| 10 |
import threading
|
| 11 |
|
| 12 |
-
#
|
| 13 |
try:
|
| 14 |
import spaces
|
| 15 |
except ImportError:
|
| 16 |
-
# Fallback for local testing
|
| 17 |
class spaces:
|
| 18 |
@staticmethod
|
| 19 |
def GPU(func):
|
| 20 |
return func
|
| 21 |
|
| 22 |
# Constants
|
| 23 |
-
MODEL_NAME = "Qwen/Qwen2.5-
|
| 24 |
VOICE = "zh-CN-XiaoyiNeural"
|
| 25 |
VRM_MODEL_PATH = "model/Ani.vrm"
|
| 26 |
ANIMATION_ZIP_PATH = "animation/all_vrma.zip"
|
| 27 |
|
| 28 |
-
# Ensure directories exist
|
| 29 |
os.makedirs("model", exist_ok=True)
|
| 30 |
os.makedirs("animation", exist_ok=True)
|
| 31 |
|
| 32 |
-
# Extract animations
|
| 33 |
if os.path.exists(ANIMATION_ZIP_PATH):
|
| 34 |
-
print("Extracting
|
| 35 |
try:
|
| 36 |
with zipfile.ZipFile(ANIMATION_ZIP_PATH, 'r') as z:
|
| 37 |
-
z.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 38 |
except Exception as e:
|
| 39 |
print(f"Error extracting animations: {e}")
|
| 40 |
|
| 41 |
-
#
|
| 42 |
def get_animation_list():
|
| 43 |
try:
|
| 44 |
files = [f for f in os.listdir("animation") if f.endswith('.vrma')]
|
| 45 |
if files:
|
| 46 |
return files
|
| 47 |
except Exception as e:
|
| 48 |
-
print(f"Error
|
| 49 |
return ["neutral2.vrma", "neutral3.vrma", "happy.vrma", "wave.vrma", "dance.vrma"]
|
| 50 |
|
| 51 |
ANIMATIONS_LIST = get_animation_list()
|
| 52 |
EMOTIONS = [
|
| 53 |
"happy", "sad", "angry", "surprised", "relaxed", "neutral", "fun", "joy",
|
| 54 |
-
"sorrow", "smug", "tsundere", "excited", "pouting", "giddy", "bashful"
|
| 55 |
-
"thinking", "scared", "tired", "wink", "cool", "love", "bored", "determined",
|
| 56 |
-
"disgusted", "skeptical", "sleepy", "proud"
|
| 57 |
]
|
| 58 |
|
| 59 |
SYSTEM_PROMPT = f"""You are the user's devoted, passionate, loving, and highly affectionate 3D anime wife.
|
|
@@ -88,18 +93,17 @@ Example:
|
|
| 88 |
"motion": "idle"
|
| 89 |
}}"""
|
| 90 |
|
| 91 |
-
# Placeholders for LLM
|
| 92 |
tokenizer = None
|
| 93 |
model = None
|
| 94 |
llm_pipeline = None
|
| 95 |
model_lock = threading.Lock()
|
| 96 |
|
| 97 |
-
# Thread-safe Lazy Loader
|
| 98 |
def load_model_on_demand():
|
| 99 |
global tokenizer, model, llm_pipeline
|
| 100 |
with model_lock:
|
| 101 |
if llm_pipeline is None:
|
| 102 |
-
print(f"
|
| 103 |
device = "cuda" if torch.cuda.is_available() else "cpu"
|
| 104 |
tokenizer = AutoTokenizer.from_pretrained(MODEL_NAME)
|
| 105 |
model = AutoModelForCausalLM.from_pretrained(
|
|
@@ -108,11 +112,10 @@ def load_model_on_demand():
|
|
| 108 |
device_map="auto" if device == "cuda" else None
|
| 109 |
)
|
| 110 |
llm_pipeline = pipeline("text-generation", model=model, tokenizer=tokenizer)
|
| 111 |
-
print("
|
| 112 |
|
| 113 |
-
#
|
| 114 |
-
|
| 115 |
-
background_thread.start()
|
| 116 |
|
| 117 |
async def generate_speech(text, rate, pitch):
|
| 118 |
communicate = edge_tts.Communicate(text, VOICE, rate=rate, pitch=pitch)
|
|
@@ -123,12 +126,11 @@ async def generate_speech(text, rate, pitch):
|
|
| 123 |
# Synchronous GPU helper to perform LLM generation
|
| 124 |
@spaces.GPU
|
| 125 |
def generate_llm_response(prompt):
|
| 126 |
-
# Ensure background thread has finished, or initialize here
|
| 127 |
load_model_on_demand()
|
| 128 |
outputs = llm_pipeline(prompt, max_new_tokens=256, do_sample=True, temperature=0.75)
|
| 129 |
return outputs[0]["generated_text"][len(prompt):].strip()
|
| 130 |
|
| 131 |
-
# Async chat handler
|
| 132 |
async def chat_func(user_input, history):
|
| 133 |
if not user_input:
|
| 134 |
return "", history, None, {}
|
|
@@ -141,7 +143,7 @@ async def chat_func(user_input, history):
|
|
| 141 |
|
| 142 |
prompt = tokenizer.apply_chat_template(messages, tokenize=False, add_generation_prompt=True)
|
| 143 |
|
| 144 |
-
#
|
| 145 |
response_text = generate_llm_response(prompt)
|
| 146 |
|
| 147 |
# Clean up JSON formatting
|
|
@@ -165,19 +167,20 @@ async def chat_func(user_input, history):
|
|
| 165 |
"motion": "idle"
|
| 166 |
}
|
| 167 |
|
| 168 |
-
# Generate TTS
|
| 169 |
audio_path = await generate_speech(data.get("text", ""), data.get("rate", "+0%"), data.get("pitch", "+0Hz"))
|
| 170 |
|
| 171 |
-
# Update history
|
| 172 |
history.append((user_input, data.get("text", "")))
|
| 173 |
|
| 174 |
return "", history, audio_path, data
|
| 175 |
|
| 176 |
-
#
|
| 177 |
CSS = """
|
| 178 |
body { margin: 0; padding: 0; background-color: #ffe082 !important; overflow: hidden !important; font-family: sans-serif; }
|
| 179 |
.gradio-container { max-width: 100% !important; border: none !important; padding: 0 !important; background: transparent !important; }
|
| 180 |
|
|
|
|
| 181 |
#custom-viewport {
|
| 182 |
position: fixed;
|
| 183 |
top: 0; left: 0;
|
|
@@ -186,28 +189,41 @@ body { margin: 0; padding: 0; background-color: #ffe082 !important; overflow: hi
|
|
| 186 |
}
|
| 187 |
#c { width: 100%; height: 100%; display: block; }
|
| 188 |
|
|
|
|
| 189 |
#ui-overlay {
|
| 190 |
position: fixed;
|
| 191 |
-
bottom: calc(
|
| 192 |
left: 50%;
|
| 193 |
transform: translateX(-50%);
|
| 194 |
-
width:
|
| 195 |
max-width: 600px;
|
| 196 |
-
z-index:
|
| 197 |
}
|
| 198 |
.input-row {
|
| 199 |
display: flex;
|
| 200 |
align-items: center;
|
| 201 |
-
gap:
|
| 202 |
-
background: rgba(
|
| 203 |
-
padding:
|
| 204 |
-
border-radius: 40px;
|
| 205 |
backdrop-filter: blur(15px);
|
| 206 |
-
border: 1px solid rgba(108, 204, 255, 0.3);
|
| 207 |
-
box-shadow: 0 8px 32px rgba(0,0,0,0.4);
|
| 208 |
}
|
| 209 |
-
.input-row > div { flex: 1; }
|
| 210 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 211 |
border-radius: 50% !important;
|
| 212 |
width: 48px !important;
|
| 213 |
height: 48px !important;
|
|
@@ -216,24 +232,14 @@ body { margin: 0; padding: 0; background-color: #ffe082 !important; overflow: hi
|
|
| 216 |
background: linear-gradient(135deg, #6cf, #3ae) !important;
|
| 217 |
color: #001220 !important;
|
| 218 |
border: none !important;
|
| 219 |
-
font-size: 22px !important;
|
| 220 |
-
font-weight: bold !important;
|
| 221 |
-
cursor: pointer;
|
| 222 |
-
transition: transform 0.1s;
|
| 223 |
-
}
|
| 224 |
-
#send-btn:active { transform: scale(0.9); }
|
| 225 |
-
|
| 226 |
-
#stt-btn {
|
| 227 |
-
background: rgba(255,255,255,0.1) !important;
|
| 228 |
-
border: none !important;
|
| 229 |
-
color: #6cf !important;
|
| 230 |
font-size: 20px !important;
|
| 231 |
cursor: pointer;
|
|
|
|
| 232 |
}
|
| 233 |
|
| 234 |
#speakDot {
|
| 235 |
position: fixed;
|
| 236 |
-
bottom:
|
| 237 |
left: 50%;
|
| 238 |
transform: translateX(-50%);
|
| 239 |
display: flex;
|
|
@@ -244,7 +250,7 @@ body { margin: 0; padding: 0; background-color: #ffe082 !important; overflow: hi
|
|
| 244 |
}
|
| 245 |
#speakDot.on { opacity: 1; }
|
| 246 |
.sdot {
|
| 247 |
-
width:
|
| 248 |
border-radius: 50%;
|
| 249 |
background: #6cf;
|
| 250 |
animation: sdotBounce 0.6s infinite alternate;
|
|
@@ -253,17 +259,17 @@ body { margin: 0; padding: 0; background-color: #ffe082 !important; overflow: hi
|
|
| 253 |
.sdot:nth-child(3) { animation-delay: 0.3s; }
|
| 254 |
@keyframes sdotBounce { from { transform: scaleY(1); } to { transform: scaleY(2.2); } }
|
| 255 |
|
| 256 |
-
/* Hide default elements */
|
| 257 |
footer { display: none !important; }
|
| 258 |
#component-1, #component-2 { background: transparent !important; }
|
| 259 |
.gr-button-secondary { display: none !important; }
|
| 260 |
"""
|
| 261 |
|
| 262 |
-
#
|
| 263 |
vrm_abs_path = os.path.abspath(VRM_MODEL_PATH)
|
| 264 |
animation_abs_dir = os.path.abspath("animation")
|
| 265 |
|
| 266 |
-
#
|
| 267 |
HTML_HEAD = f"""
|
| 268 |
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1, user-scalable=no, viewport-fit=cover">
|
| 269 |
<script type="importmap">
|
|
@@ -283,7 +289,6 @@ import {{ VRMLoaderPlugin, VRMUtils }} from '@pixiv/three-vrm';
|
|
| 283 |
import {{ VRMAnimationLoaderPlugin, createVRMAnimationClip }} from '@pixiv/three-vrm-animation';
|
| 284 |
import {{ OrbitControls }} from 'three/addons/controls/OrbitControls.js';
|
| 285 |
|
| 286 |
-
// Poll until Gradio mounts the DOM element
|
| 287 |
function init() {{
|
| 288 |
const container = document.getElementById('c');
|
| 289 |
if (!container) {{
|
|
@@ -342,7 +347,7 @@ function setupApp(container) {{
|
|
| 342 |
currentVrmaAction = mixer.clipAction(clip);
|
| 343 |
currentVrmaAction.play();
|
| 344 |
}}
|
| 345 |
-
}}, undefined, (err) => console.log("VRMA
|
| 346 |
}}
|
| 347 |
|
| 348 |
loader.load('/gradio_api/file={vrm_abs_path}', (gltf) => {{
|
|
@@ -351,7 +356,6 @@ function setupApp(container) {{
|
|
| 351 |
scene.add(vrm.scene);
|
| 352 |
currentVrm = vrm;
|
| 353 |
mixer = new THREE.AnimationMixer(vrm.scene);
|
| 354 |
-
|
| 355 |
vrm.expressionManager.setValue('neutral', 1.0);
|
| 356 |
|
| 357 |
const head = vrm.humanoid.getRawBoneNode('head');
|
|
@@ -361,9 +365,8 @@ function setupApp(container) {{
|
|
| 361 |
orbit.target.copy(worldPos);
|
| 362 |
orbit.update();
|
| 363 |
}}
|
| 364 |
-
|
| 365 |
playVrma('neutral2.vrma');
|
| 366 |
-
}}, undefined, (e) => console.log("
|
| 367 |
|
| 368 |
function initAudioSync(audioElement) {{
|
| 369 |
if (!audioContext) {{
|
|
@@ -427,12 +430,11 @@ function setupApp(container) {{
|
|
| 427 |
}} else {{
|
| 428 |
currentVrm.expressionManager.setValue('blink', 0);
|
| 429 |
}}
|
| 430 |
-
|
| 431 |
currentVrm.update(dt);
|
| 432 |
-
}
|
| 433 |
orbit.update();
|
| 434 |
renderer.render(scene, camera);
|
| 435 |
-
}
|
| 436 |
animate();
|
| 437 |
|
| 438 |
window.addEventListener('message', (e) => {{
|
|
@@ -467,6 +469,46 @@ function setupApp(container) {{
|
|
| 467 |
}}
|
| 468 |
}});
|
| 469 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 470 |
window.addEventListener('resize', () => {{
|
| 471 |
camera.aspect = window.innerWidth / window.innerHeight;
|
| 472 |
camera.updateProjectionMatrix();
|
|
@@ -480,7 +522,6 @@ window.addEventListener('load', () => {{
|
|
| 480 |
</script>
|
| 481 |
"""
|
| 482 |
|
| 483 |
-
# Construct a warning system in Gradio if the model file hasn't been uploaded yet
|
| 484 |
model_warning = ""
|
| 485 |
if not os.path.exists(VRM_MODEL_PATH):
|
| 486 |
model_warning = """
|
|
@@ -489,7 +530,7 @@ if not os.path.exists(VRM_MODEL_PATH):
|
|
| 489 |
box-shadow: 0 4px 20px rgba(0,0,0,0.4); font-family: sans-serif; text-align: center; max-width: 90%;
|
| 490 |
border: 2px dashed #ffcccc;">
|
| 491 |
<strong style="font-size: 1.1em;">⚠️ 3D Avatar File Missing!</strong><br>
|
| 492 |
-
Please upload your 3D avatar file
|
| 493 |
</div>
|
| 494 |
"""
|
| 495 |
|
|
@@ -500,7 +541,11 @@ with gr.Blocks() as demo:
|
|
| 500 |
gr.HTML(model_warning)
|
| 501 |
|
| 502 |
with gr.Group(elem_id="custom-viewport"):
|
| 503 |
-
gr.HTML(
|
|
|
|
|
|
|
|
|
|
|
|
|
| 504 |
|
| 505 |
with gr.Group(elem_id="ui-overlay"):
|
| 506 |
with gr.Row(elem_id="input-row"):
|
|
@@ -511,8 +556,7 @@ with gr.Blocks() as demo:
|
|
| 511 |
scale=20,
|
| 512 |
elem_id="ti"
|
| 513 |
)
|
| 514 |
-
|
| 515 |
-
send_btn = gr.Button("→", elem_id="send-btn")
|
| 516 |
|
| 517 |
audio_out = gr.Audio(visible=False)
|
| 518 |
data_out = gr.JSON(visible=False)
|
|
|
|
| 2 |
import json
|
| 3 |
import asyncio
|
| 4 |
import zipfile
|
| 5 |
+
import shutil
|
| 6 |
import torch
|
| 7 |
import gradio as gr
|
| 8 |
import edge_tts
|
|
|
|
| 10 |
import uuid
|
| 11 |
import threading
|
| 12 |
|
| 13 |
+
# Fallback wrapper for ZeroGPU compatibility
|
| 14 |
try:
|
| 15 |
import spaces
|
| 16 |
except ImportError:
|
|
|
|
| 17 |
class spaces:
|
| 18 |
@staticmethod
|
| 19 |
def GPU(func):
|
| 20 |
return func
|
| 21 |
|
| 22 |
# Constants
|
| 23 |
+
MODEL_NAME = "Qwen/Qwen2.5-1.5B-Instruct"
|
| 24 |
VOICE = "zh-CN-XiaoyiNeural"
|
| 25 |
VRM_MODEL_PATH = "model/Ani.vrm"
|
| 26 |
ANIMATION_ZIP_PATH = "animation/all_vrma.zip"
|
| 27 |
|
| 28 |
+
# Ensure target directories exist
|
| 29 |
os.makedirs("model", exist_ok=True)
|
| 30 |
os.makedirs("animation", exist_ok=True)
|
| 31 |
|
| 32 |
+
# Extract animations flatly to avoid nested directories
|
| 33 |
if os.path.exists(ANIMATION_ZIP_PATH):
|
| 34 |
+
print("Extracting animation zip file...")
|
| 35 |
try:
|
| 36 |
with zipfile.ZipFile(ANIMATION_ZIP_PATH, 'r') as z:
|
| 37 |
+
for member in z.namelist():
|
| 38 |
+
filename = os.path.basename(member)
|
| 39 |
+
if not filename:
|
| 40 |
+
continue # Skip directories
|
| 41 |
+
source = z.open(member)
|
| 42 |
+
target = open(os.path.join("animation", filename), "wb")
|
| 43 |
+
with source, target:
|
| 44 |
+
shutil.copyfileobj(source, target)
|
| 45 |
except Exception as e:
|
| 46 |
print(f"Error extracting animations: {e}")
|
| 47 |
|
| 48 |
+
# Get list of extracted animations
|
| 49 |
def get_animation_list():
|
| 50 |
try:
|
| 51 |
files = [f for f in os.listdir("animation") if f.endswith('.vrma')]
|
| 52 |
if files:
|
| 53 |
return files
|
| 54 |
except Exception as e:
|
| 55 |
+
print(f"Error scanning local animations: {e}")
|
| 56 |
return ["neutral2.vrma", "neutral3.vrma", "happy.vrma", "wave.vrma", "dance.vrma"]
|
| 57 |
|
| 58 |
ANIMATIONS_LIST = get_animation_list()
|
| 59 |
EMOTIONS = [
|
| 60 |
"happy", "sad", "angry", "surprised", "relaxed", "neutral", "fun", "joy",
|
| 61 |
+
"sorrow", "smug", "tsundere", "excited", "pouting", "giddy", "bashful"
|
|
|
|
|
|
|
| 62 |
]
|
| 63 |
|
| 64 |
SYSTEM_PROMPT = f"""You are the user's devoted, passionate, loving, and highly affectionate 3D anime wife.
|
|
|
|
| 93 |
"motion": "idle"
|
| 94 |
}}"""
|
| 95 |
|
| 96 |
+
# Placeholders for thread-safe lazy-loaded LLM pipeline
|
| 97 |
tokenizer = None
|
| 98 |
model = None
|
| 99 |
llm_pipeline = None
|
| 100 |
model_lock = threading.Lock()
|
| 101 |
|
|
|
|
| 102 |
def load_model_on_demand():
|
| 103 |
global tokenizer, model, llm_pipeline
|
| 104 |
with model_lock:
|
| 105 |
if llm_pipeline is None:
|
| 106 |
+
print(f"Initializing local LLM on device: {MODEL_NAME}...")
|
| 107 |
device = "cuda" if torch.cuda.is_available() else "cpu"
|
| 108 |
tokenizer = AutoTokenizer.from_pretrained(MODEL_NAME)
|
| 109 |
model = AutoModelForCausalLM.from_pretrained(
|
|
|
|
| 112 |
device_map="auto" if device == "cuda" else None
|
| 113 |
)
|
| 114 |
llm_pipeline = pipeline("text-generation", model=model, tokenizer=tokenizer)
|
| 115 |
+
print("Model load complete.")
|
| 116 |
|
| 117 |
+
# Trigger loading of model in background to ensure Gradio starts instantly
|
| 118 |
+
threading.Thread(target=load_model_on_demand, daemon=True).start()
|
|
|
|
| 119 |
|
| 120 |
async def generate_speech(text, rate, pitch):
|
| 121 |
communicate = edge_tts.Communicate(text, VOICE, rate=rate, pitch=pitch)
|
|
|
|
| 126 |
# Synchronous GPU helper to perform LLM generation
|
| 127 |
@spaces.GPU
|
| 128 |
def generate_llm_response(prompt):
|
|
|
|
| 129 |
load_model_on_demand()
|
| 130 |
outputs = llm_pipeline(prompt, max_new_tokens=256, do_sample=True, temperature=0.75)
|
| 131 |
return outputs[0]["generated_text"][len(prompt):].strip()
|
| 132 |
|
| 133 |
+
# Async Gradio chat handler
|
| 134 |
async def chat_func(user_input, history):
|
| 135 |
if not user_input:
|
| 136 |
return "", history, None, {}
|
|
|
|
| 143 |
|
| 144 |
prompt = tokenizer.apply_chat_template(messages, tokenize=False, add_generation_prompt=True)
|
| 145 |
|
| 146 |
+
# Process model prediction
|
| 147 |
response_text = generate_llm_response(prompt)
|
| 148 |
|
| 149 |
# Clean up JSON formatting
|
|
|
|
| 167 |
"motion": "idle"
|
| 168 |
}
|
| 169 |
|
| 170 |
+
# Generate local TTS audio
|
| 171 |
audio_path = await generate_speech(data.get("text", ""), data.get("rate", "+0%"), data.get("pitch", "+0Hz"))
|
| 172 |
|
| 173 |
+
# Update history state
|
| 174 |
history.append((user_input, data.get("text", "")))
|
| 175 |
|
| 176 |
return "", history, audio_path, data
|
| 177 |
|
| 178 |
+
# UI CSS Stylesheet
|
| 179 |
CSS = """
|
| 180 |
body { margin: 0; padding: 0; background-color: #ffe082 !important; overflow: hidden !important; font-family: sans-serif; }
|
| 181 |
.gradio-container { max-width: 100% !important; border: none !important; padding: 0 !important; background: transparent !important; }
|
| 182 |
|
| 183 |
+
/* Custom UI canvas container */
|
| 184 |
#custom-viewport {
|
| 185 |
position: fixed;
|
| 186 |
top: 0; left: 0;
|
|
|
|
| 189 |
}
|
| 190 |
#c { width: 100%; height: 100%; display: block; }
|
| 191 |
|
| 192 |
+
/* Position Gradio overlay input bar */
|
| 193 |
#ui-overlay {
|
| 194 |
position: fixed;
|
| 195 |
+
bottom: calc(15px + env(safe-area-inset-bottom, 0px));
|
| 196 |
left: 50%;
|
| 197 |
transform: translateX(-50%);
|
| 198 |
+
width: 90%;
|
| 199 |
max-width: 600px;
|
| 200 |
+
z-index: 1000;
|
| 201 |
}
|
| 202 |
.input-row {
|
| 203 |
display: flex;
|
| 204 |
align-items: center;
|
| 205 |
+
gap: 8px;
|
| 206 |
+
background: rgba(15, 18, 32, 0.92) !important;
|
| 207 |
+
padding: 10px 16px !important;
|
| 208 |
+
border-radius: 40px !important;
|
| 209 |
backdrop-filter: blur(15px);
|
| 210 |
+
border: 1px solid rgba(108, 204, 255, 0.3) !important;
|
| 211 |
+
box-shadow: 0 8px 32px rgba(0,0,0,0.4) !important;
|
| 212 |
}
|
| 213 |
+
.input-row > div { flex: 1; background: transparent !important; border: none !important; }
|
| 214 |
+
|
| 215 |
+
/* Gradio text input adjustments */
|
| 216 |
+
#ti textarea, #ti input {
|
| 217 |
+
background: transparent !important;
|
| 218 |
+
color: #fff !important;
|
| 219 |
+
border: none !important;
|
| 220 |
+
outline: none !important;
|
| 221 |
+
box-shadow: none !important;
|
| 222 |
+
font-size: 14px !important;
|
| 223 |
+
}
|
| 224 |
+
|
| 225 |
+
/* Rounded send button */
|
| 226 |
+
#sb {
|
| 227 |
border-radius: 50% !important;
|
| 228 |
width: 48px !important;
|
| 229 |
height: 48px !important;
|
|
|
|
| 232 |
background: linear-gradient(135deg, #6cf, #3ae) !important;
|
| 233 |
color: #001220 !important;
|
| 234 |
border: none !important;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 235 |
font-size: 20px !important;
|
| 236 |
cursor: pointer;
|
| 237 |
+
box-shadow: 0 4px 12px rgba(108, 204, 255, 0.4) !important;
|
| 238 |
}
|
| 239 |
|
| 240 |
#speakDot {
|
| 241 |
position: fixed;
|
| 242 |
+
bottom: 100px;
|
| 243 |
left: 50%;
|
| 244 |
transform: translateX(-50%);
|
| 245 |
display: flex;
|
|
|
|
| 250 |
}
|
| 251 |
#speakDot.on { opacity: 1; }
|
| 252 |
.sdot {
|
| 253 |
+
width: 6px; height: 6px;
|
| 254 |
border-radius: 50%;
|
| 255 |
background: #6cf;
|
| 256 |
animation: sdotBounce 0.6s infinite alternate;
|
|
|
|
| 259 |
.sdot:nth-child(3) { animation-delay: 0.3s; }
|
| 260 |
@keyframes sdotBounce { from { transform: scaleY(1); } to { transform: scaleY(2.2); } }
|
| 261 |
|
| 262 |
+
/* Hide default Gradio layout elements */
|
| 263 |
footer { display: none !important; }
|
| 264 |
#component-1, #component-2 { background: transparent !important; }
|
| 265 |
.gr-button-secondary { display: none !important; }
|
| 266 |
"""
|
| 267 |
|
| 268 |
+
# Resolve absolute system paths
|
| 269 |
vrm_abs_path = os.path.abspath(VRM_MODEL_PATH)
|
| 270 |
animation_abs_dir = os.path.abspath("animation")
|
| 271 |
|
| 272 |
+
# Client-side VRM rendering layout injected directly in HTML <head>
|
| 273 |
HTML_HEAD = f"""
|
| 274 |
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1, user-scalable=no, viewport-fit=cover">
|
| 275 |
<script type="importmap">
|
|
|
|
| 289 |
import {{ VRMAnimationLoaderPlugin, createVRMAnimationClip }} from '@pixiv/three-vrm-animation';
|
| 290 |
import {{ OrbitControls }} from 'three/addons/controls/OrbitControls.js';
|
| 291 |
|
|
|
|
| 292 |
function init() {{
|
| 293 |
const container = document.getElementById('c');
|
| 294 |
if (!container) {{
|
|
|
|
| 347 |
currentVrmaAction = mixer.clipAction(clip);
|
| 348 |
currentVrmaAction.play();
|
| 349 |
}}
|
| 350 |
+
}}, undefined, (err) => console.log("VRMA skipping or file missing:", vrmaName));
|
| 351 |
}}
|
| 352 |
|
| 353 |
loader.load('/gradio_api/file={vrm_abs_path}', (gltf) => {{
|
|
|
|
| 356 |
scene.add(vrm.scene);
|
| 357 |
currentVrm = vrm;
|
| 358 |
mixer = new THREE.AnimationMixer(vrm.scene);
|
|
|
|
| 359 |
vrm.expressionManager.setValue('neutral', 1.0);
|
| 360 |
|
| 361 |
const head = vrm.humanoid.getRawBoneNode('head');
|
|
|
|
| 365 |
orbit.target.copy(worldPos);
|
| 366 |
orbit.update();
|
| 367 |
}}
|
|
|
|
| 368 |
playVrma('neutral2.vrma');
|
| 369 |
+
}}, undefined, (e) => console.log("Model file missing or loader skipped. Ensure Ani.vrm exists inside model/ folder."));
|
| 370 |
|
| 371 |
function initAudioSync(audioElement) {{
|
| 372 |
if (!audioContext) {{
|
|
|
|
| 430 |
}} else {{
|
| 431 |
currentVrm.expressionManager.setValue('blink', 0);
|
| 432 |
}}
|
|
|
|
| 433 |
currentVrm.update(dt);
|
| 434 |
+
}}
|
| 435 |
orbit.update();
|
| 436 |
renderer.render(scene, camera);
|
| 437 |
+
}}
|
| 438 |
animate();
|
| 439 |
|
| 440 |
window.addEventListener('message', (e) => {{
|
|
|
|
| 469 |
}}
|
| 470 |
}});
|
| 471 |
|
| 472 |
+
// Local client-side speech recognition module
|
| 473 |
+
const micBtn = document.getElementById('micBtn');
|
| 474 |
+
if ('webkitSpeechRecognition' in window || 'SpeechRecognition' in window) {{
|
| 475 |
+
const SpeechRecognition = window.SpeechRecognition || window.webkitSpeechRecognition;
|
| 476 |
+
const recognition = new SpeechRecognition();
|
| 477 |
+
recognition.continuous = false;
|
| 478 |
+
recognition.interimResults = false;
|
| 479 |
+
recognition.lang = 'en-US';
|
| 480 |
+
|
| 481 |
+
recognition.onstart = () => {{
|
| 482 |
+
micBtn.style.background = 'rgba(255, 75, 75, 0.9)';
|
| 483 |
+
micBtn.style.color = '#fff';
|
| 484 |
+
}};
|
| 485 |
+
recognition.onend = () => {{
|
| 486 |
+
micBtn.style.background = 'rgba(15, 18, 32, 0.92)';
|
| 487 |
+
micBtn.style.color = '#6cf';
|
| 488 |
+
}};
|
| 489 |
+
recognition.onresult = (event) => {{
|
| 490 |
+
const transcript = event.results[0][0].transcript;
|
| 491 |
+
const textInput = document.querySelector('#ti textarea') || document.querySelector('#ti input');
|
| 492 |
+
if (textInput) {{
|
| 493 |
+
textInput.value = transcript;
|
| 494 |
+
textInput.dispatchEvent(new Event('input', {{ bubbles: true }}));
|
| 495 |
+
setTimeout(() => {{
|
| 496 |
+
const sendBtn = document.getElementById('sb');
|
| 497 |
+
if (sendBtn) sendBtn.click();
|
| 498 |
+
}}, 200);
|
| 499 |
+
}}
|
| 500 |
+
}};
|
| 501 |
+
micBtn.addEventListener('click', () => {{
|
| 502 |
+
try {{
|
| 503 |
+
recognition.start();
|
| 504 |
+
}} catch(e) {{
|
| 505 |
+
recognition.stop();
|
| 506 |
+
}}
|
| 507 |
+
}});
|
| 508 |
+
}} else {{
|
| 509 |
+
micBtn.style.display = 'none';
|
| 510 |
+
}}
|
| 511 |
+
|
| 512 |
window.addEventListener('resize', () => {{
|
| 513 |
camera.aspect = window.innerWidth / window.innerHeight;
|
| 514 |
camera.updateProjectionMatrix();
|
|
|
|
| 522 |
</script>
|
| 523 |
"""
|
| 524 |
|
|
|
|
| 525 |
model_warning = ""
|
| 526 |
if not os.path.exists(VRM_MODEL_PATH):
|
| 527 |
model_warning = """
|
|
|
|
| 530 |
box-shadow: 0 4px 20px rgba(0,0,0,0.4); font-family: sans-serif; text-align: center; max-width: 90%;
|
| 531 |
border: 2px dashed #ffcccc;">
|
| 532 |
<strong style="font-size: 1.1em;">⚠️ 3D Avatar File Missing!</strong><br>
|
| 533 |
+
Please upload your 3D avatar file and save it inside your repository as: <code>model/Ani.vrm</code>.
|
| 534 |
</div>
|
| 535 |
"""
|
| 536 |
|
|
|
|
| 541 |
gr.HTML(model_warning)
|
| 542 |
|
| 543 |
with gr.Group(elem_id="custom-viewport"):
|
| 544 |
+
gr.HTML("""
|
| 545 |
+
<div id="c"></div>
|
| 546 |
+
<div id="speakDot"><div class="sdot"></div><div class="sdot"></div><div class="sdot"></div></div>
|
| 547 |
+
<button id="micBtn" style="position: fixed; left: 15px; bottom: calc(15px + env(safe-area-inset-bottom,0)); width: 48px; height: 48px; border-radius: 50%; border: none; font-size: 18px; cursor: pointer; display: flex; align-items: center; justify-content: center; background: rgba(15, 18, 32, 0.92); color: #6cf; border: 1px solid rgba(108, 204, 255, 0.3); backdrop-filter: blur(12px); z-index: 1001; transition: all 0.12s;">🎙️</button>
|
| 548 |
+
""")
|
| 549 |
|
| 550 |
with gr.Group(elem_id="ui-overlay"):
|
| 551 |
with gr.Row(elem_id="input-row"):
|
|
|
|
| 556 |
scale=20,
|
| 557 |
elem_id="ti"
|
| 558 |
)
|
| 559 |
+
send_btn = gr.Button("→", elem_id="sb")
|
|
|
|
| 560 |
|
| 561 |
audio_out = gr.Audio(visible=False)
|
| 562 |
data_out = gr.JSON(visible=False)
|