Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Continuous mode not working #148

Closed
wants to merge 40 commits into from
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
40 commits
Select commit Hold shift + click to select a range
9fde685
Update README.md for Transcribe
vivekuppal Jun 29, 2023
1483fea
Merge pull request #1 from vivekuppal/vu-readme-updates
vivekuppal Jun 29, 2023
391d728
Allow usage without a valid OPEN API key. (#2)
vivekuppal Jun 29, 2023
ab4245d
Update README.md (#3)
vivekuppal Jun 29, 2023
ebb6f2f
Allow user to choose model. Add arguments to main file.
vivekuppal Jun 29, 2023
8f5a595
Code clean up, add linting. (#4)
vivekuppal Jun 30, 2023
59d5c91
UI Text Chronology (#5)
vivekuppal Jun 30, 2023
f772bb8
Update readme with Enhancements. Allow copy of text from UI window. R…
vivekuppal Jun 30, 2023
87a38b1
Save conversation to text. (#9)
vivekuppal Jun 30, 2023
65d6dcf
Add Contextual Information to Responses (#11)
vivekuppal Jun 30, 2023
d1b3c45
Allow users to pause audio transcription. Change the default for gett…
vivekuppal Jul 3, 2023
cfca51a
Update main.py (#15)
abhinavuppal1 Jul 11, 2023
152bad3
Code reorg to separate UI code (#16)
vivekuppal Jul 12, 2023
addf17f
Add support for multiple languages (#18)
vivekuppal Jul 12, 2023
e5cda88
Easy install for non developers on windows (#20)
vivekuppal Jul 18, 2023
9896c1c
Disabled winrar UI (#22)
Adarsha-gg Jul 18, 2023
901501b
When using API, we do not need to specify language, absorb the lang p…
vivekuppal Jul 18, 2023
bd48b61
Language combo fix (#26)
Adarsha-gg Jul 19, 2023
7c9ca88
Added gdrive (#27)
Adarsha-gg Jul 19, 2023
2429c97
Allow usage of API Key in installed version of Transcribe (#28)
vivekuppal Jul 19, 2023
12ef846
updated the drive link (#30)
Adarsha-gg Jul 20, 2023
4be26c7
Add a duration class to easily measure the time taken for an operatio…
vivekuppal Jul 21, 2023
6e53b31
--api option was not working correctly (#34)
vivekuppal Jul 21, 2023
bd42b8c
Initial unit tests for the speech recognition library (#36)
vivekuppal Jul 24, 2023
af87eff
user reported defect fixes. (#39)
vivekuppal Jul 26, 2023
26cfaad
Optimize LLM usage (#40)
vivekuppal Jul 26, 2023
f8d5857
Bug fixes for exceptions observed during usage. Add further plumbing …
vivekuppal Jul 27, 2023
1356a78
Add logging infrastructure (#42)
vivekuppal Jul 27, 2023
a1cc48b
Get Response from LLM on demand (#44)
vivekuppal Jul 28, 2023
ea5f392
Models from open ai site (#43)
Adarsha-gg Jul 28, 2023
b4e03a4
List all active devices (#45)
vivekuppal Aug 1, 2023
85d09ed
Allow user to select input, output audio devices (#48)
vivekuppal Aug 21, 2023
28d1e9a
Disable mic speaker selectively (#49)
vivekuppal Aug 23, 2023
e48bdb8
Add Audio Response for LLM generated content (#50)
vivekuppal Aug 27, 2023
6baa77f
Update, upload latest binaries (#54)
Adarsha-gg Aug 30, 2023
fa55416
Multiturn prompts, bug fixes (#55)
vivekuppal Sep 5, 2023
ce5a1e1
Allow enable/disable speaker and microphone from UI (#56)
Adarsha-gg Sep 6, 2023
e445856
Update gdrive link (#58)
Adarsha-gg Sep 7, 2023
b50f58c
Bring readme up to date with current functionality. Describe content …
vivekuppal Sep 8, 2023
a7ea2cc
Continuous mode broke after updates to the UI.
vivekuppal Sep 8, 2023
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
Prev Previous commit
Next Next commit
Optimize LLM usage (#40)
* Do not ping LLM if we do not need to. Previously we were always pinging LLM even when response suggestions were off. Partial refactoring work towards separating the conversation its own object.

* Make openai model configurable using parameters.yaml so it is easy to change for end user.
  • Loading branch information
vivekuppal committed Jul 26, 2023
commit 26cfaad40581332247375f00ebc9e792f0194db4
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,5 @@ __pycache__/
*.wav
.venv/
venv
output
output
tiny.pt
14 changes: 11 additions & 3 deletions AudioTranscriber.py
Original file line number Diff line number Diff line change
@@ -1,19 +1,20 @@
import os
import queue
import threading
import io
from datetime import timedelta
import wave
import tempfile
import whisper
import custom_speech_recognition as sr
import pyaudiowpatch as pyaudio
from heapq import merge
import conversation

PHRASE_TIMEOUT = 3.05


class AudioTranscriber:
def __init__(self, mic_source, speaker_source, model):
def __init__(self, mic_source, speaker_source, model, convo: conversation.Conversation):
self.transcript_data = {"You": [], "Speaker": []}
self.transcript_changed_event = threading.Event()
self.audio_model = model
Expand All @@ -38,8 +39,9 @@ def __init__(self, mic_source, speaker_source, model):
"process_data_func": self.process_speaker_data
}
}
self.conversation = convo

def transcribe_audio_queue(self, audio_queue):
def transcribe_audio_queue(self, audio_queue: queue.Queue):
"""Transcribe data from audio sources. In this case we have 2 sources, microphone, speaker.
Args:
audio_queue: queue object with reference to audio files
Expand Down Expand Up @@ -109,9 +111,15 @@ def update_transcript(self, who_spoke, text, time_spoken):

if source_info["new_phrase"] or len(transcript) == 0:
transcript.append((f"{who_spoke}: [{text}]\n\n", time_spoken))
self.conversation.update_conversation(persona=who_spoke,
time_spoken=time_spoken,
text=text)
else:
transcript.pop()
transcript.append((f"{who_spoke}: [{text}]\n\n", time_spoken))
self.conversation.update_conversation(persona=who_spoke,
time_spoken=time_spoken,
text=text, pop=True)

def get_transcript(self, length: int = 0):
"""Get the audio transcript
Expand Down
31 changes: 25 additions & 6 deletions GPTResponder.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,23 +2,29 @@
import GlobalVars
from prompts import create_prompt, INITIAL_RESPONSE
import time

import conversation
import constants
import configuration

# Number of phrases to use for generating a response
MAX_PHRASES = 10
MAX_PHRASES = 20


class GPTResponder:
def __init__(self):
def __init__(self, convo: conversation.Conversation):
self.response = INITIAL_RESPONSE
self.response_interval = 2
openai.api_key = GlobalVars.TranscriptionGlobals().api_key
self.gl_vars = GlobalVars.TranscriptionGlobals()
openai.api_key = self.gl_vars.api_key
self.conversation = convo
self.config = configuration.Config().get_data()
self.model = self.config['OpenAI']['ai_model']

def generate_response_from_transcript(self, transcript):
def generate_response_from_transcript_no_check(self, transcript):
try:
prompt_content = create_prompt(transcript)
response = openai.ChatCompletion.create(
model="gpt-3.5-turbo-0301",
model=self.model,
messages=[{"role": "system", "content": prompt_content}],
temperature=0.0
)
Expand All @@ -31,8 +37,18 @@ def generate_response_from_transcript(self, transcript):
except:
return ''

def generate_response_from_transcript(self, transcript):
"""Ping OpenAI LLM model to get response from the Assistant
"""

if self.gl_vars.freeze_state[0]:
return ''

return generate_response_from_transcript_no_check(self, transcript)

def respond_to_transcriber(self, transcriber):
while True:

if transcriber.transcript_changed_event.is_set():
start_time = time.time()

Expand All @@ -45,6 +61,9 @@ def respond_to_transcriber(self, transcriber):

if response != '':
self.response = response
self.conversation.update_conversation(persona=constants.PERSONA_ASSISTANT,
text=response,
time_spoken=end_time)

remaining_time = self.response_interval - execution_time
if remaining_time > 0:
Expand Down
7 changes: 7 additions & 0 deletions constants.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
"""Globally used constants
"""

PERSONA_YOU = "You"
PERSONA_ASSISTANT = "Assistant"
PERSONA_SYSTEM = "System"
PERSONA_SPEAKER = "Speaker"
60 changes: 60 additions & 0 deletions conversation.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
from heapq import merge
import constants
import configuration


class Conversation:
"""Encapsulates the complete conversation.
Has text from Speakers, Microphone, LLM, Instructions to LLM
"""

def __init__(self):
self.transcript_data = {constants.PERSONA_SYSTEM: [],
constants.PERSONA_YOU: [],
constants.PERSONA_SPEAKER: [],
constants.PERSONA_ASSISTANT: []}
config = configuration.Config().get_data()

def clear_conversation_data(self):
"""Clear all conversation data
"""
self.transcript_data[constants.PERSONA_YOU].clear()
self.transcript_data[constants.PERSONA_SPEAKER].clear()
self.transcript_data[constants.PERSONA_SYSTEM].clear()
self.transcript_data[constants.PERSONA_ASSISTANT].clear()

def update_conversation(self, persona: str, text: str, time_spoken, pop: bool = False):
"""Update conversation with new data
Args:
person: person this part of conversation is attributed to
text: Actual words
time_spoken: Time at which conversation happened, this is typically reported in local time
"""
transcript = self.transcript_data[persona]
if pop:
transcript.pop()
transcript.append((f"{persona}: [{text}]\n\n", time_spoken))

def get_conversation(self,
sources: list = None,
length: int = 0):
"""Get the complete transcript
Args:
sources: Get data from which sources (You, Speaker, Assistant, System)
length: Get the last length elements from the audio transcript.
Default value = 0, gives the complete transcript
"""
if sources is None:
sources = [constants.PERSONA_YOU,
constants.PERSONA_SPEAKER,
constants.PERSONA_ASSISTANT,
constants.PERSONA_SYSTEM]

combined_transcript = list(merge(
self.transcript_data[constants.PERSONA_YOU][-length:],
self.transcript_data[constants.PERSONA_SPEAKER][-length:],
self.transcript_data[constants.PERSONA_ASSISTANT][-length:],
self.transcript_data[constants.PERSONA_SYSTEM][-length:],
key=lambda x: x[1]))
combined_transcript = combined_transcript[-length:]
return "".join([t[0] for t in combined_transcript])
12 changes: 7 additions & 5 deletions main.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
from language import LANGUAGES_DICT
import GlobalVars
import configuration

import conversation

def main():
# Set up all arguments
Expand Down Expand Up @@ -95,16 +95,20 @@ def main():
time.sleep(2)

global_vars.speaker_audio_recorder.record_into_queue(global_vars.audio_queue)
global_vars.freeze_state = [True]
convo = conversation.Conversation()

# Transcribe and Respond threads, both work on the same instance of the AudioTranscriber class
global_vars.transcriber = AudioTranscriber(global_vars.user_audio_recorder.source,
global_vars.speaker_audio_recorder.source, model)
global_vars.speaker_audio_recorder.source,
model,
convo=convo)
transcribe_thread = threading.Thread(target=global_vars.transcriber.transcribe_audio_queue,
args=(global_vars.audio_queue,))
transcribe_thread.daemon = True
transcribe_thread.start()

global_vars.responder = GPTResponder()
global_vars.responder = GPTResponder(convo=convo)

respond_thread = threading.Thread(target=global_vars.responder.respond_to_transcriber,
args=(global_vars.transcriber,))
Expand All @@ -120,8 +124,6 @@ def main():
root.grid_columnconfigure(0, weight=2)
root.grid_columnconfigure(1, weight=1)

global_vars.freeze_state = [True]

ui_cb = ui.ui_callbacks()
global_vars.freeze_button.configure(command=ui_cb.freeze_unfreeze)
label_text = f'Update Response interval: {update_interval_slider.get()} seconds'
Expand Down
9 changes: 9 additions & 0 deletions parameters.yaml
Original file line number Diff line number Diff line change
@@ -1,2 +1,11 @@
OpenAI:
api_key: 'API_KEY'

# Possible model values
# gpt-3.5-turbo, gpt-3.5-turbo-16k, gpt-3.5-turbo-0613, gpt-3.5-turbo-16k-0613
# gpt-4, gpt-4-0613, gpt-4-32k, gpt-4-32k-0613
# Legacy models
# text-davinci-003, text-davinci-002, code-davinci-002
# See this link for available models
# https://platform.openai.com/docs/models/continuous-model-upgrades
ai_model: gpt-3.5-turbo-0301