summaryrefslogtreecommitdiffstats
path: root/g4f/Provider/Phind.py
diff options
context:
space:
mode:
authorTekky <98614666+xtekky@users.noreply.github.com>2023-11-18 12:24:39 +0100
committerGitHub <noreply@github.com>2023-11-18 12:24:39 +0100
commit2fcb3f949bbde7f5380acc68dc6b1f938dec12bc (patch)
tree035bd400b4057a744b29a0ebee96579bfbf429f6 /g4f/Provider/Phind.py
parent~ | g4f `v-0.1.8.5` (diff)
parentAdd AiChatOnline, ChatgptDemoAi, ChatgptNext Providers (diff)
downloadgpt4free-2fcb3f949bbde7f5380acc68dc6b1f938dec12bc.tar
gpt4free-2fcb3f949bbde7f5380acc68dc6b1f938dec12bc.tar.gz
gpt4free-2fcb3f949bbde7f5380acc68dc6b1f938dec12bc.tar.bz2
gpt4free-2fcb3f949bbde7f5380acc68dc6b1f938dec12bc.tar.lz
gpt4free-2fcb3f949bbde7f5380acc68dc6b1f938dec12bc.tar.xz
gpt4free-2fcb3f949bbde7f5380acc68dc6b1f938dec12bc.tar.zst
gpt4free-2fcb3f949bbde7f5380acc68dc6b1f938dec12bc.zip
Diffstat (limited to '')
-rw-r--r--g4f/Provider/Phind.py79
1 files changed, 39 insertions, 40 deletions
diff --git a/g4f/Provider/Phind.py b/g4f/Provider/Phind.py
index 0cb17ddb..34abbe35 100644
--- a/g4f/Provider/Phind.py
+++ b/g4f/Provider/Phind.py
@@ -23,49 +23,31 @@ class Phind(BaseProvider):
timeout: int = 120,
browser: WebDriver = None,
creative_mode: bool = None,
- headless: bool = True,
**kwargs
) -> CreateResult:
- driver = browser if browser else get_browser("", headless, proxy)
-
- from selenium.webdriver.common.by import By
- from selenium.webdriver.support.ui import WebDriverWait
- from selenium.webdriver.support import expected_conditions as EC
+ try:
+ driver = browser if browser else get_browser("", False, proxy)
- prompt = quote(format_prompt(messages))
- driver.get(f"{cls.url}/search?q={prompt}&source=searchbox")
+ from selenium.webdriver.common.by import By
+ from selenium.webdriver.support.ui import WebDriverWait
+ from selenium.webdriver.support import expected_conditions as EC
- # Need to change settinge
- if model.startswith("gpt-4") or creative_mode:
- wait = WebDriverWait(driver, timeout)
- # Open settings dropdown
- wait.until(EC.visibility_of_element_located((By.CSS_SELECTOR, "button.text-dark.dropdown-toggle")))
- driver.find_element(By.CSS_SELECTOR, "button.text-dark.dropdown-toggle").click()
- # Wait for dropdown toggle
- wait.until(EC.visibility_of_element_located((By.XPATH, "//button[text()='GPT-4']")))
- # Enable GPT-4
- if model.startswith("gpt-4"):
- driver.find_element(By.XPATH, "//button[text()='GPT-4']").click()
- # Enable creative mode
- if creative_mode or creative_mode == None:
- driver.find_element(By.ID, "Creative Mode").click()
- # Submit changes
- driver.find_element(By.CSS_SELECTOR, ".search-bar-input-group button[type='submit']").click()
- # Wait for page reload
- wait.until(EC.visibility_of_element_located((By.CSS_SELECTOR, ".search-container")))
+ prompt = quote(format_prompt(messages))
+ driver.get(f"{cls.url}/search?q={prompt}&source=searchbox")
- try:
- # Add fetch hook
- script = """
+ # Register fetch hook
+ driver.execute_script("""
window._fetch = window.fetch;
window.fetch = (url, options) => {
// Call parent fetch method
const result = window._fetch(url, options);
- if (url != "/api/infer/answer") return result;
+ if (url != "/api/infer/answer") {
+ return result;
+ }
// Load response reader
result.then((response) => {
if (!response.body.locked) {
- window.reader = response.body.getReader();
+ window._reader = response.body.getReader();
}
});
// Return dummy response
@@ -73,12 +55,31 @@ window.fetch = (url, options) => {
resolve(new Response(new ReadableStream()))
});
}
-"""
- # Read response from reader
- driver.execute_script(script)
- script = """
-if(window.reader) {
- chunk = await window.reader.read();
+""")
+
+ # Need to change settings
+ if model.startswith("gpt-4") or creative_mode:
+ wait = WebDriverWait(driver, timeout)
+ # Open settings dropdown
+ wait.until(EC.visibility_of_element_located((By.CSS_SELECTOR, "button.text-dark.dropdown-toggle")))
+ driver.find_element(By.CSS_SELECTOR, "button.text-dark.dropdown-toggle").click()
+ # Wait for dropdown toggle
+ wait.until(EC.visibility_of_element_located((By.XPATH, "//button[text()='GPT-4']")))
+ # Enable GPT-4
+ if model.startswith("gpt-4"):
+ driver.find_element(By.XPATH, "//button[text()='GPT-4']").click()
+ # Enable creative mode
+ if creative_mode or creative_mode == None:
+ driver.find_element(By.ID, "Creative Mode").click()
+ # Submit changes
+ driver.find_element(By.CSS_SELECTOR, ".search-bar-input-group button[type='submit']").click()
+ # Wait for page reload
+ wait.until(EC.visibility_of_element_located((By.CSS_SELECTOR, ".search-container")))
+
+ while True:
+ chunk = driver.execute_script("""
+if(window._reader) {
+ chunk = await window._reader.read();
if (chunk['done']) return null;
text = (new TextDecoder()).decode(chunk['value']);
content = '';
@@ -95,9 +96,7 @@ if(window.reader) {
} else {
return ''
}
-"""
- while True:
- chunk = driver.execute_script(script)
+""")
if chunk:
yield chunk
elif chunk != "":