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

XFEstudio/gpt4free

Resolve images in Gemini Provider

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

代码差异

3 个文件 +42 -6
Modified g4f/Provider/needs_auth/Gemini.py +39 -2
@@ -1,16 +1,25 @@
1 1 from __future__ import annotations
2 2
3 import os
3 4 import json
4 5 import random
5 6 import re
6 7
7 8 from aiohttp import ClientSession
8 9
10 try:
11 from selenium.webdriver.common.by import By
12 from selenium.webdriver.support.ui import WebDriverWait
13 from selenium.webdriver.support import expected_conditions as EC
14 except ImportError:
15 pass
16
9 17 from ...typing import Messages, Cookies, ImageType, AsyncResult
10 18 from ..base_provider import AsyncGeneratorProvider
11 19 from ..helper import format_prompt, get_cookies
12 from ...errors import MissingAuthError
20 from ...errors import MissingAuthError, MissingRequirementsError
13 21 from ...image import to_bytes, ImageResponse
22 from ...webdriver import get_browser, get_driver_cookies
14 23
15 24 REQUEST_HEADERS = {
16 25 "authority": "gemini.google.com",
@@ -55,6 +64,27 @@ class Gemini(AsyncGeneratorProvider):
55 64 **kwargs
56 65 ) -> AsyncResult:
57 66 prompt = format_prompt(messages)
67
68 try:
69 driver = get_browser(proxy=proxy)
70 try:
71 driver.get(f"{cls.url}/app")
72 WebDriverWait(driver, 5).until(
73 EC.visibility_of_element_located((By.CSS_SELECTOR, "div.ql-editor.textarea"))
74 )
75 except:
76 login_url = os.environ.get("G4F_LOGIN_URL")
77 if login_url:
78 yield f"Please login: [Google Gemini]({login_url})\n\n"
79 WebDriverWait(driver, 240).until(
80 EC.visibility_of_element_located((By.CSS_SELECTOR, "div.ql-editor.textarea"))
81 )
82 cookies = get_driver_cookies(driver)
83 except MissingRequirementsError:
84 pass
85 finally:
86 driver.close()
87
58 88 if not cookies:
59 89 cookies = get_cookies(".google.com", False)
60 90 if "__Secure-1PSID" not in cookies:
@@ -108,7 +138,14 @@ class Gemini(AsyncGeneratorProvider):
108 138 yield content
109 139 if image_prompt:
110 140 images = [image[0][3][3] for image in response_part[4][0][12][7][0]]
111 yield ImageResponse(images, image_prompt)
141 resolved_images = []
142 for image in images:
143 async with session.get(image, allow_redirects=False) as fetch:
144 image = fetch.headers["location"]
145 async with session.get(image, allow_redirects=False) as fetch:
146 image = fetch.headers["location"]
147 resolved_images.append(image)
148 yield ImageResponse(resolved_images, image_prompt, {"orginal_links": images})
112 149
113 150 def build_request(
114 151 prompt: str,
Modified g4f/gui/client/html/index.html +1 -1
@@ -154,7 +154,7 @@
154 154 <option value="Bing">Bing</option>
155 155 <option value="OpenaiChat">OpenaiChat</option>
156 156 <option value="HuggingChat">HuggingChat</option>
157 <option value="Bard">Bard</option>
157 <option value="Gemini">Gemini</option>
158 158 <option value="Liaobots">Liaobots</option>
159 159 <option value="Phind">Phind</option>
160 160 <option value="">----</option>
Modified g4f/image.py +2 -3
@@ -46,9 +46,8 @@ def to_image(image: ImageType, is_svg: bool = False) -> Image:
46 46 return open_image(BytesIO(image))
47 47 elif not isinstance(image, Image):
48 48 image = open_image(image)
49 copy = image.copy()
50 copy.format = image.format
51 return copy
49 image.load()
50 return image
52 51 return image
53 52
54 53 def is_allowed_extension(filename: str) -> bool: