diff options
author | Heiner Lohaus <hlohaus@users.noreply.github.com> | 2024-11-19 10:39:22 +0100 |
---|---|---|
committer | Heiner Lohaus <hlohaus@users.noreply.github.com> | 2024-11-19 10:39:22 +0100 |
commit | b79a4d6aeb2432392912b8df2470aa276e9b49ca (patch) | |
tree | c33b708448722d72bc750b4c0080abb0da212dbb /g4f/Provider | |
parent | Add image upload to Copilot provider (diff) | |
download | gpt4free-b79a4d6aeb2432392912b8df2470aa276e9b49ca.tar gpt4free-b79a4d6aeb2432392912b8df2470aa276e9b49ca.tar.gz gpt4free-b79a4d6aeb2432392912b8df2470aa276e9b49ca.tar.bz2 gpt4free-b79a4d6aeb2432392912b8df2470aa276e9b49ca.tar.lz gpt4free-b79a4d6aeb2432392912b8df2470aa276e9b49ca.tar.xz gpt4free-b79a4d6aeb2432392912b8df2470aa276e9b49ca.tar.zst gpt4free-b79a4d6aeb2432392912b8df2470aa276e9b49ca.zip |
Diffstat (limited to 'g4f/Provider')
-rw-r--r-- | g4f/Provider/Copilot.py | 24 |
1 files changed, 14 insertions, 10 deletions
diff --git a/g4f/Provider/Copilot.py b/g4f/Provider/Copilot.py index 3bf6f193..cb803296 100644 --- a/g4f/Provider/Copilot.py +++ b/g4f/Provider/Copilot.py @@ -69,7 +69,7 @@ class Copilot(AbstractProvider): access_token = None headers = None cookies = conversation.cookie_jar if conversation is not None else None - if cls.needs_auth: + if cls.needs_auth or image is not None: if conversation is None or conversation.access_token is None: access_token, cookies = asyncio.run(cls.get_access_token_and_cookies(proxy)) else: @@ -109,6 +109,7 @@ class Copilot(AbstractProvider): headers={"content-type": is_accepted_format(data)}, data=data ) + raise_for_status(response) images.append({"type":"image", "url": response.json().get("url")}) wss = session.ws_connect(cls.websocket_url) @@ -121,24 +122,27 @@ class Copilot(AbstractProvider): }], "mode": "chat" }).encode(), CurlWsFlag.TEXT) + + is_started = False + msg = None while True: try: - msg = json.loads(wss.recv()[0]) + msg = wss.recv()[0] + msg = json.loads(msg) except: break if msg.get("event") == "appendText": yield msg.get("text") elif msg.get("event") in ["done", "partCompleted"]: break + if not is_started: + raise RuntimeError("Last message: {msg}") @classmethod async def get_access_token_and_cookies(cls, proxy: str = None): if not has_nodriver: raise MissingRequirementsError('Install "nodriver" package | pip install -U nodriver') - if has_platformdirs: - user_data_dir = user_config_dir("g4f-nodriver") - else: - user_data_dir = None + user_data_dir = user_config_dir("g4f-nodriver") if has_platformdirs else None if debug.logging: print(f"Copilot: Open nodriver with user_dir: {user_data_dir}") browser = await nodriver.start( @@ -146,7 +150,8 @@ class Copilot(AbstractProvider): browser_args=None if proxy is None else [f"--proxy-server={proxy}"], ) page = await browser.get(cls.url) - while True: + access_token = None + while access_token is None: access_token = await page.evaluate(""" (() => { for (var i = 0; i < localStorage.length; i++) { @@ -159,9 +164,8 @@ class Copilot(AbstractProvider): } })() """) - if access_token: - break - asyncio.sleep(1) + if access_token is None: + asyncio.sleep(1) cookies = {} for c in await page.send(nodriver.cdp.network.get_cookies([cls.url])): cookies[c.name] = c.value |