2025-06-20 23:48:10 +02:00
|
|
|
import os
|
|
|
|
import json
|
|
|
|
import markovify
|
|
|
|
import pickle
|
|
|
|
from modules.globalvars import *
|
2025-07-06 21:06:04 +02:00
|
|
|
from modules.volta.main import _
|
2025-07-09 15:05:06 +02:00
|
|
|
import logging
|
|
|
|
logger = logging.getLogger("goober")
|
2025-07-22 19:34:46 +02:00
|
|
|
|
2025-07-22 20:06:59 +02:00
|
|
|
def get_file_info(file_path: str) -> dict:
|
2025-06-20 23:48:10 +02:00
|
|
|
try:
|
|
|
|
file_size = os.path.getsize(file_path)
|
|
|
|
with open(file_path, "r") as f:
|
|
|
|
lines = f.readlines()
|
2025-07-22 20:06:59 +02:00
|
|
|
return {
|
|
|
|
"file_size_bytes": file_size,
|
|
|
|
"line_count": len(lines)
|
|
|
|
}
|
2025-06-20 23:48:10 +02:00
|
|
|
except Exception as e:
|
|
|
|
return {"error": str(e)}
|
2025-07-22 20:06:59 +02:00
|
|
|
|
|
|
|
def load_memory() -> list:
|
2025-06-20 23:48:10 +02:00
|
|
|
try:
|
|
|
|
with open(MEMORY_FILE, "r") as f:
|
2025-07-22 20:06:59 +02:00
|
|
|
return json.load(f)
|
2025-06-20 23:48:10 +02:00
|
|
|
except FileNotFoundError:
|
2025-07-22 20:06:59 +02:00
|
|
|
return []
|
2025-06-20 23:48:10 +02:00
|
|
|
|
2025-07-22 20:06:59 +02:00
|
|
|
def save_memory(memory: list) -> None:
|
2025-06-20 23:48:10 +02:00
|
|
|
with open(MEMORY_FILE, "w") as f:
|
|
|
|
json.dump(memory, f, indent=4)
|
2025-07-22 20:06:59 +02:00
|
|
|
|
|
|
|
def train_markov_model(memory: list, additional_data: list = None):
|
|
|
|
lines = [line for line in (memory or []) if isinstance(line, str)]
|
|
|
|
|
2025-06-20 23:48:10 +02:00
|
|
|
if additional_data:
|
2025-07-22 20:06:59 +02:00
|
|
|
lines.extend(line for line in additional_data if isinstance(line, str))
|
|
|
|
|
|
|
|
if not lines:
|
2025-07-16 19:27:12 +02:00
|
|
|
return None
|
2025-07-22 20:06:59 +02:00
|
|
|
|
|
|
|
text = "\n".join(lines)
|
|
|
|
return markovify.NewlineText(text, state_size=2)
|
|
|
|
|
|
|
|
def save_markov_model(model, filename: str = 'markov_model.pkl') -> None:
|
2025-06-20 23:48:10 +02:00
|
|
|
with open(filename, 'wb') as f:
|
|
|
|
pickle.dump(model, f)
|
2025-07-22 20:06:59 +02:00
|
|
|
|
|
|
|
def load_markov_model(filename: str = 'markov_model.pkl'):
|
2025-06-20 23:48:10 +02:00
|
|
|
try:
|
|
|
|
with open(filename, 'rb') as f:
|
|
|
|
model = pickle.load(f)
|
2025-07-09 15:05:06 +02:00
|
|
|
logger.info(f"{_('model_loaded')} {filename}.{RESET}")
|
2025-06-20 23:48:10 +02:00
|
|
|
return model
|
|
|
|
except FileNotFoundError:
|
2025-07-09 15:05:06 +02:00
|
|
|
logger.error(f"{filename} {_('not_found')}{RESET}")
|
2025-07-22 20:06:59 +02:00
|
|
|
return None
|