Spaces:
Runtime error
Runtime error
Create storage/fallback_storage.py
Browse files- storage/fallback_storage.py +74 -0
storage/fallback_storage.py
ADDED
|
@@ -0,0 +1,74 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# storage/fallback_storage.py
|
| 2 |
+
|
| 3 |
+
import streamlit as st
|
| 4 |
+
from datetime import datetime
|
| 5 |
+
|
| 6 |
+
class FallbackStorageManager:
|
| 7 |
+
"""Fallback storage using Streamlit session state when persistent storage isn't available"""
|
| 8 |
+
|
| 9 |
+
@staticmethod
|
| 10 |
+
def init_session_storage():
|
| 11 |
+
"""Initialize session storage"""
|
| 12 |
+
if 'fallback_storage' not in st.session_state:
|
| 13 |
+
st.session_state.fallback_storage = {
|
| 14 |
+
'chats': [],
|
| 15 |
+
'images': {},
|
| 16 |
+
'context': {}
|
| 17 |
+
}
|
| 18 |
+
|
| 19 |
+
@staticmethod
|
| 20 |
+
def save_chat(chat_data, images=None):
|
| 21 |
+
"""Save chat to session state"""
|
| 22 |
+
FallbackStorageManager.init_session_storage()
|
| 23 |
+
|
| 24 |
+
chat_id = f"chat_{datetime.now().strftime('%Y%m%d_%H%M%S')}"
|
| 25 |
+
|
| 26 |
+
# Store chat data
|
| 27 |
+
chat_data['id'] = chat_id
|
| 28 |
+
st.session_state.fallback_storage['chats'].append(chat_data)
|
| 29 |
+
|
| 30 |
+
# Store images
|
| 31 |
+
if images:
|
| 32 |
+
st.session_state.fallback_storage['images'][chat_id] = images
|
| 33 |
+
|
| 34 |
+
return chat_id
|
| 35 |
+
|
| 36 |
+
@staticmethod
|
| 37 |
+
def get_all_chats():
|
| 38 |
+
"""Get all chats from session state"""
|
| 39 |
+
FallbackStorageManager.init_session_storage()
|
| 40 |
+
return sorted(
|
| 41 |
+
st.session_state.fallback_storage['chats'],
|
| 42 |
+
key=lambda x: x.get('timestamp', ''),
|
| 43 |
+
reverse=True
|
| 44 |
+
)
|
| 45 |
+
|
| 46 |
+
@staticmethod
|
| 47 |
+
def get_chat(chat_id):
|
| 48 |
+
"""Get specific chat with images"""
|
| 49 |
+
FallbackStorageManager.init_session_storage()
|
| 50 |
+
|
| 51 |
+
# Find chat
|
| 52 |
+
chat = next(
|
| 53 |
+
(chat for chat in st.session_state.fallback_storage['chats']
|
| 54 |
+
if chat.get('id') == chat_id),
|
| 55 |
+
None
|
| 56 |
+
)
|
| 57 |
+
|
| 58 |
+
if chat:
|
| 59 |
+
# Add images if they exist
|
| 60 |
+
chat['images'] = st.session_state.fallback_storage['images'].get(chat_id, [])
|
| 61 |
+
|
| 62 |
+
return chat
|
| 63 |
+
|
| 64 |
+
@staticmethod
|
| 65 |
+
def save_context(context_data):
|
| 66 |
+
"""Save context to session state"""
|
| 67 |
+
FallbackStorageManager.init_session_storage()
|
| 68 |
+
st.session_state.fallback_storage['context'] = context_data
|
| 69 |
+
|
| 70 |
+
@staticmethod
|
| 71 |
+
def get_context():
|
| 72 |
+
"""Get context from session state"""
|
| 73 |
+
FallbackStorageManager.init_session_storage()
|
| 74 |
+
return st.session_state.fallback_storage.get('context')
|