add word filter

This commit is contained in:
misygauziya
2023-07-06 07:05:43 +09:00
parent cac3b2189a
commit 401bc7f504
2 changed files with 46 additions and 0 deletions

18
VRCT.py
View File

@@ -4,6 +4,7 @@ import queue
import tkinter as tk
import customtkinter
from PIL import Image
from flashtext import KeywordProcessor
import utils
import osc_tools
@@ -21,9 +22,11 @@ class App(customtkinter.CTk):
# init instance
self.translator = translation.Translator()
self.keyword_processor = KeywordProcessor()
# init config
self.PATH_CONFIG = "./config.json"
## main window
self.ENABLE_TRANSLATION = False
self.ENABLE_TRANSCRIPTION_SEND = False
@@ -48,6 +51,7 @@ class App(customtkinter.CTk):
self.INPUT_MIC_RECORD_TIMEOUT = 3
self.INPUT_MIC_PHRASE_TIMEOUT = 3
self.INPUT_MIC_MAX_PHRASES = 10
self.INPUT_MIC_WORD_FILTER = []
## Transcription Receive
self.CHOICE_SPEAKER_DEVICE = audio_utils.get_default_output_device()["name"]
self.INPUT_SPEAKER_VOICE_LANGUAGE = list(languages.transcription_lang.keys())[1]
@@ -139,6 +143,9 @@ class App(customtkinter.CTk):
if "INPUT_MIC_MAX_PHRASES" in config.keys():
if type(config["INPUT_MIC_MAX_PHRASES"]) is int:
self.INPUT_MIC_MAX_PHRASES = config["INPUT_MIC_MAX_PHRASES"]
if "INPUT_MIC_WORD_FILTER" in config.keys():
if type(config["INPUT_MIC_WORD_FILTER"]) is list:
self.INPUT_MIC_WORD_FILTER = config["INPUT_MIC_WORD_FILTER"]
if "CHOICE_SPEAKER_DEVICE" in config.keys():
if config["CHOICE_SPEAKER_DEVICE"] in [device["name"] for device in audio_utils.get_output_device_list()]:
@@ -201,6 +208,7 @@ class App(customtkinter.CTk):
"INPUT_MIC_RECORD_TIMEOUT": self.INPUT_MIC_RECORD_TIMEOUT,
"INPUT_MIC_PHRASE_TIMEOUT": self.INPUT_MIC_PHRASE_TIMEOUT,
"INPUT_MIC_MAX_PHRASES": self.INPUT_MIC_MAX_PHRASES,
"INPUT_MIC_WORD_FILTER": self.INPUT_MIC_WORD_FILTER,
"CHOICE_SPEAKER_DEVICE": self.CHOICE_SPEAKER_DEVICE,
"INPUT_SPEAKER_VOICE_LANGUAGE": self.INPUT_SPEAKER_VOICE_LANGUAGE,
"INPUT_SPEAKER_ENERGY_THRESHOLD": self.INPUT_SPEAKER_ENERGY_THRESHOLD,
@@ -392,6 +400,10 @@ class App(customtkinter.CTk):
else:
self.checkbox_foreground.deselect()
## set word filter
for f in self.INPUT_MIC_WORD_FILTER:
self.keyword_processor.add_keyword(f)
## set bind entry message box
self.entry_message_box.bind("<Return>", self.entry_message_box_press_key_enter)
self.entry_message_box.bind("<Any-KeyPress>", self.entry_message_box_press_key_any)
@@ -459,6 +471,12 @@ class App(customtkinter.CTk):
self.mic_transcriber.transcribe_audio_queue(self.mic_audio_queue)
message = self.mic_transcriber.get_transcript()
if len(message) > 0:
# word filter
if len(self.keyword_processor.extract_keywords(message)) != 0:
utils.print_textbox(self.textbox_message_log, f"Detect WordFilter :{message}", "INFO")
utils.print_textbox(self.textbox_message_system_log, f"Detect WordFilter :{message}", "INFO")
return
# translate
if self.checkbox_translation.get() is False:
voice_message = f"{message}"

View File

@@ -1,6 +1,8 @@
import os
import tkinter as tk
import customtkinter
from flashtext import KeywordProcessor
import utils
import audio_utils
import languages
@@ -346,6 +348,23 @@ class ToplevelWindowConfig(customtkinter.CTkToplevel):
self.entry_input_mic_max_phrases.grid(row=row, column=1, columnspan=1 ,padx=padx, pady=pady, sticky="nsew")
self.entry_input_mic_max_phrases.bind("<Any-KeyRelease>", self.entry_input_mic_max_phrases_callback)
## entry input mic word filter
row +=1
self.label_input_mic_word_filter = customtkinter.CTkLabel(
self.tabview_config.tab("Transcription"),
text="Input Mic Word Filter:",
fg_color="transparent",
font=customtkinter.CTkFont(family=self.parent.FONT_FAMILY)
)
self.label_input_mic_word_filter.grid(row=row, column=0, columnspan=1, padx=padx, pady=pady, sticky="nsw")
self.entry_input_mic_word_filter = customtkinter.CTkEntry(
self.tabview_config.tab("Transcription"),
textvariable=customtkinter.StringVar(value=",".join(self.parent.INPUT_MIC_WORD_FILTER)),
font=customtkinter.CTkFont(family=self.parent.FONT_FAMILY)
)
self.entry_input_mic_word_filter.grid(row=row, column=1, columnspan=1 ,padx=padx, pady=pady, sticky="nsew")
self.entry_input_mic_word_filter.bind("<Any-KeyRelease>", self.entry_input_mic_word_filters_callback)
## optionmenu input speaker device
row +=1
self.label_input_speaker_device = customtkinter.CTkLabel(
@@ -613,6 +632,8 @@ class ToplevelWindowConfig(customtkinter.CTkToplevel):
self.entry_input_mic_phrase_timeout.configure(font=customtkinter.CTkFont(family=choice))
self.label_input_mic_max_phrases.configure(font=customtkinter.CTkFont(family=choice))
self.entry_input_mic_max_phrases.configure(font=customtkinter.CTkFont(family=choice))
self.label_input_mic_word_filter.configure(font=customtkinter.CTkFont(family=choice))
self.entry_input_mic_word_filter.configure(font=customtkinter.CTkFont(family=choice))
self.label_input_speaker_device.configure(font=customtkinter.CTkFont(family=choice))
self.optionmenu_input_speaker_device.configure(font=customtkinter.CTkFont(family=choice))
self.optionmenu_input_speaker_device._dropdown_menu.configure(font=customtkinter.CTkFont(family=choice))
@@ -734,6 +755,13 @@ class ToplevelWindowConfig(customtkinter.CTkToplevel):
self.parent.INPUT_MIC_MAX_PHRASES = int(self.entry_input_mic_max_phrases.get())
utils.save_json(self.parent.PATH_CONFIG, "INPUT_MIC_MAX_PHRASES", self.parent.INPUT_MIC_MAX_PHRASES)
def entry_input_mic_word_filters_callback(self, event):
self.parent.INPUT_MIC_WORD_FILTER = self.entry_input_mic_word_filter.get().split(",")
self.parent.keyword_processor = KeywordProcessor()
for f in self.parent.INPUT_MIC_WORD_FILTER:
self.parent.keyword_processor.add_keyword(f)
utils.save_json(self.parent.PATH_CONFIG, "INPUT_MIC_WORD_FILTER", self.parent.INPUT_MIC_WORD_FILTER)
def optionmenu_input_speaker_device_callback(self, choice):
self.parent.CHOICE_SPEAKER_DEVICE = choice
utils.save_json(self.parent.PATH_CONFIG, "CHOICE_SPEAKER_DEVICE", self.parent.CHOICE_SPEAKER_DEVICE)