summaryrefslogtreecommitdiffstats
path: root/g4f/Provider/needs_auth/Gemini.py
diff options
context:
space:
mode:
authorHeiner Lohaus <hlohaus@users.noreply.github.com>2024-02-09 03:31:05 +0100
committerHeiner Lohaus <hlohaus@users.noreply.github.com>2024-02-09 03:31:05 +0100
commit47900f23718e398fc086a6dfbf6590b4c5859c28 (patch)
tree0469ae9e03628c2e1a1aadfe456169e85bceaea0 /g4f/Provider/needs_auth/Gemini.py
parentAdd Gemini Provider with image upload and generation (diff)
downloadgpt4free-47900f23718e398fc086a6dfbf6590b4c5859c28.tar
gpt4free-47900f23718e398fc086a6dfbf6590b4c5859c28.tar.gz
gpt4free-47900f23718e398fc086a6dfbf6590b4c5859c28.tar.bz2
gpt4free-47900f23718e398fc086a6dfbf6590b4c5859c28.tar.lz
gpt4free-47900f23718e398fc086a6dfbf6590b4c5859c28.tar.xz
gpt4free-47900f23718e398fc086a6dfbf6590b4c5859c28.tar.zst
gpt4free-47900f23718e398fc086a6dfbf6590b4c5859c28.zip
Diffstat (limited to 'g4f/Provider/needs_auth/Gemini.py')
-rw-r--r--g4f/Provider/needs_auth/Gemini.py41
1 files changed, 39 insertions, 2 deletions
diff --git a/g4f/Provider/needs_auth/Gemini.py b/g4f/Provider/needs_auth/Gemini.py
index a6e4c15d..da7230dd 100644
--- a/g4f/Provider/needs_auth/Gemini.py
+++ b/g4f/Provider/needs_auth/Gemini.py
@@ -1,16 +1,25 @@
from __future__ import annotations
+import os
import json
import random
import re
from aiohttp import ClientSession
+try:
+ from selenium.webdriver.common.by import By
+ from selenium.webdriver.support.ui import WebDriverWait
+ from selenium.webdriver.support import expected_conditions as EC
+except ImportError:
+ pass
+
from ...typing import Messages, Cookies, ImageType, AsyncResult
from ..base_provider import AsyncGeneratorProvider
from ..helper import format_prompt, get_cookies
-from ...errors import MissingAuthError
+from ...errors import MissingAuthError, MissingRequirementsError
from ...image import to_bytes, ImageResponse
+from ...webdriver import get_browser, get_driver_cookies
REQUEST_HEADERS = {
"authority": "gemini.google.com",
@@ -55,6 +64,27 @@ class Gemini(AsyncGeneratorProvider):
**kwargs
) -> AsyncResult:
prompt = format_prompt(messages)
+
+ try:
+ driver = get_browser(proxy=proxy)
+ try:
+ driver.get(f"{cls.url}/app")
+ WebDriverWait(driver, 5).until(
+ EC.visibility_of_element_located((By.CSS_SELECTOR, "div.ql-editor.textarea"))
+ )
+ except:
+ login_url = os.environ.get("G4F_LOGIN_URL")
+ if login_url:
+ yield f"Please login: [Google Gemini]({login_url})\n\n"
+ WebDriverWait(driver, 240).until(
+ EC.visibility_of_element_located((By.CSS_SELECTOR, "div.ql-editor.textarea"))
+ )
+ cookies = get_driver_cookies(driver)
+ except MissingRequirementsError:
+ pass
+ finally:
+ driver.close()
+
if not cookies:
cookies = get_cookies(".google.com", False)
if "__Secure-1PSID" not in cookies:
@@ -108,7 +138,14 @@ class Gemini(AsyncGeneratorProvider):
yield content
if image_prompt:
images = [image[0][3][3] for image in response_part[4][0][12][7][0]]
- yield ImageResponse(images, image_prompt)
+ resolved_images = []
+ for image in images:
+ async with session.get(image, allow_redirects=False) as fetch:
+ image = fetch.headers["location"]
+ async with session.get(image, allow_redirects=False) as fetch:
+ image = fetch.headers["location"]
+ resolved_images.append(image)
+ yield ImageResponse(resolved_images, image_prompt, {"orginal_links": images})
def build_request(
prompt: str,