Skip to content

Commit

Permalink
hook up openai to transcripts
Browse files Browse the repository at this point in the history
  • Loading branch information
Jef808 committed Dec 11, 2023
1 parent 869bffb commit 5913070
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 24 deletions.
3 changes: 0 additions & 3 deletions run-mock-speech-reco.sh

This file was deleted.

8 changes: 0 additions & 8 deletions speech-to-text/mock_speech_reco.py

This file was deleted.

51 changes: 38 additions & 13 deletions speech-to-text/speech_reco.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@
import sys
import signal
import wave
import requests
from openai import OpenAI
from contextlib import closing

p = pyaudio.PyAudio()
Expand Down Expand Up @@ -215,7 +217,7 @@ def on_message(ws, msg):

if message_type == 'SessionBegins':
_AAI_SESSION_START_TIME = time.time()
_LOGGER.setup(f"logs/{payload['session_id']}.log")
_LOGGER.setup(f"logs/{payload['session_id']}")

elif message_type == 'FinalTranscript':
FINAL_TRANSCRIPTS.append(payload)
Expand All @@ -227,10 +229,12 @@ def on_message(ws, msg):
########################
# Retrieve credentials #
########################
API_KEY = os.getenv("ASSEMBLYAI_API_KEY")
auth_header = {"Authorization": f"{API_KEY}"}
ASSEMBLYAI_API_KEY = os.getenv("ASSEMBLYAI_API_KEY")
auth_header = {"Authorization": f"{ASSEMBLYAI_API_KEY}"}

if not API_KEY:
openai_client = OpenAI()

if not ASSEMBLYAI_API_KEY:
print("ERROR: Failed to retrieve ASSEMBLYAI_API_KEY env variable", file=sys.stderr)
p.terminate()
sys.exit(1)
Expand All @@ -254,7 +258,6 @@ def on_message(ws, msg):

_PYAUDIO_START_TIME = time.time()


def on_error(ws, *err):
_LOGGER.write(*err)
print(f"Error: {err}", file=sys.stderr)
Expand All @@ -270,7 +273,7 @@ def on_error(ws, *err):
on_close=on_close,
on_open=on_open)
except Exception as e:
print(f"Error while initiating the websocket: {e}")
print(f"Error while initiating the websocket: {e}", file=sys.stderr)
stream.close()
p.terminate()

Expand All @@ -281,12 +284,34 @@ def on_error(ws, *err):
with closing(_LOGGER):
_AAI_SESSION_START_REQUEST_TIME = time.time()
ec = ws.run_forever()
print(' '.join(transcript['text'] for transcript in FINAL_TRANSCRIPTS))
_LOGGER.write("\n**** SUMMARY *****\n"
f"SESSION_START: {_PYAUDIO_START_TIME}\n"
f"_AAI_SESSION_REQUEST: {_AAI_SESSION_START_REQUEST_TIME}\n"
f"_AAI_SESSION_START: {_AAI_SESSION_START_TIME}\n"
f"_AAI_SESSION_END_REQUEST: {_AAI_SESSION_END_REQUEST_TIME}\n"
f"_AAI_SESSION_END: {_AAI_SESSION_END_TIME}\n")
_LOGGER.write("{SUMMARY: "
f"SESSION_START: {_PYAUDIO_START_TIME}, "
f"AAI_SESSION_REQUEST: {_AAI_SESSION_START_REQUEST_TIME}, "
f"AAI_SESSION_START: {_AAI_SESSION_START_TIME}, "
f"AAI_SESSION_END_REQUEST: {_AAI_SESSION_END_REQUEST_TIME}, "
f"AAI_SESSION_END: {_AAI_SESSION_END_TIME}"
"}")

transcript = ' '.join(transcript['text'] for transcript in FINAL_TRANSCRIPTS)
FINAL_TRANSCRIPTS = []

# openai api call
payload = {
"model": "gpt-3.5-turbo",
"messages": [
{"role": "system", "content": "The prompt is generated from an assemblyAI real-time transcript. Preprocess it and use what you get as if it was the original prompt"},
{"role": "user", "content": transcript}
]
}
_LOGGER.write(json.dumps({"transcript": transcript}))

response = openai_client.chat.completions.create(**payload)
_LOGGER.write(json.dumps({"openai_response": response.model_dump_json()}))

py_response = response.model_dump()

content = py_response['choices'][0]['message']['content']

print(content)

sys.exit(ec)

0 comments on commit 5913070

Please sign in to comment.