Pepguy commited on
Commit
a42a99c
·
verified ·
1 Parent(s): 447ef90

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +64 -18
app.py CHANGED
@@ -15,6 +15,7 @@ app = Flask(__name__)
15
  LAMBDA_URL = os.getenv("LAMBDA_URL", "https://your-lambda-function-url")
16
  GEMINI_KEY = os.getenv("GEMINI_API_KEY", "")
17
  FLUSH_INTERVAL = 30 # seconds between DB backups per user
 
18
 
19
  client = genai.Client(api_key=GEMINI_KEY)
20
  user_memory = {} # { user_id: { "history": [], "last_sync": timestamp } }
@@ -26,10 +27,12 @@ def flush_loop():
26
  for uid, data in list(user_memory.items()):
27
  if now - data.get("last_sync", 0) >= FLUSH_INTERVAL and data["history"]:
28
  try:
29
- payload = {"user_id": uid, "history": data["history"]}
 
 
30
  requests.post(LAMBDA_URL, json=payload, timeout=5)
31
  user_memory[uid]["last_sync"] = now
32
- app.logger.info(f"Synced memory for {uid}")
33
  except Exception as e:
34
  app.logger.warning(f"Failed sync for {uid}: {e}")
35
  time.sleep(5)
@@ -86,24 +89,51 @@ HTML = """
86
  </body></html>
87
  """
88
 
89
- # --- Gemini Generation ---
90
- def generate_from_gemini(prompt, image_bytes=None):
91
  start_time = time.time()
92
- parts = []
93
- if prompt: parts.append(types.Part.from_text(text=prompt))
94
- if image_bytes: parts.append(types.Part.from_bytes(data=image_bytes, mime_type="image/jpeg"))
95
-
96
- contents = [types.Content(role="user", parts=parts)]
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
97
  cfg = types.GenerateContentConfig(response_mime_type="text/plain")
98
 
99
  model_start = time.time()
100
- res = client.models.generate_content(model="gemini-2.5-flash-lite", contents=contents, config=cfg)
 
 
 
 
101
  model_end = time.time()
102
 
103
- return {"text": res.text, "timing": {
104
- "total_ms": int((time.time() - start_time)*1000),
105
- "model_ms": int((model_end - model_start)*1000)
106
- }}
 
 
 
107
 
108
  # --- History Management ---
109
  def get_user_history(uid):
@@ -111,8 +141,13 @@ def get_user_history(uid):
111
  try:
112
  resp = requests.get(f"{LAMBDA_URL}?user_id={uid}", timeout=5)
113
  resp.raise_for_status()
114
- user_memory[uid] = {"history": resp.json().get("history", []), "last_sync": 0}
115
- app.logger.info(f"Loaded history for {uid}")
 
 
 
 
 
116
  except Exception as e:
117
  app.logger.warning(f"Failed to load history for {uid}: {e}")
118
  user_memory[uid] = {"history": [], "last_sync": 0}
@@ -121,6 +156,11 @@ def get_user_history(uid):
121
  def update_user_history(uid, prompt, response):
122
  entry = {"prompt": prompt, "response": response, "timestamp": time.time()}
123
  user_memory.setdefault(uid, {"history": [], "last_sync": 0})["history"].append(entry)
 
 
 
 
 
124
 
125
  # --- Routes ---
126
  @app.route("/")
@@ -140,9 +180,15 @@ def gen():
140
  return jsonify({"error": "No prompt or image provided"}), 400
141
 
142
  try:
143
- _ = get_user_history(uid)
144
- result = generate_from_gemini(prompt, img_bytes)
 
 
 
 
 
145
  update_user_history(uid, prompt, result["text"])
 
146
  return jsonify({"result": result["text"], "timing": result["timing"]})
147
  except Exception as e:
148
  app.logger.exception("Generation failed")
 
15
  LAMBDA_URL = os.getenv("LAMBDA_URL", "https://your-lambda-function-url")
16
  GEMINI_KEY = os.getenv("GEMINI_API_KEY", "")
17
  FLUSH_INTERVAL = 30 # seconds between DB backups per user
18
+ MAX_HISTORY_TURNS = 10 # Maximum conversation turns to keep in context
19
 
20
  client = genai.Client(api_key=GEMINI_KEY)
21
  user_memory = {} # { user_id: { "history": [], "last_sync": timestamp } }
 
27
  for uid, data in list(user_memory.items()):
28
  if now - data.get("last_sync", 0) >= FLUSH_INTERVAL and data["history"]:
29
  try:
30
+ # Only sync the most recent MAX_HISTORY_TURNS entries
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)
 
89
  </body></html>
90
  """
91
 
92
+ # --- Gemini Generation with History ---
93
+ def generate_from_gemini(prompt, image_bytes=None, history=None):
94
  start_time = time.time()
95
+
96
+ # Build contents list with history
97
+ contents = []
98
+
99
+ # Add historical messages (limit to recent turns to avoid token limits)
100
+ if history:
101
+ recent_history = history[-MAX_HISTORY_TURNS:]
102
+ for entry in recent_history:
103
+ # Add user message
104
+ user_parts = [types.Part.from_text(text=entry["prompt"])]
105
+ contents.append(types.Content(role="user", parts=user_parts))
106
+
107
+ # Add model response
108
+ model_parts = [types.Part.from_text(text=entry["response"])]
109
+ contents.append(types.Content(role="model", parts=model_parts))
110
+
111
+ # Add current user message
112
+ current_parts = []
113
+ if prompt:
114
+ current_parts.append(types.Part.from_text(text=prompt))
115
+ if image_bytes:
116
+ current_parts.append(types.Part.from_bytes(data=image_bytes, mime_type="image/jpeg"))
117
+
118
+ contents.append(types.Content(role="user", parts=current_parts))
119
+
120
  cfg = types.GenerateContentConfig(response_mime_type="text/plain")
121
 
122
  model_start = time.time()
123
+ res = client.models.generate_content(
124
+ model="gemini-2.5-flash-lite",
125
+ contents=contents,
126
+ config=cfg
127
+ )
128
  model_end = time.time()
129
 
130
+ return {
131
+ "text": res.text,
132
+ "timing": {
133
+ "total_ms": int((time.time() - start_time)*1000),
134
+ "model_ms": int((model_end - model_start)*1000)
135
+ }
136
+ }
137
 
138
  # --- History Management ---
139
  def get_user_history(uid):
 
141
  try:
142
  resp = requests.get(f"{LAMBDA_URL}?user_id={uid}", timeout=5)
143
  resp.raise_for_status()
144
+ loaded_history = resp.json().get("history", [])
145
+ # Only keep the most recent MAX_HISTORY_TURNS when loading
146
+ user_memory[uid] = {
147
+ "history": loaded_history[-MAX_HISTORY_TURNS:],
148
+ "last_sync": 0
149
+ }
150
+ app.logger.info(f"Loaded history for {uid} ({len(user_memory[uid]['history'])} turns)")
151
  except Exception as e:
152
  app.logger.warning(f"Failed to load history for {uid}: {e}")
153
  user_memory[uid] = {"history": [], "last_sync": 0}
 
156
  def update_user_history(uid, prompt, response):
157
  entry = {"prompt": prompt, "response": response, "timestamp": time.time()}
158
  user_memory.setdefault(uid, {"history": [], "last_sync": 0})["history"].append(entry)
159
+
160
+ # Trim history to MAX_HISTORY_TURNS to prevent unbounded growth
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("/")
 
180
  return jsonify({"error": "No prompt or image provided"}), 400
181
 
182
  try:
183
+ # Load user's conversation history
184
+ history = get_user_history(uid)
185
+
186
+ # Generate response with history context
187
+ result = generate_from_gemini(prompt, img_bytes, history=history)
188
+
189
+ # Update history with new exchange
190
  update_user_history(uid, prompt, result["text"])
191
+
192
  return jsonify({"result": result["text"], "timing": result["timing"]})
193
  except Exception as e:
194
  app.logger.exception("Generation failed")