Spaces:
Running
Running
Update main.py
Browse files
main.py
CHANGED
|
@@ -19,12 +19,29 @@ ALLOWED_EXTENSIONS = {'wav', 'mp3', 'mp4', 'mpeg', 'mpga', 'm4a', 'webm', 'flac'
|
|
| 19 |
|
| 20 |
# Initialize model variable
|
| 21 |
model = None
|
| 22 |
-
MODEL_SIZE = "
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 23 |
|
| 24 |
def load_whisper_model():
|
| 25 |
"""Load Whisper model with proper error handling"""
|
| 26 |
global model
|
| 27 |
try:
|
|
|
|
|
|
|
|
|
|
| 28 |
# Try multiple import strategies for openai-whisper
|
| 29 |
whisper_module = None
|
| 30 |
|
|
@@ -67,7 +84,15 @@ def load_whisper_model():
|
|
| 67 |
return False
|
| 68 |
|
| 69 |
logger.info(f"Loading Whisper model: {MODEL_SIZE}")
|
| 70 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 71 |
logger.info("Whisper model loaded successfully")
|
| 72 |
return True
|
| 73 |
|
|
@@ -82,12 +107,42 @@ def load_whisper_model():
|
|
| 82 |
logger.error("1. pip uninstall whisper")
|
| 83 |
logger.error("2. pip install openai-whisper torch torchaudio")
|
| 84 |
return False
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 85 |
except Exception as e:
|
| 86 |
logger.error(f"Error loading Whisper model: {e}")
|
| 87 |
logger.error("This could be due to:")
|
| 88 |
logger.error("- Insufficient memory")
|
| 89 |
logger.error("- Missing PyTorch/CUDA dependencies")
|
| 90 |
logger.error("- Network issues downloading the model")
|
|
|
|
| 91 |
return False
|
| 92 |
|
| 93 |
# Try to load the model at startup
|
|
|
|
| 19 |
|
| 20 |
# Initialize model variable
|
| 21 |
model = None
|
| 22 |
+
MODEL_SIZE = "tiny" # Use 'tiny' for faster loading on Hugging Face Spaces, change to 'base', 'small', 'medium', or 'large' as needed
|
| 23 |
+
|
| 24 |
+
# Set cache directory for Hugging Face Spaces
|
| 25 |
+
def setup_cache_directory():
|
| 26 |
+
"""Setup cache directory for Hugging Face Spaces"""
|
| 27 |
+
# Create a writable cache directory in the current working directory
|
| 28 |
+
cache_dir = os.path.join(os.getcwd(), ".whisper_cache")
|
| 29 |
+
os.makedirs(cache_dir, exist_ok=True)
|
| 30 |
+
|
| 31 |
+
# Set environment variables for Whisper cache
|
| 32 |
+
os.environ['XDG_CACHE_HOME'] = cache_dir
|
| 33 |
+
os.environ['WHISPER_CACHE'] = cache_dir
|
| 34 |
+
|
| 35 |
+
logger.info(f"Cache directory set to: {cache_dir}")
|
| 36 |
+
return cache_dir
|
| 37 |
|
| 38 |
def load_whisper_model():
|
| 39 |
"""Load Whisper model with proper error handling"""
|
| 40 |
global model
|
| 41 |
try:
|
| 42 |
+
# Setup cache directory first
|
| 43 |
+
cache_dir = setup_cache_directory()
|
| 44 |
+
|
| 45 |
# Try multiple import strategies for openai-whisper
|
| 46 |
whisper_module = None
|
| 47 |
|
|
|
|
| 84 |
return False
|
| 85 |
|
| 86 |
logger.info(f"Loading Whisper model: {MODEL_SIZE}")
|
| 87 |
+
logger.info(f"Using cache directory: {cache_dir}")
|
| 88 |
+
|
| 89 |
+
# Load model with explicit download root
|
| 90 |
+
try:
|
| 91 |
+
model = whisper_module.load_model(MODEL_SIZE, download_root=cache_dir)
|
| 92 |
+
except TypeError:
|
| 93 |
+
# Fallback if download_root parameter is not supported
|
| 94 |
+
model = whisper_module.load_model(MODEL_SIZE)
|
| 95 |
+
|
| 96 |
logger.info("Whisper model loaded successfully")
|
| 97 |
return True
|
| 98 |
|
|
|
|
| 107 |
logger.error("1. pip uninstall whisper")
|
| 108 |
logger.error("2. pip install openai-whisper torch torchaudio")
|
| 109 |
return False
|
| 110 |
+
except PermissionError as e:
|
| 111 |
+
logger.error(f"Permission error: {e}")
|
| 112 |
+
logger.error("Cannot write to cache directory. This might be a Hugging Face Spaces limitation.")
|
| 113 |
+
logger.error("Trying alternative cache locations...")
|
| 114 |
+
|
| 115 |
+
# Try alternative cache locations
|
| 116 |
+
alternative_dirs = [
|
| 117 |
+
"/tmp/.whisper_cache",
|
| 118 |
+
os.path.expanduser("~/.whisper_cache"),
|
| 119 |
+
"./whisper_models"
|
| 120 |
+
]
|
| 121 |
+
|
| 122 |
+
for alt_dir in alternative_dirs:
|
| 123 |
+
try:
|
| 124 |
+
os.makedirs(alt_dir, exist_ok=True)
|
| 125 |
+
os.environ['XDG_CACHE_HOME'] = alt_dir
|
| 126 |
+
os.environ['WHISPER_CACHE'] = alt_dir
|
| 127 |
+
logger.info(f"Trying alternative cache: {alt_dir}")
|
| 128 |
+
|
| 129 |
+
import whisper
|
| 130 |
+
model = whisper.load_model(MODEL_SIZE, download_root=alt_dir)
|
| 131 |
+
logger.info(f"Successfully loaded model with cache: {alt_dir}")
|
| 132 |
+
return True
|
| 133 |
+
except Exception as alt_e:
|
| 134 |
+
logger.warning(f"Alternative cache {alt_dir} failed: {alt_e}")
|
| 135 |
+
continue
|
| 136 |
+
|
| 137 |
+
logger.error("All cache directory attempts failed")
|
| 138 |
+
return False
|
| 139 |
except Exception as e:
|
| 140 |
logger.error(f"Error loading Whisper model: {e}")
|
| 141 |
logger.error("This could be due to:")
|
| 142 |
logger.error("- Insufficient memory")
|
| 143 |
logger.error("- Missing PyTorch/CUDA dependencies")
|
| 144 |
logger.error("- Network issues downloading the model")
|
| 145 |
+
logger.error("- Hugging Face Spaces limitations")
|
| 146 |
return False
|
| 147 |
|
| 148 |
# Try to load the model at startup
|