Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update fasterwhisper.py #134

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 10 additions & 7 deletions transcription-api/backends/fasterwhisper.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,11 @@
from faster_whisper import WhisperModel, download_model, decode_audio

class FasterWhisperBackend(Backend):
device: str = "cpu" # cpu, cuda
quantization: str = "int8" # int8, float16
device: str = 'cpu' # cpu, cuda? (not working)
quantization: str = "default" # default,int8,float16,float32
model: WhisperModel | None = None

def __init__(self, model_size, device: str = "cpu"):
def __init__(self, model_size, device: str = None):
self.model_size = model_size
self.device = device
self.__post_init__()
Expand All @@ -26,8 +26,8 @@ def model_path(self) -> str:
raise RuntimeError(f"model not found in {local_model_path}")

def load(self) -> None:
# Get CPU threads env variable or default to 4
cpu_threads = int(os.environ.get("CPU_THREADS", 4))
# Get CPU threads env variable or default to 6
cpu_threads = int(os.environ.get("CPU_THREADS", 6))
self.model = WhisperModel(
self.model_path(), device=self.device, compute_type=self.quantization, cpu_threads=cpu_threads
)
Expand All @@ -40,10 +40,13 @@ def get_model(self) -> None:
if not os.path.exists(local_model_path):
os.makedirs(local_model_path)
try:
print("Model check start...")
download_model(self.model_size, output_dir=local_model_path, local_files_only=True, cache_dir=local_model_cache)
print("Model already cached...")
print("Model ceck ended...")
except:
print("Model download start...")
download_model(self.model_size, output_dir=local_model_path, local_files_only=False, cache_dir=local_model_cache)
print("Model download ended.")

def transcribe(
self, input: np.ndarray, silent: bool = False, language: str = None
Expand Down Expand Up @@ -96,4 +99,4 @@ def transcribe(
"duration": info.duration,
"segments": result,
}
return transcription
return transcription