Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 5 additions & 5 deletions requirements.txt
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
numpy
openai==0.27.8
pynput==1.7.6
python-dotenv==1.0.0
scipy==1.8.0
sounddevice==0.4.6
openai==2.7.2
pynput==1.8.1
python-dotenv==1.2.1
scipy==1.16.3
sounddevice==0.5.3
13 changes: 7 additions & 6 deletions wkey/whisper.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
import os

from dotenv import load_dotenv
import openai
from openai import OpenAI

client = OpenAI(api_key=os.environ["OPENAI_API_KEY"])

load_dotenv()
WHISPER_MODEL = "whisper-1"
openai.api_key = os.environ["OPENAI_API_KEY"]


def apply_whisper(filepath: str, mode: str) -> str:
Expand All @@ -14,13 +15,13 @@ def apply_whisper(filepath: str, mode: str) -> str:
raise ValueError(f"Invalid mode: {mode}")

prompt = "Hello, this is a properly structured message. GPT, ChatGPT."

with open(filepath, "rb") as audio_file:
if mode == "translate":
response = openai.Audio.translate(WHISPER_MODEL, audio_file, prompt=prompt)
response = client.audio.translations.create(model=WHISPER_MODEL, file=audio_file, prompt=prompt)
elif mode == "transcribe":
response = openai.Audio.transcribe(WHISPER_MODEL, audio_file, prompt=prompt)
response = client.audio.transcriptions.create(model=WHISPER_MODEL, file=audio_file, prompt=prompt)

transcript = response["text"]
transcript = response.text
return transcript

12 changes: 6 additions & 6 deletions wkey/wkey.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
def on_press(key):
global recording
global audio_data

if key == RECORD_KEY:
recording = True
audio_data = []
Expand All @@ -39,26 +39,26 @@ def on_press(key):
def on_release(key):
global recording
global audio_data

if key == RECORD_KEY:
recording = False
print("Transcribing...")

try:
audio_data_np = np.concatenate(audio_data, axis=0)
except ValueError as e:
print(e)
return

audio_data_int16 = (audio_data_np * np.iinfo(np.int16).max).astype(np.int16)
wavfile.write('recording.wav', sample_rate, audio_data_int16)

transcript = None
try:
transcript = apply_whisper('recording.wav', 'transcribe')
except openai.error.InvalidRequestError as e:
except openai.BadRequestError as e:
print(e)

if transcript:
processed_transcript = process_transcript(transcript)
print(processed_transcript)
Expand Down