Upload app.py with huggingface_hub
Browse files
app.py
CHANGED
|
@@ -1,8 +1,9 @@
|
|
| 1 |
-
|
| 2 |
-
|
| 3 |
-
|
| 4 |
-
|
| 5 |
-
|
|
|
|
| 6 |
|
| 7 |
import os
|
| 8 |
import sys
|
|
@@ -28,7 +29,8 @@ from train import AudioUniRepLKNet, load_audio, extract_fbank
|
|
| 28 |
# ------------------------------------------------------------------------------
|
| 29 |
# 1. Model Loading
|
| 30 |
# ------------------------------------------------------------------------------
|
| 31 |
-
|
|
|
|
| 32 |
|
| 33 |
CANDIDATE_CKPTS = [
|
| 34 |
ROOT_DIR / "checkpoints" / "best_model.pth",
|
|
@@ -58,6 +60,7 @@ def get_model():
|
|
| 58 |
print(f"[Warning] No checkpoint found in {[str(p) for p in CANDIDATE_CKPTS]}")
|
| 59 |
return None
|
| 60 |
|
|
|
|
| 61 |
print(f"[Loading] Loading model from: {CKPT_PATH} on {device}...")
|
| 62 |
ckpt = torch.load(CKPT_PATH, map_location=device, weights_only=False)
|
| 63 |
|
|
@@ -80,10 +83,6 @@ def get_model():
|
|
| 80 |
return model
|
| 81 |
|
| 82 |
|
| 83 |
-
# Preload model
|
| 84 |
-
get_model()
|
| 85 |
-
|
| 86 |
-
|
| 87 |
# ------------------------------------------------------------------------------
|
| 88 |
# 2. Acoustic Analysis & Dynamic Explanation
|
| 89 |
# ------------------------------------------------------------------------------
|
|
@@ -193,6 +192,7 @@ def render_result_card(dominant_class: str, confidence: float, latency_ms: float
|
|
| 193 |
# ------------------------------------------------------------------------------
|
| 194 |
# 4. Inference Function
|
| 195 |
# ------------------------------------------------------------------------------
|
|
|
|
| 196 |
@torch.no_grad()
|
| 197 |
def predict_emotion(audio_path):
|
| 198 |
if audio_path is None:
|
|
@@ -202,6 +202,9 @@ def predict_emotion(audio_path):
|
|
| 202 |
if m is None:
|
| 203 |
return "<div style='color: #ef4444; padding: 20px;'>⚠️ Chưa tìm thấy checkpoint mô hình `best_model.pth`.</div>"
|
| 204 |
|
|
|
|
|
|
|
|
|
|
| 205 |
try:
|
| 206 |
data, sr = sf.read(audio_path, dtype="float32")
|
| 207 |
if data.ndim > 1:
|
|
|
|
| 1 |
+
try:
|
| 2 |
+
import spaces
|
| 3 |
+
gpu_decorator = spaces.GPU
|
| 4 |
+
except (ImportError, AttributeError):
|
| 5 |
+
def gpu_decorator(fn):
|
| 6 |
+
return fn
|
| 7 |
|
| 8 |
import os
|
| 9 |
import sys
|
|
|
|
| 29 |
# ------------------------------------------------------------------------------
|
| 30 |
# 1. Model Loading
|
| 31 |
# ------------------------------------------------------------------------------
|
| 32 |
+
def get_device():
|
| 33 |
+
return torch.device("cuda" if torch.cuda.is_available() else "cpu")
|
| 34 |
|
| 35 |
CANDIDATE_CKPTS = [
|
| 36 |
ROOT_DIR / "checkpoints" / "best_model.pth",
|
|
|
|
| 60 |
print(f"[Warning] No checkpoint found in {[str(p) for p in CANDIDATE_CKPTS]}")
|
| 61 |
return None
|
| 62 |
|
| 63 |
+
device = get_device()
|
| 64 |
print(f"[Loading] Loading model from: {CKPT_PATH} on {device}...")
|
| 65 |
ckpt = torch.load(CKPT_PATH, map_location=device, weights_only=False)
|
| 66 |
|
|
|
|
| 83 |
return model
|
| 84 |
|
| 85 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 86 |
# ------------------------------------------------------------------------------
|
| 87 |
# 2. Acoustic Analysis & Dynamic Explanation
|
| 88 |
# ------------------------------------------------------------------------------
|
|
|
|
| 192 |
# ------------------------------------------------------------------------------
|
| 193 |
# 4. Inference Function
|
| 194 |
# ------------------------------------------------------------------------------
|
| 195 |
+
@gpu_decorator
|
| 196 |
@torch.no_grad()
|
| 197 |
def predict_emotion(audio_path):
|
| 198 |
if audio_path is None:
|
|
|
|
| 202 |
if m is None:
|
| 203 |
return "<div style='color: #ef4444; padding: 20px;'>⚠️ Chưa tìm thấy checkpoint mô hình `best_model.pth`.</div>"
|
| 204 |
|
| 205 |
+
device = get_device()
|
| 206 |
+
m = m.to(device)
|
| 207 |
+
|
| 208 |
try:
|
| 209 |
data, sr = sf.read(audio_path, dtype="float32")
|
| 210 |
if data.ndim > 1:
|