Conversation
| chatbot: Optional[Chatbot] = None | ||
| app_cache = AppCache() | ||
| voices_by_features = dict() | ||
| voices_by_features = {} |
There was a problem hiding this comment.
Lines 26-26 refactored with the following changes:
- Replace
dict()with{}(dict-literal)
| 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'}) |
There was a problem hiding this comment.
Function setup refactored with the following changes:
- Swap if/else branches (
swap-if-else-branches) - Remove unnecessary else after guard condition (
remove-unnecessary-else)
| app_cache.bot_recordings = list() | ||
| app_cache.bot_recordings = [] |
There was a problem hiding this comment.
Function get_response refactored with the following changes:
- Replace
list()with[](list-literal)
| user_recording = memory[message_id]['user_recording'] | ||
| if user_recording: | ||
| if user_recording := memory[message_id]['user_recording']: |
There was a problem hiding this comment.
Function play_user_message refactored with the following changes:
- Use named expression to simplify assignment and conditional (
use-named-expression)
| data = [ | ||
| {"role": m["role"], "content": m["content"]} | ||
| for m in memory.get_chat_history()[1:] | ||
| ] |
There was a problem hiding this comment.
Function save_session refactored with the following changes:
- Convert for loop into list comprehension (
list-comprehension) - Replace
list()with[](list-literal)
| self._memory: List[dict] = list() | ||
| self._updates: List[dict] = list() | ||
| self._memory: List[dict] = [] | ||
| self._updates: List[dict] = [] |
There was a problem hiding this comment.
Function Memory.__init__ refactored with the following changes:
- Replace
list()with[][×2] (list-literal)
| 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 |
There was a problem hiding this comment.
Function Memory.add refactored with the following changes:
- Replace
list()with[](list-literal) - Merge dictionary updates via the union operator (
dict-assign-update-to-union)
| :return: Dict: {lang: {gender: [list of voices]}} | ||
| """ | ||
| voices_dict = dict() | ||
| voices_dict = {} |
There was a problem hiding this comment.
Function voices_by_features refactored with the following changes:
- Replace
dict()with{}[×2] (dict-literal) - Replace
list()with[](list-literal)
| 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] |
There was a problem hiding this comment.
Function split_to_sentences refactored with the following changes:
- Use f-string instead of string concatenation (
use-fstring-for-concatenation) - Lift return into if (
lift-return-into-if) - Replace unneeded comprehension with generator (
comprehension-to-generator) - Replace if statement with if expression (
assign-if-exp)
| Credentials.from_service_account_info(sa) | ||
| if (sa := config.get("google_sa", None)) | ||
| else None | ||
| ) |
There was a problem hiding this comment.
Function get_gcs_credentials refactored with the following changes:
- Use named expression to simplify assignment and conditional (
use-named-expression) - Replace if statement with if expression (
assign-if-exp) - Inline variable that is immediately returned (
inline-immediately-returned-variable)
Branch
mainrefactored by Sourcery.If you're happy with these changes, merge this Pull Request using the Squash and merge strategy.
See our documentation here.
Run Sourcery locally
Reduce the feedback loop during development by using the Sourcery editor plugin:
Review changes via command line
To manually merge these changes, make sure you're on the
mainbranch, then run:Help us improve this pull request!