Spaces:
Running
Running
Update main.py
Browse files
main.py
CHANGED
|
@@ -1,10 +1,10 @@
|
|
|
|
|
| 1 |
from flask import Flask, request, jsonify
|
| 2 |
import tempfile
|
| 3 |
import os
|
| 4 |
from werkzeug.utils import secure_filename
|
| 5 |
import logging
|
| 6 |
from datetime import datetime
|
| 7 |
-
from flask_cors import CORS
|
| 8 |
|
| 9 |
# Configure logging
|
| 10 |
logging.basicConfig(level=logging.INFO)
|
|
@@ -25,23 +25,69 @@ def load_whisper_model():
|
|
| 25 |
"""Load Whisper model with proper error handling"""
|
| 26 |
global model
|
| 27 |
try:
|
| 28 |
-
# Try
|
| 29 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 30 |
logger.info(f"Loading Whisper model: {MODEL_SIZE}")
|
| 31 |
-
model =
|
| 32 |
logger.info("Whisper model loaded successfully")
|
| 33 |
return True
|
| 34 |
-
|
| 35 |
-
|
|
|
|
|
|
|
| 36 |
return False
|
| 37 |
except AttributeError as e:
|
| 38 |
logger.error(f"Whisper import error: {e}")
|
| 39 |
logger.error("Make sure you have the correct whisper package installed:")
|
| 40 |
-
logger.error("
|
| 41 |
-
logger.error("pip
|
|
|
|
| 42 |
return False
|
| 43 |
except Exception as e:
|
| 44 |
logger.error(f"Error loading Whisper model: {e}")
|
|
|
|
|
|
|
|
|
|
|
|
|
| 45 |
return False
|
| 46 |
|
| 47 |
# Try to load the model at startup
|
|
|
|
| 1 |
+
from flask_cors import CORS
|
| 2 |
from flask import Flask, request, jsonify
|
| 3 |
import tempfile
|
| 4 |
import os
|
| 5 |
from werkzeug.utils import secure_filename
|
| 6 |
import logging
|
| 7 |
from datetime import datetime
|
|
|
|
| 8 |
|
| 9 |
# Configure logging
|
| 10 |
logging.basicConfig(level=logging.INFO)
|
|
|
|
| 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 |
+
|
| 31 |
+
# Strategy 1: Direct import (most common)
|
| 32 |
+
try:
|
| 33 |
+
import whisper as whisper_module
|
| 34 |
+
except ImportError:
|
| 35 |
+
pass
|
| 36 |
+
|
| 37 |
+
# Strategy 2: Try importing as openai_whisper
|
| 38 |
+
if whisper_module is None:
|
| 39 |
+
try:
|
| 40 |
+
import openai_whisper as whisper_module
|
| 41 |
+
except ImportError:
|
| 42 |
+
pass
|
| 43 |
+
|
| 44 |
+
# Strategy 3: Try importing with explicit path
|
| 45 |
+
if whisper_module is None:
|
| 46 |
+
try:
|
| 47 |
+
import sys
|
| 48 |
+
import importlib.util
|
| 49 |
+
# This is a fallback - usually not needed
|
| 50 |
+
import whisper as whisper_module
|
| 51 |
+
except ImportError:
|
| 52 |
+
pass
|
| 53 |
+
|
| 54 |
+
if whisper_module is None:
|
| 55 |
+
logger.error("OpenAI Whisper not installed. Install with: pip install openai-whisper")
|
| 56 |
+
return False
|
| 57 |
+
|
| 58 |
+
# Check if the module has the load_model function
|
| 59 |
+
if not hasattr(whisper_module, 'load_model'):
|
| 60 |
+
logger.error("Whisper module found but missing 'load_model' function")
|
| 61 |
+
logger.error("This suggests you have the wrong 'whisper' package installed")
|
| 62 |
+
logger.error("Solution:")
|
| 63 |
+
logger.error("1. pip uninstall whisper")
|
| 64 |
+
logger.error("2. pip uninstall openai-whisper (if exists)")
|
| 65 |
+
logger.error("3. pip install openai-whisper")
|
| 66 |
+
logger.error("4. pip install torch torchaudio")
|
| 67 |
+
return False
|
| 68 |
+
|
| 69 |
logger.info(f"Loading Whisper model: {MODEL_SIZE}")
|
| 70 |
+
model = whisper_module.load_model(MODEL_SIZE)
|
| 71 |
logger.info("Whisper model loaded successfully")
|
| 72 |
return True
|
| 73 |
+
|
| 74 |
+
except ImportError as e:
|
| 75 |
+
logger.error(f"Import error: {e}")
|
| 76 |
+
logger.error("OpenAI Whisper not installed. Install with: pip install openai-whisper torch torchaudio")
|
| 77 |
return False
|
| 78 |
except AttributeError as e:
|
| 79 |
logger.error(f"Whisper import error: {e}")
|
| 80 |
logger.error("Make sure you have the correct whisper package installed:")
|
| 81 |
+
logger.error("Solution:")
|
| 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
|