From 433c33906f0507da8a8558383d81294c071e17e6 Mon Sep 17 00:00:00 2001 From: keenborder786 <21110290@lums.edu.pk> Date: Sat, 22 Jun 2024 14:47:18 +0500 Subject: [PATCH] [fix] --- README.md | 18 ++++++---- examples/gpt.py | 4 +-- src/free_llms/models.py | 58 ++++----------------------------- tests/unit_tests/test_models.py | 19 ++++------- 4 files changed, 25 insertions(+), 74 deletions(-) diff --git a/README.md b/README.md index 25fd22c..c23b366 100644 --- a/README.md +++ b/README.md @@ -34,14 +34,18 @@ pip install free_llms ```python from free_llms.models import GPTChrome -driver_config = [] # pass in selnium driver config except for the following ["--disable-gpu", f"--window-size=1920,1080"] -with GPTChrome(driver_config=driver_config, - email = 'email', - password = 'password') as session: # A single session started with ChartGPT + +driver_config = [] # pass in selnium driver config except for the following ["--disable-gpu", f"--window-size=1920,1080"] +with GPTChrome( + driver_config=driver_config, + email="", # for gpt we do not need email + password="", # # for gpt we do not need password +) as session: # A single session started with ChartGPT data = session.send_prompt("""Write an SQL Query which shows how to get third highest salary - """) # First Message - data1 = session.send_prompt('Now convert it into python') # Second message - print(session.messages) # Messages in the current session in pair of + """) # First Message + data1 = session.send_prompt("Now convert it into python") # Second message + print(session.messages) # Messages in the current session in pair of + ``` diff --git a/examples/gpt.py b/examples/gpt.py index c3aa4b7..52125bc 100644 --- a/examples/gpt.py +++ b/examples/gpt.py @@ -3,8 +3,8 @@ driver_config = [] # pass in selnium driver config except for the following ["--disable-gpu", f"--window-size=1920,1080"] with GPTChrome( driver_config=driver_config, - email="21110290@lums.edu.pk", - password="password", + email="", # for gpt we do not need email + password="", # # for gpt we do not need password ) as session: # A single session started with ChartGPT data = session.send_prompt("""Write an SQL Query which shows how to get third highest salary """) # First Message diff --git a/src/free_llms/models.py b/src/free_llms/models.py index b3926ea..0334540 100644 --- a/src/free_llms/models.py +++ b/src/free_llms/models.py @@ -177,69 +177,23 @@ class GPTChrome(LLMChrome): @property def _model_url(self) -> str: - return "https://chatgpt.com/auth/login?sso=" + return "https://chatgpt.com/" @property def _elements_identifier(self) -> Dict[str, str]: return { - "Login": '//*[@id="__next"]/div[1]/div[2]/div[1]/div/div/button[1]', # noqa: E501 - "Email": "username", - "Email_Continue": "action", - "Password": '//*[@id="password"]', "Prompt_Text_Area": "prompt-textarea", - "Prompt_Text_Output": '//*[@id="__next"]/div[1]/div[2]/main/div[2]/div[1]/div/div/div/div[{current}]/div/div/div[2]/div[2]/div[1]/div/div', # noqa: E501 + "Prompt_Text_Output": "/html/body/div[1]/div[1]/div[2]/main/div[2]/div[1]/div/div/div/div/div[{current}]/div/div/div[2]/div[2]/div[1]/div/div", # noqa: E501 } def login(self, retries_attempt: int = 3) -> bool: self.driver.get(self._model_url) - for i in range(retries_attempt): - self.run_manager.on_text(text=f"Making login attempt no. {i+1} on ChatGPT", verbose=self.verbosity) - try: - login_button = WebDriverWait(self.driver, self.waiting_time).until( - EC.element_to_be_clickable((By.XPATH, self._elements_identifier["Login"])) - ) - login_button.click() - email_input = WebDriverWait(self.driver, self.waiting_time).until( - EC.presence_of_element_located((By.ID, self._elements_identifier["Email"])) - ) - email_input.click() - email_input.send_keys(self.email) - continue_button = WebDriverWait(self.driver, self.waiting_time).until( - EC.presence_of_element_located((By.NAME, self._elements_identifier["Email_Continue"])) - ) - continue_button.click() - password_button = WebDriverWait(self.driver, self.waiting_time).until( - EC.element_to_be_clickable((By.XPATH, self._elements_identifier["Password"])) - ) - password_button.clear() - password_button.click() - password_button.send_keys(self.password) - password_button.submit() - self.run_manager.on_text(text=f"Login succeed on attempt no. {i+1}", verbose=self.verbosity) - return True - except TimeoutException: - continue - return False + return True def send_prompt(self, query: str) -> AIMessage: - while True: - try: - text_area = WebDriverWait(self.driver, self.waiting_time).until( - EC.presence_of_element_located((By.ID, self._elements_identifier["Prompt_Text_Area"])) - ) - break - except TimeoutException: - current_url = self.driver.current_url - self.driver.quit() - _chrome_version = os.environ.get("CHROME_VERSION", None) - - self.driver = uc.Chrome( - options=configure_options(self.driver_config + DRIVERS_DEFAULT_CONFIG), - version_main=int(_chrome_version) if _chrome_version is not None else _chrome_version, - headless=True, - ) - self.run_manager.on_text(text="Captacha Detected on ChatGPT. Starting Annoymous Session", verbose=self.verbosity) - self.driver.get(current_url) + text_area = WebDriverWait(self.driver, self.waiting_time).until( + EC.presence_of_element_located((By.ID, self._elements_identifier["Prompt_Text_Area"])) + ) text_area.click() text_area.send_keys(query) diff --git a/tests/unit_tests/test_models.py b/tests/unit_tests/test_models.py index 28c95a3..56fca7c 100644 --- a/tests/unit_tests/test_models.py +++ b/tests/unit_tests/test_models.py @@ -4,19 +4,12 @@ def test_gpt_chrome(): - with pytest.raises(ValueError, match="Cannot Login given the credentials"): - with GPTChrome(driver_config=[], email="wrong_email", password="wrong_password", retries_attempt=1) as session: - pass - assert GPTChrome(driver_config=[], email="wrong_email", password="wrong_password")._elements_identifier == { - "Login": '//*[@id="__next"]/div[1]/div[2]/div[1]/div/div/button[1]', # noqa: E501 - "Email": "username", - "Email_Continue": "action", - "Password": '//*[@id="password"]', - "Prompt_Text_Area": "prompt-textarea", - "Prompt_Text_Output": '//*[@id="__next"]/div[1]/div[2]/main/div[2]/div[1]/div/div/div/div[{current}]/div/div/div[2]/div[2]/div[1]/div/div', # noqa: E501 - } - assert GPTChrome(driver_config=[], email="wrong_email", password="wrong_password")._model_url == "https://chatgpt.com/auth/login?sso=" - chrome_instance = GPTChrome(driver_config=[], email="wrong_email", password="wrong_password", retries_attempt=1) + assert GPTChrome(driver_config=[], email="", password="")._elements_identifier == { + "Prompt_Text_Area": "prompt-textarea", + "Prompt_Text_Output": '/html/body/div[1]/div[1]/div[2]/main/div[2]/div[1]/div/div/div/div/div[{current}]/div/div/div[2]/div[2]/div[1]/div/div', # noqa: E501 + } + assert GPTChrome(driver_config=[], email="", password="")._model_url == "https://chatgpt.com/" + chrome_instance = GPTChrome(driver_config=[], email="", password="", retries_attempt=1) chrome_instance.driver.get("https://chatgpt.com/") ans = chrome_instance.send_prompt("How are you doing?") assert isinstance(ans, AIMessage)