diff --git a/src/dialogs.py b/src/dialogs.py index 18fb312..225aafb 100644 --- a/src/dialogs.py +++ b/src/dialogs.py @@ -58,9 +58,11 @@ def delete_chat(self, chat_name): # RENAME CHAT | WORKS def rename_chat_response(self, dialog, task, old_chat_name, entry, label_element): - if not entry: return + if not entry: + return new_chat_name = entry.get_text() - if old_chat_name == new_chat_name: return + if old_chat_name == new_chat_name: + return if new_chat_name and (task is None or dialog.choose_finish(task) == "rename"): self.rename_chat(old_chat_name, new_chat_name, label_element) @@ -86,7 +88,8 @@ def rename_chat(self, chat_name, label_element): def new_chat_response(self, dialog, task, entry): chat_name = _("New Chat") - if entry is not None and entry.get_text() != "": chat_name = entry.get_text() + if entry is not None and entry.get_text() != "": + chat_name = entry.get_text() if chat_name and (task is None or dialog.choose_finish(task) == "create"): self.new_chat(chat_name) @@ -247,15 +250,15 @@ def create_model_from_existing(self): ) def create_model_from_file_response(self, file_dialog, result): - try: file = file_dialog.open_finish(result) - except: - logger.error(e) - return try: - self.create_model(file.get_path(), True) + file = file_dialog.open_finish(result) + try: + self.create_model(file.get_path(), True) + except Exception as e: + logger.error(e) + self.show_toast(_("An error occurred while creating the model"), self.main_overlay) except Exception as e: logger.error(e) - self.show_toast(_("An error occurred while creating the model"), self.main_overlay) def create_model_from_file(self): file_dialog = Gtk.FileDialog(default_filter=self.file_filter_gguf) @@ -290,24 +293,24 @@ def attach_file_response(self, file_dialog, result): "image": ["png", "jpeg", "jpg", "webp", "gif"], "pdf": ["pdf"] } - try: file = file_dialog.open_finish(result) - except: + try: + file = file_dialog.open_finish(result) + except Exception as e: logger.error(e) return extension = file.get_path().split(".")[-1] file_type = next(key for key, value in file_types.items() if extension in value) - if not file_type: return + if not file_type: + return if file_type == 'image' and not self.verify_if_image_can_be_used(): self.show_toast(_("Image recognition is only available on specific models"), self.main_overlay) return self.attach_file(file.get_path(), file_type) - def attach_file(self, filter): file_dialog = Gtk.FileDialog(default_filter=filter) file_dialog.open(self, None, lambda file_dialog, result: attach_file_response(self, file_dialog, result)) - # YouTube caption | WORKS def youtube_caption_response(self, dialog, task, video_url, caption_drop_down): @@ -325,7 +328,7 @@ def youtube_caption_response(self, dialog, task, video_url, caption_drop_down): if not os.path.exists(os.path.join(self.cache_dir, 'tmp/youtube')): os.makedirs(os.path.join(self.cache_dir, 'tmp/youtube')) file_path = os.path.join(os.path.join(self.cache_dir, 'tmp/youtube'), f'{yt.title} ({selected_caption.split(" | ")[0]})') - with open(file_path, 'w+') as f: + with open(file_path, 'w+', encoding="utf-8") as f: f.write(text) self.attach_file(file_path, 'youtube') @@ -337,7 +340,8 @@ def youtube_caption(self, video_url): self.show_toast(_("This video does not have any transcriptions"), self.main_overlay) return caption_list = Gtk.StringList() - for caption in captions: caption_list.append("{} | {}".format(caption.name, caption.code)) + for caption in captions: + caption_list.append("{} | {}".format(caption.name, caption.code)) caption_drop_down = Gtk.DropDown( enable_search=True, model=caption_list @@ -373,7 +377,7 @@ def attach_website_response(self, dialog, task, url): os.makedirs('/tmp/alpaca/websites/') md_name = self.generate_numbered_name('website.md', os.listdir('/tmp/alpaca/websites')) file_path = os.path.join('/tmp/alpaca/websites/', md_name) - with open(file_path, 'w+') as f: + with open(file_path, 'w+', encoding="utf-8") as f: f.write('{}\n\n{}'.format(url, md)) self.attach_file(file_path, 'website') else: @@ -394,4 +398,3 @@ def attach_website(self, url): cancellable = None, callback = lambda dialog, task, url=url: attach_website_response(self, dialog, task, url) ) - diff --git a/src/window.py b/src/window.py index 4c13207..68030a2 100644 --- a/src/window.py +++ b/src/window.py @@ -785,7 +785,7 @@ Generate a title following these rules: self.connection_error() def save_server_config(self): - with open(os.path.join(self.config_dir, "server.json"), "w+") as f: + with open(os.path.join(self.config_dir, "server.json"), "w+", encoding="utf-8") as f: json.dump({'remote_url': self.remote_url, 'remote_bearer_token': self.remote_bearer_token, 'run_remote': self.run_remote, 'local_port': local_instance.port, 'run_on_background': self.run_on_background, 'model_tweaks': self.model_tweaks, 'ollama_overrides': local_instance.overrides}, f, indent=6) def verify_connection(self): @@ -1161,7 +1161,7 @@ Generate a title following these rules: def save_history(self): logger.debug("Saving history") - with open(os.path.join(self.data_dir, "chats", "chats.json"), "w+") as f: + with open(os.path.join(self.data_dir, "chats", "chats.json"), "w+", encoding="utf-8") as f: json.dump(self.chats, f, indent=4) def load_history_into_chat(self): @@ -1180,7 +1180,7 @@ Generate a title following these rules: logger.debug("Loading history") if os.path.exists(os.path.join(self.data_dir, "chats", "chats.json")): try: - with open(os.path.join(self.data_dir, "chats", "chats.json"), "r") as f: + with open(os.path.join(self.data_dir, "chats", "chats.json"), "r", encoding="utf-8") as f: self.chats = json.load(f) if len(list(self.chats["chats"].keys())) == 0: self.chats["chats"][_("New Chat")] = {"messages": {}} if "selected_chat" not in self.chats or self.chats["selected_chat"] not in self.chats["chats"]: self.chats["selected_chat"] = list(self.chats["chats"].keys())[0] @@ -1379,7 +1379,7 @@ Generate a title following these rules: with tempfile.TemporaryDirectory() as temp_dir: json_path = os.path.join(temp_dir, "data.json") - with open(json_path, "wb") as json_file: + with open(json_path, "wb", encoding="utf-8") as json_file: json_file.write(json_data) tar_path = os.path.join(temp_dir, chat_name) @@ -1389,7 +1389,7 @@ Generate a title following these rules: if os.path.exists(directory) and os.path.isdir(directory): tar.add(directory, arcname=os.path.basename(directory)) - with open(tar_path, "rb") as tar: + with open(tar_path, "rb", encoding="utf-8") as tar: tar_content = tar.read() file.replace_contents_async( @@ -1416,7 +1416,7 @@ Generate a title following these rules: with tempfile.TemporaryDirectory() as temp_dir: tar_filename = os.path.join(temp_dir, "imported_chat.tar") - with open(tar_filename, "wb") as tar_file: + with open(tar_filename, "wb", encoding="utf-8") as tar_file: tar_file.write(tar_content.get_data()) with tarfile.open(tar_filename, "r") as tar: @@ -1426,7 +1426,7 @@ Generate a title following these rules: for member in tar.getmembers(): if member.name == "data.json": json_filepath = os.path.join(temp_dir, member.name) - with open(json_filepath, "r") as json_file: + with open(json_filepath, "r", encoding="utf-8") as json_file: data = json.load(json_file) for chat_name, chat_content in data.items(): new_chat_name = self.generate_numbered_name(chat_name, list(self.chats['chats'].keys())) @@ -1474,7 +1474,7 @@ Generate a title following these rules: logger.error(e) self.show_toast(_("Cannot open image"), self.main_overlay) elif file_type == 'plain_text' or file_type == 'youtube' or file_type == 'website': - with open(file_path, 'r') as f: + with open(file_path, 'r', encoding="utf-8") as f: return f.read() elif file_type == 'pdf': reader = PdfReader(file_path) @@ -1603,7 +1603,7 @@ Generate a title following these rules: def __init__(self, **kwargs): super().__init__(**kwargs) GtkSource.init() - with open(os.path.join(source_dir, 'available_models.json'), 'r') as f: + with open(os.path.join(source_dir, 'available_models.json'), 'r', encoding="utf-8") as f: self.available_models = json.load(f) if not os.path.exists(os.path.join(self.data_dir, "chats")): os.makedirs(os.path.join(self.data_dir, "chats")) @@ -1634,7 +1634,7 @@ Generate a title following these rules: self.background_switch.connect("notify", lambda pspec, user_data : self.switch_run_on_background()) self.setup_model_dropdown() if os.path.exists(os.path.join(self.config_dir, "server.json")): - with open(os.path.join(self.config_dir, "server.json"), "r") as f: + with open(os.path.join(self.config_dir, "server.json"), "r", encoding="utf-8") as f: data = json.load(f) self.run_remote = data['run_remote'] local_instance.port = data['local_port']