Skip to content

Commit

Permalink
Merge pull request #46 from Integration-Automation/dev
Browse files Browse the repository at this point in the history
Dev
  • Loading branch information
JE-Chen committed Nov 7, 2023
2 parents 8ff0ef5 + fe93da2 commit 5469e66
Show file tree
Hide file tree
Showing 10 changed files with 66 additions and 50 deletions.
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ build-backend = "setuptools.build_meta"

[project]
name = "re_edge_gpt_dev"
version = "0.0.15"
version = "0.0.17"
authors = [
{ name = "JE-Chen", email = "jechenmailman@gmail.com" },
]
Expand Down
15 changes: 3 additions & 12 deletions re_edge_gpt/chathub.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
from .constants import HEADERS_INIT_CONVER
from .conversation import Conversation
from .conversation_style import CONVERSATION_STYLE_TYPE
from .proxy import get_proxy
from .request import ChatHubRequest
from .utilities import append_identifier
from .utilities import get_ran_hex
Expand Down Expand Up @@ -50,19 +51,9 @@ def __init__(
conversation_id=conversation.struct["conversationId"],
)
self.cookies = cookies
self.proxy: str = proxy
proxy = (
proxy
or os.environ.get("all_proxy")
or os.environ.get("ALL_PROXY")
or os.environ.get("https_proxy")
or os.environ.get("HTTPS_PROXY")
or None
)
if proxy is not None and proxy.startswith("socks5h://"):
proxy = "socks5://" + proxy[len("socks5h://"):]
self.proxy: str = get_proxy(proxy)
self.session = httpx.AsyncClient(
proxies=proxy,
proxies=self.proxy,
timeout=900,
headers=HEADERS_INIT_CONVER,
)
Expand Down
28 changes: 16 additions & 12 deletions re_edge_gpt/constants.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,21 +13,23 @@
"accept-language": "en,zh-TW;q=0.9,zh;q=0.8,en-GB;q=0.7,en-US;q=0.6",
"accept-encoding": "gzip, deflate, br, zsdch",
"content-type": "application/json",
"sec-ch-ua": '"Chromium";v="118", "Microsoft Edge";v="118", "Not=A?Brand";v="99"',
"sec-ch-ua": '"Microsoft Edge";v="119", '
'"Chromium";v="119", '
'"Not?A_Brand";v="24"',
"sec-ch-ua-arch": '"x86"',
"sec-ch-ua-bitness": '"64"',
"sec-ch-ua-full-version": '"118.0.2088.76"',
"sec-ch-ua-full-version-list": '"Chromium";v="118.0.5993.118", '
'"Microsoft Edge";v="118.0.2088.76", '
'"Not=A?Brand";v="99.0.0.0"',
"sec-ch-ua-full-version": '"119.0.2151.44"',
"sec-ch-ua-full-version-list": '"Microsoft Edge";v="119.0.2151.44", '
'"Chromium";v="119.0.6045.105", '
'"Not?A_Brand";v="24.0.0.0"',
"sec-ch-ua-mobile": "?0",
"sec-ch-ua-model": "",
"sec-ch-ua-platform": '"Windows"',
"sec-ch-ua-platform-version": '"15.0.0"',
"sec-fetch-dest": "empty",
"sec-fetch-mode": "cors",
"sec-fetch-site": "same-origin",
"sec-ms-gec-version": "1-118.0.2088.76",
"sec-ms-gec-version": "1-119.0.2151.44",
"x-ms-client-request-id": str(uuid.uuid4()),
"x-ms-useragent": "azsdk-js-api-client-factory/1.0.0-beta.1 core-rest-pipeline/1.12.0 OS/Windows",
"Referer": "https://www.bing.com/search?",
Expand All @@ -40,21 +42,23 @@
"accept": "application/json",
"accept-language": "en,zh-TW;q=0.9,zh;q=0.8,en-GB;q=0.7,en-US;q=0.6",
"cache-control": "max-age=0",
"sec-ch-ua": '"Chromium";v="118", "Microsoft Edge";v="118", "Not=A?Brand";v="99"',
"sec-ch-ua": '"Microsoft Edge";v="119", '
'"Chromium";v="119", '
'"Not?A_Brand";v="24"',
"sec-ch-ua-arch": '"x86"',
"sec-ch-ua-bitness": '"64"',
"sec-ch-ua-full-version": '"118.0.2088.76"',
"sec-ch-ua-full-version-list": '"Chromium";v="118.0.5993.118", '
'"Microsoft Edge";v="118.0.2088.76", '
'"Not=A?Brand";v="99.0.0.0"',
"sec-ch-ua-full-version": '"119.0.2151.44"',
"sec-ch-ua-full-version-list": '"Microsoft Edge";v="119.0.2151.44", '
'"Chromium";v="119.0.6045.105", '
'"Not?A_Brand";v="24.0.0.0"',
"sec-ch-ua-mobile": "?0",
"sec-ch-ua-model": '""',
"sec-ch-ua-platform": '"Windows"',
"sec-ch-ua-platform-version": '"15.0.0"',
"upgrade-insecure-requests": "1",
"user-agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) "
"AppleWebKit/537.36 (KHTML, like Gecko) "
"Chrome/118.0.0.0 Safari/537.36 Edg/118.0.2088.76",
"Chrome/119.0.0.0 Safari/537.36 Edg/119.0.0.0",
"x-edge-shopping-flag": "1",
"x-forwarded-for": FORWARDED_IP,
}
13 changes: 2 additions & 11 deletions re_edge_gpt/conversation.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

from .constants import HEADERS_INIT_CONVER
from .exceptions import NotAllowedToAccess
from .proxy import get_proxy


class Conversation:
Expand Down Expand Up @@ -78,17 +79,7 @@ async def create(
"conversationSignature": None,
"result": {"value": "Success", "message": None},
}
self.proxy = proxy
proxy = (
proxy
or os.environ.get("all_proxy")
or os.environ.get("ALL_PROXY")
or os.environ.get("https_proxy")
or os.environ.get("HTTPS_PROXY")
or None
)
if proxy is not None and proxy.startswith("socks5h://"):
proxy = "socks5://" + proxy[len("socks5h://"):]
self.proxy = get_proxy(proxy)
transport = httpx.AsyncHTTPTransport(retries=900)
# Convert cookie format to httpx format
formatted_cookies = None
Expand Down
29 changes: 22 additions & 7 deletions re_edge_gpt/image_genearation.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@
import regex
import requests

from re_edge_gpt.proxy import get_proxy

take_ip_socket = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
take_ip_socket.connect(("8.8.8.8", 80))
FORWARDED_IP: str = take_ip_socket.getsockname()[0]
Expand All @@ -23,18 +25,18 @@
BING_URL = os.getenv("BING_URL", "https://www.bing.com")

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

Expand Down Expand Up @@ -79,8 +81,18 @@ def __init__(
debug_file: Union[str, None] = None,
quiet: bool = False,
all_cookies: List[Dict] = None,
proxy: str = None,
proxy_user: Dict[str, str] = None
) -> None:
if proxy_user is None:
proxy_user = {"http_user": "http", "https_user": "https"}
self.session: requests.Session = requests.Session()
self.proxy: str = get_proxy(proxy)
if self.proxy is not None:
self.session.proxies.update({
proxy_user.get("http_user", "http"): self.proxy,
proxy_user.get("https_user", "https"): self.proxy
})
self.session.headers = HEADERS
self.session.cookies.set("_U", auth_cookie)
if all_cookies:
Expand Down Expand Up @@ -169,7 +181,7 @@ def get_images(self, prompt: str, timeout: int = 200, max_generate_time_sec: int
time.sleep(1)
time_sec = time_sec + 1
if time_sec >= max_generate_time_sec:
raise TimeoutError
raise TimeoutError("Out of generate time")
continue
else:
break
Expand Down Expand Up @@ -259,10 +271,13 @@ def __init__(
debug_file: Union[str, None] = None,
quiet: bool = False,
all_cookies: List[Dict] = None,
proxy: str = None
) -> None:
if auth_cookie is None and not all_cookies:
raise Exception("No auth cookie provided")
self.proxy: str = get_proxy(proxy)
self.session = httpx.AsyncClient(
proxies=self.proxy,
headers=HEADERS,
trust_env=True,
)
Expand Down Expand Up @@ -344,7 +359,7 @@ async def get_images(self, prompt: str, timeout: int = 200, max_generate_time_se
await asyncio.sleep(1)
time_sec = time_sec + 1
if time_sec >= max_generate_time_sec:
raise TimeoutError
raise TimeoutError("Out of generate time")
continue
# Use regex to search for src=""
image_links = regex.findall(r'src="([^"]+)"', content)
Expand Down
15 changes: 15 additions & 0 deletions re_edge_gpt/proxy.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import os


def get_proxy(proxy: str = None):
proxy = (
proxy
or os.environ.get("all_proxy")
or os.environ.get("ALL_PROXY")
or os.environ.get("https_proxy")
or os.environ.get("HTTPS_PROXY")
or None
)
if proxy is not None and proxy.startswith("socks5h://"):
proxy = "socks5://" + proxy[len("socks5h://"):]
return proxy
2 changes: 1 addition & 1 deletion stable.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ build-backend = "setuptools.build_meta"

[project]
name = "re_edge_gpt"
version = "0.0.15"
version = "0.0.17"
authors = [
{ name = "JE-Chen", email = "jechenmailman@gmail.com" },
]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ async def test_ask() -> None:
str(Path(str(Path.cwd()) + "/bing_cookies.json")), encoding="utf-8").read())
bot = await Chatbot.create(cookies=cookies)
response = await bot.ask(
prompt="How to boil the egg",
prompt="Deer soup",
conversation_style=ConversationStyle.balanced,
simplify_response=True
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
test_output_dir = "test_output"
# download a test image
test_image_url = "https://picsum.photos/200"
auth_cooker = open("bing_cookies.txt", "r+").read()
auth_cooker = open("../test_generate_image/bing_cookies.txt", "r+").read()
sync_gen = ImageGen(auth_cookie=auth_cooker)
async_gen = ImageGenAsync(auth_cookie=auth_cooker)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,14 @@
test_output_dir = "test_output"
# download a test image
test_image_url = "https://picsum.photos/200"
auth_cooker = open("bing_cookies.txt", "r+").read()
auth_cooker = open("../test_generate_image/bing_cookies.txt", "r+").read()
async_gen = ImageGenAsync(auth_cookie=auth_cooker)


# Generate image list async
async def test_generate_image_async():
print("Generate 🐱")
image_list = await async_gen.get_images("🐱")
print("Generate Pigeon")
image_list = await async_gen.get_images("Pigeon")
print(image_list)


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

0 comments on commit 5469e66

Please sign in to comment.