Spaces:
Runtime error
Runtime error
Create utils/file_handlers.py
Browse files- utils/file_handlers.py +70 -0
utils/file_handlers.py
ADDED
|
@@ -0,0 +1,70 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# src/utils/file_handlers.py
|
| 2 |
+
|
| 3 |
+
import json
|
| 4 |
+
from datetime import datetime
|
| 5 |
+
from pathlib import Path
|
| 6 |
+
from ..config.settings import CHAT_HISTORIES_DIR, CHAT_IMAGES_DIR
|
| 7 |
+
|
| 8 |
+
def save_chat_history(chat_history, image_data=None, filename=None):
|
| 9 |
+
"""Save chat history and associated image"""
|
| 10 |
+
timestamp = datetime.now().strftime("%Y%m%d_%H%M%S")
|
| 11 |
+
|
| 12 |
+
# Get the stock name from the latest analysis
|
| 13 |
+
stock_name = "Unknown"
|
| 14 |
+
if chat_history:
|
| 15 |
+
latest_analysis = chat_history[-1].get('analysis', '')
|
| 16 |
+
if "analyzing" in latest_analysis.lower():
|
| 17 |
+
words = latest_analysis.split()
|
| 18 |
+
for i, word in enumerate(words):
|
| 19 |
+
if word.lower() == "analyzing":
|
| 20 |
+
stock_name = words[i + 1].strip("(),.:")
|
| 21 |
+
|
| 22 |
+
# Create filename with metadata
|
| 23 |
+
base_filename = filename or f"{stock_name}_{timestamp}"
|
| 24 |
+
|
| 25 |
+
# Save image if provided
|
| 26 |
+
image_filename = None
|
| 27 |
+
if image_data:
|
| 28 |
+
image_filename = f"{base_filename}.jpg"
|
| 29 |
+
image_path = CHAT_IMAGES_DIR / image_filename
|
| 30 |
+
with open(image_path, "wb") as f:
|
| 31 |
+
f.write(image_data)
|
| 32 |
+
|
| 33 |
+
# Add metadata to chat history
|
| 34 |
+
chat_data = {
|
| 35 |
+
'metadata': {
|
| 36 |
+
'stock_name': stock_name,
|
| 37 |
+
'date_created': timestamp,
|
| 38 |
+
'image_file': image_filename
|
| 39 |
+
},
|
| 40 |
+
'conversations': chat_history
|
| 41 |
+
}
|
| 42 |
+
|
| 43 |
+
# Save chat history
|
| 44 |
+
json_filename = f"{base_filename}.json"
|
| 45 |
+
filepath = CHAT_HISTORIES_DIR / json_filename
|
| 46 |
+
with open(filepath, "w") as f:
|
| 47 |
+
json.dump(chat_data, f)
|
| 48 |
+
|
| 49 |
+
return json_filename
|
| 50 |
+
|
| 51 |
+
def load_chat_history(filename):
|
| 52 |
+
"""Load chat history and associated image"""
|
| 53 |
+
filepath = CHAT_HISTORIES_DIR / filename
|
| 54 |
+
|
| 55 |
+
try:
|
| 56 |
+
with open(filepath, "r") as f:
|
| 57 |
+
chat_data = json.load(f)
|
| 58 |
+
|
| 59 |
+
# Load associated image if it exists
|
| 60 |
+
image_data = None
|
| 61 |
+
if chat_data.get('metadata', {}).get('image_file'):
|
| 62 |
+
image_path = CHAT_IMAGES_DIR / chat_data['metadata']['image_file']
|
| 63 |
+
if image_path.exists():
|
| 64 |
+
with open(image_path, "rb") as f:
|
| 65 |
+
image_data = f.read()
|
| 66 |
+
|
| 67 |
+
return chat_data, image_data
|
| 68 |
+
except Exception as e:
|
| 69 |
+
print(f"Error loading chat history: {e}")
|
| 70 |
+
return None, None
|