Skip to content
This repository was archived by the owner on Dec 18, 2024. It is now read-only.

Commit 5469e66

Browse files
authored
Merge pull request #46 from Integration-Automation/dev
Dev
2 parents 8ff0ef5 + fe93da2 commit 5469e66

File tree

10 files changed

+66
-50
lines changed

10 files changed

+66
-50
lines changed

pyproject.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ build-backend = "setuptools.build_meta"
66

77
[project]
88
name = "re_edge_gpt_dev"
9-
version = "0.0.15"
9+
version = "0.0.17"
1010
authors = [
1111
{ name = "JE-Chen", email = "jechenmailman@gmail.com" },
1212
]

re_edge_gpt/chathub.py

Lines changed: 3 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
from .constants import HEADERS_INIT_CONVER
1919
from .conversation import Conversation
2020
from .conversation_style import CONVERSATION_STYLE_TYPE
21+
from .proxy import get_proxy
2122
from .request import ChatHubRequest
2223
from .utilities import append_identifier
2324
from .utilities import get_ran_hex
@@ -50,19 +51,9 @@ def __init__(
5051
conversation_id=conversation.struct["conversationId"],
5152
)
5253
self.cookies = cookies
53-
self.proxy: str = proxy
54-
proxy = (
55-
proxy
56-
or os.environ.get("all_proxy")
57-
or os.environ.get("ALL_PROXY")
58-
or os.environ.get("https_proxy")
59-
or os.environ.get("HTTPS_PROXY")
60-
or None
61-
)
62-
if proxy is not None and proxy.startswith("socks5h://"):
63-
proxy = "socks5://" + proxy[len("socks5h://"):]
54+
self.proxy: str = get_proxy(proxy)
6455
self.session = httpx.AsyncClient(
65-
proxies=proxy,
56+
proxies=self.proxy,
6657
timeout=900,
6758
headers=HEADERS_INIT_CONVER,
6859
)

re_edge_gpt/constants.py

Lines changed: 16 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -13,21 +13,23 @@
1313
"accept-language": "en,zh-TW;q=0.9,zh;q=0.8,en-GB;q=0.7,en-US;q=0.6",
1414
"accept-encoding": "gzip, deflate, br, zsdch",
1515
"content-type": "application/json",
16-
"sec-ch-ua": '"Chromium";v="118", "Microsoft Edge";v="118", "Not=A?Brand";v="99"',
16+
"sec-ch-ua": '"Microsoft Edge";v="119", '
17+
'"Chromium";v="119", '
18+
'"Not?A_Brand";v="24"',
1719
"sec-ch-ua-arch": '"x86"',
1820
"sec-ch-ua-bitness": '"64"',
19-
"sec-ch-ua-full-version": '"118.0.2088.76"',
20-
"sec-ch-ua-full-version-list": '"Chromium";v="118.0.5993.118", '
21-
'"Microsoft Edge";v="118.0.2088.76", '
22-
'"Not=A?Brand";v="99.0.0.0"',
21+
"sec-ch-ua-full-version": '"119.0.2151.44"',
22+
"sec-ch-ua-full-version-list": '"Microsoft Edge";v="119.0.2151.44", '
23+
'"Chromium";v="119.0.6045.105", '
24+
'"Not?A_Brand";v="24.0.0.0"',
2325
"sec-ch-ua-mobile": "?0",
2426
"sec-ch-ua-model": "",
2527
"sec-ch-ua-platform": '"Windows"',
2628
"sec-ch-ua-platform-version": '"15.0.0"',
2729
"sec-fetch-dest": "empty",
2830
"sec-fetch-mode": "cors",
2931
"sec-fetch-site": "same-origin",
30-
"sec-ms-gec-version": "1-118.0.2088.76",
32+
"sec-ms-gec-version": "1-119.0.2151.44",
3133
"x-ms-client-request-id": str(uuid.uuid4()),
3234
"x-ms-useragent": "azsdk-js-api-client-factory/1.0.0-beta.1 core-rest-pipeline/1.12.0 OS/Windows",
3335
"Referer": "https://www.bing.com/search?",
@@ -40,21 +42,23 @@
4042
"accept": "application/json",
4143
"accept-language": "en,zh-TW;q=0.9,zh;q=0.8,en-GB;q=0.7,en-US;q=0.6",
4244
"cache-control": "max-age=0",
43-
"sec-ch-ua": '"Chromium";v="118", "Microsoft Edge";v="118", "Not=A?Brand";v="99"',
45+
"sec-ch-ua": '"Microsoft Edge";v="119", '
46+
'"Chromium";v="119", '
47+
'"Not?A_Brand";v="24"',
4448
"sec-ch-ua-arch": '"x86"',
4549
"sec-ch-ua-bitness": '"64"',
46-
"sec-ch-ua-full-version": '"118.0.2088.76"',
47-
"sec-ch-ua-full-version-list": '"Chromium";v="118.0.5993.118", '
48-
'"Microsoft Edge";v="118.0.2088.76", '
49-
'"Not=A?Brand";v="99.0.0.0"',
50+
"sec-ch-ua-full-version": '"119.0.2151.44"',
51+
"sec-ch-ua-full-version-list": '"Microsoft Edge";v="119.0.2151.44", '
52+
'"Chromium";v="119.0.6045.105", '
53+
'"Not?A_Brand";v="24.0.0.0"',
5054
"sec-ch-ua-mobile": "?0",
5155
"sec-ch-ua-model": '""',
5256
"sec-ch-ua-platform": '"Windows"',
5357
"sec-ch-ua-platform-version": '"15.0.0"',
5458
"upgrade-insecure-requests": "1",
5559
"user-agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) "
5660
"AppleWebKit/537.36 (KHTML, like Gecko) "
57-
"Chrome/118.0.0.0 Safari/537.36 Edg/118.0.2088.76",
61+
"Chrome/119.0.0.0 Safari/537.36 Edg/119.0.0.0",
5862
"x-edge-shopping-flag": "1",
5963
"x-forwarded-for": FORWARDED_IP,
6064
}

re_edge_gpt/conversation.py

Lines changed: 2 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77

88
from .constants import HEADERS_INIT_CONVER
99
from .exceptions import NotAllowedToAccess
10+
from .proxy import get_proxy
1011

1112

1213
class Conversation:
@@ -78,17 +79,7 @@ async def create(
7879
"conversationSignature": None,
7980
"result": {"value": "Success", "message": None},
8081
}
81-
self.proxy = proxy
82-
proxy = (
83-
proxy
84-
or os.environ.get("all_proxy")
85-
or os.environ.get("ALL_PROXY")
86-
or os.environ.get("https_proxy")
87-
or os.environ.get("HTTPS_PROXY")
88-
or None
89-
)
90-
if proxy is not None and proxy.startswith("socks5h://"):
91-
proxy = "socks5://" + proxy[len("socks5h://"):]
82+
self.proxy = get_proxy(proxy)
9283
transport = httpx.AsyncHTTPTransport(retries=900)
9384
# Convert cookie format to httpx format
9485
formatted_cookies = None

re_edge_gpt/image_genearation.py

Lines changed: 22 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@
1515
import regex
1616
import requests
1717

18+
from re_edge_gpt.proxy import get_proxy
19+
1820
take_ip_socket = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
1921
take_ip_socket.connect(("8.8.8.8", 80))
2022
FORWARDED_IP: str = take_ip_socket.getsockname()[0]
@@ -23,18 +25,18 @@
2325
BING_URL = os.getenv("BING_URL", "https://www.bing.com")
2426

2527
HEADERS = {
26-
"accept": "text/html,application/xhtml+xml,application/"
27-
"xml;q=0.9,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.7",
28-
"accept-language": "en-US,en;q=0.9",
28+
"accept": "text/html,application/xhtml+xml,application/xml;q=0.9,"
29+
"image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.77",
30+
"accept-language": "en,zh-TW;q=0.9,zh;q=0.8,en-GB;q=0.7,en-US;q=0.6",
2931
"cache-control": "max-age=0",
3032
"content-type": "application/x-www-form-urlencoded",
3133
"referrer": "https://www.bing.com/images/create/",
3234
"origin": "https://www.bing.com",
3335
"user-agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) "
3436
"AppleWebKit/537.36 (KHTML, like Gecko) "
35-
"Chrome/115.0.0.0 "
37+
"Chrome/119.0.0.0 "
3638
"Safari/537.36 "
37-
"Edg/115.0.1901.188",
39+
"Edg/119.0.0.0",
3840
"x-forwarded-for": FORWARDED_IP,
3941
}
4042

@@ -79,8 +81,18 @@ def __init__(
7981
debug_file: Union[str, None] = None,
8082
quiet: bool = False,
8183
all_cookies: List[Dict] = None,
84+
proxy: str = None,
85+
proxy_user: Dict[str, str] = None
8286
) -> None:
87+
if proxy_user is None:
88+
proxy_user = {"http_user": "http", "https_user": "https"}
8389
self.session: requests.Session = requests.Session()
90+
self.proxy: str = get_proxy(proxy)
91+
if self.proxy is not None:
92+
self.session.proxies.update({
93+
proxy_user.get("http_user", "http"): self.proxy,
94+
proxy_user.get("https_user", "https"): self.proxy
95+
})
8496
self.session.headers = HEADERS
8597
self.session.cookies.set("_U", auth_cookie)
8698
if all_cookies:
@@ -169,7 +181,7 @@ def get_images(self, prompt: str, timeout: int = 200, max_generate_time_sec: int
169181
time.sleep(1)
170182
time_sec = time_sec + 1
171183
if time_sec >= max_generate_time_sec:
172-
raise TimeoutError
184+
raise TimeoutError("Out of generate time")
173185
continue
174186
else:
175187
break
@@ -259,10 +271,13 @@ def __init__(
259271
debug_file: Union[str, None] = None,
260272
quiet: bool = False,
261273
all_cookies: List[Dict] = None,
274+
proxy: str = None
262275
) -> None:
263276
if auth_cookie is None and not all_cookies:
264277
raise Exception("No auth cookie provided")
278+
self.proxy: str = get_proxy(proxy)
265279
self.session = httpx.AsyncClient(
280+
proxies=self.proxy,
266281
headers=HEADERS,
267282
trust_env=True,
268283
)
@@ -344,7 +359,7 @@ async def get_images(self, prompt: str, timeout: int = 200, max_generate_time_se
344359
await asyncio.sleep(1)
345360
time_sec = time_sec + 1
346361
if time_sec >= max_generate_time_sec:
347-
raise TimeoutError
362+
raise TimeoutError("Out of generate time")
348363
continue
349364
# Use regex to search for src=""
350365
image_links = regex.findall(r'src="([^"]+)"', content)

re_edge_gpt/proxy.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
import os
2+
3+
4+
def get_proxy(proxy: str = None):
5+
proxy = (
6+
proxy
7+
or os.environ.get("all_proxy")
8+
or os.environ.get("ALL_PROXY")
9+
or os.environ.get("https_proxy")
10+
or os.environ.get("HTTPS_PROXY")
11+
or None
12+
)
13+
if proxy is not None and proxy.startswith("socks5h://"):
14+
proxy = "socks5://" + proxy[len("socks5h://"):]
15+
return proxy

stable.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ build-backend = "setuptools.build_meta"
66

77
[project]
88
name = "re_edge_gpt"
9-
version = "0.0.15"
9+
version = "0.0.17"
1010
authors = [
1111
{ name = "JE-Chen", email = "jechenmailman@gmail.com" },
1212
]

test/unit_test/test_bot/test_bot_manual.py renamed to test/unit_test/manual_test/test_bot_manual.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ async def test_ask() -> None:
1717
str(Path(str(Path.cwd()) + "/bing_cookies.json")), encoding="utf-8").read())
1818
bot = await Chatbot.create(cookies=cookies)
1919
response = await bot.ask(
20-
prompt="How to boil the egg",
20+
prompt="Deer soup",
2121
conversation_style=ConversationStyle.balanced,
2222
simplify_response=True
2323
)

test/unit_test/test_generate_image/test_generation_manual.py renamed to test/unit_test/manual_test/test_generation_manual.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
test_output_dir = "test_output"
1111
# download a test image
1212
test_image_url = "https://picsum.photos/200"
13-
auth_cooker = open("bing_cookies.txt", "r+").read()
13+
auth_cooker = open("../test_generate_image/bing_cookies.txt", "r+").read()
1414
sync_gen = ImageGen(auth_cookie=auth_cooker)
1515
async_gen = ImageGenAsync(auth_cookie=auth_cooker)
1616

test/unit_test/test_generate_image/test_generation_manual_async_loop.py renamed to test/unit_test/manual_test/test_generation_manual_async_loop.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,14 +9,14 @@
99
test_output_dir = "test_output"
1010
# download a test image
1111
test_image_url = "https://picsum.photos/200"
12-
auth_cooker = open("bing_cookies.txt", "r+").read()
12+
auth_cooker = open("../test_generate_image/bing_cookies.txt", "r+").read()
1313
async_gen = ImageGenAsync(auth_cookie=auth_cooker)
1414

1515

1616
# Generate image list async
1717
async def test_generate_image_async():
18-
print("Generate 🐱")
19-
image_list = await async_gen.get_images("🐱")
18+
print("Generate Pigeon")
19+
image_list = await async_gen.get_images("Pigeon")
2020
print(image_list)
2121

2222

@@ -31,6 +31,6 @@ async def test_generate_image_async():
3131
# Remove dir
3232
shutil.rmtree(test_output_dir)
3333
except Exception as error:
34-
raise error
34+
print(repr(error), file=sys.stderr)
3535
finally:
3636
sys.exit(0)

0 commit comments

Comments
 (0)