Spaces:
Sleeping
Sleeping
| import json | |
| import os | |
| from datetime import datetime | |
| DATA_FILE = "data.json" | |
| def _load() -> dict: | |
| """Load data.json, create if missing.""" | |
| if not os.path.exists(DATA_FILE): | |
| default = { | |
| "resume": {"raw_text": "", "last_updated": ""}, | |
| "applications": [], | |
| "job_cache": [], | |
| "settings": { | |
| "preferred_roles": [], | |
| "preferred_keywords": [], | |
| "groq_model": "llama-3.1-8b-instant" | |
| }, | |
| "chat_history": [] | |
| } | |
| _save(default) | |
| return default | |
| with open(DATA_FILE, "r", encoding="utf-8") as f: | |
| return json.load(f) | |
| def _save(data: dict): | |
| """Write dict back to data.json.""" | |
| with open(DATA_FILE, "w", encoding="utf-8") as f: | |
| json.dump(data, f, indent=2, ensure_ascii=False) | |
| # ββ Resume ββββββββββββββββββββββββββββββββββββββββββββ | |
| def save_resume(text: str): | |
| data = _load() | |
| data["resume"]["raw_text"] = text.strip() | |
| data["resume"]["last_updated"] = datetime.now().isoformat() | |
| _save(data) | |
| def load_resume() -> str: | |
| return _load()["resume"]["raw_text"] | |
| def resume_last_updated() -> str: | |
| return _load()["resume"].get("last_updated", "") | |
| # ββ Applications ββββββββββββββββββββββββββββββββββββββ | |
| def save_application(app: dict): | |
| data = _load() | |
| # Avoid duplicate by job_id | |
| existing_ids = [a["job_id"] for a in data["applications"]] | |
| if app["job_id"] not in existing_ids: | |
| data["applications"].append(app) | |
| _save(data) | |
| return True | |
| return False # Already applied | |
| def load_applications() -> list: | |
| return _load()["applications"] | |
| def update_application_status(job_id: str, new_status: str): | |
| data = _load() | |
| for app in data["applications"]: | |
| if app["job_id"] == job_id: | |
| app["status"] = new_status | |
| break | |
| _save(data) | |
| def delete_application(job_id: str): | |
| data = _load() | |
| data["applications"] = [ | |
| a for a in data["applications"] if a["job_id"] != job_id | |
| ] | |
| _save(data) | |
| # ββ Job Cache βββββββββββββββββββββββββββββββββββββββββ | |
| def save_job_cache(jobs: list): | |
| data = _load() | |
| data["job_cache"] = jobs | |
| _save(data) | |
| def load_job_cache() -> list: | |
| return _load()["job_cache"] | |
| # ββ Settings ββββββββββββββββββββββββββββββββββββββββββ | |
| def load_settings() -> dict: | |
| return _load()["settings"] | |
| def save_settings(settings: dict): | |
| data = _load() | |
| data["settings"].update(settings) | |
| _save(data) | |
| # ββ Chat History ββββββββββββββββββββββββββββββββββββββ | |
| def save_chat_message(role: str, content: str): | |
| data = _load() | |
| data["chat_history"].append({ | |
| "role": role, | |
| "content": content, | |
| "timestamp": datetime.now().isoformat() | |
| }) | |
| # Keep last 50 messages only | |
| data["chat_history"] = data["chat_history"][-50:] | |
| _save(data) | |
| def load_chat_history() -> list: | |
| return _load()["chat_history"] | |
| def clear_chat_history(): | |
| data = _load() | |
| data["chat_history"] = [] | |
| _save(data) | |
| # ββ Stats βββββββββββββββββββββββββββββββββββββββββββββ | |
| def get_stats() -> dict: | |
| apps = load_applications() | |
| return { | |
| "total": len(apps), | |
| "applied": len([a for a in apps if a["status"] == "Applied"]), | |
| "pending": len([a for a in apps if a["status"] == "Pending"]), | |
| "skipped": len([a for a in apps if a["status"] == "Skipped"]), | |
| } |