webscout 3.0b0__py3-none-any.whl → 3.1b0__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.

Potentially problematic release.


This version of webscout might be problematic. Click here for more details.

@@ -1,217 +1,217 @@
1
- import time
2
- import uuid
3
- from selenium import webdriver
4
- from selenium.webdriver.chrome.options import Options
5
- from selenium.webdriver.common.by import By
6
- from selenium.webdriver.support import expected_conditions as EC
7
- from selenium.webdriver.support.ui import WebDriverWait
8
- import click
9
- import requests
10
- from requests import get
11
- from uuid import uuid4
12
- from re import findall
13
- from requests.exceptions import RequestException
14
- from curl_cffi.requests import get, RequestsError
15
- import g4f
16
- from random import randint
17
- from PIL import Image
18
- import io
19
- import re
20
- import json
21
- import yaml
22
- from ..AIutel import Optimizers
23
- from ..AIutel import Conversation
24
- from ..AIutel import AwesomePrompts, sanitize_stream
25
- from ..AIbase import Provider, AsyncProvider
26
- from Helpingai_T2 import Perplexity
27
- from webscout import exceptions
28
- from typing import Any, AsyncGenerator, Dict
29
- import logging
30
- import httpx
31
- from Bard import Chatbot
32
- import logging
33
- from os import path
34
- from json import load
35
- from json import dumps
36
- import warnings
37
- logging.getLogger("httpx").setLevel(logging.ERROR)
38
- warnings.simplefilter("ignore", category=UserWarning)
39
- class GEMINI(Provider):
40
- def __init__(
41
- self,
42
- cookie_file: str,
43
- proxy: dict = {},
44
- timeout: int = 30,
45
- ):
46
- """Initializes GEMINI
47
-
48
- Args:
49
- cookie_file (str): Path to `bard.google.com.cookies.json` file
50
- proxy (dict, optional): Http request proxy. Defaults to {}.
51
- timeout (int, optional): Http request timeout. Defaults to 30.
52
- """
53
- self.conversation = Conversation(False)
54
- self.session_auth1 = None
55
- self.session_auth2 = None
56
- assert isinstance(
57
- cookie_file, str
58
- ), f"cookie_file should be of {str} only not '{type(cookie_file)}'"
59
- if path.isfile(cookie_file):
60
- # let's assume auth is a path to exported .json cookie-file
61
- with open(cookie_file) as fh:
62
- entries = load(fh)
63
- for entry in entries:
64
- if entry["name"] == "__Secure-1PSID":
65
- self.session_auth1 = entry["value"]
66
- elif entry["name"] == "__Secure-1PSIDTS":
67
- self.session_auth2 = entry["value"]
68
-
69
- assert all(
70
- [self.session_auth1, self.session_auth2]
71
- ), f"Failed to extract the required cookie value from file '{cookie_file}'"
72
- else:
73
- raise Exception(f"{cookie_file} is not a valid file path")
74
-
75
- self.session = Chatbot(self.session_auth1, self.session_auth2, proxy, timeout)
76
- self.last_response = {}
77
- self.__available_optimizers = (
78
- method
79
- for method in dir(Optimizers)
80
- if callable(getattr(Optimizers, method)) and not method.startswith("__")
81
- )
82
-
83
- def ask(
84
- self,
85
- prompt: str,
86
- stream: bool = False,
87
- raw: bool = False,
88
- optimizer: str = None,
89
- conversationally: bool = False,
90
- ) -> dict:
91
- """Chat with AI
92
-
93
- Args:
94
- prompt (str): Prompt to be send.
95
- stream (bool, optional): Flag for streaming response. Defaults to False.
96
- raw (bool, optional): Stream back raw response as received. Defaults to False.
97
- optimizer (str, optional): Prompt optimizer name - `[code, shell_command]`. Defeaults to None
98
- conversationally (bool, optional): Chat conversationally when using optimizer. Defaults to False.
99
- Returns:
100
- dict : {}
101
- ```json
102
- {
103
- "content": "General Kenobi! \n\n(I couldn't help but respond with the iconic Star Wars greeting since you used it first. )\n\nIs there anything I can help you with today?\n[Image of Hello there General Kenobi]",
104
- "conversation_id": "c_f13f6217f9a997aa",
105
- "response_id": "r_d3665f95975c368f",
106
- "factualityQueries": null,
107
- "textQuery": [
108
- "hello there",
109
- 1
110
- ],
111
- "choices": [
112
- {
113
- "id": "rc_ea075c9671bfd8cb",
114
- "content": [
115
- "General Kenobi! \n\n(I couldn't help but respond with the iconic Star Wars greeting since you used it first. )\n\nIs there anything I can help you with today?\n[Image of Hello there General Kenobi]"
116
- ]
117
- },
118
- {
119
- "id": "rc_de6dd3fb793a5402",
120
- "content": [
121
- "General Kenobi! (or just a friendly hello, whichever you prefer!). \n\nI see you're a person of culture as well. *Star Wars* references are always appreciated. \n\nHow can I help you today?\n"
122
- ]
123
- },
124
- {
125
- "id": "rc_a672ac089caf32db",
126
- "content": [
127
- "General Kenobi! (or just a friendly hello if you're not a Star Wars fan!). \n\nHow can I help you today? Feel free to ask me anything, or tell me what you'd like to chat about. I'm here to assist in any way I can.\n[Image of Obi-Wan Kenobi saying hello there]"
128
- ]
129
- }
130
- ],
131
-
132
- "images": [
133
- "https://i.pinimg.com/originals/40/74/60/407460925c9e419d82b93313f0b42f71.jpg"
134
- ]
135
- }
136
-
137
- ```
138
- """
139
- conversation_prompt = self.conversation.gen_complete_prompt(prompt)
140
- if optimizer:
141
- if optimizer in self.__available_optimizers:
142
- conversation_prompt = getattr(Optimizers, optimizer)(
143
- conversation_prompt if conversationally else prompt
144
- )
145
- else:
146
- raise Exception(
147
- f"Optimizer is not one of {self.__available_optimizers}"
148
- )
149
-
150
- def for_stream():
151
- response = self.session.ask(prompt)
152
- self.last_response.update(response)
153
- self.conversation.update_chat_history(
154
- prompt, self.get_message(self.last_response)
155
- )
156
- yield dumps(response) if raw else response
157
-
158
- def for_non_stream():
159
- # let's make use of stream
160
- for _ in for_stream():
161
- pass
162
- return self.last_response
163
-
164
- return for_stream() if stream else for_non_stream()
165
-
166
- def chat(
167
- self,
168
- prompt: str,
169
- stream: bool = False,
170
- optimizer: str = None,
171
- conversationally: bool = False,
172
- ) -> str:
173
- """Generate response `str`
174
- Args:
175
- prompt (str): Prompt to be send.
176
- stream (bool, optional): Flag for streaming response. Defaults to False.
177
- optimizer (str, optional): Prompt optimizer name - `[code, shell_command]`. Defaults to None.
178
- conversationally (bool, optional): Chat conversationally when using optimizer. Defaults to False.
179
- Returns:
180
- str: Response generated
181
- """
182
-
183
- def for_stream():
184
- for response in self.ask(
185
- prompt, True, optimizer=optimizer, conversationally=conversationally
186
- ):
187
- yield self.get_message(response)
188
-
189
- def for_non_stream():
190
- return self.get_message(
191
- self.ask(
192
- prompt,
193
- False,
194
- optimizer=optimizer,
195
- conversationally=conversationally,
196
- )
197
- )
198
-
199
- return for_stream() if stream else for_non_stream()
200
-
201
- def get_message(self, response: dict) -> str:
202
- """Retrieves message only from response
203
-
204
- Args:
205
- response (dict): Response generated by `self.ask`
206
-
207
- Returns:
208
- str: Message extracted
209
- """
210
- assert isinstance(response, dict), "Response should be of dict data-type only"
211
- return response["content"]
212
-
213
- def reset(self):
214
- """Reset the current conversation"""
215
- self.session.async_chatbot.conversation_id = ""
216
- self.session.async_chatbot.response_id = ""
1
+ import time
2
+ import uuid
3
+ from selenium import webdriver
4
+ from selenium.webdriver.chrome.options import Options
5
+ from selenium.webdriver.common.by import By
6
+ from selenium.webdriver.support import expected_conditions as EC
7
+ from selenium.webdriver.support.ui import WebDriverWait
8
+ import click
9
+ import requests
10
+ from requests import get
11
+ from uuid import uuid4
12
+ from re import findall
13
+ from requests.exceptions import RequestException
14
+ from curl_cffi.requests import get, RequestsError
15
+ import g4f
16
+ from random import randint
17
+ from PIL import Image
18
+ import io
19
+ import re
20
+ import json
21
+ import yaml
22
+ from ..AIutel import Optimizers
23
+ from ..AIutel import Conversation
24
+ from ..AIutel import AwesomePrompts, sanitize_stream
25
+ from ..AIbase import Provider, AsyncProvider
26
+ from Helpingai_T2 import Perplexity
27
+ from webscout import exceptions
28
+ from typing import Any, AsyncGenerator, Dict
29
+ import logging
30
+ import httpx
31
+ from Bard import Chatbot
32
+ import logging
33
+ from os import path
34
+ from json import load
35
+ from json import dumps
36
+ import warnings
37
+ logging.getLogger("httpx").setLevel(logging.ERROR)
38
+ warnings.simplefilter("ignore", category=UserWarning)
39
+ class GEMINI(Provider):
40
+ def __init__(
41
+ self,
42
+ cookie_file: str,
43
+ proxy: dict = {},
44
+ timeout: int = 30,
45
+ ):
46
+ """Initializes GEMINI
47
+
48
+ Args:
49
+ cookie_file (str): Path to `bard.google.com.cookies.json` file
50
+ proxy (dict, optional): Http request proxy. Defaults to {}.
51
+ timeout (int, optional): Http request timeout. Defaults to 30.
52
+ """
53
+ self.conversation = Conversation(False)
54
+ self.session_auth1 = None
55
+ self.session_auth2 = None
56
+ assert isinstance(
57
+ cookie_file, str
58
+ ), f"cookie_file should be of {str} only not '{type(cookie_file)}'"
59
+ if path.isfile(cookie_file):
60
+ # let's assume auth is a path to exported .json cookie-file
61
+ with open(cookie_file) as fh:
62
+ entries = load(fh)
63
+ for entry in entries:
64
+ if entry["name"] == "__Secure-1PSID":
65
+ self.session_auth1 = entry["value"]
66
+ elif entry["name"] == "__Secure-1PSIDTS":
67
+ self.session_auth2 = entry["value"]
68
+
69
+ assert all(
70
+ [self.session_auth1, self.session_auth2]
71
+ ), f"Failed to extract the required cookie value from file '{cookie_file}'"
72
+ else:
73
+ raise Exception(f"{cookie_file} is not a valid file path")
74
+
75
+ self.session = Chatbot(self.session_auth1, self.session_auth2, proxy, timeout)
76
+ self.last_response = {}
77
+ self.__available_optimizers = (
78
+ method
79
+ for method in dir(Optimizers)
80
+ if callable(getattr(Optimizers, method)) and not method.startswith("__")
81
+ )
82
+
83
+ def ask(
84
+ self,
85
+ prompt: str,
86
+ stream: bool = False,
87
+ raw: bool = False,
88
+ optimizer: str = None,
89
+ conversationally: bool = False,
90
+ ) -> dict:
91
+ """Chat with AI
92
+
93
+ Args:
94
+ prompt (str): Prompt to be send.
95
+ stream (bool, optional): Flag for streaming response. Defaults to False.
96
+ raw (bool, optional): Stream back raw response as received. Defaults to False.
97
+ optimizer (str, optional): Prompt optimizer name - `[code, shell_command]`. Defeaults to None
98
+ conversationally (bool, optional): Chat conversationally when using optimizer. Defaults to False.
99
+ Returns:
100
+ dict : {}
101
+ ```json
102
+ {
103
+ "content": "General Kenobi! \n\n(I couldn't help but respond with the iconic Star Wars greeting since you used it first. )\n\nIs there anything I can help you with today?\n[Image of Hello there General Kenobi]",
104
+ "conversation_id": "c_f13f6217f9a997aa",
105
+ "response_id": "r_d3665f95975c368f",
106
+ "factualityQueries": null,
107
+ "textQuery": [
108
+ "hello there",
109
+ 1
110
+ ],
111
+ "choices": [
112
+ {
113
+ "id": "rc_ea075c9671bfd8cb",
114
+ "content": [
115
+ "General Kenobi! \n\n(I couldn't help but respond with the iconic Star Wars greeting since you used it first. )\n\nIs there anything I can help you with today?\n[Image of Hello there General Kenobi]"
116
+ ]
117
+ },
118
+ {
119
+ "id": "rc_de6dd3fb793a5402",
120
+ "content": [
121
+ "General Kenobi! (or just a friendly hello, whichever you prefer!). \n\nI see you're a person of culture as well. *Star Wars* references are always appreciated. \n\nHow can I help you today?\n"
122
+ ]
123
+ },
124
+ {
125
+ "id": "rc_a672ac089caf32db",
126
+ "content": [
127
+ "General Kenobi! (or just a friendly hello if you're not a Star Wars fan!). \n\nHow can I help you today? Feel free to ask me anything, or tell me what you'd like to chat about. I'm here to assist in any way I can.\n[Image of Obi-Wan Kenobi saying hello there]"
128
+ ]
129
+ }
130
+ ],
131
+
132
+ "images": [
133
+ "https://i.pinimg.com/originals/40/74/60/407460925c9e419d82b93313f0b42f71.jpg"
134
+ ]
135
+ }
136
+
137
+ ```
138
+ """
139
+ conversation_prompt = self.conversation.gen_complete_prompt(prompt)
140
+ if optimizer:
141
+ if optimizer in self.__available_optimizers:
142
+ conversation_prompt = getattr(Optimizers, optimizer)(
143
+ conversation_prompt if conversationally else prompt
144
+ )
145
+ else:
146
+ raise Exception(
147
+ f"Optimizer is not one of {self.__available_optimizers}"
148
+ )
149
+
150
+ def for_stream():
151
+ response = self.session.ask(prompt)
152
+ self.last_response.update(response)
153
+ self.conversation.update_chat_history(
154
+ prompt, self.get_message(self.last_response)
155
+ )
156
+ yield dumps(response) if raw else response
157
+
158
+ def for_non_stream():
159
+ # let's make use of stream
160
+ for _ in for_stream():
161
+ pass
162
+ return self.last_response
163
+
164
+ return for_stream() if stream else for_non_stream()
165
+
166
+ def chat(
167
+ self,
168
+ prompt: str,
169
+ stream: bool = False,
170
+ optimizer: str = None,
171
+ conversationally: bool = False,
172
+ ) -> str:
173
+ """Generate response `str`
174
+ Args:
175
+ prompt (str): Prompt to be send.
176
+ stream (bool, optional): Flag for streaming response. Defaults to False.
177
+ optimizer (str, optional): Prompt optimizer name - `[code, shell_command]`. Defaults to None.
178
+ conversationally (bool, optional): Chat conversationally when using optimizer. Defaults to False.
179
+ Returns:
180
+ str: Response generated
181
+ """
182
+
183
+ def for_stream():
184
+ for response in self.ask(
185
+ prompt, True, optimizer=optimizer, conversationally=conversationally
186
+ ):
187
+ yield self.get_message(response)
188
+
189
+ def for_non_stream():
190
+ return self.get_message(
191
+ self.ask(
192
+ prompt,
193
+ False,
194
+ optimizer=optimizer,
195
+ conversationally=conversationally,
196
+ )
197
+ )
198
+
199
+ return for_stream() if stream else for_non_stream()
200
+
201
+ def get_message(self, response: dict) -> str:
202
+ """Retrieves message only from response
203
+
204
+ Args:
205
+ response (dict): Response generated by `self.ask`
206
+
207
+ Returns:
208
+ str: Message extracted
209
+ """
210
+ assert isinstance(response, dict), "Response should be of dict data-type only"
211
+ return response["content"]
212
+
213
+ def reset(self):
214
+ """Reset the current conversation"""
215
+ self.session.async_chatbot.conversation_id = ""
216
+ self.session.async_chatbot.response_id = ""
217
217
  self.session.async_chatbot.choice_id = ""