Update app.py
Browse files
app.py
CHANGED
|
@@ -18,11 +18,14 @@ templates = Jinja2Templates(directory="templates")
|
|
| 18 |
|
| 19 |
# --- Data Storage ---
|
| 20 |
# Define the path for our temporary data storage
|
| 21 |
-
|
|
|
|
| 22 |
CHAT_FILE = TMP_DIR / "chat_history.json"
|
| 23 |
|
| 24 |
-
#
|
| 25 |
-
|
|
|
|
|
|
|
| 26 |
|
| 27 |
# --- Helper Functions ---
|
| 28 |
def load_chat_history():
|
|
@@ -30,7 +33,11 @@ def load_chat_history():
|
|
| 30 |
if CHAT_FILE.exists():
|
| 31 |
with open(CHAT_FILE, "r") as f:
|
| 32 |
try:
|
| 33 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 34 |
except json.JSONDecodeError:
|
| 35 |
return []
|
| 36 |
return []
|
|
@@ -88,4 +95,5 @@ def post_chat_message(message: str = Form(...)):
|
|
| 88 |
save_chat_history(chat_history)
|
| 89 |
|
| 90 |
# Redirect back to the main page to see the updated chat
|
| 91 |
-
return RedirectResponse(url="/", status_code=303)
|
|
|
|
|
|
| 18 |
|
| 19 |
# --- Data Storage ---
|
| 20 |
# Define the path for our temporary data storage
|
| 21 |
+
# CHANGE: Use the system's writable /tmp directory instead of a local one.
|
| 22 |
+
TMP_DIR = Path("/tmp")
|
| 23 |
CHAT_FILE = TMP_DIR / "chat_history.json"
|
| 24 |
|
| 25 |
+
# We don't need to create /tmp, the system provides it.
|
| 26 |
+
# We can ensure the file exists, or let the save function create it.
|
| 27 |
+
if not CHAT_FILE.exists():
|
| 28 |
+
CHAT_FILE.touch() # Create an empty file if it doesn't exist
|
| 29 |
|
| 30 |
# --- Helper Functions ---
|
| 31 |
def load_chat_history():
|
|
|
|
| 33 |
if CHAT_FILE.exists():
|
| 34 |
with open(CHAT_FILE, "r") as f:
|
| 35 |
try:
|
| 36 |
+
# Handle case where file is empty
|
| 37 |
+
content = f.read()
|
| 38 |
+
if not content:
|
| 39 |
+
return []
|
| 40 |
+
return json.loads(content)
|
| 41 |
except json.JSONDecodeError:
|
| 42 |
return []
|
| 43 |
return []
|
|
|
|
| 95 |
save_chat_history(chat_history)
|
| 96 |
|
| 97 |
# Redirect back to the main page to see the updated chat
|
| 98 |
+
return RedirectResponse(url="/", status_code=303)
|
| 99 |
+
|