From 7c10a036ed63b0a14f69b27552801dde4f220b4e Mon Sep 17 00:00:00 2001 From: Heiner Lohaus Date: Tue, 19 Nov 2024 10:23:24 +0100 Subject: Add image upload to Copilot provider Remove prompt lock from gui Fix discord link in gui --- g4f/Provider/Copilot.py | 17 ++- g4f/Provider/needs_auth/CopilotAccount.py | 3 +- g4f/Provider/needs_auth/OpenaiChat.py | 2 +- g4f/gui/client/index.html | 9 +- g4f/gui/client/static/css/style.css | 32 +++-- g4f/gui/client/static/js/chat.v1.js | 232 ++++++++++++++++-------------- 6 files changed, 160 insertions(+), 135 deletions(-) diff --git a/g4f/Provider/Copilot.py b/g4f/Provider/Copilot.py index f10202bf..3bf6f193 100644 --- a/g4f/Provider/Copilot.py +++ b/g4f/Provider/Copilot.py @@ -23,9 +23,10 @@ except ImportError: from .base_provider import AbstractProvider, BaseConversation from .helper import format_prompt -from ..typing import CreateResult, Messages +from ..typing import CreateResult, Messages, ImageType from ..errors import MissingRequirementsError from ..requests.raise_for_status import raise_for_status +from ..image import to_bytes, is_accepted_format from .. import debug class Conversation(BaseConversation): @@ -43,6 +44,7 @@ class Copilot(AbstractProvider): url = "https://copilot.microsoft.com" working = True supports_stream = True + default_model = "Copilot" websocket_url = "wss://copilot.microsoft.com/c/api/chat?api-version=2" conversation_url = f"{url}/c/api/conversations" @@ -55,6 +57,7 @@ class Copilot(AbstractProvider): stream: bool = False, proxy: str = None, timeout: int = 900, + image: ImageType = None, conversation: Conversation = None, return_conversation: bool = False, **kwargs @@ -98,11 +101,21 @@ class Copilot(AbstractProvider): if debug.logging: print(f"Copilot: Use conversation: {conversation_id}") + images = [] + if image is not None: + data = to_bytes(image) + response = session.post( + "https://copilot.microsoft.com/c/api/attachments", + headers={"content-type": is_accepted_format(data)}, + data=data + ) + images.append({"type":"image", "url": response.json().get("url")}) + wss = session.ws_connect(cls.websocket_url) wss.send(json.dumps({ "event": "send", "conversationId": conversation_id, - "content": [{ + "content": [*images, { "type": "text", "text": prompt, }], diff --git a/g4f/Provider/needs_auth/CopilotAccount.py b/g4f/Provider/needs_auth/CopilotAccount.py index fa43867e..76e51278 100644 --- a/g4f/Provider/needs_auth/CopilotAccount.py +++ b/g4f/Provider/needs_auth/CopilotAccount.py @@ -5,4 +5,5 @@ from ..Copilot import Copilot class CopilotAccount(Copilot): needs_auth = True parent = "Copilot" - default_model = "" \ No newline at end of file + default_model = "Copilot" + default_vision_model = default_model \ No newline at end of file diff --git a/g4f/Provider/needs_auth/OpenaiChat.py b/g4f/Provider/needs_auth/OpenaiChat.py index 13e15f1d..22264dd9 100644 --- a/g4f/Provider/needs_auth/OpenaiChat.py +++ b/g4f/Provider/needs_auth/OpenaiChat.py @@ -396,7 +396,7 @@ class OpenaiChat(AsyncGeneratorProvider, ProviderModelMixin): f"{cls.url}/backend-anon/sentinel/chat-requirements" if cls._api_key is None else f"{cls.url}/backend-api/sentinel/chat-requirements", - json={"p": get_requirements_token(RequestConfig.proof_token)}, + json={"p": get_requirements_token(RequestConfig.proof_token) if RequestConfig.proof_token else None}, headers=cls._headers ) as response: cls._update_request_args(session) diff --git a/g4f/gui/client/index.html b/g4f/gui/client/index.html index 63e47b3f..b256b0be 100644 --- a/g4f/gui/client/index.html +++ b/g4f/gui/client/index.html @@ -72,12 +72,12 @@
- discord ~ discord.gg/6yrm7H4B + discord ~ discord.gg/6yrm7H4B
- github ~ @xtekky/gpt4free + github ~ @xtekky/gpt4free
@@ -87,7 +87,6 @@