WanIrfan commited on
Commit
bd24f4f
·
verified ·
1 Parent(s): 82597a1

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +22 -2
app.py CHANGED
@@ -32,6 +32,7 @@ VALID_API_KEYS = {
32
  "irfan_test_key_456": "Irfan (self)",
33
  "sistem_gelap": "Gelap"
34
  }
 
35
  # --- 1. NEW HELPER FUNCTIONS TO FIX 'TypeError' ---
36
  def hydrate_history(raw_history_list: list) -> list:
37
  """Converts a list of dicts from session back into LangChain Message objects."""
@@ -380,11 +381,30 @@ def reset_metrics(domain):
380
  return jsonify({"error": str(e)}), 500
381
 
382
  # Helper function to check API key
 
 
383
  def check_api_key(request_data):
384
  api_key = request_data.get("api_key")
 
 
385
  if not api_key or api_key not in VALID_API_KEYS:
386
- return False, {"error": "Invalid or missing API key"}, 401
387
- logger.info(f"API request authenticated for user: {VALID_API_KEYS[api_key]}")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
388
  return True, None, None
389
 
390
  # Helper function to save and process uploaded files (Base64)
 
32
  "irfan_test_key_456": "Irfan (self)",
33
  "sistem_gelap": "Gelap"
34
  }
35
+
36
  # --- 1. NEW HELPER FUNCTIONS TO FIX 'TypeError' ---
37
  def hydrate_history(raw_history_list: list) -> list:
38
  """Converts a list of dicts from session back into LangChain Message objects."""
 
381
  return jsonify({"error": str(e)}), 500
382
 
383
  # Helper function to check API key
384
+ API_USAGE = {}
385
+
386
  def check_api_key(request_data):
387
  api_key = request_data.get("api_key")
388
+
389
+ # 1. Check if key exists
390
  if not api_key or api_key not in VALID_API_KEYS:
391
+ return False, {"error": "Invalid API key"}, 401
392
+
393
+ # 2. Initialize counter for this key if new
394
+ if api_key not in API_USAGE:
395
+ API_USAGE[api_key] = 0
396
+
397
+ # 3. Check Quota (The "Selling" Logic)
398
+ # Example: "public_demo_key_789" only gets 5 tries
399
+ if api_key in ["anak_mail_123", "sistem_gelap"] and API_USAGE[api_key] >= 5:
400
+ return False, {
401
+ "error": "Quota exceeded! Please buy the Premium Plan."
402
+ }, 429
403
+
404
+ # 4. Increment Counter
405
+ API_USAGE[api_key] += 1
406
+ logger.info(f"User {VALID_API_KEYS[api_key]} used {API_USAGE[api_key]} requests.")
407
+
408
  return True, None, None
409
 
410
  # Helper function to save and process uploaded files (Base64)