Skip to content

Commit

Permalink
20231121
Browse files Browse the repository at this point in the history
  • Loading branch information
CheshireCC committed Nov 21, 2023
1 parent 5c18734 commit 12daf5e
Show file tree
Hide file tree
Showing 5 changed files with 185 additions and 129 deletions.
2 changes: 1 addition & 1 deletion config/config.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"QFluentWidgets": {
"ThemeColor": "#ff009faa",
"ThemeMode": "Dark"
"ThemeMode": "Light"
}
}
6 changes: 3 additions & 3 deletions fasterWhisperGUIConfig.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"theme": "dark",
"theme": "light",
"demucs": {
"overlap": 0.1,
"segment": 7.8,
Expand Down Expand Up @@ -37,7 +37,7 @@
"autoClearTempFiles": true
},
"Transcription_param": {
"language": 1,
"language": 8,
"task": false,
"beam_size": "5",
"best_of": "1",
Expand Down Expand Up @@ -69,7 +69,7 @@
"closeDisplayMode": 0,
"whisperXMinSpeaker": 1,
"whisperXMaxSpeaker": 1,
"outputFormat": 4,
"outputFormat": 0,
"outputEncoding": 1
}
}
104 changes: 62 additions & 42 deletions faster_whisper_GUI/mainWindows.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@
, CAPTURE_PARA
)

from .modelLoad import loadModel
from .modelLoad import LoadModelWorker
from .convertModel import ConvertModel
from .transcribe import (
TranscribeWorker
Expand Down Expand Up @@ -109,6 +109,7 @@ def __init__(self, translator:QTranslator= None):
self.whisperXWorker = None
self.outputWorker = None
self.demucsWorker = None
self.loadModelWorker = None

self.stateTool = None

Expand Down Expand Up @@ -139,14 +140,14 @@ def getDownloadCacheDir(self):
self.download_cache_path = path

# ==============================================================================================================
# 将于下一版本废弃
# 重定向输出到文本框
# ==============================================================================================================
def setTextAndMoveCursorToProcessBrowser(self, text:str):
self.page_process.processResultText.moveCursor(QTextCursor.MoveOperation.End, QTextCursor.MoveMode.MoveAnchor)
self.page_process.processResultText.insertPlainText(text)

# ==============================================================================================================
# 将于下一版本废弃
# 输出重定向,但目前不再进行错误信息重定向,错误信息始终输出到 log 文件
# ==============================================================================================================
def redirectOutput(self, target : callable):
# 重定向输出
Expand All @@ -167,7 +168,7 @@ def onModelLoadClicked(self):

model_param = self.getParam_model()
for key, value in model_param.items():
print(f"{key}: {value}")
print(f" -{key}: {value}")

model_size_or_path = model_param["model_size_or_path"]

Expand All @@ -184,42 +185,58 @@ def onModelLoadClicked(self):
content = self.tr("在线下载模型")

infoBar = InfoBar(
icon=InfoBarIcon.INFORMATION,
title='',
content=content,
isClosable=False,
orient=Qt.Orientation.Vertical, # vertical layout
position=InfoBarPosition.TOP,
duration=2000,
parent=self
)

icon=InfoBarIcon.INFORMATION,
title='',
content=content,
isClosable=False,
orient=Qt.Orientation.Vertical, # vertical layout
position=InfoBarPosition.TOP,
duration=2000,
parent=self
)
infoBar.show()

def go():
self.FasterWhisperModel = loadModel(
model_size_or_path=model_param["model_size_or_path"]
, device=model_param["device"]
, device_index=model_param["device_index"]
, compute_type=model_param["compute_type"]
, cpu_threads=model_param["cpu_threads"]
, num_workers=model_param["num_workers"]
, download_root=model_param["download_root"]
, local_files_only=model_param["local_files_only"]
, setStatusSignal=self.statusToolSignalStore.LoadModelSignal
)

if self.page_model.switchButton_use_v3.isChecked():
# 修正 V3 模型的 mel 滤波器组参数
print("\n[Using V3 model, modify number of mel-filters to 128]")
self.FasterWhisperModel.feature_extractor.mel_filters = self.FasterWhisperModel.feature_extractor.get_mel_filters(self.FasterWhisperModel.feature_extractor.sampling_rate, self.FasterWhisperModel.feature_extractor.n_fft, n_mels=128)
# del self.FasterWhisperModel.model.is_multilingual
# self.FasterWhisperModel.model.is_multilingual = True


thread_go = Thread(target= go, daemon=True)
thread_go.start()
param_for_model_load = {
"model_size_or_path":model_param["model_size_or_path"],
"device":model_param["device"] ,
"device_index":model_param["device_index"],
"compute_type":model_param["compute_type"],
"cpu_threads":model_param["cpu_threads"],
"num_workers":model_param["num_workers"],
"download_root":model_param["download_root"],
"local_files_only":model_param["local_files_only"]
}

self.loadModelWorker = LoadModelWorker(param_for_model_load, use_v3_model=model_param["use_v3_model"] ,parent = self)
self.loadModelWorker.setStatusSignal.connect(self.loadModelResult)
self.loadModelWorker.setStatusSignal.connect(self.setModelStatusLabelTextForAll)
self.setStateTool(self.tr("加载模型"), self.tr("模型加载中,请稍候"), False)
# self.FasterWhisperModel = self.loadModelWorker.model
# self.FasterWhisperModel = self.loadModelWorker.run()
self.loadModelWorker.start()

# def go():
# self.FasterWhisperModel = loadModel(
# model_size_or_path=model_param["model_size_or_path"]
# , device =model_param["device"]
# , device_index =model_param["device_index"]
# , compute_type =model_param["compute_type"]
# , cpu_threads =model_param["cpu_threads"]
# , num_workers =model_param["num_workers"]
# , download_root =model_param["download_root"]
# , local_files_only=model_param["local_files_only"]
# , setStatusSignal =self.statusToolSignalStore.LoadModelSignal
# )

# if self.model_param["use_v3_model"]:
# # 修正 V3 模型的 mel 滤波器组参数
# print("\n[Using V3 model, modify number of mel-filters to 128]")
# self.FasterWhisperModel.feature_extractor.mel_filters = self.FasterWhisperModel.feature_extractor.get_mel_filters(self.FasterWhisperModel.feature_extractor.sampling_rate, self.FasterWhisperModel.feature_extractor.n_fft, n_mels=128)
# # del self.FasterWhisperModel.model.is_multilingual
# # self.FasterWhisperModel.model.is_multilingual = True
# thread_go = Thread(target= go, daemon=True)
# thread_go.start()

# sys.stdout = self.oubak
# sys.stderr = self.errbak
Expand All @@ -243,7 +260,8 @@ def getParam_model(self) -> dict:
num_workers: int = int(self.page_model.LineEdit_num_workers.text().replace(" ", ""))
download_root: str = self.page_model.LineEdit_download_root.text().replace(" ", "")
local_files_only: bool = self.page_model.switchButton_local_files_only.isChecked()
# local_files_only = local_files_only != "False"
use_v3_model: bool = self.page_model.switchButton_use_v3.isChecked()

model_dict : dict = {
"model_size_or_path" : model_size_or_path,
"device" : device,
Expand All @@ -252,7 +270,8 @@ def getParam_model(self) -> dict:
"cpu_threads" : cpu_threads,
"num_workers" : num_workers,
"download_root" : download_root,
"local_files_only" : local_files_only
"local_files_only" : local_files_only,
"use_v3_model" : use_v3_model
}

return model_dict
Expand Down Expand Up @@ -552,9 +571,9 @@ def transcribeOver(self, segments_path_info:list):
segment_, path, info = segments
print(path, info.language)
print(f"len:{len(segment_)}")
# for segment in segment_:
# print(f"[{segment.start}s --> {segment.end}] | {segment.text}")
# print(f"len_words: {len(segment.words)}")
for segment in segment_:
print(f"[{segment.start}s --> {segment.end}] | {segment.text}")
print(f"len_words: {len(segment.words)}")

print(f"len_segments_path_info_result: {len(segments_path_info)}")
self.showResultInTable(self.result_faster_whisper)
Expand Down Expand Up @@ -763,6 +782,7 @@ def loadModelResult(self, state:bool):
title=self.tr('加载结束'),
content=self.tr("模型加载成功")
)
self.FasterWhisperModel = self.loadModelWorker.model

elif not state:
self.setStateTool(text=self.tr("结束"), status=True)
Expand Down
144 changes: 90 additions & 54 deletions faster_whisper_GUI/modelLoad.py
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
Loading

0 comments on commit 12daf5e

Please sign in to comment.