-
Notifications
You must be signed in to change notification settings - Fork 0
Sourcery refactored main branch #1
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -23,7 +23,7 @@ | |
| memory: Optional[Memory] = None | ||
| chatbot: Optional[Chatbot] = None | ||
| app_cache = AppCache() | ||
| voices_by_features = dict() | ||
| voices_by_features = {} | ||
|
|
||
| @app.route('/') | ||
| def home(): | ||
|
|
@@ -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
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Function
|
||
|
|
||
|
|
||
| @app.route('/get_language_voices', methods=['POST']) | ||
|
|
@@ -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
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Function
|
||
| first_message = next(app_cache.message_generator) | ||
| app_cache.generated_message = first_message | ||
| except Exception as e: | ||
|
|
@@ -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
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Function
|
||
| app_cache.play_recordings_queue.put(user_recording) | ||
| return jsonify({'message': 'User message played successfully'}) | ||
|
|
||
|
|
@@ -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:] | ||
| ] | ||
|
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Function
|
||
| json_data = json.dumps(data, indent=4) # Convert the list of dictionaries to JSON format | ||
|
|
||
| with open(SAVED_SESSION_FILE, "w") as f: | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -3,15 +3,17 @@ | |
| from typing import Generator, List, Optional | ||
|
|
||
|
|
||
|
Comment on lines
4
to
5
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Lines
|
||
|
|
||
|
|
||
| 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 = '' | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -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
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Function
|
||
|
|
||
| def __getitem__(self, index): | ||
| return self._memory[index] | ||
|
|
@@ -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
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Function
|
||
| self._memory.append(mem) | ||
|
|
||
| def update(self, index, **kwargs) -> None: | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -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 = {} | ||
|
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Function
|
||
| voices = t2s_client.list_voices().voices | ||
|
|
||
| for voice in voices: | ||
|
|
@@ -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) | ||
|
|
||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -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
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Function
|
||
|
|
||
|
|
||
| def bot_text_to_speech(text: str, message_index: int, counter: int) -> str: | ||
|
|
@@ -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 | ||
| ) | ||
|
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Function
|
||
|
|
||
|
|
||
| def get_error_message_from_exception(e: Exception) -> str: | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Lines
26-26refactored with the following changes:dict()with{}(dict-literal)