bugfix to permission err on model
Browse files- src/utils.py +18 -3
src/utils.py
CHANGED
|
@@ -22,7 +22,6 @@ model_names = {
|
|
| 22 |
|
| 23 |
# Using only the two specified sherpa-onnx models from Hugging Face
|
| 24 |
sensevoice_models = {
|
| 25 |
-
"SenseVoice Small (2025)": "csukuangfj/sherpa-onnx-sense-voice-zh-en-ja-ko-yue-2025-09-09",
|
| 26 |
"SenseVoice Small (2025 int8)": "csukuangfj/sherpa-onnx-sense-voice-zh-en-ja-ko-yue-int8-2025-09-09",
|
| 27 |
"SenseVoice Small (2024)": "csukuangfj/sherpa-onnx-sense-voice-zh-en-ja-ko-yue-2024-07-17"
|
| 28 |
}
|
|
@@ -35,6 +34,20 @@ available_gguf_llms = {
|
|
| 35 |
|
| 36 |
s2tw_converter = opencc.OpenCC('s2twp')
|
| 37 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 38 |
def download_sensevoice_model(model_name: str) -> Path:
|
| 39 |
"""Download SenseVoice model from Hugging Face using official tools"""
|
| 40 |
|
|
@@ -46,7 +59,8 @@ def download_sensevoice_model(model_name: str) -> Path:
|
|
| 46 |
|
| 47 |
# Use model_name directly as repo_id
|
| 48 |
repo_id = model_name
|
| 49 |
-
|
|
|
|
| 50 |
|
| 51 |
# Check if model already exists
|
| 52 |
model_file = "model.int8.onnx" if "int8" in model_name else "model.onnx"
|
|
@@ -119,7 +133,8 @@ def load_sensevoice_model(model_name: str = "csukuangfj/sherpa-onnx-sense-voice-
|
|
| 119 |
except Exception as e:
|
| 120 |
print(f"Failed to load SenseVoice model: {e}")
|
| 121 |
# Try to force redownload on next attempt
|
| 122 |
-
|
|
|
|
| 123 |
if model_dir.exists():
|
| 124 |
import shutil
|
| 125 |
print(f"Removing model directory for redownload: {model_dir}")
|
|
|
|
| 22 |
|
| 23 |
# Using only the two specified sherpa-onnx models from Hugging Face
|
| 24 |
sensevoice_models = {
|
|
|
|
| 25 |
"SenseVoice Small (2025 int8)": "csukuangfj/sherpa-onnx-sense-voice-zh-en-ja-ko-yue-int8-2025-09-09",
|
| 26 |
"SenseVoice Small (2024)": "csukuangfj/sherpa-onnx-sense-voice-zh-en-ja-ko-yue-2024-07-17"
|
| 27 |
}
|
|
|
|
| 34 |
|
| 35 |
s2tw_converter = opencc.OpenCC('s2twp')
|
| 36 |
|
| 37 |
+
def get_writable_model_dir():
|
| 38 |
+
"""Get appropriate model directory for HF Spaces"""
|
| 39 |
+
# Check for HF Spaces environment
|
| 40 |
+
if os.environ.get('SPACE_ID'):
|
| 41 |
+
# Use HF Spaces cache directory
|
| 42 |
+
cache_dir = Path('/tmp/models')
|
| 43 |
+
else:
|
| 44 |
+
# Use standard cache directory
|
| 45 |
+
cache_dir = Path.home() / ".cache" / "speech_assistant" / "models"
|
| 46 |
+
|
| 47 |
+
# Ensure directory exists
|
| 48 |
+
cache_dir.mkdir(parents=True, exist_ok=True)
|
| 49 |
+
return cache_dir
|
| 50 |
+
|
| 51 |
def download_sensevoice_model(model_name: str) -> Path:
|
| 52 |
"""Download SenseVoice model from Hugging Face using official tools"""
|
| 53 |
|
|
|
|
| 59 |
|
| 60 |
# Use model_name directly as repo_id
|
| 61 |
repo_id = model_name
|
| 62 |
+
model_cache_dir = get_writable_model_dir()
|
| 63 |
+
local_dir = model_cache_dir / model_name.replace("/", "--")
|
| 64 |
|
| 65 |
# Check if model already exists
|
| 66 |
model_file = "model.int8.onnx" if "int8" in model_name else "model.onnx"
|
|
|
|
| 133 |
except Exception as e:
|
| 134 |
print(f"Failed to load SenseVoice model: {e}")
|
| 135 |
# Try to force redownload on next attempt
|
| 136 |
+
model_cache_dir = get_writable_model_dir()
|
| 137 |
+
model_dir = model_cache_dir / model_name.replace("/", "--")
|
| 138 |
if model_dir.exists():
|
| 139 |
import shutil
|
| 140 |
print(f"Removing model directory for redownload: {model_dir}")
|