Update Bard.py

This commit is contained in:
H Lohaus 2023-11-16 19:28:59 +01:00 committed by GitHub
parent 79c9051743
commit 29df5a21a8
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -1,15 +1,10 @@
from __future__ import annotations from __future__ import annotations
import time import time
try:
from selenium.webdriver.remote.webdriver import WebDriver
except ImportError:
class WebDriver():
pass
from ...typing import CreateResult, Messages from ...typing import CreateResult, Messages
from ..base_provider import BaseProvider from ..base_provider import BaseProvider
from ..helper import format_prompt, get_browser from ..helper import WebDriver, format_prompt, get_browser
class Bard(BaseProvider): class Bard(BaseProvider):
url = "https://bard.google.com" url = "https://bard.google.com"
@ -24,14 +19,14 @@ class Bard(BaseProvider):
stream: bool, stream: bool,
proxy: str = None, proxy: str = None,
browser: WebDriver = None, browser: WebDriver = None,
display: bool = True, hidden_display: bool = True,
**kwargs **kwargs
) -> CreateResult: ) -> CreateResult:
prompt = format_prompt(messages) prompt = format_prompt(messages)
if browser: if browser:
driver = browser driver = browser
else: else:
if display: if hidden_display:
driver, display = get_browser(None, True, proxy) driver, display = get_browser(None, True, proxy)
else: else:
driver = get_browser(None, False, proxy) driver = get_browser(None, False, proxy)
@ -49,7 +44,7 @@ class Bard(BaseProvider):
if not browser: if not browser:
driver.quit() driver.quit()
# New browser should be visible # New browser should be visible
if display: if hidden_display:
display.stop() display.stop()
driver = get_browser(None, False, proxy) driver = get_browser(None, False, proxy)
driver.get(f"{cls.url}/chat") driver.get(f"{cls.url}/chat")
@ -74,11 +69,11 @@ XMLHttpRequest.prototype.open = function(method, url) {
""" """
driver.execute_script(script) driver.execute_script(script)
# Send prompt # Input and submit prompt
driver.find_element(By.CSS_SELECTOR, "div.ql-editor.ql-blank.textarea").send_keys(prompt) driver.find_element(By.CSS_SELECTOR, "div.ql-editor.ql-blank.textarea").send_keys(prompt)
driver.find_element(By.CSS_SELECTOR, "button.send-button").click() driver.find_element(By.CSS_SELECTOR, "button.send-button").click()
# Read response # Yield response
script = "return window._message;" script = "return window._message;"
while True: while True:
chunk = driver.execute_script(script) chunk = driver.execute_script(script)
@ -92,5 +87,5 @@ XMLHttpRequest.prototype.open = function(method, url) {
if not browser: if not browser:
time.sleep(0.1) time.sleep(0.1)
driver.quit() driver.quit()
if display: if hidden_display:
display.stop() display.stop()