Browse Source

fix async subprocess

subDesTagesMitExtraKaese 2 years ago
parent
commit
d701bc3049
4 changed files with 17 additions and 17 deletions
  1. 1 3
      Dockerfile
  2. 3 3
      main.py
  3. 12 10
      speech_recognition.py
  4. 1 1
      whisper.cpp

+ 1 - 3
Dockerfile

@@ -8,9 +8,7 @@ RUN apt-get update && apt-get install --no-install-recommends -y \
 
 # Install Whisper.cpp
 ADD whisper.cpp/ /build/
-RUN gcc -pthread -O3 -march=native -c ggml.c && \
-    g++ -pthread -O3 -std=c++11 -c main.cpp && \
-    g++ -pthread -o main ggml.o main.o
+RUN make
 
 # main image
 FROM python:3.9-slim-bullseye

+ 3 - 3
main.py

@@ -18,8 +18,8 @@ creds = botlib.Creds(
 
 config = botlib.Config()
 config.encryption_enabled = True
-config.emoji_verify = True
-config.ignore_unverified_devices = True
+config.emoji_verify = False
+config.ignore_unverified_devices = False
 config.store_path = '/data/crypto_store/'
 bot = botlib.Bot(creds, config)
 
@@ -34,7 +34,7 @@ async def on_audio_message(room, event):
     url = urlparse(event.url)
     response = await bot.async_client.download(server_name=url.netloc, media_id=url.path[1:])
     print(response)
-    result = asr.transcribe(response.body)
+    result = await asr.transcribe(response.body)
 
     await bot.async_client.room_typing(room.machine_name, False)
     if response.filename:

+ 12 - 10
speech_recognition.py

@@ -1,6 +1,6 @@
 import ffmpeg
+import asyncio
 import subprocess
-from itertools import takewhile
 import os
 
 SAMPLE_RATE = 16000
@@ -37,19 +37,21 @@ class ASR():
       subprocess.run(["wget", self.model_url, "-O", self.model_path], check=True)
       print("Done.")
 
-  def transcribe(self, audio: bytes) -> str:
+  async def transcribe(self, audio: bytes) -> str:
     convert_audio(audio)
-    stdout, stderr = subprocess.Popen(
-        ["./main", "-m", self.model_path, "-f", "audio.wav", "--no_timestamps"], 
-        stdout=subprocess.PIPE
-      ).communicate()
+    proc = await asyncio.create_subprocess_exec(
+        "./main", "-m", self.model_path, "-f", "audio.wav", "--no_timestamps", 
+        stdout=asyncio.subprocess.PIPE,
+        stderr=asyncio.subprocess.PIPE
+      )
+    stdout, stderr = await proc.communicate()
 
     os.remove("audio.wav")
 
     if stderr:
       print(stderr.decode())
+      
+    text = stdout.decode()
+    print(text)
 
-    lines = stdout.decode().splitlines()[23:]
-    print('\n'.join(lines))
-    text = takewhile(lambda x: x, lines)
-    return '\n'.join(text)
+    return text

+ 1 - 1
whisper.cpp

@@ -1 +1 @@
-Subproject commit b506e9b5b3ebf8d1ebe8122fdbb75703af2afe55
+Subproject commit b2f1600aa380e3ec5dc0de6194e38f27245c2a6a