ecoute/main.py

128 lines
5.0 KiB
Python
Raw Normal View History

2023-05-08 02:10:48 +00:00
import threading
2023-05-09 04:10:55 +00:00
from AudioTranscriber import AudioTranscriber
2023-05-09 23:06:49 +00:00
from GPTResponder import GPTResponder
2023-05-08 02:10:48 +00:00
import customtkinter as ctk
2023-05-11 00:52:52 +00:00
import AudioRecorder
2023-05-09 04:10:55 +00:00
import queue
2023-05-11 01:34:00 +00:00
import time
2023-05-14 16:00:47 +00:00
import torch
2023-05-30 00:34:23 +00:00
import sys
import TranscriberModels
2023-06-03 15:43:50 +00:00
import subprocess
2023-05-08 02:10:48 +00:00
def write_in_textbox(textbox, text):
textbox.delete("0.0", "end")
textbox.insert("0.0", text)
2023-05-09 04:10:55 +00:00
def update_transcript_UI(transcriber, textbox):
transcript_string = transcriber.get_transcript()
2023-05-13 03:01:36 +00:00
write_in_textbox(textbox, transcript_string)
2023-05-09 04:10:55 +00:00
textbox.after(300, update_transcript_UI, transcriber, textbox)
2023-05-08 02:10:48 +00:00
2023-05-13 23:20:26 +00:00
def update_response_UI(responder, textbox, update_interval_slider_label, update_interval_slider, freeze_state):
if not freeze_state[0]:
response = responder.response
2023-05-09 23:06:49 +00:00
2023-05-13 23:20:26 +00:00
textbox.configure(state="normal")
write_in_textbox(textbox, response)
textbox.configure(state="disabled")
2023-05-09 23:06:49 +00:00
2023-05-13 23:20:26 +00:00
update_interval = int(update_interval_slider.get())
responder.update_response_interval(update_interval)
update_interval_slider_label.configure(text=f"Update interval: {update_interval} seconds")
2023-05-09 23:06:49 +00:00
2023-05-13 23:20:26 +00:00
textbox.after(300, update_response_UI, responder, textbox, update_interval_slider_label, update_interval_slider, freeze_state)
2023-05-08 02:10:48 +00:00
2023-05-13 21:20:55 +00:00
def clear_context(transcriber, audio_queue):
2023-05-13 03:01:36 +00:00
transcriber.clear_transcript_data()
2023-05-13 21:20:55 +00:00
with audio_queue.mutex:
audio_queue.queue.clear()
2023-05-09 23:06:49 +00:00
2023-05-13 03:01:36 +00:00
def create_ui_components(root):
2023-05-08 02:10:48 +00:00
ctk.set_appearance_mode("dark")
ctk.set_default_color_theme("dark-blue")
root.title("Ecoute")
root.configure(bg='#252422')
root.geometry("1000x600")
2023-05-13 03:01:36 +00:00
2023-05-08 02:10:48 +00:00
font_size = 20
transcript_textbox = ctk.CTkTextbox(root, width=300, font=("Arial", font_size), text_color='#FFFCF2', wrap="word")
transcript_textbox.grid(row=0, column=0, padx=10, pady=20, sticky="nsew")
response_textbox = ctk.CTkTextbox(root, width=300, font=("Arial", font_size), text_color='#639cdc', wrap="word")
response_textbox.grid(row=0, column=1, padx=10, pady=20, sticky="nsew")
2023-05-13 23:35:57 +00:00
freeze_button = ctk.CTkButton(root, text="Freeze", command=None)
freeze_button.grid(row=1, column=1, padx=10, pady=3, sticky="nsew")
2023-05-08 02:10:48 +00:00
update_interval_slider_label = ctk.CTkLabel(root, text=f"", font=("Arial", 12), text_color="#FFFCF2")
2023-05-13 23:35:57 +00:00
update_interval_slider_label.grid(row=2, column=1, padx=10, pady=3, sticky="nsew")
2023-05-08 02:10:48 +00:00
update_interval_slider = ctk.CTkSlider(root, from_=1, to=10, width=300, height=20, number_of_steps=9)
update_interval_slider.set(2)
update_interval_slider.grid(row=3, column=1, padx=10, pady=10, sticky="nsew")
2023-05-13 03:01:36 +00:00
2023-05-13 23:20:26 +00:00
return transcript_textbox, response_textbox, update_interval_slider, update_interval_slider_label, freeze_button
2023-05-13 03:01:36 +00:00
def main():
2023-06-03 15:43:50 +00:00
try:
subprocess.run(["ffmpeg", "-version"], stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL)
except FileNotFoundError:
print("ERROR: The ffmpeg library is not installed. Please install ffmpeg and try again.")
return
2023-05-13 03:01:36 +00:00
root = ctk.CTk()
2023-05-13 23:20:26 +00:00
transcript_textbox, response_textbox, update_interval_slider, update_interval_slider_label, freeze_button = create_ui_components(root)
2023-05-13 03:01:36 +00:00
2023-05-09 04:10:55 +00:00
audio_queue = queue.Queue()
2023-05-08 02:10:48 +00:00
2023-05-11 00:52:52 +00:00
user_audio_recorder = AudioRecorder.DefaultMicRecorder()
user_audio_recorder.record_into_queue(audio_queue)
2023-05-09 04:10:55 +00:00
2023-05-11 01:34:00 +00:00
time.sleep(2)
speaker_audio_recorder = AudioRecorder.DefaultSpeakerRecorder()
speaker_audio_recorder.record_into_queue(audio_queue)
2023-05-30 00:34:23 +00:00
model = TranscriberModels.get_model('--api' in sys.argv)
transcriber = AudioTranscriber(user_audio_recorder.source, speaker_audio_recorder.source, model)
transcribe = threading.Thread(target=transcriber.transcribe_audio_queue, args=(audio_queue,))
2023-05-23 01:28:16 +00:00
transcribe.daemon = True
2023-05-09 04:10:55 +00:00
transcribe.start()
2023-05-08 02:10:48 +00:00
2023-05-12 02:06:14 +00:00
responder = GPTResponder()
2023-05-30 00:34:23 +00:00
respond = threading.Thread(target=responder.respond_to_transcriber, args=(transcriber,))
2023-05-23 01:28:16 +00:00
respond.daemon = True
2023-05-12 02:06:14 +00:00
respond.start()
print("READY")
2023-05-09 23:06:49 +00:00
2023-05-08 02:10:48 +00:00
root.grid_rowconfigure(0, weight=100)
2023-05-13 23:35:57 +00:00
root.grid_rowconfigure(1, weight=1)
2023-05-08 02:10:48 +00:00
root.grid_rowconfigure(2, weight=1)
root.grid_rowconfigure(3, weight=1)
root.grid_columnconfigure(0, weight=2)
root.grid_columnconfigure(1, weight=1)
2023-05-09 23:33:15 +00:00
# Add the clear transcript button to the UI
2023-05-30 00:34:23 +00:00
clear_transcript_button = ctk.CTkButton(root, text="Clear Transcript", command=lambda: clear_context(transcriber, audio_queue, ))
2023-05-09 23:33:15 +00:00
clear_transcript_button.grid(row=1, column=0, padx=10, pady=3, sticky="nsew")
2023-05-13 23:20:26 +00:00
freeze_state = [False] # Using list to be able to change its content inside inner functions
def freeze_unfreeze():
freeze_state[0] = not freeze_state[0] # Invert the freeze state
freeze_button.configure(text="Unfreeze" if freeze_state[0] else "Freeze")
freeze_button.configure(command=freeze_unfreeze)
2023-05-13 23:35:57 +00:00
update_interval_slider_label.configure(text=f"Update interval: {update_interval_slider.get()} seconds")
2023-05-30 00:34:23 +00:00
update_transcript_UI(transcriber, transcript_textbox)
2023-05-13 23:20:26 +00:00
update_response_UI(responder, response_textbox, update_interval_slider_label, update_interval_slider, freeze_state)
2023-05-09 21:07:51 +00:00
2023-05-13 03:01:36 +00:00
root.mainloop()
if __name__ == "__main__":
main()