This commit is contained in:
Paul Gauthier 2023-08-10 16:53:34 -03:00
parent ec91eb670e
commit fed24aa444
3 changed files with 16 additions and 22 deletions

View file

@ -232,7 +232,6 @@ class InputOutput:
return inp return inp
def user_input(self, inp, log_only=True): def user_input(self, inp, log_only=True):
if not log_only: if not log_only:
style = dict(style=self.user_input_color) if self.user_input_color else dict() style = dict(style=self.user_input_color) if self.user_input_color else dict()
self.console.print(inp, **style) self.console.print(inp, **style)

View file

@ -1,45 +1,41 @@
import sounddevice as sd
import numpy as np
import keyboard
import openai
import io
import tempfile
import queue
import soundfile as sf
import os import os
import queue
import tempfile
import openai
import sounddevice as sd
import soundfile as sf
from .dump import dump # noqa: F401
from .dump import dump
def record_and_transcribe(): def record_and_transcribe():
q = queue.Queue() q = queue.Queue()
def callback(indata, frames, time, status): def callback(indata, frames, time, status):
"""This is called (from a separate thread) for each audio block.""" """This is called (from a separate thread) for each audio block."""
if status:
print(status, file=sys.stderr)
q.put(indata.copy()) q.put(indata.copy())
filename = tempfile.mktemp(prefix="delme_rec_unlimited_", suffix=".wav", dir="")
filename = tempfile.mktemp(prefix='delme_rec_unlimited_', suffix='.wav', dir='')
sample_rate = 16000 # 16kHz sample_rate = 16000 # 16kHz
with sf.SoundFile(filename, mode='x', samplerate=sample_rate, channels=1) as file: with sf.SoundFile(filename, mode="x", samplerate=sample_rate, channels=1) as file:
with sd.InputStream(samplerate=sample_rate, channels=1, callback=callback): with sd.InputStream(samplerate=sample_rate, channels=1, callback=callback):
input('Press ENTER when done speaking...') input("Press ENTER when done speaking...")
while not q.empty(): while not q.empty():
file.write(q.get()) file.write(q.get())
with open(filename, 'rb') as fh: with open(filename, "rb") as fh:
transcript = openai.Audio.transcribe("whisper-1", fh) transcript = openai.Audio.transcribe("whisper-1", fh)
text = transcript['text'] text = transcript["text"]
return text return text
if __name__ == "__main__": if __name__ == "__main__":
api_key = os.getenv('OPENAI_API_KEY') api_key = os.getenv("OPENAI_API_KEY")
if not api_key: if not api_key:
raise ValueError("Please set the OPENAI_API_KEY environment variable.") raise ValueError("Please set the OPENAI_API_KEY environment variable.")
print(record_and_transcribe()) print(record_and_transcribe())

View file

@ -29,7 +29,7 @@ class TestCoder(unittest.TestCase):
io = InputOutput(yes=True) io = InputOutput(yes=True)
# Initialize the Coder object with the mocked IO and mocked repo # Initialize the Coder object with the mocked IO and mocked repo
coder = Coder.create(models.GPT4, None, io, fnames=[str(fname)]) Coder.create(models.GPT4, None, io, fnames=[str(fname)])
self.assertTrue(fname.exists()) self.assertTrue(fname.exists())
@ -40,7 +40,6 @@ class TestCoder(unittest.TestCase):
# Check that the latest commit message is "I added str(fname)" # Check that the latest commit message is "I added str(fname)"
self.assertEqual(commit_message, f"I added {str(fname)}") self.assertEqual(commit_message, f"I added {str(fname)}")
def test_allowed_to_edit(self): def test_allowed_to_edit(self):
with GitTemporaryDirectory(): with GitTemporaryDirectory():
repo = git.Repo(Path.cwd()) repo = git.Repo(Path.cwd())