mirror of
https://github.com/xtekky/gpt4free.git
synced 2024-09-12 08:29:30 -04:00
Update PerplexityAi.py
This commit is contained in:
parent
cbc08d6014
commit
0625bd0a06
@ -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 PerplexityAi(BaseProvider):
|
class PerplexityAi(BaseProvider):
|
||||||
url = "https://www.perplexity.ai"
|
url = "https://www.perplexity.ai"
|
||||||
@ -27,13 +22,13 @@ class PerplexityAi(BaseProvider):
|
|||||||
timeout: int = 120,
|
timeout: int = 120,
|
||||||
browser: WebDriver = None,
|
browser: WebDriver = None,
|
||||||
copilot: bool = False,
|
copilot: bool = False,
|
||||||
display: bool = True,
|
hidden_display: bool = True,
|
||||||
**kwargs
|
**kwargs
|
||||||
) -> CreateResult:
|
) -> CreateResult:
|
||||||
if browser:
|
if browser:
|
||||||
driver = browser
|
driver = browser
|
||||||
else:
|
else:
|
||||||
if display:
|
if hidden_display:
|
||||||
driver, display = get_browser("", True, proxy)
|
driver, display = get_browser("", True, proxy)
|
||||||
else:
|
else:
|
||||||
driver = get_browser("", False, proxy)
|
driver = get_browser("", False, proxy)
|
||||||
@ -50,6 +45,7 @@ class PerplexityAi(BaseProvider):
|
|||||||
# Page loaded?
|
# Page loaded?
|
||||||
wait.until(EC.visibility_of_element_located((By.CSS_SELECTOR, "textarea[placeholder='Ask anything...']")))
|
wait.until(EC.visibility_of_element_located((By.CSS_SELECTOR, "textarea[placeholder='Ask anything...']")))
|
||||||
|
|
||||||
|
# Add WebSocket hook
|
||||||
script = """
|
script = """
|
||||||
window._message = window._last_message = "";
|
window._message = window._last_message = "";
|
||||||
window._message_finished = false;
|
window._message_finished = false;
|
||||||
@ -80,10 +76,12 @@ WebSocket.prototype.send = function(...args) {
|
|||||||
|
|
||||||
if copilot:
|
if copilot:
|
||||||
try:
|
try:
|
||||||
|
# Check account
|
||||||
driver.find_element(By.CSS_SELECTOR, "img[alt='User avatar']")
|
driver.find_element(By.CSS_SELECTOR, "img[alt='User avatar']")
|
||||||
|
# Enable copilot
|
||||||
driver.find_element(By.CSS_SELECTOR, "button[data-testid='copilot-toggle']").click()
|
driver.find_element(By.CSS_SELECTOR, "button[data-testid='copilot-toggle']").click()
|
||||||
except:
|
except:
|
||||||
pass
|
raise RuntimeError("For copilot you needs a account")
|
||||||
|
|
||||||
# Enter question
|
# Enter question
|
||||||
driver.find_element(By.CSS_SELECTOR, "textarea[placeholder='Ask anything...']").send_keys(prompt)
|
driver.find_element(By.CSS_SELECTOR, "textarea[placeholder='Ask anything...']").send_keys(prompt)
|
||||||
@ -91,6 +89,7 @@ WebSocket.prototype.send = function(...args) {
|
|||||||
driver.find_element(By.CSS_SELECTOR, "button.bg-super svg[data-icon='arrow-right']").click()
|
driver.find_element(By.CSS_SELECTOR, "button.bg-super svg[data-icon='arrow-right']").click()
|
||||||
|
|
||||||
try:
|
try:
|
||||||
|
# Yield response
|
||||||
script = """
|
script = """
|
||||||
if(window._message && window._message != window._last_message) {
|
if(window._message && window._message != window._last_message) {
|
||||||
try {
|
try {
|
||||||
@ -117,5 +116,5 @@ if(window._message && window._message != window._last_message) {
|
|||||||
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()
|
Loading…
Reference in New Issue
Block a user