transcriber-prompt / toApi.py
harshp8l's picture
Upload folder using huggingface_hub
9690d29
# USAGE keep using , (to prompt the bot via voice and keep adding on layers to message content for chat completion),
# after successful run through backtick is the exit, early stop after instructions have been fulfilled
import os
import pyaudio
import wave
from pynput import keyboard
import speech_recognition as sr
import openai
openai.api_key = "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"
FORMAT = pyaudio.paInt16
CHANNELS = 1
RATE = 44100
CHUNK = 1024
RECORD_SECONDS = 5
#WAVE_OUTPUT_FILENAME = "2.wav"
frames = []
def update_chat(messages, content):
messages.append({"role": "user", "content": content})
return messages
messages = [
{"role": "system", "content": "You are a super helpful tutor and excellent interviewee. In general you explain your thought process and concepts very well. You first explain simple brute force solutions to interview problem (no need to code) but still go over the time and space complexity, then you explain the steps leading you to the most optimized solution. You explain the concepts and procedures of this optimized solution and then you MUST provide the final code in python with its time and space complexity."}
]
def on_press(key):
#if key == keyboard.Key.esc:
# recording = False
# return False
if key == keyboard.KeyCode.from_char(','):
print("Recording audio...")
# Start recording
global stream, audio
audio = pyaudio.PyAudio()
stream = audio.open(format=FORMAT, channels=CHANNELS,
rate=RATE, input=True,
frames_per_buffer=CHUNK)
for i in range(0, int(RATE / CHUNK * RECORD_SECONDS)):
data = stream.read(CHUNK)
frames.append(data)
# Stop recording
stream.stop_stream()
stream.close()
audio.terminate()
print("Finished recording audio.")
audio_data = sr.AudioData(b''.join(frames), RATE, 2)
print(audio_data)
#r = sr.Recognizer()
#text = r.recognize_google(audio_data)
#print("Finished transcription")
#response = openai.ChatCompletion.create(model="gpt-3.5-turbo", messages=update_chat(messages, text))
os.system('clear')
#print(response['choices'][0]['message']['content'])
elif key == keyboard.KeyCode.from_char('`'):
print("Exiting")
return False
def on_release(key):
if key == keyboard.Key.esc:
return False
# Start the listener
with keyboard.Listener(on_press=on_press, on_release=on_release) as listener:
listener.join()