-
Notifications
You must be signed in to change notification settings - Fork 122
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
5c18734
commit 12daf5e
Showing
5 changed files
with
185 additions
and
129 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,6 @@ | ||
{ | ||
"QFluentWidgets": { | ||
"ThemeColor": "#ff009faa", | ||
"ThemeMode": "Dark" | ||
"ThemeMode": "Light" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,66 +1,102 @@ | ||
# coding:utf-8 | ||
|
||
from threading import Thread | ||
from typing import (List, Optional, Union) | ||
from PySide6.QtCore import Signal | ||
# from threading import Thread | ||
from typing import (List, Optional, TypedDict, Union) | ||
from PySide6.QtCore import QThread, Signal | ||
from faster_whisper import WhisperModel | ||
from concurrent import futures | ||
|
||
def loadModel(model_size_or_path: str | ||
, device: str = "auto" | ||
, device_index: Union[int, List[int]] = 0 | ||
, compute_type: str = "default" | ||
, cpu_threads: int = 0 | ||
, num_workers: int = 1 | ||
, download_root: Optional[str] = None | ||
, local_files_only: bool = False | ||
, setStatusSignal: Signal = None | ||
): | ||
|
||
model_dict = {} | ||
def go(model : dict): | ||
try: | ||
model_ = WhisperModel(model_size_or_path, | ||
device=device, | ||
device_index=device_index, | ||
compute_type=compute_type, | ||
cpu_threads=cpu_threads, | ||
num_workers=num_workers, | ||
download_root=download_root, | ||
local_files_only=local_files_only | ||
) | ||
except Exception as e: | ||
model_ = None | ||
if setStatusSignal is not None: | ||
setStatusSignal.emit(False) | ||
raise e | ||
class modelParamDict(TypedDict): | ||
model_size_or_path: str | ||
device: str | ||
device_index: Union[int, List[int]] | ||
compute_type: str | ||
cpu_threads: int | ||
num_workers: int | ||
download_root: Optional[str] | ||
local_files_only: bool | ||
|
||
class LoadModelWorker(QThread): | ||
setStatusSignal = Signal(bool) | ||
loadModelOverSignal = Signal(bool) | ||
|
||
model["model"] = model_ | ||
def __init__(self, modelParam: modelParamDict, use_v3_model:bool = False , parent = None): | ||
super().__init__(parent=parent) | ||
self.isRunning = False | ||
self.model_size_or_path: str = modelParam["model_size_or_path"] | ||
self.device: str = modelParam["device"] | ||
self.device_index: Union[int, List[int]] = modelParam["device_index"] | ||
self.compute_type: str = modelParam["compute_type"] | ||
self.cpu_threads: int = modelParam["cpu_threads"] | ||
self.num_workers: int = modelParam["num_workers"] | ||
self.download_root: Optional[str] = modelParam["download_root"] | ||
self.local_files_only: bool = modelParam["local_files_only"] | ||
self.use_v3_model :bool = use_v3_model | ||
|
||
Thread_go = Thread(target=go, daemon=True, args=[model_dict]) | ||
Thread_go.run() | ||
self.model = None | ||
|
||
def run(self) -> None: | ||
self.isRunning = True | ||
|
||
self.model = self.loadModel() | ||
# with futures.ThreadPoolExecutor() as executor: | ||
# # 提交任务 | ||
# # futures_ = [executor.submit(self.loadModel)] | ||
|
||
# while(Thread_go.is_alive()): | ||
# if os.path.isdir(model_size_or_path): | ||
# print(".", end="", flush=True) | ||
# time.sleep(3) | ||
# results = executor.map(self.loadModel,[self.model_size_or_path]) | ||
|
||
try: | ||
model = model_dict["model"] | ||
print("\nLoad over") | ||
print(model_size_or_path) | ||
print(f"{'max_length: ':23}",model.max_length) | ||
print(f"{'num_samples_per_token: ':23}", model.num_samples_per_token) | ||
print("time_precision: ", model.time_precision) | ||
print("tokens_per_second: ", model.tokens_per_second) | ||
print("input_stride: ", model.input_stride) | ||
# for result in results: | ||
# self.model = result | ||
|
||
except Exception as e: | ||
if setStatusSignal is not None: | ||
setStatusSignal.emit(False) | ||
# for future in futures.as_completed(results): | ||
# pass | ||
# self.model = future.result() | ||
|
||
raise e | ||
if self.use_v3_model: | ||
# 修正 V3 模型的 mel 滤波器组参数 | ||
print("\n[Using V3 model, modify number of mel-filters to 128]") | ||
self.model.feature_extractor.mel_filters = self.model.feature_extractor.get_mel_filters(self.model.feature_extractor.sampling_rate, self.model.feature_extractor.n_fft, n_mels=128) | ||
|
||
self.isRunning = False | ||
# return self.model | ||
|
||
def stop(self): | ||
self.isRunning = False | ||
|
||
if setStatusSignal is not None: | ||
setStatusSignal.emit(True) | ||
def loadModel(self,model_size_or_path:str=None): | ||
|
||
model = None | ||
try: | ||
model = WhisperModel( | ||
self.model_size_or_path, | ||
device=self.device, | ||
device_index=self.device_index, | ||
compute_type=self.compute_type, | ||
cpu_threads=self.cpu_threads, | ||
num_workers=self.num_workers, | ||
download_root=self.download_root, | ||
local_files_only=self.local_files_only | ||
) | ||
except Exception as e: | ||
model = None | ||
self.setStatusSignal.emit(False) | ||
raise e | ||
|
||
try: | ||
print("\nLoad over") | ||
print(self.model_size_or_path) | ||
print(f"{'max_length: ':23}",model.max_length) | ||
print(f"{'num_samples_per_token: ':23}", model.num_samples_per_token) | ||
print("time_precision: ", model.time_precision) | ||
print("tokens_per_second: ", model.tokens_per_second) | ||
print("input_stride: ", model.input_stride) | ||
|
||
except Exception as e: | ||
|
||
self.setStatusSignal.emit(False) | ||
|
||
raise e | ||
|
||
self.setStatusSignal.emit(True) | ||
|
||
return model | ||
return model |
Oops, something went wrong.