Fedir Zadniprovskyi commited on
Commit
93d8861
·
1 Parent(s): 49f71ac

chore: minor changes to scripts/client.py

Browse files
Files changed (2) hide show
  1. pyproject.toml +1 -0
  2. scripts/client.py +26 -22
pyproject.toml CHANGED
@@ -70,6 +70,7 @@ ignore = [
70
  "W505",
71
  "ISC001", # recommended to disable for formatting
72
  "INP001",
 
73
  ]
74
 
75
  [tool.ruff.lint.isort]
 
70
  "W505",
71
  "ISC001", # recommended to disable for formatting
72
  "INP001",
73
+ "PT018",
74
  ]
75
 
76
  [tool.ruff.lint.isort]
scripts/client.py CHANGED
@@ -2,6 +2,7 @@ import os
2
  from pathlib import Path
3
  import subprocess
4
  import threading
 
5
 
6
  import httpx
7
  import keyboard
@@ -14,11 +15,12 @@ import keyboard
14
  # The audio file will be sent to the server for transcription.
15
  # The transcription will be copied to the clipboard.
16
  # When having a short audio of a couple of sentences and running inference on a GPU the response time is very fast (less than 2 seconds). # noqa: E501
 
17
 
18
  CHUNK = 2**12
19
  AUDIO_RECORD_CMD = [
20
  "ffmpeg",
21
- # "-hide_banner",
22
  # "-loglevel",
23
  # "quiet",
24
  "-f",
@@ -27,15 +29,6 @@ AUDIO_RECORD_CMD = [
27
  "default",
28
  "-f",
29
  "wav",
30
- # "-ac",
31
- # "1",
32
- # "-ar",
33
- # "16000",
34
- # "-f",
35
- # "s16le",
36
- # "-acodec",
37
- # "pcm_s16le",
38
- # "-",
39
  ]
40
  COPY_TO_CLIPBOARD_CMD = "wl-copy"
41
  OPENAI_BASE_URL = "ws://localhost:8000/v1"
@@ -48,12 +41,13 @@ RESPONSE_FORMAT = "text"
48
 
49
  client = httpx.Client(base_url=OPENAI_BASE_URL, timeout=TIMEOUT)
50
  is_running = threading.Event()
51
- file = Path("test.wav") # TODO: use tempfile
 
52
 
53
 
54
  while True:
55
  keyboard.wait(KEYBIND)
56
- print("Action started")
57
  process = subprocess.Popen(
58
  [*AUDIO_RECORD_CMD, "-y", str(file.name)],
59
  stdout=subprocess.PIPE,
@@ -63,17 +57,27 @@ while True:
63
  )
64
  keyboard.wait(KEYBIND)
65
  process.kill()
66
- print("Action finished")
 
 
 
 
67
 
68
- with open(file, "rb") as f:
69
- res = client.post(
70
- OPENAI_BASE_URL + TRANSCRIBE_PATH,
71
- files={"file": f},
72
- data={
73
- "response_format": RESPONSE_FORMAT,
74
- "language": LANGUAGE,
75
- },
76
- )
 
 
 
 
77
  transcription = res.text
78
  print(transcription)
79
  subprocess.run([COPY_TO_CLIPBOARD_CMD], input=transcription.encode(), check=True)
 
 
 
2
  from pathlib import Path
3
  import subprocess
4
  import threading
5
+ import time
6
 
7
  import httpx
8
  import keyboard
 
15
  # The audio file will be sent to the server for transcription.
16
  # The transcription will be copied to the clipboard.
17
  # When having a short audio of a couple of sentences and running inference on a GPU the response time is very fast (less than 2 seconds). # noqa: E501
18
+ # Run this with `sudo -E python scripts/client.py`
19
 
20
  CHUNK = 2**12
21
  AUDIO_RECORD_CMD = [
22
  "ffmpeg",
23
+ "-hide_banner",
24
  # "-loglevel",
25
  # "quiet",
26
  "-f",
 
29
  "default",
30
  "-f",
31
  "wav",
 
 
 
 
 
 
 
 
 
32
  ]
33
  COPY_TO_CLIPBOARD_CMD = "wl-copy"
34
  OPENAI_BASE_URL = "ws://localhost:8000/v1"
 
41
 
42
  client = httpx.Client(base_url=OPENAI_BASE_URL, timeout=TIMEOUT)
43
  is_running = threading.Event()
44
+
45
+ file = Path("test.wav") # HACK: I had a hard time trying to use a temporary file due to permissions issues
46
 
47
 
48
  while True:
49
  keyboard.wait(KEYBIND)
50
+ print("Recording started")
51
  process = subprocess.Popen(
52
  [*AUDIO_RECORD_CMD, "-y", str(file.name)],
53
  stdout=subprocess.PIPE,
 
57
  )
58
  keyboard.wait(KEYBIND)
59
  process.kill()
60
+ stdout, stderr = process.communicate()
61
+ if stdout or stderr:
62
+ print(f"stdout: {stdout}")
63
+ print(f"stderr: {stderr}")
64
+ print(f"Recording finished. File size: {file.stat().st_size} bytes")
65
 
66
+ try:
67
+ with open(file, "rb") as fd:
68
+ start = time.perf_counter()
69
+ res = client.post(
70
+ OPENAI_BASE_URL + TRANSCRIBE_PATH,
71
+ files={"file": fd},
72
+ data={
73
+ "response_format": RESPONSE_FORMAT,
74
+ "language": LANGUAGE,
75
+ },
76
+ )
77
+ end = time.perf_counter()
78
+ print(f"Transcription took {end - start} seconds")
79
  transcription = res.text
80
  print(transcription)
81
  subprocess.run([COPY_TO_CLIPBOARD_CMD], input=transcription.encode(), check=True)
82
+ except httpx.ConnectError as e:
83
+ print(f"Couldn't connect to server: {e}")