Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
@@ -16,6 +16,7 @@ from flask_cors import CORS
|
|
16 |
from typing import Dict, Any
|
17 |
from requests.adapters import HTTPAdapter
|
18 |
from urllib3.util.connection import create_connection
|
|
|
19 |
|
20 |
# Constants
|
21 |
CHAT_COMPLETION_CHUNK = 'chat.completion.chunk'
|
@@ -33,14 +34,23 @@ proxy_url = os.getenv('PROXY_URL')
|
|
33 |
|
34 |
# 获取环境变量中指定的 IP 地址
|
35 |
NOTDIAMOND_IP = os.getenv('NOTDIAMOND_IP')
|
|
|
|
|
|
|
|
|
|
|
36 |
|
37 |
# 自定义连接函数
|
38 |
def patched_create_connection(address, *args, **kwargs):
|
39 |
host, port = address
|
40 |
-
if host ==
|
|
|
41 |
return create_connection((NOTDIAMOND_IP, port), *args, **kwargs)
|
42 |
return create_connection(address, *args, **kwargs)
|
43 |
|
|
|
|
|
|
|
44 |
# 自定义 HTTPAdapter
|
45 |
class CustomHTTPAdapter(HTTPAdapter):
|
46 |
def init_poolmanager(self, *args, **kwargs):
|
@@ -471,20 +481,38 @@ def make_request(payload):
|
|
471 |
url = get_notdiamond_url()
|
472 |
headers = get_notdiamond_headers()
|
473 |
session = create_custom_session()
|
474 |
-
|
475 |
-
|
476 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
477 |
|
|
|
478 |
auth_manager.refresh_user_token()
|
479 |
headers = get_notdiamond_headers()
|
480 |
-
|
481 |
-
|
482 |
-
|
|
|
|
|
|
|
|
|
|
|
483 |
|
|
|
484 |
auth_manager.login()
|
485 |
headers = get_notdiamond_headers()
|
486 |
-
|
487 |
-
|
|
|
|
|
|
|
|
|
|
|
488 |
|
489 |
if __name__ == "__main__":
|
490 |
port = int(os.environ.get("PORT", 3000))
|
|
|
16 |
from typing import Dict, Any
|
17 |
from requests.adapters import HTTPAdapter
|
18 |
from urllib3.util.connection import create_connection
|
19 |
+
import urllib3
|
20 |
|
21 |
# Constants
|
22 |
CHAT_COMPLETION_CHUNK = 'chat.completion.chunk'
|
|
|
34 |
|
35 |
# 获取环境变量中指定的 IP 地址
|
36 |
NOTDIAMOND_IP = os.getenv('NOTDIAMOND_IP')
|
37 |
+
NOTDIAMOND_DOMAIN = 'not-diamond-workers.t7-cc4.workers.dev'
|
38 |
+
|
39 |
+
if not NOTDIAMOND_IP:
|
40 |
+
logger.error("NOTDIAMOND_IP environment variable is not set!")
|
41 |
+
raise ValueError("NOTDIAMOND_IP must be set")
|
42 |
|
43 |
# 自定义连接函数
|
44 |
def patched_create_connection(address, *args, **kwargs):
|
45 |
host, port = address
|
46 |
+
if host == NOTDIAMOND_DOMAIN:
|
47 |
+
logger.info(f"Connecting to {NOTDIAMOND_DOMAIN} using IP: {NOTDIAMOND_IP}")
|
48 |
return create_connection((NOTDIAMOND_IP, port), *args, **kwargs)
|
49 |
return create_connection(address, *args, **kwargs)
|
50 |
|
51 |
+
# 替换 urllib3 的默认连接函数
|
52 |
+
urllib3.util.connection.create_connection = patched_create_connection
|
53 |
+
|
54 |
# 自定义 HTTPAdapter
|
55 |
class CustomHTTPAdapter(HTTPAdapter):
|
56 |
def init_poolmanager(self, *args, **kwargs):
|
|
|
481 |
url = get_notdiamond_url()
|
482 |
headers = get_notdiamond_headers()
|
483 |
session = create_custom_session()
|
484 |
+
logger.info(f"Sending request to URL: {url}")
|
485 |
+
try:
|
486 |
+
response = executor.submit(session.post, url, headers=headers, json=payload, stream=True).result()
|
487 |
+
logger.info(f"Response status code: {response.status_code}")
|
488 |
+
if response.status_code == 200 and response.headers.get('Content-Type') == 'text/event-stream':
|
489 |
+
return response
|
490 |
+
except Exception as e:
|
491 |
+
logger.error(f"Error in make_request: {str(e)}")
|
492 |
+
raise
|
493 |
|
494 |
+
logger.info("Refreshing user token...")
|
495 |
auth_manager.refresh_user_token()
|
496 |
headers = get_notdiamond_headers()
|
497 |
+
try:
|
498 |
+
response = executor.submit(session.post, url, headers=headers, json=payload, stream=True).result()
|
499 |
+
logger.info(f"Response status code after token refresh: {response.status_code}")
|
500 |
+
if response.status_code == 200 and response.headers.get('Content-Type') == 'text/event-stream':
|
501 |
+
return response
|
502 |
+
except Exception as e:
|
503 |
+
logger.error(f"Error in make_request after token refresh: {str(e)}")
|
504 |
+
raise
|
505 |
|
506 |
+
logger.info("Logging in again...")
|
507 |
auth_manager.login()
|
508 |
headers = get_notdiamond_headers()
|
509 |
+
try:
|
510 |
+
response = executor.submit(session.post, url, headers=headers, json=payload, stream=True).result()
|
511 |
+
logger.info(f"Response status code after login: {response.status_code}")
|
512 |
+
return response
|
513 |
+
except Exception as e:
|
514 |
+
logger.error(f"Error in make_request after login: {str(e)}")
|
515 |
+
raise
|
516 |
|
517 |
if __name__ == "__main__":
|
518 |
port = int(os.environ.get("PORT", 3000))
|