Spaces:
Runtime error
Runtime error
:gem: [Feature] OSEnver: Auto set proxy for creator and connector
Browse files
conversations/conversation_connector.py
CHANGED
@@ -9,8 +9,9 @@ from networks import (
|
|
9 |
OpenaiStreamOutputer,
|
10 |
)
|
11 |
from utils.logger import logger
|
|
|
12 |
|
13 |
-
|
14 |
|
15 |
|
16 |
class ConversationConnector:
|
@@ -61,7 +62,7 @@ class ConversationConnector:
|
|
61 |
self.wss = await self.aiohttp_session.ws_connect(
|
62 |
self.ws_url,
|
63 |
headers=headers_constructor.request_headers,
|
64 |
-
proxy=http_proxy,
|
65 |
)
|
66 |
await self.init_handshake()
|
67 |
|
|
|
9 |
OpenaiStreamOutputer,
|
10 |
)
|
11 |
from utils.logger import logger
|
12 |
+
from utils.enver import enver
|
13 |
|
14 |
+
enver.set_envs(proxies=True)
|
15 |
|
16 |
|
17 |
class ConversationConnector:
|
|
|
62 |
self.wss = await self.aiohttp_session.ws_connect(
|
63 |
self.ws_url,
|
64 |
headers=headers_constructor.request_headers,
|
65 |
+
proxy=enver.envs["http_proxy"],
|
66 |
)
|
67 |
await self.init_handshake()
|
68 |
|
conversations/conversation_creator.py
CHANGED
@@ -1,8 +1,9 @@
|
|
1 |
import httpx
|
2 |
import json
|
3 |
from pprint import pprint
|
|
|
4 |
|
5 |
-
|
6 |
|
7 |
|
8 |
class ConversationCreator:
|
@@ -23,13 +24,13 @@ class ConversationCreator:
|
|
23 |
"X-Forwarded-For": "65.49.22.66",
|
24 |
}
|
25 |
|
26 |
-
def create(self
|
27 |
self.construct_cookies()
|
28 |
self.construct_headers()
|
29 |
self.response = httpx.get(
|
30 |
self.conversation_create_url,
|
31 |
headers=self.request_headers,
|
32 |
-
proxies=http_proxy
|
33 |
cookies=self.httpx_cookies,
|
34 |
)
|
35 |
self.response_content = json.loads(self.response.content.decode("utf-8"))
|
|
|
1 |
import httpx
|
2 |
import json
|
3 |
from pprint import pprint
|
4 |
+
from utils.enver import enver
|
5 |
|
6 |
+
enver.set_envs(proxies=True)
|
7 |
|
8 |
|
9 |
class ConversationCreator:
|
|
|
24 |
"X-Forwarded-For": "65.49.22.66",
|
25 |
}
|
26 |
|
27 |
+
def create(self):
|
28 |
self.construct_cookies()
|
29 |
self.construct_headers()
|
30 |
self.response = httpx.get(
|
31 |
self.conversation_create_url,
|
32 |
headers=self.request_headers,
|
33 |
+
proxies=enver.envs["http_proxy"],
|
34 |
cookies=self.httpx_cookies,
|
35 |
)
|
36 |
self.response_content = json.loads(self.response.content.decode("utf-8"))
|
utils/enver.py
ADDED
@@ -0,0 +1,54 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import json
|
2 |
+
import os
|
3 |
+
|
4 |
+
from pathlib import Path
|
5 |
+
|
6 |
+
|
7 |
+
class OSEnver:
|
8 |
+
def __init__(self, global_scope=True):
|
9 |
+
self.envs_stack = []
|
10 |
+
self.global_scope = global_scope
|
11 |
+
self.envs = os.environ.copy()
|
12 |
+
|
13 |
+
def store_envs(self):
|
14 |
+
self.envs_stack.append(self.envs)
|
15 |
+
|
16 |
+
def restore_envs(self):
|
17 |
+
self.envs = self.envs_stack.pop()
|
18 |
+
if self.global_scope:
|
19 |
+
os.environ = self.envs
|
20 |
+
|
21 |
+
def set_envs(self, secrets=True, proxies=None, store_envs=True):
|
22 |
+
# caller_info = inspect.stack()[1]
|
23 |
+
# logger.back(f"OS Envs is set by: {caller_info.filename}")
|
24 |
+
|
25 |
+
if store_envs:
|
26 |
+
self.store_envs()
|
27 |
+
|
28 |
+
if secrets:
|
29 |
+
secrets_path = Path(__file__).parents[1] / "secrets.json"
|
30 |
+
if secrets_path.exists():
|
31 |
+
with open(secrets_path, "r") as rf:
|
32 |
+
secrets = json.load(rf)
|
33 |
+
else:
|
34 |
+
secrets = {}
|
35 |
+
|
36 |
+
if proxies:
|
37 |
+
for proxy_env in ["http_proxy", "https_proxy"]:
|
38 |
+
if isinstance(proxies, str):
|
39 |
+
self.envs[proxy_env] = proxies
|
40 |
+
elif "http_proxy" in secrets.keys():
|
41 |
+
self.envs[proxy_env] = secrets["http_proxy"]
|
42 |
+
elif os.getenv("http_proxy"):
|
43 |
+
self.envs[proxy_env] = os.getenv("http_proxy")
|
44 |
+
else:
|
45 |
+
continue
|
46 |
+
print(f"Set {proxy_env} to {self.envs[proxy_env]}")
|
47 |
+
else:
|
48 |
+
pass
|
49 |
+
|
50 |
+
if self.global_scope:
|
51 |
+
os.environ = self.envs
|
52 |
+
|
53 |
+
|
54 |
+
enver = OSEnver()
|