Merge pull request #13 from makaveli10/update_faster_whisper
Update faster whisper.
This commit is contained in:
@@ -45,8 +45,12 @@ function startRecording() {
|
|||||||
socket.send("handshake");
|
socket.send("handshake");
|
||||||
};
|
};
|
||||||
|
|
||||||
|
let isServerReady = false;
|
||||||
socket.onmessage = (event) => {
|
socket.onmessage = (event) => {
|
||||||
// console.log(event.data);
|
if (!isServerReady){
|
||||||
|
isServerReady = true;
|
||||||
|
return;
|
||||||
|
}
|
||||||
const data = event.data;
|
const data = event.data;
|
||||||
browser.runtime.sendMessage({ action: "transcript", data })
|
browser.runtime.sendMessage({ action: "transcript", data })
|
||||||
.catch(function(error) {
|
.catch(function(error) {
|
||||||
@@ -64,7 +68,7 @@ function startRecording() {
|
|||||||
recorder = audioContext.createScriptProcessor(4096, 1, 1);
|
recorder = audioContext.createScriptProcessor(4096, 1, 1);
|
||||||
|
|
||||||
recorder.onaudioprocess = async (event) => {
|
recorder.onaudioprocess = async (event) => {
|
||||||
if (!audioContext || !isCapturing) return;
|
if (!audioContext || !isCapturing || !isServerReady) return;
|
||||||
|
|
||||||
const inputData = event.inputBuffer.getChannelData(0);
|
const inputData = event.inputBuffer.getChannelData(0);
|
||||||
const audioData16kHz = resampleTo16kHZ(inputData, audioContext.sampleRate);
|
const audioData16kHz = resampleTo16kHZ(inputData, audioContext.sampleRate);
|
||||||
|
|||||||
@@ -81,25 +81,32 @@ async function startRecord(option) {
|
|||||||
};
|
};
|
||||||
|
|
||||||
const socket = new WebSocket("ws://localhost:9090/");
|
const socket = new WebSocket("ws://localhost:9090/");
|
||||||
|
let isServerReady = false;
|
||||||
socket.onopen = function(e) {
|
socket.onopen = function(e) {
|
||||||
socket.send("handshake");
|
socket.send("handshake");
|
||||||
};
|
};
|
||||||
|
|
||||||
socket.onmessage = async (event) => {
|
socket.onmessage = async (event) => {
|
||||||
// console.log(event.data);
|
console.log(event.data);
|
||||||
|
if (isServerReady === false){
|
||||||
|
isServerReady = true;
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
res = await sendMessageToTab(option.currentTabId, {
|
res = await sendMessageToTab(option.currentTabId, {
|
||||||
type: "transcript",
|
type: "transcript",
|
||||||
data: event.data,
|
data: event.data,
|
||||||
});
|
});
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
const audioDataCache = [];
|
const audioDataCache = [];
|
||||||
const context = new AudioContext();
|
const context = new AudioContext();
|
||||||
const mediaStream = context.createMediaStreamSource(stream);
|
const mediaStream = context.createMediaStreamSource(stream);
|
||||||
const recorder = context.createScriptProcessor(4096, 1, 1);
|
const recorder = context.createScriptProcessor(4096, 1, 1);
|
||||||
|
|
||||||
recorder.onaudioprocess = async (event) => {
|
recorder.onaudioprocess = async (event) => {
|
||||||
if (!context) return;
|
if (!context || !isServerReady) return;
|
||||||
|
|
||||||
const inputData = event.inputBuffer.getChannelData(0);
|
const inputData = event.inputBuffer.getChannelData(0);
|
||||||
const audioData16kHz = resampleTo16kHZ(inputData, context.sampleRate);
|
const audioData16kHz = resampleTo16kHZ(inputData, context.sampleRate);
|
||||||
@@ -114,6 +121,7 @@ async function startRecord(option) {
|
|||||||
mediaStream.connect(recorder);
|
mediaStream.connect(recorder);
|
||||||
recorder.connect(context.destination);
|
recorder.connect(context.destination);
|
||||||
mediaStream.connect(context.destination);
|
mediaStream.connect(context.destination);
|
||||||
|
// }
|
||||||
} else {
|
} else {
|
||||||
window.close();
|
window.close();
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -17,11 +17,15 @@ FORMAT = pyaudio.paInt16
|
|||||||
CHANNELS = 1
|
CHANNELS = 1
|
||||||
RATE = 16000
|
RATE = 16000
|
||||||
RECORD_SECONDS = 60000
|
RECORD_SECONDS = 60000
|
||||||
|
START_RECORDING = False
|
||||||
|
|
||||||
|
|
||||||
def on_message(ws, message):
|
def on_message(ws, message):
|
||||||
|
global START_RECORDING
|
||||||
message = json.loads(message)
|
message = json.loads(message)
|
||||||
|
if message == "SERVER_READY":
|
||||||
|
START_RECORDING = True
|
||||||
|
return
|
||||||
text = []
|
text = []
|
||||||
if len(message):
|
if len(message):
|
||||||
for seg in message:
|
for seg in message:
|
||||||
@@ -35,6 +39,9 @@ def on_message(ws, message):
|
|||||||
wrapper = textwrap.TextWrapper(width=60)
|
wrapper = textwrap.TextWrapper(width=60)
|
||||||
word_list = wrapper.wrap(text="".join(text))
|
word_list = wrapper.wrap(text="".join(text))
|
||||||
# Print each line.
|
# Print each line.
|
||||||
|
if os.name=='nt':
|
||||||
|
os.system('cls')
|
||||||
|
else:
|
||||||
os.system('clear')
|
os.system('clear')
|
||||||
for element in word_list:
|
for element in word_list:
|
||||||
print(element)
|
print(element)
|
||||||
@@ -222,6 +229,15 @@ if __name__=="__main__":
|
|||||||
opt = parser.parse_args()
|
opt = parser.parse_args()
|
||||||
c = Client(host=opt.host, port=opt.port)
|
c = Client(host=opt.host, port=opt.port)
|
||||||
|
|
||||||
|
# while loop to wait for server to be ready
|
||||||
|
print("Waiting for server ready ...")
|
||||||
|
while not START_RECORDING:
|
||||||
|
pass
|
||||||
|
if os.name=='nt':
|
||||||
|
os.system('cls')
|
||||||
|
else:
|
||||||
|
os.system('clear')
|
||||||
|
|
||||||
if opt.audio is not None:
|
if opt.audio is not None:
|
||||||
resampled_file = resample(opt.audio)
|
resampled_file = resample(opt.audio)
|
||||||
c.play_file(resampled_file)
|
c.play_file(resampled_file)
|
||||||
|
|||||||
@@ -1,5 +1,5 @@
|
|||||||
PyAudio
|
PyAudio
|
||||||
faster-whisper
|
faster-whisper==0.6.0
|
||||||
paho-mqtt
|
paho-mqtt
|
||||||
--extra-index-url https://download.pytorch.org/whl/cu113
|
--extra-index-url https://download.pytorch.org/whl/cu113
|
||||||
torch==1.12.1
|
torch==1.12.1
|
||||||
|
|||||||
@@ -19,6 +19,7 @@ from transcriber import WhisperModel
|
|||||||
|
|
||||||
|
|
||||||
clients = {}
|
clients = {}
|
||||||
|
SERVER_READY = "SERVER_READY"
|
||||||
|
|
||||||
def recv_audio(websocket):
|
def recv_audio(websocket):
|
||||||
"""
|
"""
|
||||||
@@ -49,7 +50,12 @@ class ServeClient:
|
|||||||
self.payload_size = struct.calcsize("Q")
|
self.payload_size = struct.calcsize("Q")
|
||||||
self.data = b""
|
self.data = b""
|
||||||
self.frames = b""
|
self.frames = b""
|
||||||
self.transcriber = WhisperModel("small.en", compute_type="float16", local_files_only=False)
|
self.transcriber = WhisperModel(
|
||||||
|
"small.en",
|
||||||
|
device="cuda",
|
||||||
|
compute_type="float16",
|
||||||
|
local_files_only=False
|
||||||
|
)
|
||||||
|
|
||||||
# voice activity detection model
|
# voice activity detection model
|
||||||
self.vad_model, _ = torch.hub.load(repo_or_dir='snakers4/silero-vad',
|
self.vad_model, _ = torch.hub.load(repo_or_dir='snakers4/silero-vad',
|
||||||
@@ -84,6 +90,7 @@ class ServeClient:
|
|||||||
self.websocket = websocket
|
self.websocket = websocket
|
||||||
self.trans_thread = threading.Thread(target=self.speech_to_text)
|
self.trans_thread = threading.Thread(target=self.speech_to_text)
|
||||||
self.trans_thread.start()
|
self.trans_thread.start()
|
||||||
|
self.websocket.send(json.dumps(SERVER_READY))
|
||||||
|
|
||||||
def fill_output(self, output):
|
def fill_output(self, output):
|
||||||
"""
|
"""
|
||||||
@@ -269,7 +276,6 @@ class ServeClient:
|
|||||||
self.transcriber.destroy()
|
self.transcriber.destroy()
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
with serve(recv_audio, "127.0.0.1", 9090) as server:
|
with serve(recv_audio, "127.0.0.1", 9090) as server:
|
||||||
server.serve_forever()
|
server.serve_forever()
|
||||||
Reference in New Issue
Block a user