summaryrefslogtreecommitdiffstats
path: root/g4f/Provider/Phind.py
diff options
context:
space:
mode:
authorHeiner Lohaus <hlohaus@users.noreply.github.com>2023-11-18 04:38:31 +0100
committerHeiner Lohaus <hlohaus@users.noreply.github.com>2023-11-18 04:38:31 +0100
commitcadc507fad2fee59b23d1d8e73c472c077f468fc (patch)
tree42b1c0c0ccf5e3aa7a843a3766d4d19b6a7ade8e /g4f/Provider/Phind.py
parentImprove providers (diff)
downloadgpt4free-cadc507fad2fee59b23d1d8e73c472c077f468fc.tar
gpt4free-cadc507fad2fee59b23d1d8e73c472c077f468fc.tar.gz
gpt4free-cadc507fad2fee59b23d1d8e73c472c077f468fc.tar.bz2
gpt4free-cadc507fad2fee59b23d1d8e73c472c077f468fc.tar.lz
gpt4free-cadc507fad2fee59b23d1d8e73c472c077f468fc.tar.xz
gpt4free-cadc507fad2fee59b23d1d8e73c472c077f468fc.tar.zst
gpt4free-cadc507fad2fee59b23d1d8e73c472c077f468fc.zip
Diffstat (limited to 'g4f/Provider/Phind.py')
-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 != "":