From b7e88842a5923d6274f7577fc8be6b3738c8a777 Mon Sep 17 00:00:00 2001 From: hanhvn Date: Sun, 20 Oct 2024 19:59:14 +0700 Subject: [PATCH] use config.ini instead of json file --- config.ini | 4 ++++ flow/configs.py | 18 ++++++++---------- flow/main.py | 5 ++++- 3 files changed, 16 insertions(+), 11 deletions(-) create mode 100644 config.ini diff --git a/config.ini b/config.ini new file mode 100644 index 0000000..7769ca6 --- /dev/null +++ b/config.ini @@ -0,0 +1,4 @@ +[DEFAULT] +model = base.en +threshold = 200 + diff --git a/flow/configs.py b/flow/configs.py index a50ae74..66cd6c5 100644 --- a/flow/configs.py +++ b/flow/configs.py @@ -1,6 +1,7 @@ import json import os import pyaudio +import configparser from enum import Enum @@ -11,6 +12,10 @@ class AppStatus(str, Enum): STOPPED = "App stopped" CLOSING = "Closing app..." +CONFIG_FILE = "./config.ini" + +config = configparser.ConfigParser() +config.read(CONFIG_FILE) # Audio parameters CHUNK = 1024 @@ -19,7 +24,7 @@ class AppStatus(str, Enum): RATE = 44100 # Voice Activity Detection parameters -THRESHOLD = int(os.getenv("THRESHOLD", "200")) # Adjust this value based on your microphone and environment +THRESHOLD = int(config["DEFAULT"]["threshold"]) # Adjust this value based on your microphone and environment SILENCE_LIMIT = 0.5 # Number of seconds of silence to stop the recording IGNORED_WORDS = {"", " "} @@ -27,14 +32,7 @@ class AppStatus(str, Enum): # directory to store the audio files RECORDINGS_DIR = "./recordings/" -CONFIG_FILE = "./config.json" - AVAILABLE_MODELS = ["tiny.en", "base.en", "small.en", "medium.en", "large", "turbo"] -if not os.path.exists(CONFIG_FILE): - with open(CONFIG_FILE, "w+") as f: - json.dump({"model": "tiny.en"}, f) - -with open(CONFIG_FILE, "r") as f: - # default whisper model - CURRENT_MODEL = json.load(f)["model"] +# default whisper model +CURRENT_MODEL = config["DEFAULT"]["model"] diff --git a/flow/main.py b/flow/main.py index 48a9984..b6cab80 100644 --- a/flow/main.py +++ b/flow/main.py @@ -34,6 +34,7 @@ def record_audio(message_queue: Queue): stream = p.open(format=FORMAT, channels=CHANNELS, rate=RATE, input=True, frames_per_buffer=CHUNK) except OSError: message_queue.put("Invalid input device") + return audio_buffer = [] @@ -175,8 +176,10 @@ def reload_model(self): self.model_loading_process = Process(target=reload_model, args=(model_name, self.message_queue)) self.model_loading_process.start() + config["DEFAULT"]["model"] = model_name + with open(CONFIG_FILE, "w") as f: - json.dump({"model": model_name}, f) + config.write(f) def start_processes(self): self.record_process = multiprocessing.Process(target=record_audio, args=(self.message_queue,))