smgc commited on
Commit
e67a674
1 Parent(s): 01de76f

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +13 -69
app.py CHANGED
@@ -9,7 +9,6 @@ from flask import Flask, request, Response, stream_with_context, jsonify
9
  from flask_cors import CORS
10
  from functools import lru_cache
11
  from concurrent.futures import ThreadPoolExecutor
12
- import base64
13
 
14
  app = Flask(__name__)
15
  CORS(app, resources={r"/*": {"origins": "*"}})
@@ -42,6 +41,18 @@ NOTDIAMOND_URLS = [
42
  def get_notdiamond_url():
43
  return random.choice(NOTDIAMOND_URLS)
44
 
 
 
 
 
 
 
 
 
 
 
 
 
45
  # 模型信息字典,包含各模型的提供商和映射
46
  MODEL_INFO = {
47
  "gpt-4-turbo-2024-04-09": {
@@ -214,54 +225,6 @@ def proxy_models():
214
  "data": models
215
  })
216
 
217
- # 缓存cookie和过期时间
218
- cached_cookie = None
219
- expires_at = 0
220
-
221
- # 获取cookie
222
- def get_cookie(email, password):
223
- global cached_cookie, expires_at
224
-
225
- current_time = int(time.time())
226
- if cached_cookie and expires_at > current_time:
227
- return cached_cookie
228
-
229
- auth_url = "https://spuckhogycrxcbomznwo.supabase.co/auth/v1/token?grant_type=password"
230
- auth_headers = {
231
- 'accept': '*/*',
232
- 'accept-language': 'zh-CN,zh;q=0.9',
233
- 'apikey': 'eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpc3MiOiJzdXBhYmFzZSIsInJlZiI6InNwdWNraG9neWNyeGNib216bndvIiwicm9sZSI6ImFub24iLCJpYXQiOjE3MDcyNDYwMzksImV4cCI6MjAyMjgyMjAzOX0.tvlGT7NZY8bijMjNIu1WhAtPnSKuDeYhtveo4DRt6xg',
234
- 'authorization': 'Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpc3MiOiJzdXBhYmFzZSIsInJlZiI6InNwdWNraG9neWNyeGNib216bndvIiwicm9sZSI6ImFub24iLCJpYXQiOjE3MDcyNDYwMzksImV4cCI6MjAyMjgyMjAzOX0.tvlGT7NZY8bijMjNIu1WhAtPnSKuDeYhtveo4DRt6xg',
235
- 'content-type': 'application/json;charset=UTF-8',
236
- 'origin': 'https://chat.notdiamond.ai',
237
- 'priority': 'u=1, i',
238
- 'referer': 'https://chat.notdiamond.ai/',
239
- 'sec-ch-ua': '"Chromium";v="128", "Not;A=Brand";v="24", "Google Chrome";v="128"',
240
- 'sec-ch-ua-mobile': '?0',
241
- 'sec-ch-ua-platform': '"Windows"',
242
- 'sec-fetch-dest': 'empty',
243
- 'sec-fetch-mode': 'cors',
244
- 'sec-fetch-site': 'cross-site',
245
- 'user-agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/128.0.0.0 Safari/537.36',
246
- 'x-client-info': 'supabase-ssr/0.4.0',
247
- 'x-supabase-api-version': '2024-01-01'
248
- }
249
- auth_data = {
250
- "email": email,
251
- "password": password,
252
- "gotrue_meta_security": {}
253
- }
254
-
255
- response = requests.post(auth_url, headers=auth_headers, json=auth_data)
256
- response.raise_for_status()
257
- auth_response = response.json()
258
-
259
- expires_at = auth_response['expires_at']
260
- encoded_token = base64.b64encode(json.dumps(auth_response).encode('utf-8')).decode('utf-8')
261
- cached_cookie = f"sb-spuckhogycrxcbomznwo-auth-token=base64-{encoded_token}"
262
-
263
- return cached_cookie
264
-
265
  # 处理请求的API
266
  @app.route('/ai/v1/chat/completions', methods=['POST'])
267
  def handle_request():
@@ -272,17 +235,6 @@ def handle_request():
272
  model = MODEL_INFO.get(model_id, {}).get('mapping', model_id)
273
  stream = request_data.get('stream', False)
274
 
275
- # 从请求头中获取Authorization信息
276
- auth_header = request.headers.get('Authorization')
277
- if not auth_header or not auth_header.startswith('Bearer '):
278
- return jsonify({'error': 'Missing or invalid Authorization header'}), 401
279
-
280
- auth_info = auth_header.split(' ')[1]
281
- email, password = auth_info.split('|')
282
-
283
- # 获取cookie
284
- cookie = get_cookie(email, password)
285
-
286
  payload = {
287
  "messages": messages,
288
  "model": model,
@@ -293,15 +245,7 @@ def handle_request():
293
  "top_p": request_data.get('top_p', 1)
294
  }
295
 
296
- headers = {
297
- 'accept': 'text/event-stream',
298
- 'accept-language': 'zh-CN,zh;q=0.9',
299
- 'content-type': 'application/json',
300
- 'next-action': get_env_or_file('NEXT_ACTION', 'next_action.txt'),
301
- 'user-agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/128.0.0.0 Safari/537.36',
302
- 'cookie': cookie
303
- }
304
-
305
  url = get_notdiamond_url()
306
 
307
  future = executor.submit(requests.post, url, headers=headers, json=[payload], stream=True)
 
9
  from flask_cors import CORS
10
  from functools import lru_cache
11
  from concurrent.futures import ThreadPoolExecutor
 
12
 
13
  app = Flask(__name__)
14
  CORS(app, resources={r"/*": {"origins": "*"}})
 
41
  def get_notdiamond_url():
42
  return random.choice(NOTDIAMOND_URLS)
43
 
44
+ # 使用LRU缓存,缓存头信息,最大容量为1
45
+ @lru_cache(maxsize=1)
46
+ def get_notdiamond_headers():
47
+ return {
48
+ 'accept': 'text/event-stream',
49
+ 'accept-language': 'zh-CN,zh;q=0.9',
50
+ 'content-type': 'application/json',
51
+ 'next-action': get_env_or_file('NEXT_ACTION', 'next_action.txt'),
52
+ 'user-agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/128.0.0.0 Safari/537.36',
53
+ 'cookie': get_env_or_file('COOKIES', 'cookies.txt')
54
+ }
55
+
56
  # 模型信息字典,包含各模型的提供商和映射
57
  MODEL_INFO = {
58
  "gpt-4-turbo-2024-04-09": {
 
225
  "data": models
226
  })
227
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
228
  # 处理请求的API
229
  @app.route('/ai/v1/chat/completions', methods=['POST'])
230
  def handle_request():
 
235
  model = MODEL_INFO.get(model_id, {}).get('mapping', model_id)
236
  stream = request_data.get('stream', False)
237
 
 
 
 
 
 
 
 
 
 
 
 
238
  payload = {
239
  "messages": messages,
240
  "model": model,
 
245
  "top_p": request_data.get('top_p', 1)
246
  }
247
 
248
+ headers = get_notdiamond_headers()
 
 
 
 
 
 
 
 
249
  url = get_notdiamond_url()
250
 
251
  future = executor.submit(requests.post, url, headers=headers, json=[payload], stream=True)