Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
77 changes: 37 additions & 40 deletions companion.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
memory: Optional[Memory] = None
chatbot: Optional[Chatbot] = None
app_cache = AppCache()
voices_by_features = dict()
voices_by_features = {}
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Lines 26-26 refactored with the following changes:


@app.route('/')
def home():
Expand Down Expand Up @@ -63,44 +63,42 @@ def setup():
"""
Web page, setup page
"""
if request.method == 'POST':
filename = request.form.get('filename')
data = {
"model": {
"name": request.form.get('model-name'),
"temperature": float(request.form.get('temperature'))
},
"user": {
"name": request.form.get('user-name'),
"image": request.form.get('profile-img-url'),
"gender": request.form.get('gender')
},
"bot": {
"name": request.form.get('tutor').split("-")[0],
"image": f"/static/bots_profile/{request.form.get('tutor').split('-')[0].lower()}.png",
"gender": request.form.get('tutor').split("-")[1].lower(),
"voice": request.form.get('voices-dropdown')
},
"language": {
"native": request.form.get('user-lang-dropdown').lower(),
"learning": request.form.get('tutor-lang-dropdown').split("-")[0].lower(),
"level": request.form.get('lang-level')
},
"behavior": {
"auto_send_recording": bool(request.form.get('auto-send-switch'))
}
}
with open(os.path.join(os.getcwd(), filename), 'w') as outfile:
yaml.dump(data, outfile, allow_unicode=True)
return jsonify({'status': 'success'})

else:
if request.method != 'POST':
return render_template('setup.html', males=MALE_TUTORS, females=FEMALE_TUTORS,
input_languages_codes_and_names=[[language.language_name_to_iso6391(lang), lang]
for lang in INPUT_LANGUAGES],
output_languages_locales_and_names=[[k, language.locale_code_to_language(k, name_in_same_language=True)]
for k in voices_by_features.keys()]
)
filename = request.form.get('filename')
data = {
"model": {
"name": request.form.get('model-name'),
"temperature": float(request.form.get('temperature'))
},
"user": {
"name": request.form.get('user-name'),
"image": request.form.get('profile-img-url'),
"gender": request.form.get('gender')
},
"bot": {
"name": request.form.get('tutor').split("-")[0],
"image": f"/static/bots_profile/{request.form.get('tutor').split('-')[0].lower()}.png",
"gender": request.form.get('tutor').split("-")[1].lower(),
"voice": request.form.get('voices-dropdown')
},
"language": {
"native": request.form.get('user-lang-dropdown').lower(),
"learning": request.form.get('tutor-lang-dropdown').split("-")[0].lower(),
"level": request.form.get('lang-level')
},
"behavior": {
"auto_send_recording": bool(request.form.get('auto-send-switch'))
}
}
with open(os.path.join(os.getcwd(), filename), 'w') as outfile:
yaml.dump(data, outfile, allow_unicode=True)
return jsonify({'status': 'success'})
Comment on lines -66 to +101
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function setup refactored with the following changes:



@app.route('/get_language_voices', methods=['POST'])
Expand Down Expand Up @@ -140,7 +138,7 @@ def get_response():
app_cache.message_generator = chatbot.get_response(is_initial_message)
app_cache.last_sentence = ''
app_cache.sentences_counter = 0
app_cache.bot_recordings = list()
app_cache.bot_recordings = []
Comment on lines -143 to +141
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function get_response refactored with the following changes:

first_message = next(app_cache.message_generator)
app_cache.generated_message = first_message
except Exception as e:
Expand Down Expand Up @@ -269,8 +267,7 @@ def play_user_message():
Play user recording if exists
"""
message_id = int(request.form['message_id'].split('_')[1])
user_recording = memory[message_id]['user_recording']
if user_recording:
if user_recording := memory[message_id]['user_recording']:
Comment on lines -272 to +270
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function play_user_message refactored with the following changes:

app_cache.play_recordings_queue.put(user_recording)
return jsonify({'message': 'User message played successfully'})

Expand Down Expand Up @@ -308,10 +305,10 @@ def save_session():
"""
Save current session as file
"""
data = list()
for m in memory.get_chat_history()[1:]:
data.append({"role": m["role"], "content": m["content"]})

data = [
{"role": m["role"], "content": m["content"]}
for m in memory.get_chat_history()[1:]
]
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function save_session refactored with the following changes:

json_data = json.dumps(data, indent=4) # Convert the list of dictionaries to JSON format

with open(SAVED_SESSION_FILE, "w") as f:
Expand Down
6 changes: 4 additions & 2 deletions python/app_cache.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,17 @@
from typing import Generator, List, Optional


Comment on lines 4 to 5
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Lines 13-14 refactored with the following changes:



class AppCache:
"""
Used to save data which needs to transfer between different threads and endpoints
"""
message_generator: Optional[Generator] = None
language: Optional[str] = None
user_recording: Optional[str] = None
bot_recordings: List[str] = list()
server_errors: List[str] = list()
bot_recordings: List[str] = []
server_errors: List[str] = []
sentences_counter: int = 0
generated_message: str = ''
last_sentence: str = ''
Expand Down
13 changes: 9 additions & 4 deletions python/memory.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ class Memory:
This class is used to remember all messages sent and received, along with metadata such as recording files, etc.
"""
def __init__(self):
self._memory: List[dict] = list()
self._updates: List[dict] = list()
self._memory: List[dict] = []
self._updates: List[dict] = []
Comment on lines -10 to +11
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function Memory.__init__ refactored with the following changes:


def __getitem__(self, index):
return self._memory[index]
Expand All @@ -33,13 +33,18 @@ def add(self, role, message, recording=None, user_recording=None) -> None:
:param user_recording: a file name of the user's recording
"""
message = re.sub(r'[^\S\n]+', ' ', message)
mem = {"role": role, "content": message, "recording": recording or list(), "user_recording": user_recording}
mem = {
"role": role,
"content": message,
"recording": recording or [],
"user_recording": user_recording,
}
updates = [u.copy() for u in self._updates]
updates = [u for u in updates if u["index"] == len(self._memory)]
[u.pop("index") for u in updates]
for u in updates:
u["recording"] = u["recording"] or []
mem.update(u)
mem |= u
Comment on lines -36 to +47
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function Memory.add refactored with the following changes:

self._memory.append(mem)

def update(self, index, **kwargs) -> None:
Expand Down
6 changes: 3 additions & 3 deletions python/speech.py
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,7 @@ def voices_by_features() -> Dict[str, Dict[str, List[str]]]:

:return: Dict: {lang: {gender: [list of voices]}}
"""
voices_dict = dict()
voices_dict = {}
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function voices_by_features refactored with the following changes:

voices = t2s_client.list_voices().voices

for voice in voices:
Expand All @@ -153,11 +153,11 @@ def voices_by_features() -> Dict[str, Dict[str, List[str]]]:

for language_code in language_codes:
if language_code not in voices_dict:
voices_dict[language_code] = dict()
voices_dict[language_code] = {}

lang_dict = voices_dict[language_code]
if gender not in lang_dict:
lang_dict[gender] = list()
lang_dict[gender] = []

lang_dict[gender].append(name)

Expand Down
27 changes: 11 additions & 16 deletions python/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,24 +19,20 @@ def split_to_sentences(text: str) -> List[str]:

:param text: the text to split
"""
characters = [c+' ' for c in ['.', '!', "?", ":", ";"]]
characters = [f'{c} ' for c in ['.', '!', "?", ":", ";"]]
escaped_characters = [re.escape(c) for c in characters]
if any([c in text for c in characters]):
if any(c in text for c in characters):
pattern = '|'.join(escaped_characters)
split_list = re.split(pattern, text)
return re.split(pattern, text)
elif '\n' in text:
lst = text.split('\n')
lst = [s for s in lst if len(s.strip()) > 0]
if len(lst) > 1:
split_list = [lst[0], "\n".join(lst[1:])]
else:
split_list = lst
return [lst[0], "\n".join(lst[1:])] if len(lst) > 1 else lst
elif ', ' in text and len(text) > 100:
lst = re.split(re.escape(',') + r'\s', text)
split_list = [lst[0], ", ".join(lst[1:])]
return [lst[0], ", ".join(lst[1:])]
else:
split_list = [text]
return split_list
return [text]
Comment on lines -22 to +35
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function split_to_sentences refactored with the following changes:



def bot_text_to_speech(text: str, message_index: int, counter: int) -> str:
Expand Down Expand Up @@ -72,12 +68,11 @@ def get_gcs_credentials(config: Config) -> Credentials:
:param config: Config
:return: Credentials
"""
sa = config.get("google_sa", None)
if sa:
credentials = Credentials.from_service_account_info(sa)
else:
credentials = None
return credentials
return (
Credentials.from_service_account_info(sa)
if (sa := config.get("google_sa", None))
else None
)
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function get_gcs_credentials refactored with the following changes:



def get_error_message_from_exception(e: Exception) -> str:
Expand Down