Skip to content

Commit

Permalink
Linting code
Browse files Browse the repository at this point in the history
  • Loading branch information
Jeffser committed Aug 5, 2024
1 parent 69fd785 commit dd95e3d
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 28 deletions.
39 changes: 21 additions & 18 deletions src/dialogs.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)

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

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

Expand All @@ -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
Expand Down Expand Up @@ -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:
Expand All @@ -394,4 +398,3 @@ def attach_website(self, url):
cancellable = None,
callback = lambda dialog, task, url=url: attach_website_response(self, dialog, task, url)
)

20 changes: 10 additions & 10 deletions src/window.py
Original file line number Diff line number Diff line change
Expand Up @@ -785,7 +785,7 @@ def update_list_local_models(self):
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):
Expand Down Expand Up @@ -1161,7 +1161,7 @@ def update_list_available_models(self):

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):
Expand All @@ -1180,7 +1180,7 @@ def load_history(self):
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]
Expand Down Expand Up @@ -1379,7 +1379,7 @@ def on_export_chat(self, file_dialog, result, chat_name):

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)
Expand All @@ -1389,7 +1389,7 @@ def on_export_chat(self, file_dialog, result, chat_name):
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(
Expand All @@ -1416,7 +1416,7 @@ def on_chat_imported(self, file_dialog, result):
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:
Expand All @@ -1426,7 +1426,7 @@ def on_chat_imported(self, file_dialog, result):
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()))
Expand Down Expand Up @@ -1474,7 +1474,7 @@ def get_content_of_file(self, file_path, file_type):
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)
Expand Down Expand Up @@ -1603,7 +1603,7 @@ def setup_model_dropdown(self):
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"))
Expand Down Expand Up @@ -1634,7 +1634,7 @@ def __init__(self, **kwargs):
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']
Expand Down

0 comments on commit dd95e3d

Please sign in to comment.