From d353d463f46651695cc0168fac69283a78e54048 Mon Sep 17 00:00:00 2001 From: JeffreyChen <33644111+JE-Chen@users.noreply.github.com> Date: Fri, 7 Jul 2023 14:47:26 +0800 Subject: [PATCH 1/3] Update stable and dev version Update stable and dev version --- dev.toml | 2 +- frontengine/ui/chat/chatthread.py | 1 - pyproject.toml | 2 +- 3 files changed, 2 insertions(+), 3 deletions(-) diff --git a/dev.toml b/dev.toml index 4316d1b..cef05e4 100644 --- a/dev.toml +++ b/dev.toml @@ -6,7 +6,7 @@ build-backend = "setuptools.build_meta" [project] name = "frontengine_dev" -version = "0.0.26" +version = "0.0.27" authors = [ { name = "JE-Chen", email = "jechenmailman@gmail.com" }, ] diff --git a/frontengine/ui/chat/chatthread.py b/frontengine/ui/chat/chatthread.py index a114a4e..e4bed82 100644 --- a/frontengine/ui/chat/chatthread.py +++ b/frontengine/ui/chat/chatthread.py @@ -57,7 +57,6 @@ async def send_chat_async(): asyncio.run(send_chat_async()) self.current_message = chat_response - print(json.dumps(self.current_message, indent=2)) for text_dict in self.current_message.get("item").get("messages"): if text_dict.get("author") == "bot": response_text: str = text_dict.get("text") diff --git a/pyproject.toml b/pyproject.toml index 3d1c546..85f879c 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -6,7 +6,7 @@ build-backend = "setuptools.build_meta" [project] name = "frontengine" -version = "0.0.25" +version = "0.0.26" authors = [ { name = "JE-Chen", email = "jechenmailman@gmail.com" }, ] From 7c046359d5278be76e3eaa7d169a52f9f33cfd24 Mon Sep 17 00:00:00 2001 From: JeffreyChen <33644111+JE-Chen@users.noreply.github.com> Date: Mon, 10 Jul 2023 16:25:24 +0800 Subject: [PATCH 2/3] Add Speech to Text and requirements.txt Add Speech to Text and requirements.txt --- dev.toml | 4 +- frontengine/ui/chat/chat_scene_input.py | 1 - frontengine/ui/chat/speech_to_text.py | 54 +++++++++++++++++++ .../setting/chat_scene/chat_scene_setting.py | 19 ++++++- frontengine/utils/multi_language/english.py | 3 ++ .../multi_language/traditional_chinese.py | 3 ++ pyproject.toml | 4 +- requirements.txt | 7 +++ 8 files changed, 87 insertions(+), 8 deletions(-) create mode 100644 frontengine/ui/chat/speech_to_text.py create mode 100644 requirements.txt diff --git a/dev.toml b/dev.toml index cef05e4..c402899 100644 --- a/dev.toml +++ b/dev.toml @@ -11,9 +11,7 @@ authors = [ { name = "JE-Chen", email = "jechenmailman@gmail.com" }, ] dependencies = [ - "PySide6", - "qt-material", - "EdgeGPT" + "PySide6", "qt-material", "EdgeGPT", "pyttsx3", "SpeechRecognition", "PyAudio" ] description = "FrontEngine is BingGPT that can use-define front end or only use like screen saver" requires-python = ">=3.8" diff --git a/frontengine/ui/chat/chat_scene_input.py b/frontengine/ui/chat/chat_scene_input.py index 5068e9e..33acc9d 100644 --- a/frontengine/ui/chat/chat_scene_input.py +++ b/frontengine/ui/chat/chat_scene_input.py @@ -1,6 +1,5 @@ import pyttsx3 from PySide6.QtCore import QTimer -from PySide6.QtGui import QScreen from PySide6.QtWidgets import QBoxLayout, QWidget, QPushButton, QHBoxLayout, QTextEdit, QMessageBox from frontengine.show.chat.chat_toast import ChatToast diff --git a/frontengine/ui/chat/speech_to_text.py b/frontengine/ui/chat/speech_to_text.py new file mode 100644 index 0000000..f38e4f1 --- /dev/null +++ b/frontengine/ui/chat/speech_to_text.py @@ -0,0 +1,54 @@ +import sys +import time + +from PySide6.QtWidgets import QWidget, QPushButton, QBoxLayout, QLineEdit +from speech_recognition import Microphone +from speech_recognition import Recognizer +from speech_recognition import RequestError, UnknownValueError +from threading import Thread + +from frontengine.utils.multi_language.language_wrapper import language_wrapper + + +def callback(recognizer: Recognizer, audio): + try: + print(recognizer.recognize_google(audio)) + except (RequestError, UnknownValueError) as error: + print(repr(error), file=sys.stderr) + + +class ChatSpeechToText(QWidget): + + def __init__(self): + super().__init__() + # Recognize + self.recognizer = Recognizer() + try: + self.microphone = Microphone() + except IOError as error: + print(repr(error), file=sys.stderr) + # UI + self.box_layout = QBoxLayout(QBoxLayout.Direction.LeftToRight) + self.voice_text_edit = QLineEdit() + self.start_listen_button = QPushButton( + language_wrapper.language_word_dict.get("chat_recognizer_voice_button")) + self.start_listen_button.clicked.connect(self.start_listener_thread) + self.send_text_button = QPushButton( + language_wrapper.language_word_dict.get("chat_scene_send_chat")) + self.box_layout.addWidget(self.voice_text_edit) + self.box_layout.addWidget(self.start_listen_button) + self.box_layout.addWidget(self.send_text_button) + self.setLayout(self.box_layout) + + def start_listener_thread(self): + listener_thread = Thread(target=self.start_listener) + listener_thread.daemon = True + listener_thread.start() + + def start_listener(self): + with self.microphone as source: + self.recognizer.adjust_for_ambient_noise(source, duration=0.3) + stop_listening = self.recognizer.listen_in_background(self.microphone, callback) + for receive_sound_time in range(50): + time.sleep(0.1) + stop_listening(wait_for_stop=False) diff --git a/frontengine/ui/setting/chat_scene/chat_scene_setting.py b/frontengine/ui/setting/chat_scene/chat_scene_setting.py index 6dabac8..8c1e82f 100644 --- a/frontengine/ui/setting/chat_scene/chat_scene_setting.py +++ b/frontengine/ui/setting/chat_scene/chat_scene_setting.py @@ -7,6 +7,7 @@ from frontengine.ui.chat.chat_model import load_scene_json, chat_model from frontengine.ui.chat.chat_scene_input import ChatInputDialog from frontengine.ui.chat.chatthread import ChatThread, DELEGATE_CHAT +from frontengine.ui.chat.speech_to_text import ChatSpeechToText from frontengine.utils.logging.loggin_instance import front_engine_logger from frontengine.utils.multi_language.language_wrapper import language_wrapper @@ -15,6 +16,7 @@ class ChatSceneUI(QWidget): def __init__(self): super().__init__() + self.voice_input = None self.grid_layout = QGridLayout() self.grid_layout = QGridLayout(self) self.grid_layout.setContentsMargins(0, 0, 0, 0) @@ -79,6 +81,10 @@ def __init__(self): self.local_box = QBoxLayout(QBoxLayout.Direction.LeftToRight) self.local_box.addWidget(self.locale_label) self.local_box.addWidget(self.locale_input) + # Start voice input + self.start_voice_input_button = QPushButton( + language_wrapper.language_word_dict.get("start_chat_voice_input_ui")) + self.start_voice_input_button.clicked.connect(self.start_voice_input) # Add to layout self.grid_layout.addWidget(self.choose_style_combobox, 0, 0) self.grid_layout.addWidget(self.close_delay_label, 0, 1) @@ -88,7 +94,8 @@ def __init__(self): self.grid_layout.addLayout(self.local_box, 0, 5) self.grid_layout.addWidget(self.new_topic_button, 0, 6) self.grid_layout.addWidget(self.scene_input_button, 0, 7) - self.grid_layout.addWidget(self.start_button, 0, 8) + self.grid_layout.addWidget(self.start_voice_input_button, 0, 8) + self.grid_layout.addWidget(self.start_button, 0, 9) self.grid_layout.addWidget(self.chat_panel_scroll_area, 1, 0, -1, -1) self.setLayout(self.grid_layout) @@ -102,6 +109,16 @@ def start_chat(self) -> None: if chat_model.rowCount() > 0: self.start_scene() + def start_voice_input(self): + self.voice_input = ChatSpeechToText() + self.voice_input.show() + self.voice_input.send_text_button.clicked.connect(self.send_voice_chat) + + def send_voice_chat(self): + chat_thread = ChatThread( + self.chat_panel, self.voice_input.voice_text_edit.text(), self.locale_input.text()) + chat_thread.start() + def send_chat(self): chat_thread = ChatThread( self.chat_panel, self.chat_input.chat_input.toPlainText(), self.locale_input.text()) diff --git a/frontengine/utils/multi_language/english.py b/frontengine/utils/multi_language/english.py index 3ddd133..a98fdb0 100644 --- a/frontengine/utils/multi_language/english.py +++ b/frontengine/utils/multi_language/english.py @@ -107,4 +107,7 @@ "chat_scene_precise": "precise", "chat_scene_balanced": "balanced", "chat_gpt_exception": "GPT error, pls retry or new topic", + # Chat voice input + "start_chat_voice_input_ui": "Open voice input ui", + "chat_recognizer_voice_button": "Start listen voice" } diff --git a/frontengine/utils/multi_language/traditional_chinese.py b/frontengine/utils/multi_language/traditional_chinese.py index 80e9e5b..735d5e3 100644 --- a/frontengine/utils/multi_language/traditional_chinese.py +++ b/frontengine/utils/multi_language/traditional_chinese.py @@ -107,4 +107,7 @@ "chat_scene_precise": "精準", "chat_scene_balanced": "平衡", "chat_gpt_exception": "GPT 錯誤,請重試或開始一個新話題", + # Chat voice input + "start_chat_voice_input_ui": "開啟聲音輸入介面", + "chat_recognizer_voice_button": "開始錄製聲音" } diff --git a/pyproject.toml b/pyproject.toml index 85f879c..09ce89b 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -11,9 +11,7 @@ authors = [ { name = "JE-Chen", email = "jechenmailman@gmail.com" }, ] dependencies = [ - "PySide6", - "qt-material", - "EdgeGPT" + "PySide6", "qt-material", "EdgeGPT", "pyttsx3", "SpeechRecognition", "PyAudio" ] description = "FrontEngine is BingGPT that can use-define front end or only use like screen saver" requires-python = ">=3.8" diff --git a/requirements.txt b/requirements.txt new file mode 100644 index 0000000..7c0829f --- /dev/null +++ b/requirements.txt @@ -0,0 +1,7 @@ +PySide6 +qt-material +EdgeGPT +pyttsx3 +SpeechRecognition +frontengine +PyAudio From 4c321bf2bdda6f648689d3c75363128e91f9a023 Mon Sep 17 00:00:00 2001 From: JeffreyChen <33644111+JE-Chen@users.noreply.github.com> Date: Thu, 13 Jul 2023 16:55:28 +0800 Subject: [PATCH 3/3] Update dev and stable version Update dev and stable version --- dev.toml | 2 +- pyproject.toml | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/dev.toml b/dev.toml index c402899..3e8cb6e 100644 --- a/dev.toml +++ b/dev.toml @@ -6,7 +6,7 @@ build-backend = "setuptools.build_meta" [project] name = "frontengine_dev" -version = "0.0.27" +version = "0.0.28" authors = [ { name = "JE-Chen", email = "jechenmailman@gmail.com" }, ] diff --git a/pyproject.toml b/pyproject.toml index 09ce89b..452b51c 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -6,7 +6,7 @@ build-backend = "setuptools.build_meta" [project] name = "frontengine" -version = "0.0.26" +version = "0.0.27" authors = [ { name = "JE-Chen", email = "jechenmailman@gmail.com" }, ]