dan92 commited on
Commit
7a19dbb
·
verified ·
1 Parent(s): 3ad739c

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +24 -149
app.py CHANGED
@@ -11,15 +11,13 @@ from functools import lru_cache, wraps
11
  from typing import Dict, Any, Callable, List, Tuple
12
  import requests
13
  import tiktoken
14
- from flask import Flask, Response, jsonify, request, stream_with_context, render_template
15
  from flask_cors import CORS
16
  from requests.adapters import HTTPAdapter
17
  from urllib3.util.connection import create_connection
18
  import urllib3
19
  from cachetools import TTLCache
20
  import threading
21
- from datetime import datetime
22
- from werkzeug.exceptions import HTTPException
23
 
24
  # 新增导入
25
  import register_bot
@@ -57,7 +55,7 @@ if not NOTDIAMOND_IP:
57
  logger.error("NOTDIAMOND_IP environment variable is not set!")
58
  raise ValueError("NOTDIAMOND_IP must be set")
59
 
60
- # API钥验证装饰器
61
  def require_api_key(f):
62
  @wraps(f)
63
  def decorated_function(*args, **kwargs):
@@ -120,10 +118,6 @@ class AuthManager:
120
  self._session: requests.Session = create_custom_session()
121
  self._logger: logging.Logger = logging.getLogger(__name__)
122
  self.model_status = {model: True for model in MODEL_INFO.keys()}
123
- self.total_requests = 0
124
- self.success_requests = 0
125
- self.failed_requests = 0
126
- self.last_used_time = None
127
 
128
  def login(self) -> bool:
129
  """使用电子邮件和密码进行用户登录,并获取用户信息。"""
@@ -258,14 +252,6 @@ class AuthManager:
258
  def reset_model_status(self):
259
  self.model_status = {model: True for model in MODEL_INFO.keys()}
260
 
261
- def record_request(self, success: bool):
262
- self.total_requests += 1
263
- if success:
264
- self.success_requests += 1
265
- else:
266
- self.failed_requests += 1
267
- self.last_used_time = datetime.now()
268
-
269
  class MultiAuthManager:
270
  def __init__(self, credentials):
271
  self.auth_managers = [AuthManager(email, password) for email, password in credentials]
@@ -491,7 +477,7 @@ def get_auth_credentials():
491
  if data.get('status') == 'success' and data.get('content'):
492
  content = data['content']
493
  credentials = []
494
- # 分割多个凭据(如果的话)
495
  for cred in content.split(';'):
496
  if '|' in cred:
497
  email, password = cred.strip().split('|')
@@ -535,109 +521,29 @@ def before_request():
535
  else:
536
  multi_auth_manager = None
537
 
538
- def get_accounts_status():
539
- """获取所有账号的状态信息"""
540
- if not multi_auth_manager:
541
- return []
542
-
543
- accounts_status = []
544
- for auth_manager in multi_auth_manager.auth_managers:
545
- account_info = {
546
- "email": auth_manager._email,
547
- "is_valid": auth_manager.is_token_valid(),
548
- "token_expiry": datetime.fromtimestamp(auth_manager._token_expiry).strftime('%Y-%m-%d %H:%M:%S'),
549
- "models_status": {
550
- model: status
551
- for model, status in auth_manager.model_status.items()
552
- }
553
- }
554
- accounts_status.append(account_info)
555
- return accounts_status
556
-
557
  @app.route('/', methods=['GET'])
558
  def root():
559
- try:
560
- accounts_status = get_accounts_status()
561
-
562
- if request.headers.get('Accept') == 'application/json':
563
- return get_json_status(accounts_status)
564
-
565
- template_data = get_template_data(accounts_status)
566
- return render_template('monitor.html', **template_data)
567
- except Exception as e:
568
- logger.error(f"Error in root route: {str(e)}", exc_info=True)
569
- if request.headers.get('Accept') == 'application/json':
570
- return jsonify({
571
- "error": "Internal Server Error",
572
- "message": str(e)
573
- }), 500
574
- # 对于 HTML 请求,返回一个简单的错误页面
575
- error_html = """
576
- <html>
577
- <head><title>Error</title></head>
578
- <body>
579
- <h1>Internal Server Error</h1>
580
- <p>An error occurred while processing your request.</p>
581
- <p>Error details: {}</p>
582
- <p><a href="javascript:location.reload()">Retry</a></p>
583
- </body>
584
- </html>
585
- """.format(str(e) if app.debug else "Please try again later")
586
- return error_html, 500
587
-
588
- def get_template_data(accounts_status):
589
- try:
590
- if not multi_auth_manager or not multi_auth_manager.auth_managers:
591
- logger.warning("No auth managers available")
592
- return {
593
- "total_accounts": 0,
594
- "valid_accounts": 0,
595
- "total_requests": 0,
596
- "accounts": [],
597
- "last_update": datetime.now().strftime('%Y-%m-%d %H:%M:%S')
598
  }
599
-
600
- total_accounts = len(accounts_status)
601
- valid_accounts = sum(1 for acc in accounts_status if acc["is_valid"])
602
-
603
- accounts_data = []
604
- total_requests = 0
605
-
606
- for auth_manager in multi_auth_manager.auth_managers:
607
- try:
608
- success_rate = 0
609
- if hasattr(auth_manager, 'total_requests') and auth_manager.total_requests > 0:
610
- success_rate = (auth_manager.success_requests / auth_manager.total_requests) * 100
611
-
612
- account_info = {
613
- "email": auth_manager._email,
614
- "is_valid": auth_manager.is_token_valid(),
615
- "total_requests": getattr(auth_manager, 'total_requests', 0),
616
- "success_requests": getattr(auth_manager, 'success_requests', 0),
617
- "failed_requests": getattr(auth_manager, 'failed_requests', 0),
618
- "success_rate": success_rate,
619
- "last_used_time": auth_manager.last_used_time.strftime('%m/%d/%Y, %I:%M:%S %p') if hasattr(auth_manager, 'last_used_time') and auth_manager.last_used_time else "从未使用"
620
- }
621
- accounts_data.append(account_info)
622
- total_requests += getattr(auth_manager, 'total_requests', 0)
623
- except Exception as e:
624
- logger.error(f"Error processing account {auth_manager._email}: {str(e)}", exc_info=True)
625
- continue
626
-
627
- return {
628
- "total_accounts": total_accounts,
629
- "valid_accounts": valid_accounts,
630
- "total_requests": total_requests,
631
- "accounts": accounts_data,
632
- "last_update": datetime.now().strftime('%Y-%m-%d %H:%M:%S')
633
- }
634
- except Exception as e:
635
- logger.error(f"Error in get_template_data: {str(e)}", exc_info=True)
636
- raise
637
-
638
- def get_json_status(accounts_status):
639
- template_data = get_template_data(accounts_status)
640
- return jsonify(template_data)
641
 
642
  @app.route('/ai/v1/models', methods=['GET'])
643
  def proxy_models():
@@ -815,9 +721,6 @@ def make_request(payload, auth_manager, model_id):
815
  stream=True
816
  ).result()
817
 
818
- success = response.status_code == 200
819
- auth_manager.record_request(success)
820
-
821
  if response.status_code == 200 and response.headers.get('Content-Type') == 'text/event-stream':
822
  return response
823
 
@@ -840,7 +743,6 @@ def make_request(payload, auth_manager, model_id):
840
  logger.error(f"Request failed with status {response.status_code} for account {auth_manager._email}")
841
 
842
  except Exception as e:
843
- auth_manager.record_request(False)
844
  logger.error(f"Request attempt {attempt + 1} failed for account {auth_manager._email}: {e}")
845
  if attempt < max_retries - 1:
846
  time.sleep(retry_delay)
@@ -880,31 +782,4 @@ if __name__ == "__main__":
880
  health_check_thread.start()
881
 
882
  port = int(os.environ.get("PORT", 3000))
883
- app.run(debug=False, host='0.0.0.0', port=port, threaded=True)
884
-
885
- # 添加错误处理器
886
- @app.errorhandler(Exception)
887
- def handle_exception(e):
888
- """处理所有异常"""
889
- logger.error(f"Unhandled exception: {str(e)}", exc_info=True)
890
-
891
- # 如果是 HTTP 异常,返回其状态码
892
- if isinstance(e, HTTPException):
893
- return jsonify({
894
- "error": {
895
- "type": "http_error",
896
- "code": e.code,
897
- "name": e.name,
898
- "description": e.description
899
- }
900
- }), e.code
901
-
902
- # 其他异常返回 500
903
- return jsonify({
904
- "error": {
905
- "type": "server_error",
906
- "message": "Internal Server Error",
907
- "details": str(e) if app.debug else "An unexpected error occurred"
908
- }
909
- }), 500
910
-
 
11
  from typing import Dict, Any, Callable, List, Tuple
12
  import requests
13
  import tiktoken
14
+ from flask import Flask, Response, jsonify, request, stream_with_context
15
  from flask_cors import CORS
16
  from requests.adapters import HTTPAdapter
17
  from urllib3.util.connection import create_connection
18
  import urllib3
19
  from cachetools import TTLCache
20
  import threading
 
 
21
 
22
  # 新增导入
23
  import register_bot
 
55
  logger.error("NOTDIAMOND_IP environment variable is not set!")
56
  raise ValueError("NOTDIAMOND_IP must be set")
57
 
58
+ # API密钥验证装饰器
59
  def require_api_key(f):
60
  @wraps(f)
61
  def decorated_function(*args, **kwargs):
 
118
  self._session: requests.Session = create_custom_session()
119
  self._logger: logging.Logger = logging.getLogger(__name__)
120
  self.model_status = {model: True for model in MODEL_INFO.keys()}
 
 
 
 
121
 
122
  def login(self) -> bool:
123
  """使用电子邮件和密码进行用户登录,并获取用户信息。"""
 
252
  def reset_model_status(self):
253
  self.model_status = {model: True for model in MODEL_INFO.keys()}
254
 
 
 
 
 
 
 
 
 
255
  class MultiAuthManager:
256
  def __init__(self, credentials):
257
  self.auth_managers = [AuthManager(email, password) for email, password in credentials]
 
477
  if data.get('status') == 'success' and data.get('content'):
478
  content = data['content']
479
  credentials = []
480
+ # 分割多个凭据(如果有的话)
481
  for cred in content.split(';'):
482
  if '|' in cred:
483
  email, password = cred.strip().split('|')
 
521
  else:
522
  multi_auth_manager = None
523
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
524
  @app.route('/', methods=['GET'])
525
  def root():
526
+ return jsonify({
527
+ "service": "AI Chat Completion Proxy",
528
+ "usage": {
529
+ "endpoint": "/ai/v1/chat/completions",
530
+ "method": "POST",
531
+ "headers": {
532
+ "Authorization": "Bearer YOUR_API_KEY"
533
+ },
534
+ "body": {
535
+ "model": "One of: " + ", ".join(MODEL_INFO.keys()),
536
+ "messages": [
537
+ {"role": "system", "content": "You are a helpful assistant."},
538
+ {"role": "user", "content": "Hello, who are you?"}
539
+ ],
540
+ "stream": False,
541
+ "temperature": 0.7
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
542
  }
543
+ },
544
+ "availableModels": list(MODEL_INFO.keys()),
545
+ "note": "API key authentication is required for other endpoints."
546
+ })
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
547
 
548
  @app.route('/ai/v1/models', methods=['GET'])
549
  def proxy_models():
 
721
  stream=True
722
  ).result()
723
 
 
 
 
724
  if response.status_code == 200 and response.headers.get('Content-Type') == 'text/event-stream':
725
  return response
726
 
 
743
  logger.error(f"Request failed with status {response.status_code} for account {auth_manager._email}")
744
 
745
  except Exception as e:
 
746
  logger.error(f"Request attempt {attempt + 1} failed for account {auth_manager._email}: {e}")
747
  if attempt < max_retries - 1:
748
  time.sleep(retry_delay)
 
782
  health_check_thread.start()
783
 
784
  port = int(os.environ.get("PORT", 3000))
785
+ app.run(debug=False, host='0.0.0.0', port=port, threaded=True)