Skip to content

Commit

Permalink
Merge pull request #45 from Integration-Automation/dev
Browse files Browse the repository at this point in the history
Dev
  • Loading branch information
JE-Chen authored Jul 13, 2023
2 parents dcdd788 + 4c321bf commit 1a51203
Show file tree
Hide file tree
Showing 9 changed files with 89 additions and 11 deletions.
6 changes: 2 additions & 4 deletions dev.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,12 @@ build-backend = "setuptools.build_meta"

[project]
name = "frontengine_dev"
version = "0.0.26"
version = "0.0.28"
authors = [
{ name = "JE-Chen", email = "[email protected]" },
]
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"
Expand Down
1 change: 0 additions & 1 deletion frontengine/ui/chat/chat_scene_input.py
Original file line number Diff line number Diff line change
@@ -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
Expand Down
1 change: 0 additions & 1 deletion frontengine/ui/chat/chatthread.py
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down
54 changes: 54 additions & 0 deletions frontengine/ui/chat/speech_to_text.py
Original file line number Diff line number Diff line change
@@ -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)
19 changes: 18 additions & 1 deletion frontengine/ui/setting/chat_scene/chat_scene_setting.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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)
Expand Down Expand Up @@ -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)
Expand All @@ -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)

Expand All @@ -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())
Expand Down
3 changes: 3 additions & 0 deletions frontengine/utils/multi_language/english.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"
}
3 changes: 3 additions & 0 deletions frontengine/utils/multi_language/traditional_chinese.py
Original file line number Diff line number Diff line change
Expand Up @@ -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": "開始錄製聲音"
}
6 changes: 2 additions & 4 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,12 @@ build-backend = "setuptools.build_meta"

[project]
name = "frontengine"
version = "0.0.25"
version = "0.0.27"
authors = [
{ name = "JE-Chen", email = "[email protected]" },
]
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"
Expand Down
7 changes: 7 additions & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
PySide6
qt-material
EdgeGPT
pyttsx3
SpeechRecognition
frontengine
PyAudio

0 comments on commit 1a51203

Please sign in to comment.