Spaces:
Runtime error
Runtime error
Create storage/storage_manager.py
Browse files- storage/storage_manager.py +97 -0
storage/storage_manager.py
ADDED
|
@@ -0,0 +1,97 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# storage/storage_manager.py
|
| 2 |
+
|
| 3 |
+
import json
|
| 4 |
+
from pathlib import Path
|
| 5 |
+
from datetime import datetime
|
| 6 |
+
import shutil
|
| 7 |
+
import streamlit as st
|
| 8 |
+
import base64
|
| 9 |
+
|
| 10 |
+
class UserStorageManager:
|
| 11 |
+
def __init__(self, storage_paths):
|
| 12 |
+
self.paths = storage_paths
|
| 13 |
+
self.ensure_directories()
|
| 14 |
+
|
| 15 |
+
def ensure_directories(self):
|
| 16 |
+
"""Ensure all required directories exist"""
|
| 17 |
+
for path in self.paths.values():
|
| 18 |
+
path.mkdir(parents=True, exist_ok=True)
|
| 19 |
+
|
| 20 |
+
def save_chat(self, chat_data, images=None):
|
| 21 |
+
"""Save chat with associated images"""
|
| 22 |
+
timestamp = datetime.now().strftime("%Y%m%d_%H%M%S")
|
| 23 |
+
chat_id = f"chat_{timestamp}"
|
| 24 |
+
|
| 25 |
+
# Save chat data
|
| 26 |
+
chat_file = self.paths["chats"] / f"{chat_id}.json"
|
| 27 |
+
with open(chat_file, "w") as f:
|
| 28 |
+
json.dump(chat_data, f)
|
| 29 |
+
|
| 30 |
+
# Save associated images
|
| 31 |
+
if images:
|
| 32 |
+
image_dir = self.paths["images"] / chat_id
|
| 33 |
+
image_dir.mkdir(exist_ok=True)
|
| 34 |
+
|
| 35 |
+
for idx, img_data in enumerate(images):
|
| 36 |
+
img_file = image_dir / f"image_{idx}.png"
|
| 37 |
+
with open(img_file, "wb") as f:
|
| 38 |
+
f.write(img_data)
|
| 39 |
+
|
| 40 |
+
# Update chat data with image references
|
| 41 |
+
chat_data["image_paths"] = [str(p) for p in image_dir.glob("*.png")]
|
| 42 |
+
with open(chat_file, "w") as f:
|
| 43 |
+
json.dump(chat_data, f)
|
| 44 |
+
|
| 45 |
+
return chat_id
|
| 46 |
+
|
| 47 |
+
def get_all_chats(self):
|
| 48 |
+
"""Get all user's chats with metadata"""
|
| 49 |
+
chats = []
|
| 50 |
+
for chat_file in self.paths["chats"].glob("*.json"):
|
| 51 |
+
with open(chat_file, "r") as f:
|
| 52 |
+
chat_data = json.load(f)
|
| 53 |
+
chat_data["id"] = chat_file.stem
|
| 54 |
+
chats.append(chat_data)
|
| 55 |
+
|
| 56 |
+
# Sort by timestamp
|
| 57 |
+
return sorted(chats, key=lambda x: x.get("timestamp", ""), reverse=True)
|
| 58 |
+
|
| 59 |
+
def get_chat(self, chat_id):
|
| 60 |
+
"""Get specific chat with images"""
|
| 61 |
+
chat_file = self.paths["chats"] / f"{chat_id}.json"
|
| 62 |
+
if not chat_file.exists():
|
| 63 |
+
return None
|
| 64 |
+
|
| 65 |
+
with open(chat_file, "r") as f:
|
| 66 |
+
chat_data = json.load(f)
|
| 67 |
+
|
| 68 |
+
# Load associated images
|
| 69 |
+
image_dir = self.paths["images"] / chat_id
|
| 70 |
+
if image_dir.exists():
|
| 71 |
+
images = []
|
| 72 |
+
for img_file in sorted(image_dir.glob("*.png")):
|
| 73 |
+
with open(img_file, "rb") as f:
|
| 74 |
+
images.append(f.read())
|
| 75 |
+
chat_data["images"] = images
|
| 76 |
+
|
| 77 |
+
return chat_data
|
| 78 |
+
|
| 79 |
+
def save_context(self, context_data):
|
| 80 |
+
"""Save conversation context"""
|
| 81 |
+
context_file = self.paths["context"] / "context.json"
|
| 82 |
+
with open(context_file, "w") as f:
|
| 83 |
+
json.dump(context_data, f)
|
| 84 |
+
|
| 85 |
+
def get_context(self):
|
| 86 |
+
"""Get saved conversation context"""
|
| 87 |
+
context_file = self.paths["context"] / "context.json"
|
| 88 |
+
if context_file.exists():
|
| 89 |
+
with open(context_file, "r") as f:
|
| 90 |
+
return json.load(f)
|
| 91 |
+
return None
|
| 92 |
+
|
| 93 |
+
def clear_context(self):
|
| 94 |
+
"""Clear conversation context"""
|
| 95 |
+
context_file = self.paths["context"] / "context.json"
|
| 96 |
+
if context_file.exists():
|
| 97 |
+
context_file.unlink()
|