Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -20,24 +20,42 @@ MAX_HISTORY_TURNS = 10 # Maximum conversation turns to keep in context
|
|
| 20 |
client = genai.Client(api_key=GEMINI_KEY)
|
| 21 |
user_memory = {} # { user_id: { "history": [], "last_sync": timestamp } }
|
| 22 |
|
| 23 |
-
# ---
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 24 |
def flush_loop():
|
| 25 |
while True:
|
| 26 |
now = time.time()
|
| 27 |
for uid, data in list(user_memory.items()):
|
| 28 |
-
if now - data.get("last_sync", 0) >= FLUSH_INTERVAL and data
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
history_to_sync = data["history"][-MAX_HISTORY_TURNS:]
|
| 32 |
-
payload = {"user_id": uid, "history": history_to_sync}
|
| 33 |
-
requests.post(LAMBDA_URL, json=payload, timeout=5)
|
| 34 |
-
user_memory[uid]["last_sync"] = now
|
| 35 |
-
app.logger.info(f"Synced memory for {uid} ({len(history_to_sync)} turns)")
|
| 36 |
-
except Exception as e:
|
| 37 |
-
app.logger.warning(f"Failed sync for {uid}: {e}")
|
| 38 |
time.sleep(5)
|
| 39 |
|
| 40 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 41 |
|
| 42 |
# --- HTML Frontend ---
|
| 43 |
HTML = """
|
|
@@ -161,6 +179,9 @@ def update_user_history(uid, prompt, response):
|
|
| 161 |
if len(user_memory[uid]["history"]) > MAX_HISTORY_TURNS:
|
| 162 |
user_memory[uid]["history"] = user_memory[uid]["history"][-MAX_HISTORY_TURNS:]
|
| 163 |
app.logger.debug(f"Trimmed history for {uid} to {MAX_HISTORY_TURNS} turns")
|
|
|
|
|
|
|
|
|
|
| 164 |
|
| 165 |
# --- Routes ---
|
| 166 |
@app.route("/")
|
|
@@ -194,6 +215,10 @@ def gen():
|
|
| 194 |
app.logger.exception("Generation failed")
|
| 195 |
return jsonify({"error": str(e)}), 500
|
| 196 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 197 |
|
| 198 |
if __name__ == "__main__":
|
| 199 |
port = int(os.getenv("PORT", 7860))
|
|
|
|
| 20 |
client = genai.Client(api_key=GEMINI_KEY)
|
| 21 |
user_memory = {} # { user_id: { "history": [], "last_sync": timestamp } }
|
| 22 |
|
| 23 |
+
# --- Helper: sync single user's history (added) ---
|
| 24 |
+
def sync_user(uid):
|
| 25 |
+
"""
|
| 26 |
+
Attempt to sync a single user's most recent history to LAMBDA_URL.
|
| 27 |
+
Updates user_memory[uid]['last_sync'] on success and logs results.
|
| 28 |
+
This runs in its own thread when called from update_user_history and in the flush loop.
|
| 29 |
+
"""
|
| 30 |
+
try:
|
| 31 |
+
data = user_memory.get(uid)
|
| 32 |
+
if not data:
|
| 33 |
+
return
|
| 34 |
+
history_to_sync = data["history"][-MAX_HISTORY_TURNS:]
|
| 35 |
+
payload = {"user_id": uid, "history": history_to_sync}
|
| 36 |
+
resp = requests.post(LAMBDA_URL, json=payload, timeout=5)
|
| 37 |
+
resp.raise_for_status()
|
| 38 |
+
user_memory[uid]["last_sync"] = time.time()
|
| 39 |
+
app.logger.info(f"Synced memory for {uid} ({len(history_to_sync)} turns)")
|
| 40 |
+
except Exception as e:
|
| 41 |
+
app.logger.warning(f"Failed sync for {uid}: {e}")
|
| 42 |
+
|
| 43 |
+
# --- Background thread for periodic flush (uses helper) ---
|
| 44 |
def flush_loop():
|
| 45 |
while True:
|
| 46 |
now = time.time()
|
| 47 |
for uid, data in list(user_memory.items()):
|
| 48 |
+
if now - data.get("last_sync", 0) >= FLUSH_INTERVAL and data.get("history"):
|
| 49 |
+
# Offload the actual network call to a short-lived thread so the loop isn't blocked.
|
| 50 |
+
threading.Thread(target=sync_user, args=(uid,), daemon=True).start()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 51 |
time.sleep(5)
|
| 52 |
|
| 53 |
+
# NOTE: don't start the thread at module import time to avoid issues with Flask reloader.
|
| 54 |
+
# We'll start it once when the app starts serving requests.
|
| 55 |
+
def start_flush_thread_once():
|
| 56 |
+
t = threading.Thread(target=flush_loop, daemon=True)
|
| 57 |
+
t.start()
|
| 58 |
+
app.logger.info("Started flush background thread")
|
| 59 |
|
| 60 |
# --- HTML Frontend ---
|
| 61 |
HTML = """
|
|
|
|
| 179 |
if len(user_memory[uid]["history"]) > MAX_HISTORY_TURNS:
|
| 180 |
user_memory[uid]["history"] = user_memory[uid]["history"][-MAX_HISTORY_TURNS:]
|
| 181 |
app.logger.debug(f"Trimmed history for {uid} to {MAX_HISTORY_TURNS} turns")
|
| 182 |
+
|
| 183 |
+
# Attempt immediate async sync so you can see logs quickly (added)
|
| 184 |
+
threading.Thread(target=sync_user, args=(uid,), daemon=True).start()
|
| 185 |
|
| 186 |
# --- Routes ---
|
| 187 |
@app.route("/")
|
|
|
|
| 215 |
app.logger.exception("Generation failed")
|
| 216 |
return jsonify({"error": str(e)}), 500
|
| 217 |
|
| 218 |
+
# Ensure background thread starts when app begins serving requests
|
| 219 |
+
@app.before_first_request
|
| 220 |
+
def _start_background_tasks():
|
| 221 |
+
start_flush_thread_once()
|
| 222 |
|
| 223 |
if __name__ == "__main__":
|
| 224 |
port = int(os.getenv("PORT", 7860))
|