Skip to content

Commit

Permalink
doubled-down on mvc
Browse files Browse the repository at this point in the history
  • Loading branch information
mohamed-chs committed Oct 8, 2023
1 parent f44e452 commit 054abec
Show file tree
Hide file tree
Showing 14 changed files with 512 additions and 479 deletions.
6 changes: 0 additions & 6 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,5 @@ node_modules/
.venv/

# testing models and visualizations on personal data
*dev*
*test*
data/
output/
project_layout.txt

# until I implement a better way to handle this
assets/stopwords/
27 changes: 14 additions & 13 deletions config.json
Original file line number Diff line number Diff line change
@@ -1,14 +1,17 @@
{
"zip_file": null,
"output_folder": null,
"message": {
"author_headers": {
"system": "### System",
"user": "# Me",
"assistant": "# ChatGPT",
"tool": "# Tool"
}
},
"conversation": {
"message": {
"author_headers": {
"system": "### System",
"user": "# Me",
"assistant": "# ChatGPT",
"tool": "# Tool"
}
"markdown": {
"latex_delimiters": "default"
},
"yaml": {
"title": true,
Expand All @@ -21,14 +24,12 @@
"message_count": true,
"content_types": true,
"custom_instructions": true
},
"markdown": {
"latex_delimiters": "default"
}
},
"wordcloud": {
"font": "ArchitectsDaughter-Regular",
"colormap": "prism",
"custom_stopwords": []
}
"colormap": "prism"
},
"node": {},
"conversation_list": {}
}
Empty file added controllers/__init__.py
Empty file.
55 changes: 55 additions & 0 deletions controllers/configuration.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
"""Module for handling user configuration and updating the models."""

import json
from pathlib import Path
from typing import Any, Dict

from models.conversation import Conversation
from models.conversation_list import ConversationList
from models.message import Message
from models.node import Node
from views.prompt_user import prompt_user

CONFIG_PATH = Path("config.json")

HOME: Path = Path.home()
DOWNLOADS: Path = HOME / "Downloads"

# most recent zip file in downloads folder
DEFAULT_ZIP_FILE: Path = max(DOWNLOADS.glob("*.zip"), key=lambda x: x.stat().st_ctime)

DEFAULT_OUTPUT_FOLDER: Path = HOME / "Documents" / "ChatGPT Data"


def get_user_configs() -> Dict[str, Any]:
"""Prompt the user for configuration options, with defaults from the config file.
Returns the user configuration as a dictionary."""

with open(CONFIG_PATH, "r", encoding="utf-8") as file:
default_configs = json.load(file)

if not default_configs["zip_file"]:
default_configs["zip_file"] = str(DEFAULT_ZIP_FILE)

if not default_configs["output_folder"]:
default_configs["output_folder"] = str(DEFAULT_OUTPUT_FOLDER)

# DEBUG
print("DEBUG: loaded default configs")

return prompt_user(default_configs)


def update_config_file(user_configs: Dict[str, Any]) -> None:
"""Update the config file with the user's configuration options."""
with open(CONFIG_PATH, "w", encoding="utf-8") as file:
json.dump(user_configs, file, indent=2)


def set_model_configs(configs: Dict[str, Any]) -> None:
"""Set the configuration for all models."""
Conversation.configuration = configs.get("conversation", {})
ConversationList.configuration = configs.get("conversation_list", {})
Message.configuration = configs.get("message", {})
Node.configuration = configs.get("node", {})
File renamed without changes.
104 changes: 104 additions & 0 deletions controllers/processes.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
"""Module for various processes that are used in the controllers."""


import json
from pathlib import Path
from typing import Any, Dict
from zipfile import ZipFile

from tqdm import tqdm

from models.conversation_list import ConversationList

from .data_visualizations import create_save_wordcloud


def load_conversations_from_zip(zip_filepath: Path) -> ConversationList:
"""Load the conversations from the zip file."""

with ZipFile(zip_filepath, "r") as file:
file.extractall(zip_filepath.with_suffix(""))

conversations_path = zip_filepath.with_suffix("") / "conversations.json"

with open(conversations_path, "r", encoding="utf-8") as file:
conversations = json.load(file)

return ConversationList(conversations)


def write_markdown_files(
conversation_list: ConversationList, markdown_folder: Path
) -> None:
"""Write the markdown files for the conversations in the conversation list."""

for conversation in tqdm(
conversation_list.conversations, desc="Writing Markdown 📄 files"
):
file_path = markdown_folder / f"{conversation.sanitized_title()}.md"
conversation.save_to_file(file_path)


def create_wordclouds(
conversation_list: ConversationList, wordcloud_folder: Path, configs: Dict[str, Any]
) -> None:
"""Create the wordclouds for the conversations in the conversation list."""

weeks_dict = conversation_list.grouped_by_week()
months_dict = conversation_list.grouped_by_month()
years_dict = conversation_list.grouped_by_year()

font_path = Path("assets/fonts") / f"{configs['wordcloud']['font']}.ttf"

colormap = configs["wordcloud"]["colormap"]

for week in tqdm(weeks_dict.keys(), desc="Creating weekly wordclouds 🔡☁️ "):
entire_week_text = (
weeks_dict[week].all_user_text()
+ "\n"
+ weeks_dict[week].all_assistant_text()
)

create_save_wordcloud(
entire_week_text,
wordcloud_folder / f"{week.strftime('%Y week %W')}.png",
font_path=str(font_path),
colormap=colormap,
)

for month in tqdm(months_dict.keys(), desc="Creating monthly wordclouds 🔡☁️ "):
entire_month_text = (
months_dict[month].all_user_text()
+ "\n"
+ months_dict[month].all_assistant_text()
)

create_save_wordcloud(
entire_month_text,
wordcloud_folder / f"{month.strftime('%B %Y')}.png",
font_path=str(font_path),
colormap=colormap,
)

for year in tqdm(years_dict.keys(), desc="Creating yearly wordclouds 🔡☁️ "):
entire_year_text = (
years_dict[year].all_user_text()
+ "\n"
+ years_dict[year].all_assistant_text()
)

create_save_wordcloud(
entire_year_text,
wordcloud_folder / f"{year.strftime('%Y')}.png",
font_path=str(font_path),
colormap=colormap,
)


def write_custom_instructions(
conversation_list: ConversationList, file_path: Path
) -> None:
"""Create JSON file for custom instructions in the conversation list."""

with open(file_path, "w", encoding="utf-8") as file:
json.dump(conversation_list.all_custom_instructions(), file, indent=2)
Loading

0 comments on commit 054abec

Please sign in to comment.