Skip to content

Commit

Permalink
Use json instead of pickle to send cli args to app
Browse files Browse the repository at this point in the history
  • Loading branch information
paulovcmedeiros committed Nov 10, 2023
1 parent 2e3639f commit b26fb7d
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 9 deletions.
4 changes: 1 addition & 3 deletions gpt_buddy_bot/app/app_page_templates.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
"""Utilities for creating pages in a streamlit app."""
import pickle
import sys
import uuid
from abc import ABC, abstractmethod
Expand Down Expand Up @@ -127,8 +126,7 @@ def chat_configs(self) -> ChatOptions:
"""Return the configs used for the page's chat object."""
if "chat_configs" not in self.state:
chat_options_file_path = sys.argv[-1]
with open(chat_options_file_path, "rb") as chat_configs_file:
self.state["chat_configs"] = pickle.load(chat_configs_file)
self.state["chat_configs"] = ChatOptions.from_file(chat_options_file_path)
return self.state["chat_configs"]

@chat_configs.setter
Expand Down
3 changes: 1 addition & 2 deletions gpt_buddy_bot/chat.py
Original file line number Diff line number Diff line change
Expand Up @@ -64,8 +64,7 @@ def cache_dir(self, value):

def save_cache(self):
"""Store the chat's configs and metadata to the cache directory."""
with open(self.configs_file, "w") as configs_f:
configs_f.write(self.configs.model_dump_json(indent=2))
self.configs.export(self.configs_file)

metadata = self.metadata # Trigger loading metadata if not yet done
with open(self.metadata_file, "w") as metadata_f:
Expand Down
12 changes: 12 additions & 0 deletions gpt_buddy_bot/chat_configs.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#!/usr/bin/env python3
"""Registration and validation of options."""
import argparse
import json
import types
import typing
from getpass import getuser
Expand Down Expand Up @@ -68,6 +69,17 @@ def __getitem__(self, item):
except AttributeError as error:
raise KeyError(item) from error

def export(self, fpath: Path):
"""Export the model's data to a file."""
with open(fpath, "w") as configs_file:
configs_file.write(self.model_dump_json(indent=2, exclude_unset=True))

@classmethod
def from_file(cls, fpath: Path):
"""Return an instance of the class given configs stored in a json file."""
with open(fpath, "r") as configs_file:
return cls.model_validate(json.load(configs_file))


class OpenAiApiCallOptions(BaseConfigModel):
"""Model for configuring options for OpenAI API calls."""
Expand Down
5 changes: 1 addition & 4 deletions gpt_buddy_bot/command_definitions.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
#!/usr/bin/env python3
"""Commands supported by the package's script."""
import pickle
import subprocess

from . import GeneralConstants
Expand All @@ -23,9 +22,7 @@ def run_on_terminal(args):

def run_on_ui(args):
"""Run the chat on the browser."""
with open(GeneralConstants.PARSED_ARGS_FILE, "wb") as chat_options_file:
pickle.dump(ChatOptions.from_cli_args(args), chat_options_file)

ChatOptions.from_cli_args(args).export(fpath=GeneralConstants.PARSED_ARGS_FILE)
try:
subprocess.run(
[ # noqa: S603, S607
Expand Down

0 comments on commit b26fb7d

Please sign in to comment.