Spaces:
Running
Running
Create history_manager.py
Browse files- components/history_manager.py +114 -0
components/history_manager.py
ADDED
|
@@ -0,0 +1,114 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import streamlit as st
|
| 2 |
+
import json
|
| 3 |
+
import uuid
|
| 4 |
+
from datetime import datetime
|
| 5 |
+
from typing import List, Dict, Optional
|
| 6 |
+
|
| 7 |
+
class HistoryManager:
|
| 8 |
+
def __init__(self):
|
| 9 |
+
self.history_key = "edugen_history"
|
| 10 |
+
|
| 11 |
+
def _get_user_id(self) -> str:
|
| 12 |
+
"""Get or create a unique user identifier"""
|
| 13 |
+
if 'user_id' not in st.session_state:
|
| 14 |
+
st.session_state.user_id = str(uuid.uuid4())
|
| 15 |
+
return st.session_state.user_id
|
| 16 |
+
|
| 17 |
+
def save_to_history(self, content_data: Dict) -> str:
|
| 18 |
+
"""Save generated content to history"""
|
| 19 |
+
try:
|
| 20 |
+
# Get user-specific history
|
| 21 |
+
user_id = self._get_user_id()
|
| 22 |
+
history = self._load_history()
|
| 23 |
+
|
| 24 |
+
# Create history entry
|
| 25 |
+
entry_id = str(uuid.uuid4())
|
| 26 |
+
history_entry = {
|
| 27 |
+
"id": entry_id,
|
| 28 |
+
"timestamp": datetime.now().isoformat(),
|
| 29 |
+
"user_type": content_data.get("user_type"),
|
| 30 |
+
"student_level": content_data.get("student_level"),
|
| 31 |
+
"topic": content_data.get("topic", ""),
|
| 32 |
+
"content_type": content_data.get("content_type", ""),
|
| 33 |
+
"prompt": content_data.get("prompt", ""),
|
| 34 |
+
"output": content_data.get("output", ""),
|
| 35 |
+
"pdf_data": content_data.get("pdf_data"),
|
| 36 |
+
"filename": content_data.get("filename", "content.pdf"),
|
| 37 |
+
"feedback_given": content_data.get("feedback_given", False)
|
| 38 |
+
}
|
| 39 |
+
|
| 40 |
+
# Add to history (newest first)
|
| 41 |
+
if user_id not in history:
|
| 42 |
+
history[user_id] = []
|
| 43 |
+
history[user_id].insert(0, history_entry)
|
| 44 |
+
|
| 45 |
+
# Keep only last 50 entries per user
|
| 46 |
+
history[user_id] = history[user_id][:50]
|
| 47 |
+
|
| 48 |
+
# Save to session state
|
| 49 |
+
st.session_state[self.history_key] = history
|
| 50 |
+
|
| 51 |
+
print(f"✅ Saved to history: {entry_id}") # Debug
|
| 52 |
+
return entry_id
|
| 53 |
+
|
| 54 |
+
except Exception as e:
|
| 55 |
+
print(f"Error saving to history: {e}")
|
| 56 |
+
return None
|
| 57 |
+
|
| 58 |
+
def _load_history(self) -> Dict:
|
| 59 |
+
"""Load history from session state"""
|
| 60 |
+
if self.history_key in st.session_state:
|
| 61 |
+
return st.session_state[self.history_key]
|
| 62 |
+
return {}
|
| 63 |
+
|
| 64 |
+
def get_user_history(self) -> List[Dict]:
|
| 65 |
+
"""Get current user's history"""
|
| 66 |
+
user_id = self._get_user_id()
|
| 67 |
+
history = self._load_history()
|
| 68 |
+
user_history = history.get(user_id, [])
|
| 69 |
+
print(f"📖 Loaded {len(user_history)} history entries for user {user_id[:8]}") # Debug
|
| 70 |
+
return user_history
|
| 71 |
+
|
| 72 |
+
def get_entry_by_id(self, entry_id: str) -> Optional[Dict]:
|
| 73 |
+
"""Get a specific history entry by ID"""
|
| 74 |
+
user_id = self._get_user_id()
|
| 75 |
+
history = self._load_history()
|
| 76 |
+
user_history = history.get(user_id, [])
|
| 77 |
+
|
| 78 |
+
for entry in user_history:
|
| 79 |
+
if entry["id"] == entry_id:
|
| 80 |
+
return entry
|
| 81 |
+
return None
|
| 82 |
+
|
| 83 |
+
def delete_entry(self, entry_id: str) -> bool:
|
| 84 |
+
"""Delete a specific history entry"""
|
| 85 |
+
try:
|
| 86 |
+
user_id = self._get_user_id()
|
| 87 |
+
history = self._load_history()
|
| 88 |
+
|
| 89 |
+
if user_id in history:
|
| 90 |
+
history[user_id] = [entry for entry in history[user_id] if entry["id"] != entry_id]
|
| 91 |
+
st.session_state[self.history_key] = history
|
| 92 |
+
return True
|
| 93 |
+
return False
|
| 94 |
+
except Exception as e:
|
| 95 |
+
print(f"Error deleting history entry: {e}")
|
| 96 |
+
return False
|
| 97 |
+
|
| 98 |
+
def clear_history(self) -> bool:
|
| 99 |
+
"""Clear all history for current user"""
|
| 100 |
+
try:
|
| 101 |
+
user_id = self._get_user_id()
|
| 102 |
+
history = self._load_history()
|
| 103 |
+
|
| 104 |
+
if user_id in history:
|
| 105 |
+
del history[user_id]
|
| 106 |
+
st.session_state[self.history_key] = history
|
| 107 |
+
return True
|
| 108 |
+
return False
|
| 109 |
+
except Exception as e:
|
| 110 |
+
print(f"Error clearing history: {e}")
|
| 111 |
+
return False
|
| 112 |
+
|
| 113 |
+
# Global history manager instance
|
| 114 |
+
history_manager = HistoryManager()
|