2023-09-03 04:26:26 -04:00
|
|
|
from __future__ import annotations
|
|
|
|
|
2023-11-16 12:10:19 -05:00
|
|
|
import time
|
2023-12-06 03:35:36 -05:00
|
|
|
import os
|
|
|
|
from selenium.webdriver.common.by import By
|
|
|
|
from selenium.webdriver.support.ui import WebDriverWait
|
|
|
|
from selenium.webdriver.support import expected_conditions as EC
|
|
|
|
from selenium.webdriver.common.keys import Keys
|
2023-09-03 04:26:26 -04:00
|
|
|
|
2023-11-16 12:10:19 -05:00
|
|
|
from ...typing import CreateResult, Messages
|
2024-01-01 11:48:57 -05:00
|
|
|
from ..base_provider import AbstractProvider
|
2023-11-20 08:00:40 -05:00
|
|
|
from ..helper import format_prompt
|
2023-12-01 23:40:07 -05:00
|
|
|
from ...webdriver import WebDriver, WebDriverSession
|
2023-07-28 06:07:17 -04:00
|
|
|
|
2024-01-01 11:48:57 -05:00
|
|
|
class Bard(AbstractProvider):
|
2023-07-28 06:07:17 -04:00
|
|
|
url = "https://bard.google.com"
|
|
|
|
working = True
|
2023-11-16 12:10:19 -05:00
|
|
|
needs_auth = True
|
2023-07-28 06:07:17 -04:00
|
|
|
|
2023-08-18 05:45:35 -04:00
|
|
|
@classmethod
|
2023-11-16 12:10:19 -05:00
|
|
|
def create_completion(
|
2023-08-18 05:45:35 -04:00
|
|
|
cls,
|
|
|
|
model: str,
|
2023-10-09 07:33:20 -04:00
|
|
|
messages: Messages,
|
2023-11-16 12:10:19 -05:00
|
|
|
stream: bool,
|
2023-08-18 05:45:35 -04:00
|
|
|
proxy: str = None,
|
2023-11-20 08:00:40 -05:00
|
|
|
webdriver: WebDriver = None,
|
2023-11-16 22:50:37 -05:00
|
|
|
user_data_dir: str = None,
|
|
|
|
headless: bool = True,
|
2023-08-27 19:43:45 -04:00
|
|
|
**kwargs
|
2023-11-16 12:10:19 -05:00
|
|
|
) -> CreateResult:
|
2023-08-27 19:43:45 -04:00
|
|
|
prompt = format_prompt(messages)
|
2023-11-20 08:00:40 -05:00
|
|
|
session = WebDriverSession(webdriver, user_data_dir, headless, proxy=proxy)
|
2023-11-18 23:36:04 -05:00
|
|
|
with session as driver:
|
|
|
|
try:
|
2023-11-16 12:10:19 -05:00
|
|
|
driver.get(f"{cls.url}/chat")
|
2023-11-18 23:36:04 -05:00
|
|
|
wait = WebDriverWait(driver, 10 if headless else 240)
|
2023-11-16 12:10:19 -05:00
|
|
|
wait.until(EC.visibility_of_element_located((By.CSS_SELECTOR, "div.ql-editor.textarea")))
|
2023-11-18 23:36:04 -05:00
|
|
|
except:
|
|
|
|
# Reopen browser for login
|
2023-11-20 08:00:40 -05:00
|
|
|
if not webdriver:
|
|
|
|
driver = session.reopen()
|
2023-11-18 23:36:04 -05:00
|
|
|
driver.get(f"{cls.url}/chat")
|
2023-12-06 03:35:36 -05:00
|
|
|
login_url = os.environ.get("G4F_LOGIN_URL")
|
|
|
|
if login_url:
|
|
|
|
yield f"Please login: [Google Bard]({login_url})\n\n"
|
2023-11-18 23:36:04 -05:00
|
|
|
wait = WebDriverWait(driver, 240)
|
|
|
|
wait.until(EC.visibility_of_element_located((By.CSS_SELECTOR, "div.ql-editor.textarea")))
|
|
|
|
else:
|
|
|
|
raise RuntimeError("Prompt textarea not found. You may not be logged in.")
|
2023-10-23 03:46:25 -04:00
|
|
|
|
2023-11-16 12:10:19 -05:00
|
|
|
# Add hook in XMLHttpRequest
|
|
|
|
script = """
|
|
|
|
const _http_request_open = XMLHttpRequest.prototype.open;
|
|
|
|
window._message = "";
|
|
|
|
XMLHttpRequest.prototype.open = function(method, url) {
|
|
|
|
if (url.includes("/assistant.lamda.BardFrontendService/StreamGenerate")) {
|
|
|
|
this.addEventListener("load", (event) => {
|
|
|
|
window._message = JSON.parse(JSON.parse(this.responseText.split("\\n")[3])[0][2])[4][0][1][0];
|
|
|
|
});
|
|
|
|
}
|
|
|
|
return _http_request_open.call(this, method, url);
|
|
|
|
}
|
|
|
|
"""
|
|
|
|
driver.execute_script(script)
|
2023-08-18 05:45:35 -04:00
|
|
|
|
2024-01-10 04:34:56 -05:00
|
|
|
textarea = driver.find_element(By.CSS_SELECTOR, "div.ql-editor.textarea")
|
|
|
|
lines = prompt.splitlines()
|
|
|
|
for idx, line in enumerate(lines):
|
|
|
|
textarea.send_keys(line)
|
|
|
|
if (len(lines) - 1 != idx):
|
|
|
|
textarea.send_keys(Keys.SHIFT + "\n")
|
|
|
|
textarea.send_keys(Keys.ENTER)
|
|
|
|
|
2023-11-16 12:10:19 -05:00
|
|
|
while True:
|
2023-11-17 05:16:49 -05:00
|
|
|
chunk = driver.execute_script("return window._message;")
|
2023-11-16 12:10:19 -05:00
|
|
|
if chunk:
|
|
|
|
yield chunk
|
|
|
|
return
|
|
|
|
else:
|
2023-11-18 23:36:04 -05:00
|
|
|
time.sleep(0.1)
|