XFE Git
XFE Studio Git
Git 首页 全局搜索
XFE 主站 文档 NuGet
公开
关注 0 Fork 0 Star 1
返回提交历史

XFEstudio/gpt4free

Fix process_image in Bing Add ImageResponse to Bing Fix cursor styling in gui

ceed364c
Heiner Lohaus <hlohaus@users.noreply.github.com>
提交于

代码差异

6 个文件 +23 -22
Modified g4f/Provider/Bing.py +14 -10
@@ -9,9 +9,10 @@ from urllib import parse
9 9 from aiohttp import ClientSession, ClientTimeout
10 10
11 11 from ..typing import AsyncResult, Messages, ImageType
12 from ..image import ImageResponse
12 13 from .base_provider import AsyncGeneratorProvider
13 14 from .bing.upload_image import upload_image
14 from .bing.create_images import create_images, format_images_markdown
15 from .bing.create_images import create_images
15 16 from .bing.conversation import Conversation, create_conversation, delete_conversation
16 17
17 18 class Tones():
@@ -172,7 +173,7 @@ def create_message(
172 173 prompt: str,
173 174 tone: str,
174 175 context: str = None,
175 image_info: dict = None,
176 image_response: ImageResponse = None,
176 177 web_search: bool = False,
177 178 gpt4_turbo: bool = False
178 179 ) -> str:
@@ -228,9 +229,9 @@ def create_message(
228 229 'target': 'chat',
229 230 'type': 4
230 231 }
231 if image_info and "imageUrl" in image_info and "originalImageUrl" in image_info:
232 struct['arguments'][0]['message']['originalImageUrl'] = image_info['originalImageUrl']
233 struct['arguments'][0]['message']['imageUrl'] = image_info['imageUrl']
232 if image_response.get('imageUrl') and image_response.get('originalImageUrl'):
233 struct['arguments'][0]['message']['originalImageUrl'] = image_response.get('originalImageUrl')
234 struct['arguments'][0]['message']['imageUrl'] = image_response.get('imageUrl')
234 235 struct['arguments'][0]['experienceType'] = None
235 236 struct['arguments'][0]['attachedFileInfo'] = {"fileName": None, "fileType": None}
236 237 if context:
@@ -262,9 +263,9 @@ async def stream_generate(
262 263 headers=headers
263 264 ) as session:
264 265 conversation = await create_conversation(session, proxy)
265 image_info = None
266 if image:
267 image_info = await upload_image(session, image, tone, proxy)
266 image_response = await upload_image(session, image, tone, proxy) if image else None
267 if image_response:
268 yield image_response
268 269 try:
269 270 async with session.ws_connect(
270 271 'wss://sydney.bing.com/sydney/ChatHub',
@@ -274,7 +275,7 @@ async def stream_generate(
274 275 ) as wss:
275 276 await wss.send_str(format_message({'protocol': 'json', 'version': 1}))
276 277 await wss.receive(timeout=timeout)
277 await wss.send_str(create_message(conversation, prompt, tone, context, image_info, web_search, gpt4_turbo))
278 await wss.send_str(create_message(conversation, prompt, tone, context, image_response, web_search, gpt4_turbo))
278 279
279 280 response_txt = ''
280 281 returned_text = ''
@@ -290,6 +291,7 @@ async def stream_generate(
290 291 response = json.loads(obj)
291 292 if response.get('type') == 1 and response['arguments'][0].get('messages'):
292 293 message = response['arguments'][0]['messages'][0]
294 image_response = None
293 295 if (message['contentOrigin'] != 'Apology'):
294 296 if 'adaptiveCards' in message:
295 297 card = message['adaptiveCards'][0]['body'][0]
@@ -301,7 +303,7 @@ async def stream_generate(
301 303 elif message.get('contentType') == "IMAGE":
302 304 prompt = message.get('text')
303 305 try:
304 response_txt += format_images_markdown(await create_images(session, prompt, proxy), prompt)
306 image_response = ImageResponse(await create_images(session, prompt, proxy), prompt)
305 307 except:
306 308 response_txt += f"\nhttps://www.bing.com/images/create?q={parse.quote(prompt)}"
307 309 final = True
@@ -310,6 +312,8 @@ async def stream_generate(
310 312 if new != "\n":
311 313 yield new
312 314 returned_text = response_txt
315 if image_response:
316 yield image_response
313 317 elif response.get('type') == 2:
314 318 result = response['item']['result']
315 319 if result.get('error'):
Modified g4f/Provider/bing/upload_image.py +3 -3
@@ -6,7 +6,7 @@ import json
6 6 import math
7 7 from ...typing import ImageType
8 8 from aiohttp import ClientSession
9 from ...image import to_image, process_image, to_base64
9 from ...image import to_image, process_image, to_base64, ImageResponse
10 10
11 11 image_config = {
12 12 "maxImagePixels": 360000,
@@ -19,7 +19,7 @@ async def upload_image(
19 19 image: ImageType,
20 20 tone: str,
21 21 proxy: str = None
22 ) -> dict:
22 ) -> ImageResponse:
23 23 image = to_image(image)
24 24 width, height = image.size
25 25 max_image_pixels = image_config['maxImagePixels']
@@ -55,7 +55,7 @@ async def upload_image(
55 55 else "https://www.bing.com/images/blob?bcid="
56 56 + result['bcid']
57 57 )
58 return result
58 return ImageResponse(result["imageUrl"], "", result)
59 59
60 60 def build_image_upload_api_payload(image_bin: str, tone: str):
61 61 payload = {
Modified g4f/Provider/needs_auth/OpenaiChat.py +1 -4
@@ -13,7 +13,6 @@ from ...webdriver import get_browser, get_driver_cookies
13 13 from ...typing import AsyncResult, Messages
14 14 from ...requests import StreamSession
15 15 from ...image import to_image, to_bytes, ImageType, ImageResponse
16 from ... import debug
17 16
18 17 models = {
19 18 "gpt-3.5": "text-davinci-002-render-sha",
@@ -242,9 +241,7 @@ class OpenaiChat(AsyncGeneratorProvider):
242 241 json=data,
243 242 headers={"Accept": "text/event-stream", **headers}
244 243 ) as response:
245 try:
246 response.raise_for_status()
247 except:
244 if not response.ok:
248 245 raise RuntimeError(f"Response {response.status_code}: {await response.text()}")
249 246 try:
250 247 last_message: int = 0
Modified g4f/gui/client/css/style.css +1 -0
@@ -566,6 +566,7 @@ select {
566 566 animation: blink 0.8s infinite;
567 567 width: 7px;
568 568 height: 15px;
569 display: inline-block;
569 570 }
570 571
571 572 @keyframes blink {
Modified g4f/gui/client/js/chat.v1.js +2 -2
@@ -104,7 +104,7 @@ const ask_gpt = async () => {
104 104 </div>
105 105 <div class="content" id="gpt_${window.token}">
106 106 <div class="provider"></div>
107 <div class="content_inner"><div id="cursor"></div></div>
107 <div class="content_inner"><span id="cursor"></span></div>
108 108 </div>
109 109 </div>
110 110 `;
@@ -168,7 +168,7 @@ const ask_gpt = async () => {
168 168 }
169 169 if (error) {
170 170 console.error(error);
171 content_inner.innerHTML = "An error occured, please try again, if the problem persists, please use a other model or provider";
171 content_inner.innerHTML += "<p>An error occured, please try again, if the problem persists, please use a other model or provider.</p>";
172 172 } else {
173 173 html = markdown_render(text);
174 174 html = html.substring(0, html.lastIndexOf('</p>')) + '<span id="cursor"></span></p>';
Modified g4f/image.py +2 -3
@@ -64,7 +64,6 @@ def get_orientation(image: Image.Image) -> int:
64 64
65 65 def process_image(img: Image.Image, new_width: int, new_height: int) -> Image.Image:
66 66 orientation = get_orientation(img)
67 new_img = Image.new("RGB", (new_width, new_height), color="#FFFFFF")
68 67 if orientation:
69 68 if orientation > 4:
70 69 img = img.transpose(Image.FLIP_LEFT_RIGHT)
@@ -74,8 +73,8 @@ def process_image(img: Image.Image, new_width: int, new_height: int) -> Image.Im
74 73 img = img.transpose(Image.ROTATE_270)
75 74 if orientation in [7, 8]:
76 75 img = img.transpose(Image.ROTATE_90)
77 new_img.paste(img, (0, 0))
78 return new_img
76 img.thumbnail((new_width, new_height))
77 return img
79 78
80 79 def to_base64(image: Image.Image, compression_rate: float) -> str:
81 80 output_buffer = BytesIO()