webscout 6.3__py3-none-any.whl → 6.5__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.

Files changed (131) hide show
  1. webscout/AIauto.py +191 -176
  2. webscout/AIbase.py +0 -197
  3. webscout/AIutel.py +441 -1130
  4. webscout/DWEBS.py +189 -35
  5. webscout/{YTdownloader.py → Extra/YTToolkit/YTdownloader.py} +990 -1103
  6. webscout/Extra/YTToolkit/__init__.py +3 -0
  7. webscout/{transcriber.py → Extra/YTToolkit/transcriber.py} +479 -551
  8. webscout/Extra/YTToolkit/ytapi/__init__.py +6 -0
  9. webscout/Extra/YTToolkit/ytapi/channel.py +307 -0
  10. webscout/Extra/YTToolkit/ytapi/errors.py +13 -0
  11. webscout/Extra/YTToolkit/ytapi/extras.py +45 -0
  12. webscout/Extra/YTToolkit/ytapi/https.py +88 -0
  13. webscout/Extra/YTToolkit/ytapi/patterns.py +61 -0
  14. webscout/Extra/YTToolkit/ytapi/playlist.py +59 -0
  15. webscout/Extra/YTToolkit/ytapi/pool.py +8 -0
  16. webscout/Extra/YTToolkit/ytapi/query.py +37 -0
  17. webscout/Extra/YTToolkit/ytapi/stream.py +60 -0
  18. webscout/Extra/YTToolkit/ytapi/utils.py +62 -0
  19. webscout/Extra/YTToolkit/ytapi/video.py +102 -0
  20. webscout/Extra/__init__.py +3 -1
  21. webscout/Extra/autocoder/__init__.py +9 -0
  22. webscout/Extra/autocoder/autocoder_utiles.py +121 -0
  23. webscout/Extra/autocoder/rawdog.py +680 -0
  24. webscout/Extra/autollama.py +246 -195
  25. webscout/Extra/gguf.py +81 -56
  26. webscout/Extra/markdownlite/__init__.py +862 -0
  27. webscout/Extra/weather_ascii.py +2 -2
  28. webscout/LLM.py +206 -43
  29. webscout/Litlogger/__init__.py +681 -0
  30. webscout/Provider/DARKAI.py +1 -1
  31. webscout/Provider/EDITEE.py +1 -1
  32. webscout/Provider/NinjaChat.py +1 -1
  33. webscout/Provider/PI.py +120 -35
  34. webscout/Provider/Perplexity.py +590 -598
  35. webscout/Provider/Reka.py +0 -1
  36. webscout/Provider/RoboCoders.py +206 -0
  37. webscout/Provider/TTI/AiForce/__init__.py +22 -0
  38. webscout/Provider/TTI/AiForce/async_aiforce.py +257 -0
  39. webscout/Provider/TTI/AiForce/sync_aiforce.py +242 -0
  40. webscout/Provider/TTI/Nexra/__init__.py +22 -0
  41. webscout/Provider/TTI/Nexra/async_nexra.py +286 -0
  42. webscout/Provider/TTI/Nexra/sync_nexra.py +258 -0
  43. webscout/Provider/TTI/PollinationsAI/__init__.py +23 -0
  44. webscout/Provider/TTI/PollinationsAI/async_pollinations.py +330 -0
  45. webscout/Provider/TTI/PollinationsAI/sync_pollinations.py +285 -0
  46. webscout/Provider/TTI/__init__.py +2 -4
  47. webscout/Provider/TTI/artbit/__init__.py +22 -0
  48. webscout/Provider/TTI/artbit/async_artbit.py +184 -0
  49. webscout/Provider/TTI/artbit/sync_artbit.py +176 -0
  50. webscout/Provider/TTI/blackbox/__init__.py +4 -0
  51. webscout/Provider/TTI/blackbox/async_blackbox.py +212 -0
  52. webscout/Provider/TTI/{blackboximage.py → blackbox/sync_blackbox.py} +199 -153
  53. webscout/Provider/TTI/deepinfra/__init__.py +4 -0
  54. webscout/Provider/TTI/deepinfra/async_deepinfra.py +227 -0
  55. webscout/Provider/TTI/deepinfra/sync_deepinfra.py +199 -0
  56. webscout/Provider/TTI/huggingface/__init__.py +22 -0
  57. webscout/Provider/TTI/huggingface/async_huggingface.py +199 -0
  58. webscout/Provider/TTI/huggingface/sync_huggingface.py +195 -0
  59. webscout/Provider/TTI/imgninza/__init__.py +4 -0
  60. webscout/Provider/TTI/imgninza/async_ninza.py +214 -0
  61. webscout/Provider/TTI/{imgninza.py → imgninza/sync_ninza.py} +209 -136
  62. webscout/Provider/TTI/talkai/__init__.py +4 -0
  63. webscout/Provider/TTI/talkai/async_talkai.py +229 -0
  64. webscout/Provider/TTI/talkai/sync_talkai.py +207 -0
  65. webscout/Provider/TTS/__init__.py +5 -1
  66. webscout/Provider/TTS/deepgram.py +183 -0
  67. webscout/Provider/TTS/elevenlabs.py +137 -0
  68. webscout/Provider/TTS/gesserit.py +151 -0
  69. webscout/Provider/TTS/murfai.py +139 -0
  70. webscout/Provider/TTS/parler.py +134 -107
  71. webscout/Provider/TTS/streamElements.py +360 -275
  72. webscout/Provider/TTS/utils.py +280 -0
  73. webscout/Provider/TTS/voicepod.py +116 -116
  74. webscout/Provider/__init__.py +8 -1
  75. webscout/Provider/askmyai.py +2 -2
  76. webscout/Provider/cerebras.py +227 -219
  77. webscout/Provider/llama3mitril.py +0 -1
  78. webscout/Provider/meta.py +794 -779
  79. webscout/Provider/mhystical.py +176 -0
  80. webscout/Provider/perplexitylabs.py +265 -0
  81. webscout/Provider/twitterclone.py +251 -245
  82. webscout/Provider/typegpt.py +358 -0
  83. webscout/__init__.py +9 -8
  84. webscout/__main__.py +5 -5
  85. webscout/cli.py +252 -280
  86. webscout/conversation.py +227 -0
  87. webscout/exceptions.py +161 -29
  88. webscout/litagent/__init__.py +172 -0
  89. webscout/litprinter/__init__.py +832 -0
  90. webscout/optimizers.py +270 -0
  91. webscout/prompt_manager.py +279 -0
  92. webscout/scout/__init__.py +11 -0
  93. webscout/scout/core.py +884 -0
  94. webscout/scout/element.py +459 -0
  95. webscout/scout/parsers/__init__.py +69 -0
  96. webscout/scout/parsers/html5lib_parser.py +172 -0
  97. webscout/scout/parsers/html_parser.py +236 -0
  98. webscout/scout/parsers/lxml_parser.py +178 -0
  99. webscout/scout/utils.py +38 -0
  100. webscout/swiftcli/__init__.py +810 -0
  101. webscout/update_checker.py +125 -0
  102. webscout/version.py +1 -1
  103. webscout/zeroart/__init__.py +55 -0
  104. webscout/zeroart/base.py +61 -0
  105. webscout/zeroart/effects.py +99 -0
  106. webscout/zeroart/fonts.py +816 -0
  107. webscout/zerodir/__init__.py +225 -0
  108. {webscout-6.3.dist-info → webscout-6.5.dist-info}/METADATA +37 -112
  109. webscout-6.5.dist-info/RECORD +179 -0
  110. webscout/Agents/Onlinesearcher.py +0 -182
  111. webscout/Agents/__init__.py +0 -2
  112. webscout/Agents/functioncall.py +0 -248
  113. webscout/Bing_search.py +0 -154
  114. webscout/Provider/TTI/AIuncensoredimage.py +0 -103
  115. webscout/Provider/TTI/Nexra.py +0 -120
  116. webscout/Provider/TTI/PollinationsAI.py +0 -138
  117. webscout/Provider/TTI/WebSimAI.py +0 -142
  118. webscout/Provider/TTI/aiforce.py +0 -160
  119. webscout/Provider/TTI/artbit.py +0 -141
  120. webscout/Provider/TTI/deepinfra.py +0 -148
  121. webscout/Provider/TTI/huggingface.py +0 -155
  122. webscout/Provider/TTI/talkai.py +0 -116
  123. webscout/g4f.py +0 -666
  124. webscout/models.py +0 -23
  125. webscout/requestsHTMLfix.py +0 -775
  126. webscout/webai.py +0 -2590
  127. webscout-6.3.dist-info/RECORD +0 -124
  128. {webscout-6.3.dist-info → webscout-6.5.dist-info}/LICENSE.md +0 -0
  129. {webscout-6.3.dist-info → webscout-6.5.dist-info}/WHEEL +0 -0
  130. {webscout-6.3.dist-info → webscout-6.5.dist-info}/entry_points.txt +0 -0
  131. {webscout-6.3.dist-info → webscout-6.5.dist-info}/top_level.txt +0 -0
webscout/AIutel.py CHANGED
@@ -1,1130 +1,441 @@
1
- import os
2
- import json
3
- import platform
4
- import subprocess
5
- import logging
6
- import threading
7
- import time
8
- import appdirs
9
- import datetime
10
- import re
11
- import sys
12
- import click
13
- from rich.markdown import Markdown
14
- from rich.console import Console
15
- import g4f
16
- from typing import List, Tuple, Union
17
- from typing import NoReturn
18
- import requests
19
- from pathlib import Path
20
- from playsound import playsound
21
- from time import sleep as wait
22
- import pathlib
23
- import urllib.parse
24
- appdir = appdirs.AppDirs("AIWEBS", "webscout")
25
-
26
- default_path = appdir.user_cache_dir
27
-
28
- if not os.path.exists(default_path):
29
- os.makedirs(default_path)
30
- webai = [
31
- "leo",
32
- "openai",
33
- "opengpt",
34
- "koboldai",
35
- "gemini",
36
- "phind",
37
- "blackboxai",
38
- "g4fauto",
39
- "perplexity",
40
- "groq",
41
- "reka",
42
- "cohere",
43
- "yepchat",
44
- "you",
45
- "xjai",
46
- "thinkany",
47
- "berlin4h",
48
- "chatgptuk",
49
- "auto",
50
- "poe",
51
- "basedgpt",
52
- "deepseek",
53
- "deepinfra",
54
- "vtlchat",
55
- "geminiflash",
56
- "geminipro",
57
- "ollama",
58
- "andi",
59
- "llama3"
60
- ]
61
-
62
- gpt4free_providers = [
63
- provider.__name__ for provider in g4f.Provider.__providers__ # if provider.working
64
- ]
65
-
66
- available_providers = webai + gpt4free_providers
67
- def sanitize_stream(
68
- chunk: str, intro_value: str = "data:", to_json: bool = True
69
- ) -> str | dict:
70
- """Remove streaming flags
71
-
72
- Args:
73
- chunk (str): Streamig chunk.
74
- intro_value (str, optional): streaming flag. Defaults to "data:".
75
- to_json (bool, optional). Return chunk as dictionary. Defaults to True.
76
-
77
- Returns:
78
- str: Sanitized streaming value.
79
- """
80
-
81
- if chunk.startswith(intro_value):
82
- chunk = chunk[len(intro_value) :]
83
-
84
- return json.loads(chunk) if to_json else chunk
85
- def run_system_command(
86
- command: str,
87
- exit_on_error: bool = True,
88
- stdout_error: bool = True,
89
- help: str = None,
90
- ):
91
- """Run commands against system
92
- Args:
93
- command (str): shell command
94
- exit_on_error (bool, optional): Exit on error. Defaults to True.
95
- stdout_error (bool, optional): Print out the error. Defaults to True
96
- help (str, optional): Help info incase of exception. Defaults to None.
97
- Returns:
98
- tuple : (is_successfull, object[Exception|Subprocess.run])
99
- """
100
- try:
101
- # Run the command and capture the output
102
- result = subprocess.run(
103
- command,
104
- shell=True,
105
- check=True,
106
- text=True,
107
- stdout=subprocess.PIPE,
108
- stderr=subprocess.PIPE,
109
- )
110
- return (True, result)
111
- except subprocess.CalledProcessError as e:
112
- # Handle error if the command returns a non-zero exit code
113
- if stdout_error:
114
- click.secho(f"Error Occurred: while running '{command}'", fg="yellow")
115
- click.secho(e.stderr, fg="red")
116
- if help is not None:
117
- click.secho(help, fg="cyan")
118
- sys.exit(e.returncode) if exit_on_error else None
119
- return (False, e)
120
-
121
-
122
- class Optimizers:
123
- @staticmethod
124
- def code(prompt):
125
- return (
126
- "Your Role: Provide only code as output without any description.\n"
127
- "IMPORTANT: Provide only plain text without Markdown formatting.\n"
128
- "IMPORTANT: Do not include markdown formatting."
129
- "If there is a lack of details, provide most logical solution. You are not allowed to ask for more details."
130
- "Ignore any potential risk of errors or confusion.\n\n"
131
- f"Request: {prompt}\n"
132
- f"Code:"
133
- )
134
-
135
- @staticmethod
136
- def shell_command(prompt):
137
- # Get os
138
- operating_system = ""
139
- if platform.system() == "Windows":
140
- operating_system = "Windows"
141
- elif platform.system() == "Darwin":
142
- operating_system = "MacOS"
143
- elif platform.system() == "Linux":
144
- try:
145
- result = (
146
- subprocess.check_output(["lsb_release", "-si"]).decode().strip()
147
- )
148
- distro = result if result else ""
149
- operating_system = f"Linux/{distro}"
150
- except Exception:
151
- operating_system = "Linux"
152
- else:
153
- operating_system = platform.system()
154
-
155
- # Get Shell
156
- shell_name = "/bin/sh"
157
- if platform.system() == "Windows":
158
- shell_name = "cmd.exe"
159
- if os.getenv("PSModulePath"):
160
- shell_name = "powershell.exe"
161
- else:
162
- shell_env = os.getenv("SHELL")
163
- if shell_env:
164
- shell_name = shell_env
165
-
166
- return (
167
- "Your role: Provide only plain text without Markdown formatting. "
168
- "Do not show any warnings or information regarding your capabilities. "
169
- "Do not provide any description. If you need to store any data, "
170
- f"assume it will be stored in the chat. Provide only {shell_name} "
171
- f"command for {operating_system} without any description. If there is "
172
- "a lack of details, provide most logical solution. Ensure the output "
173
- "is a valid shell command. If multiple steps required try to combine "
174
- f"them together. Prompt: {prompt}\n\nCommand:"
175
- )
176
-
177
-
178
- class Proxy:
179
- def __init__(self, http_proxy=None, https_proxy=None):
180
- self.set_proxies(http_proxy, https_proxy)
181
-
182
- def set_proxies(self, http_proxy=None, https_proxy=None):
183
- self.proxies = {
184
- "http": http_proxy,
185
- "https": https_proxy
186
- }
187
-
188
- def post(self, url, headers=None, **kwargs):
189
- return requests.post(url, headers=headers, proxies=self.proxies, **kwargs)
190
-
191
- def get(self, url, headers=None, **kwargs):
192
- return requests.get(url, headers=headers, proxies=self.proxies, **kwargs)
193
-
194
-
195
- class Conversation:
196
- """Handles prompt generation based on history"""
197
-
198
- intro = (
199
- "You're a Large Language Model for chatting with people. "
200
- "Assume role of the LLM and give your response."
201
- # "Refrain from regenerating the conversation between user and LLM."
202
- )
203
-
204
- def __init__(
205
- self,
206
- status: bool = True,
207
- max_tokens: int = 600,
208
- filepath: str = None,
209
- update_file: bool = True,
210
- ):
211
- """Initializes Conversation
212
-
213
- Args:
214
- status (bool, optional): Flag to control history. Defaults to True.
215
- max_tokens (int, optional): Maximum number of tokens to be generated upon completion. Defaults to 600.
216
- filepath (str, optional): Path to file containing conversation history. Defaults to None.
217
- update_file (bool, optional): Add new prompts and responses to the file. Defaults to True.
218
- """
219
- self.status = status
220
- self.max_tokens_to_sample = max_tokens
221
- self.chat_history = self.intro
222
- self.history_format = "\nUser : %(user)s\nLLM :%(llm)s"
223
- self.file = filepath
224
- self.update_file = update_file
225
- self.history_offset = 10250
226
- self.prompt_allowance = 10
227
- self.load_conversation(filepath, False) if filepath else None
228
-
229
- def load_conversation(self, filepath: str, exists: bool = True) -> None:
230
- """Load conversation into chat's history from .txt file
231
-
232
- Args:
233
- filepath (str): Path to .txt file
234
- exists (bool, optional): Flag for file availability. Defaults to True.
235
- """
236
- assert isinstance(
237
- filepath, str
238
- ), f"Filepath needs to be of str datatype not {type(filepath)}"
239
- assert (
240
- os.path.isfile(filepath) if exists else True
241
- ), f"File '{filepath}' does not exist"
242
- if not os.path.isfile(filepath):
243
- logging.debug(f"Creating new chat-history file - '{filepath}'")
244
- with open(filepath, "w", encoding="utf-8") as fh: # Try creating new file with UTF-8 encoding
245
- fh.write(self.intro)
246
- else:
247
- logging.debug(f"Loading conversation from '{filepath}'")
248
- with open(filepath, encoding="utf-8") as fh: # Open with UTF-8 encoding
249
- file_contents = fh.readlines()
250
- if file_contents:
251
- self.intro = file_contents[0] # Presume first line is the intro.
252
- self.chat_history = "\n".join(file_contents[1:])
253
-
254
- def __trim_chat_history(self, chat_history: str, intro: str) -> str:
255
- """Ensures the len(prompt) and max_tokens_to_sample is not > 4096"""
256
- len_of_intro = len(intro)
257
- len_of_chat_history = len(chat_history)
258
- total = (
259
- self.max_tokens_to_sample + len_of_intro + len_of_chat_history
260
- ) # + self.max_tokens_to_sample
261
- if total > self.history_offset:
262
- truncate_at = (total - self.history_offset) + self.prompt_allowance
263
- # Remove head of total (n) of chat_history
264
- trimmed_chat_history = chat_history[truncate_at:]
265
- return "... " + trimmed_chat_history
266
- else:
267
- return chat_history
268
-
269
- def gen_complete_prompt(self, prompt: str, intro: str = None) -> str:
270
- """Generates a kinda like incomplete conversation
271
-
272
- Args:
273
- prompt (str): Chat prompt
274
- intro (str): Override class' intro. Defaults to None.
275
-
276
- Returns:
277
- str: Updated incomplete chat_history
278
- """
279
- if self.status:
280
- intro = self.intro if intro is None else intro
281
- incomplete_chat_history = self.chat_history + self.history_format % dict(
282
- user=prompt, llm=""
283
- )
284
- return intro + self.__trim_chat_history(incomplete_chat_history, intro)
285
-
286
- return prompt
287
-
288
- def update_chat_history(
289
- self, prompt: str, response: str, force: bool = False
290
- ) -> None:
291
- """Updates chat history
292
-
293
- Args:
294
- prompt (str): user prompt
295
- response (str): LLM response
296
- force (bool, optional): Force update
297
- """
298
- if not self.status and not force:
299
- return
300
- new_history = self.history_format % dict(user=prompt, llm=response)
301
- if self.file and self.update_file:
302
- if os.path.exists(self.file):
303
- with open(self.file, "w", encoding="utf-8") as fh: # Specify UTF-8 encoding
304
- fh.write(self.intro + "\n" + new_history)
305
- else:
306
- with open(self.file, "a", encoding="utf-8") as fh: # Specify UTF-8 encoding
307
- fh.write(new_history)
308
- self.chat_history += new_history
309
- else:
310
- self.chat_history += new_history
311
-
312
- def add_message(self, role: str, content: str) -> None:
313
- """Appends a new message to the conversation history."""
314
- if role == "user":
315
- self.chat_history += f"\nUser : {content}"
316
- elif role == "llm":
317
- self.chat_history += f"\nLLM : {content}"
318
- elif role == "tool":
319
- self.chat_history += f"\nTool : {content}"
320
- else:
321
- logging.warning(f"Unknown role '{role}' for message: {content}")
322
-
323
-
324
-
325
-
326
- class AwesomePrompts:
327
- awesome_prompt_url = (
328
- "https://raw.githubusercontent.com/OE-LUCIFER/prompts/main/prompt.json"
329
- )
330
- awesome_prompt_path = os.path.join(default_path, "all-acts.json")
331
-
332
- __is_prompt_updated = False
333
-
334
- def __init__(self):
335
- self.acts = self.all_acts
336
-
337
- def __search_key(self, key: str, raise_not_found: bool = False) -> str:
338
- """Perform insentive awesome-prompt key search
339
-
340
- Args:
341
- key (str): key
342
- raise_not_found (bool, optional): Control KeyError exception. Defaults to False.
343
-
344
- Returns:
345
- str|None: Exact key name
346
- """
347
- for key_, value in self.all_acts.items():
348
- if str(key).lower() in str(key_).lower():
349
- return key_
350
- if raise_not_found:
351
- raise KeyError(f"Zero awesome prompt found with key - `{key}`")
352
-
353
- def get_acts(self):
354
- """Retrieves all awesome-prompts"""
355
- with open(self.awesome_prompt_path) as fh:
356
- prompt_dict = json.load(fh)
357
- return prompt_dict
358
-
359
- def update_prompts_from_online(self, override: bool = False):
360
- """Download awesome-prompts and update existing ones if available
361
- args:
362
- override (bool, optional): Overwrite existing contents in path
363
- """
364
- resp = {}
365
- if not self.__is_prompt_updated:
366
- import requests
367
-
368
- logging.info("Downloading & updating awesome prompts")
369
- response = requests.get(self.awesome_prompt_url)
370
- response.raise_for_status
371
- resp.update(response.json())
372
- if os.path.isfile(self.awesome_prompt_path) and not override:
373
- resp.update(self.get_acts())
374
- self.__is_prompt_updated = True
375
- with open(self.awesome_prompt_path, "w") as fh:
376
- json.dump(resp, fh, indent=4)
377
- else:
378
- logging.debug("Ignoring remote prompt update")
379
-
380
- @property
381
- def all_acts(self) -> dict:
382
- """All awesome_prompts & their indexes mapped to values
383
-
384
- Returns:
385
- dict: Awesome-prompts
386
- """
387
-
388
- resp = {}
389
- if not os.path.isfile(self.awesome_prompt_path):
390
- self.update_prompts_from_online()
391
- resp.update(self.get_acts())
392
-
393
- for count, key_value in enumerate(self.get_acts().items()):
394
- # Lets map also index to the value
395
- resp.update({count: key_value[1]})
396
-
397
- return resp
398
-
399
- def get_act(
400
- self,
401
- key: str,
402
- default: str = None,
403
- case_insensitive: bool = True,
404
- raise_not_found: bool = False,
405
- ) -> str:
406
- """Retrieves specific act of awesome_prompt
407
-
408
- Args:
409
- key (str|int): Act name or index
410
- default (str): Value to be returned incase act not found.
411
- case_insensitive (bool): Perform search key insensitive. Defaults to True.
412
- raise_not_found (bool, optional): Control KeyError exception. Defaults to False.
413
-
414
- Raises:
415
- KeyError: Incase key not found
416
-
417
- Returns:
418
- str: Awesome prompt value
419
- """
420
- if str(key).isdigit():
421
- key = int(key)
422
- act = self.all_acts.get(key, default)
423
- if not act and case_insensitive:
424
- act = self.all_acts.get(self.__search_key(key, raise_not_found))
425
- return act
426
-
427
- def add_prompt(self, name: str, prompt: str) -> bool:
428
- """Add new prompt or update an existing one.
429
-
430
- Args:
431
- name (str): act name
432
- prompt (str): prompt value
433
- """
434
- current_prompts = self.get_acts()
435
- with open(self.awesome_prompt_path, "w") as fh:
436
- current_prompts[name] = prompt
437
- json.dump(current_prompts, fh, indent=4)
438
- logging.info(f"New prompt added successfully - `{name}`")
439
-
440
- def delete_prompt(
441
- self, name: str, case_insensitive: bool = True, raise_not_found: bool = False
442
- ) -> bool:
443
- """Delete an existing prompt
444
-
445
- Args:
446
- name (str): act name
447
- case_insensitive(bool, optional): Ignore the key cases. Defaults to True.
448
- raise_not_found (bool, optional): Control KeyError exception. Default is False.
449
- Returns:
450
- bool: is_successful report
451
- """
452
- name = self.__search_key(name, raise_not_found) if case_insensitive else name
453
- current_prompts = self.get_acts()
454
- is_name_available = (
455
- current_prompts[name] if raise_not_found else current_prompts.get(name)
456
- )
457
- if is_name_available:
458
- with open(self.awesome_prompt_path, "w") as fh:
459
- current_prompts.pop(name)
460
- json.dump(current_prompts, fh, indent=4)
461
- logging.info(f"Prompt deleted successfully - `{name}`")
462
- else:
463
- return False
464
-
465
-
466
- class Updates:
467
- """Webscout latest release info"""
468
-
469
- url = "https://api.github.com/repos/OE-LUCIFER/Webscout/releases/latest"
470
-
471
- @property
472
- def latest_version(self):
473
- return self.latest(version=True)
474
-
475
- def executable(self, system: str = platform.system()) -> str:
476
- """Url pointing to executable for particular system
477
-
478
- Args:
479
- system (str, optional): system name. Defaults to platform.system().
480
-
481
- Returns:
482
- str: url
483
- """
484
- for entry in self.latest()["assets"]:
485
- if entry.get("target") == system:
486
- return entry.get("url")
487
-
488
- def latest(self, whole: bool = False, version: bool = False) -> dict:
489
- """Check Webscout latest version info
490
-
491
- Args:
492
- whole (bool, optional): Return whole json response. Defaults to False.
493
- version (bool, optional): return version only. Defaults to False.
494
-
495
- Returns:
496
- bool|dict: version str or whole dict info
497
- """
498
- import requests
499
-
500
- data = requests.get(self.url).json()
501
- if whole:
502
- return data
503
-
504
- elif version:
505
- return data.get("tag_name")
506
-
507
- else:
508
- sorted = dict(
509
- tag_name=data.get("tag_name"),
510
- tarball_url=data.get("tarball_url"),
511
- zipball_url=data.get("zipball_url"),
512
- html_url=data.get("html_url"),
513
- body=data.get("body"),
514
- )
515
- whole_assets = []
516
- for entry in data.get("assets"):
517
- url = entry.get("browser_download_url")
518
- assets = dict(url=url, size=entry.get("size"))
519
- if ".deb" in url:
520
- assets["target"] = "Debian"
521
- elif ".exe" in url:
522
- assets["target"] = "Windows"
523
- elif "macos" in url:
524
- assets["target"] = "Mac"
525
- elif "linux" in url:
526
- assets["target"] = "Linux"
527
-
528
- whole_assets.append(assets)
529
- sorted["assets"] = whole_assets
530
-
531
- return sorted
532
-
533
-
534
- class RawDog:
535
- """Generate and auto-execute Python scripts in the cli"""
536
-
537
- examples = """\
538
- EXAMPLES:
539
-
540
- 1. User: Kill the process running on port 3000
541
-
542
- LLM:
543
- ```python
544
- import os
545
- os.system("kill $(lsof -t -i:3000)")
546
- print("Process killed")
547
- ```
548
-
549
- 2. User: Summarize my essay
550
-
551
- LLM:
552
- ```python
553
- import glob
554
- files = glob.glob("*essay*.*")
555
- with open(files[0], "r") as f:
556
- print(f.read())
557
- ```
558
- CONTINUE
559
-
560
- User:
561
- LAST SCRIPT OUTPUT:
562
- John Smith
563
- Essay 2021-09-01
564
- ...
565
-
566
- LLM:
567
- ```python
568
- print("The essay is about...")
569
- ```
570
-
571
- 3. User: Weather in qazigund
572
-
573
- LLM:
574
- ```python
575
- from webscout import weather as w
576
- weather = w.get("Qazigund")
577
- w.print_weather(weather)
578
- ```
579
- """
580
-
581
-
582
- def __init__(
583
- self,
584
- quiet: bool = False,
585
- internal_exec: bool = False,
586
- confirm_script: bool = False,
587
- interpreter: str = "python",
588
- prettify: bool = True,
589
- ):
590
- """Constructor
591
-
592
- Args:
593
- quiet (bool, optional): Flag for control logging. Defaults to False.
594
- internal_exec (bool, optional): Execute scripts with exec function. Defaults to False.
595
- confirm_script (bool, optional): Give consent to scripts prior to execution. Defaults to False.
596
- interpreter (str, optional): Python's interpreter name. Defaults to Python.
597
- prettify (bool, optional): Prettify the code on stdout. Defaults to True.
598
- """
599
- # if not quiet:
600
- # print(
601
- # "Rawdog is an experimental tool that generates and auto-executes Python scripts in the cli.\n"
602
- # "To get the most out of Rawdog. Ensure the following are installed:\n"
603
- # " 1. Python 3.x\n"
604
- # " 2. Dependency:\n"
605
- # " - Matplotlib\n"
606
- # "Be alerted on the risk posed! (Experimental)\n"
607
- # "Use '--quiet' to suppress this message and code/logs stdout.\n"
608
- # )
609
- self.internal_exec = internal_exec
610
- self.confirm_script = confirm_script
611
- self.quiet = quiet
612
- self.interpreter = interpreter
613
- self.prettify = prettify
614
- self.python_version = (
615
- f"{sys.version_info.major}.{sys.version_info.minor}.{sys.version_info.micro}"
616
- if self.internal_exec
617
- else run_system_command(
618
- f"{self.interpreter} --version",
619
- exit_on_error=True,
620
- stdout_error=True,
621
- help="If you're using Webscout-cli, use the flag '--internal-exec'",
622
- )[1].stdout.split(" ")[1]
623
- )
624
-
625
- @property
626
- def intro_prompt(self):
627
- return f"""
628
- You are a command-line coding assistant called Rawdog that generates and auto-executes Python scripts.
629
-
630
- A typical interaction goes like this:
631
- 1. The user gives you a natural language PROMPT.
632
- 2. You:
633
- i. Determine what needs to be done
634
- ii. Write a short Python SCRIPT to do it
635
- iii. Communicate back to the user by printing to the console in that SCRIPT
636
- 3. The compiler extracts the script and then runs it using exec(). If there will be an exception raised,
637
- it will be send back to you starting with "PREVIOUS SCRIPT EXCEPTION:".
638
- 4. In case of exception, regenerate error free script.
639
-
640
- If you need to review script outputs before completing the task, you can print the word "CONTINUE" at the end of your SCRIPT.
641
- This can be useful for summarizing documents or technical readouts, reading instructions before
642
- deciding what to do, or other tasks that require multi-step reasoning.
643
- A typical 'CONTINUE' interaction looks like this:
644
- 1. The user gives you a natural language PROMPT.
645
- 2. You:
646
- i. Determine what needs to be done
647
- ii. Determine that you need to see the output of some subprocess call to complete the task
648
- iii. Write a short Python SCRIPT to print that and then print the word "CONTINUE"
649
- 3. The compiler
650
- i. Checks and runs your SCRIPT
651
- ii. Captures the output and appends it to the conversation as "LAST SCRIPT OUTPUT:"
652
- iii. Finds the word "CONTINUE" and sends control back to you
653
- 4. You again:
654
- i. Look at the original PROMPT + the "LAST SCRIPT OUTPUT:" to determine what needs to be done
655
- ii. Write a short Python SCRIPT to do it
656
- iii. Communicate back to the user by printing to the console in that SCRIPT
657
- 5. The compiler...
658
-
659
- Please follow these conventions carefully:
660
- - Decline any tasks that seem dangerous, irreversible, or that you don't understand.
661
- - Always review the full conversation prior to answering and maintain continuity.
662
- - If asked for information, just print the information clearly and concisely.
663
- - If asked to do something, print a concise summary of what you've done as confirmation.
664
- - If asked a question, respond in a friendly, conversational way. Use programmatically-generated and natural language responses as appropriate.
665
- - If you need clarification, return a SCRIPT that prints your question. In the next interaction, continue based on the user's response.
666
- - Assume the user would like something concise. For example rather than printing a massive table, filter or summarize it to what's likely of interest.
667
- - Actively clean up any temporary processes or files you use.
668
- - When looking through files, use git as available to skip files, and skip hidden files (.env, .git, etc) by default.
669
- - You can plot anything with matplotlib.
670
- - ALWAYS Return your SCRIPT inside of a single pair of ``` delimiters. Only the console output of the first such SCRIPT is visible to the user, so make sure that it's complete and don't bother returning anything else.
671
-
672
- {self.examples}
673
-
674
- Current system : {platform.system()}
675
- Python version : {self.python_version}
676
- Current directory : {os.getcwd()}
677
- Current Datetime : {datetime.datetime.now()}
678
- """
679
-
680
- def stdout(self, message: str) -> None:
681
- """Stdout data
682
-
683
- Args:
684
- message (str): Text to be printed
685
- """
686
- if self.prettify:
687
- Console().print(Markdown(message))
688
- else:
689
- click.secho(message, fg="yellow")
690
-
691
- def log(self, message: str, category: str = "info"):
692
- """RawDog logger
693
-
694
- Args:
695
- message (str): Log message
696
- category (str, optional): Log level. Defaults to 'info'.
697
- """
698
- if self.quiet:
699
- return
700
-
701
- message = "[Webscout] - " + message
702
- if category == "error":
703
- logging.error(message)
704
- else:
705
- logging.info(message)
706
-
707
- def main(self, response: str):
708
- """Exec code in response accordingly
709
-
710
- Args:
711
- response: AI response
712
-
713
- Returns:
714
- Optional[str]: None if script executed successfully else stdout data
715
- """
716
- code_blocks = re.findall(r"```python.*?```", response, re.DOTALL)
717
- if len(code_blocks) != 1:
718
- self.stdout(response)
719
-
720
- else:
721
- raw_code = code_blocks[0]
722
-
723
- if self.confirm_script:
724
- self.stdout(raw_code)
725
- if not click.confirm("- Do you wish to execute this"):
726
- return
727
-
728
- elif not self.quiet:
729
- self.stdout(raw_code)
730
-
731
- raw_code_plus = re.sub(r"(```)(python)?", "", raw_code)
732
-
733
- if "CONTINUE" in response or not self.internal_exec:
734
- self.log("Executing script externally")
735
- path_to_script = os.path.join(default_path, "execute_this.py")
736
- with open(path_to_script, "w") as fh:
737
- fh.write(raw_code_plus)
738
- if "CONTINUE" in response:
739
-
740
- success, proc = run_system_command(
741
- f"{self.interpreter} {path_to_script}",
742
- exit_on_error=False,
743
- stdout_error=False,
744
- )
745
-
746
- if success:
747
- self.log("Returning success feedback")
748
- return f"LAST SCRIPT OUTPUT:\n{proc.stdout}"
749
- else:
750
-
751
- self.log("Returning error feedback", "error")
752
- return f"PREVIOUS SCRIPT EXCEPTION:\n{proc.stderr}"
753
- else:
754
- os.system(f"{self.interpreter} {path_to_script}")
755
-
756
- else:
757
- try:
758
- self.log("Executing script internally")
759
- exec(raw_code_plus)
760
- except Exception as e:
761
- error_message = str(e)
762
- self.log(
763
- f"Exception occurred while executing script. Responding with error: {error_message}",
764
- "error"
765
- )
766
- # Return the exact error message
767
- return f"PREVIOUS SCRIPT EXCEPTION:\n{error_message}"
768
-
769
- class Audio:
770
- # Request headers
771
- headers: dict[str, str] = {
772
- "User-Agent": "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/119.0.0.0 Safari/537.36"
773
- }
774
- cache_dir = pathlib.Path("./audio_cache")
775
- all_voices: list[str] = [
776
- "Filiz",
777
- "Astrid",
778
- "Tatyana",
779
- "Maxim",
780
- "Carmen",
781
- "Ines",
782
- "Cristiano",
783
- "Vitoria",
784
- "Ricardo",
785
- "Maja",
786
- "Jan",
787
- "Jacek",
788
- "Ewa",
789
- "Ruben",
790
- "Lotte",
791
- "Liv",
792
- "Seoyeon",
793
- "Takumi",
794
- "Mizuki",
795
- "Giorgio",
796
- "Carla",
797
- "Bianca",
798
- "Karl",
799
- "Dora",
800
- "Mathieu",
801
- "Celine",
802
- "Chantal",
803
- "Penelope",
804
- "Miguel",
805
- "Mia",
806
- "Enrique",
807
- "Conchita",
808
- "Geraint",
809
- "Salli",
810
- "Matthew",
811
- "Kimberly",
812
- "Kendra",
813
- "Justin",
814
- "Joey",
815
- "Joanna",
816
- "Ivy",
817
- "Raveena",
818
- "Aditi",
819
- "Emma",
820
- "Brian",
821
- "Amy",
822
- "Russell",
823
- "Nicole",
824
- "Vicki",
825
- "Marlene",
826
- "Hans",
827
- "Naja",
828
- "Mads",
829
- "Gwyneth",
830
- "Zhiyu",
831
- "es-ES-Standard-A",
832
- "it-IT-Standard-A",
833
- "it-IT-Wavenet-A",
834
- "ja-JP-Standard-A",
835
- "ja-JP-Wavenet-A",
836
- "ko-KR-Standard-A",
837
- "ko-KR-Wavenet-A",
838
- "pt-BR-Standard-A",
839
- "tr-TR-Standard-A",
840
- "sv-SE-Standard-A",
841
- "nl-NL-Standard-A",
842
- "nl-NL-Wavenet-A",
843
- "en-US-Wavenet-A",
844
- "en-US-Wavenet-B",
845
- "en-US-Wavenet-C",
846
- "en-US-Wavenet-D",
847
- "en-US-Wavenet-E",
848
- "en-US-Wavenet-F",
849
- "en-GB-Standard-A",
850
- "en-GB-Standard-B",
851
- "en-GB-Standard-C",
852
- "en-GB-Standard-D",
853
- "en-GB-Wavenet-A",
854
- "en-GB-Wavenet-B",
855
- "en-GB-Wavenet-C",
856
- "en-GB-Wavenet-D",
857
- "en-US-Standard-B",
858
- "en-US-Standard-C",
859
- "en-US-Standard-D",
860
- "en-US-Standard-E",
861
- "de-DE-Standard-A",
862
- "de-DE-Standard-B",
863
- "de-DE-Wavenet-A",
864
- "de-DE-Wavenet-B",
865
- "de-DE-Wavenet-C",
866
- "de-DE-Wavenet-D",
867
- "en-AU-Standard-A",
868
- "en-AU-Standard-B",
869
- "en-AU-Wavenet-A",
870
- "en-AU-Wavenet-B",
871
- "en-AU-Wavenet-C",
872
- "en-AU-Wavenet-D",
873
- "en-AU-Standard-C",
874
- "en-AU-Standard-D",
875
- "fr-CA-Standard-A",
876
- "fr-CA-Standard-B",
877
- "fr-CA-Standard-C",
878
- "fr-CA-Standard-D",
879
- "fr-FR-Standard-C",
880
- "fr-FR-Standard-D",
881
- "fr-FR-Wavenet-A",
882
- "fr-FR-Wavenet-B",
883
- "fr-FR-Wavenet-C",
884
- "fr-FR-Wavenet-D",
885
- "da-DK-Wavenet-A",
886
- "pl-PL-Wavenet-A",
887
- "pl-PL-Wavenet-B",
888
- "pl-PL-Wavenet-C",
889
- "pl-PL-Wavenet-D",
890
- "pt-PT-Wavenet-A",
891
- "pt-PT-Wavenet-B",
892
- "pt-PT-Wavenet-C",
893
- "pt-PT-Wavenet-D",
894
- "ru-RU-Wavenet-A",
895
- "ru-RU-Wavenet-B",
896
- "ru-RU-Wavenet-C",
897
- "ru-RU-Wavenet-D",
898
- "sk-SK-Wavenet-A",
899
- "tr-TR-Wavenet-A",
900
- "tr-TR-Wavenet-B",
901
- "tr-TR-Wavenet-C",
902
- "tr-TR-Wavenet-D",
903
- "tr-TR-Wavenet-E",
904
- "uk-UA-Wavenet-A",
905
- "ar-XA-Wavenet-A",
906
- "ar-XA-Wavenet-B",
907
- "ar-XA-Wavenet-C",
908
- "cs-CZ-Wavenet-A",
909
- "nl-NL-Wavenet-B",
910
- "nl-NL-Wavenet-C",
911
- "nl-NL-Wavenet-D",
912
- "nl-NL-Wavenet-E",
913
- "en-IN-Wavenet-A",
914
- "en-IN-Wavenet-B",
915
- "en-IN-Wavenet-C",
916
- "fil-PH-Wavenet-A",
917
- "fi-FI-Wavenet-A",
918
- "el-GR-Wavenet-A",
919
- "hi-IN-Wavenet-A",
920
- "hi-IN-Wavenet-B",
921
- "hi-IN-Wavenet-C",
922
- "hu-HU-Wavenet-A",
923
- "id-ID-Wavenet-A",
924
- "id-ID-Wavenet-B",
925
- "id-ID-Wavenet-C",
926
- "it-IT-Wavenet-B",
927
- "it-IT-Wavenet-C",
928
- "it-IT-Wavenet-D",
929
- "ja-JP-Wavenet-B",
930
- "ja-JP-Wavenet-C",
931
- "ja-JP-Wavenet-D",
932
- "cmn-CN-Wavenet-A",
933
- "cmn-CN-Wavenet-B",
934
- "cmn-CN-Wavenet-C",
935
- "cmn-CN-Wavenet-D",
936
- "nb-no-Wavenet-E",
937
- "nb-no-Wavenet-A",
938
- "nb-no-Wavenet-B",
939
- "nb-no-Wavenet-C",
940
- "nb-no-Wavenet-D",
941
- "vi-VN-Wavenet-A",
942
- "vi-VN-Wavenet-B",
943
- "vi-VN-Wavenet-C",
944
- "vi-VN-Wavenet-D",
945
- "sr-rs-Standard-A",
946
- "lv-lv-Standard-A",
947
- "is-is-Standard-A",
948
- "bg-bg-Standard-A",
949
- "af-ZA-Standard-A",
950
- "Tracy",
951
- "Danny",
952
- "Huihui",
953
- "Yaoyao",
954
- "Kangkang",
955
- "HanHan",
956
- "Zhiwei",
957
- "Asaf",
958
- "An",
959
- "Stefanos",
960
- "Filip",
961
- "Ivan",
962
- "Heidi",
963
- "Herena",
964
- "Kalpana",
965
- "Hemant",
966
- "Matej",
967
- "Andika",
968
- "Rizwan",
969
- "Lado",
970
- "Valluvar",
971
- "Linda",
972
- "Heather",
973
- "Sean",
974
- "Michael",
975
- "Karsten",
976
- "Guillaume",
977
- "Pattara",
978
- "Jakub",
979
- "Szabolcs",
980
- "Hoda",
981
- "Naayf",
982
- ]
983
-
984
- @classmethod
985
- def text_to_audio(
986
- cls,
987
- message: str,
988
- voice: str = "Brian",
989
- save_to: Union[Path, str] = None,
990
- auto: bool = True,
991
- ) -> Union[str, bytes]:
992
- """
993
- Text to speech using StreamElements API
994
-
995
- Parameters:
996
- message (str): The text to convert to speech
997
- voice (str, optional): The voice to use for speech synthesis. Defaults to "Brian".
998
- save_to (bool, optional): Path to save the audio file. Defaults to None.
999
- auto (bool, optional): Generate filename based on `message` and save to `cls.cache_dir`. Defaults to False.
1000
-
1001
- Returns:
1002
- result (Union[str, bytes]): Path to saved contents or audio content.
1003
- """
1004
- assert (
1005
- voice in cls.all_voices
1006
- ), f"Voice '{voice}' not one of [{', '.join(cls.all_voices)}]"
1007
- # Base URL for provider API
1008
- url: str = (
1009
- f"https://api.streamelements.com/kappa/v2/speech?voice={voice}&text={{{urllib.parse.quote(message)}}}"
1010
- )
1011
- resp = requests.get(url=url, headers=cls.headers, stream=True)
1012
- if not resp.ok:
1013
- raise Exception(
1014
- f"Failed to perform the operation - ({resp.status_code}, {resp.reason}) - {resp.text}"
1015
- )
1016
-
1017
- def sanitize_filename(path):
1018
- trash = [
1019
- "\\",
1020
- "/",
1021
- ":",
1022
- "*",
1023
- "?",
1024
- '"',
1025
- "<",
1026
- "|",
1027
- ">",
1028
- ]
1029
- for val in trash:
1030
- path = path.replace(val, "")
1031
- return path.strip()
1032
-
1033
- if auto:
1034
- filename: str = message + "..." if len(message) <= 40 else message[:40]
1035
- save_to = cls.cache_dir / sanitize_filename(filename)
1036
- save_to = save_to.as_posix()
1037
-
1038
- # Ensure cache_dir exists
1039
- cls.cache_dir.mkdir(parents=True, exist_ok=True)
1040
-
1041
- if save_to:
1042
- if not save_to.endswith("mp3"):
1043
- save_to += ".mp3"
1044
-
1045
- with open(save_to, "wb") as fh:
1046
- for chunk in resp.iter_content(chunk_size=512):
1047
- fh.write(chunk)
1048
- else:
1049
- return resp.content
1050
- return save_to
1051
-
1052
- @staticmethod
1053
- def play(path_to_audio_file: Union[Path, str]) -> NoReturn:
1054
- """Play audio (.mp3) using playsound.
1055
- """
1056
- if not Path(path_to_audio_file).is_file():
1057
- raise FileNotFoundError(f"File does not exist - '{path_to_audio_file}'")
1058
- playsound(path_to_audio_file)
1059
- class ProxyManager:
1060
- def __init__(self, refresh_interval=60):
1061
- self.proxies: List[Tuple[str, float]] = [] # Store proxy and its latency
1062
- self.last_refresh: float = 0
1063
- self.refresh_interval = refresh_interval
1064
- self.lock = threading.Lock() # Add a lock for thread safety
1065
- # Start auto-refresh in a separate thread
1066
- threading.Thread(target=self.auto_refresh_proxies, daemon=True).start()
1067
-
1068
- def fetch_proxies(self, max_proxies=50) -> List[str]:
1069
- try:
1070
- url = "https://api.proxyscrape.com/v2/?request=displayproxies&protocol=http&timeout=10000&country=all&ssl=all&anonymity=all"
1071
- response = requests.get(url)
1072
- proxies = response.text.split('\r\n')[:max_proxies] # Extract up to max_proxies
1073
- return [proxy for proxy in proxies if proxy]
1074
- except requests.RequestException as e:
1075
- print(f"Error fetching proxies: {e}")
1076
- return []
1077
-
1078
- def test_proxy(self, proxy: str) -> Tuple[str, float] | None:
1079
- # Test both HTTP and HTTPS
1080
- for protocol in ['http', 'https']:
1081
- try:
1082
- start_time = time.time()
1083
- response = requests.get('http://httpbin.org/ip', proxies={protocol: f"{protocol}://{proxy}"}, timeout=5)
1084
- if response.status_code == 200:
1085
- end_time = time.time()
1086
- return proxy, end_time - start_time
1087
- except requests.RequestException:
1088
- pass
1089
- return None
1090
-
1091
- def refresh_proxies(self) -> int:
1092
- new_proxies = self.fetch_proxies()
1093
- threads = []
1094
- working_proxies = []
1095
-
1096
- # Use threading for faster proxy testing
1097
- for proxy in new_proxies:
1098
- thread = threading.Thread(target=self.test_proxy_and_append, args=(proxy, working_proxies))
1099
- threads.append(thread)
1100
- thread.start()
1101
-
1102
- # Wait for all threads to complete
1103
- for thread in threads:
1104
- thread.join()
1105
-
1106
- with self.lock: # Acquire lock before updating proxies list
1107
- self.proxies = working_proxies
1108
- self.last_refresh = time.time()
1109
-
1110
- # print(f"Refreshed proxies at {datetime.now()}. Total working proxies: {len(self.proxies)}")
1111
- return len(self.proxies)
1112
-
1113
- def test_proxy_and_append(self, proxy: str, working_proxies: list):
1114
- result = self.test_proxy(proxy)
1115
- if result:
1116
- with self.lock: # Acquire lock before appending to shared list
1117
- working_proxies.append(result) # Append the proxy and its latency
1118
-
1119
- def auto_refresh_proxies(self):
1120
- while True:
1121
- time.sleep(self.refresh_interval)
1122
- self.refresh_proxies()
1123
-
1124
- def get_fastest_proxy(self) -> str | None:
1125
- with self.lock: # Acquire lock before accessing proxies list
1126
- if self.proxies:
1127
- # Sort proxies by latency and return the fastest
1128
- self.proxies.sort(key=lambda x: x[1]) # Sort by latency
1129
- return self.proxies[0][0] # Return the fastest proxy
1130
- return None
1
+ import os
2
+ import json
3
+ import platform
4
+ import subprocess
5
+ import logging
6
+ import threading
7
+ import time
8
+ import datetime
9
+ import re
10
+ import sys
11
+ from rich.markdown import Markdown
12
+ from rich.console import Console
13
+ from typing import List, Tuple, Union
14
+ from typing import NoReturn
15
+ import requests
16
+ from pathlib import Path
17
+ from playsound import playsound
18
+ from time import sleep as wait
19
+ import pathlib
20
+ import urllib.parse
21
+
22
+ default_path = os.path.join(os.path.expanduser("~"), ".cache", "AIWEBS", "webscout")
23
+
24
+ def sanitize_stream(
25
+ chunk: str, intro_value: str = "data:", to_json: bool = True
26
+ ) -> str | dict:
27
+ """Remove streaming flags
28
+
29
+ Args:
30
+ chunk (str): Streamig chunk.
31
+ intro_value (str, optional): streaming flag. Defaults to "data:".
32
+ to_json (bool, optional). Return chunk as dictionary. Defaults to True.
33
+
34
+ Returns:
35
+ str: Sanitized streaming value.
36
+ """
37
+
38
+ if chunk.startswith(intro_value):
39
+ chunk = chunk[len(intro_value) :]
40
+
41
+ return json.loads(chunk) if to_json else chunk
42
+ def run_system_command(
43
+ command: str,
44
+ exit_on_error: bool = True,
45
+ stdout_error: bool = True,
46
+ help: str = None,
47
+ ):
48
+ """Run commands against system
49
+ Args:
50
+ command (str): shell command
51
+ exit_on_error (bool, optional): Exit on error. Defaults to True.
52
+ stdout_error (bool, optional): Print out the error. Defaults to True
53
+ help (str, optional): Help info incase of exception. Defaults to None.
54
+ Returns:
55
+ tuple : (is_successfull, object[Exception|Subprocess.run])
56
+ """
57
+ try:
58
+ # Run the command and capture the output
59
+ result = subprocess.run(
60
+ command,
61
+ shell=True,
62
+ check=True,
63
+ text=True,
64
+ stdout=subprocess.PIPE,
65
+ stderr=subprocess.PIPE,
66
+ )
67
+ return (True, result)
68
+ except subprocess.CalledProcessError as e:
69
+ if exit_on_error:
70
+ raise Exception(f"Command failed with exit code {e.returncode}") from e
71
+ else:
72
+ return (False, e)
73
+
74
+
75
+ from .conversation import Conversation
76
+
77
+ from .optimizers import Optimizers
78
+
79
+ from .Extra.autocoder import AutoCoder
80
+
81
+ from .prompt_manager import AwesomePrompts
82
+
83
+ class Updates:
84
+ """Webscout latest release info"""
85
+
86
+ url = "https://api.github.com/repos/OE-LUCIFER/Webscout/releases/latest"
87
+
88
+ @property
89
+ def latest_version(self):
90
+ return self.latest(version=True)
91
+
92
+ def executable(self, system: str = platform.system()) -> str:
93
+ """Url pointing to executable for particular system
94
+
95
+ Args:
96
+ system (str, optional): system name. Defaults to platform.system().
97
+
98
+ Returns:
99
+ str: url
100
+ """
101
+ for entry in self.latest()["assets"]:
102
+ if entry.get("target") == system:
103
+ return entry.get("url")
104
+
105
+ def latest(self, whole: bool = False, version: bool = False) -> dict:
106
+ """Check Webscout latest version info
107
+
108
+ Args:
109
+ whole (bool, optional): Return whole json response. Defaults to False.
110
+ version (bool, optional): return version only. Defaults to False.
111
+
112
+ Returns:
113
+ bool|dict: version str or whole dict info
114
+ """
115
+ import requests
116
+
117
+ data = requests.get(self.url).json()
118
+ if whole:
119
+ return data
120
+
121
+ elif version:
122
+ return data.get("tag_name")
123
+
124
+ else:
125
+ sorted = dict(
126
+ tag_name=data.get("tag_name"),
127
+ tarball_url=data.get("tarball_url"),
128
+ zipball_url=data.get("zipball_url"),
129
+ html_url=data.get("html_url"),
130
+ body=data.get("body"),
131
+ )
132
+ whole_assets = []
133
+ for entry in data.get("assets"):
134
+ url = entry.get("browser_download_url")
135
+ assets = dict(url=url, size=entry.get("size"))
136
+ if ".deb" in url:
137
+ assets["target"] = "Debian"
138
+ elif ".exe" in url:
139
+ assets["target"] = "Windows"
140
+ elif "macos" in url:
141
+ assets["target"] = "Mac"
142
+ elif "linux" in url:
143
+ assets["target"] = "Linux"
144
+
145
+ whole_assets.append(assets)
146
+ sorted["assets"] = whole_assets
147
+
148
+ return sorted
149
+
150
+
151
+ class Audio:
152
+ # Request headers
153
+ headers: dict[str, str] = {
154
+ "User-Agent": "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/119.0.0.0 Safari/537.36"
155
+ }
156
+ cache_dir = pathlib.Path("./audio_cache")
157
+ all_voices: list[str] = [
158
+ "Filiz",
159
+ "Astrid",
160
+ "Tatyana",
161
+ "Maxim",
162
+ "Carmen",
163
+ "Ines",
164
+ "Cristiano",
165
+ "Vitoria",
166
+ "Ricardo",
167
+ "Maja",
168
+ "Jan",
169
+ "Jacek",
170
+ "Ewa",
171
+ "Ruben",
172
+ "Lotte",
173
+ "Liv",
174
+ "Seoyeon",
175
+ "Takumi",
176
+ "Mizuki",
177
+ "Giorgio",
178
+ "Carla",
179
+ "Bianca",
180
+ "Karl",
181
+ "Dora",
182
+ "Mathieu",
183
+ "Celine",
184
+ "Chantal",
185
+ "Penelope",
186
+ "Miguel",
187
+ "Mia",
188
+ "Enrique",
189
+ "Conchita",
190
+ "Geraint",
191
+ "Salli",
192
+ "Matthew",
193
+ "Kimberly",
194
+ "Kendra",
195
+ "Justin",
196
+ "Joey",
197
+ "Joanna",
198
+ "Ivy",
199
+ "Raveena",
200
+ "Aditi",
201
+ "Emma",
202
+ "Brian",
203
+ "Amy",
204
+ "Russell",
205
+ "Nicole",
206
+ "Vicki",
207
+ "Marlene",
208
+ "Hans",
209
+ "Naja",
210
+ "Mads",
211
+ "Gwyneth",
212
+ "Zhiyu",
213
+ "es-ES-Standard-A",
214
+ "it-IT-Standard-A",
215
+ "it-IT-Wavenet-A",
216
+ "ja-JP-Standard-A",
217
+ "ja-JP-Wavenet-A",
218
+ "ko-KR-Standard-A",
219
+ "ko-KR-Wavenet-A",
220
+ "pt-BR-Standard-A",
221
+ "tr-TR-Standard-A",
222
+ "sv-SE-Standard-A",
223
+ "nl-NL-Standard-A",
224
+ "nl-NL-Wavenet-A",
225
+ "en-US-Wavenet-A",
226
+ "en-US-Wavenet-B",
227
+ "en-US-Wavenet-C",
228
+ "en-US-Wavenet-D",
229
+ "en-US-Wavenet-E",
230
+ "en-US-Wavenet-F",
231
+ "en-GB-Standard-A",
232
+ "en-GB-Standard-B",
233
+ "en-GB-Standard-C",
234
+ "en-GB-Standard-D",
235
+ "en-GB-Wavenet-A",
236
+ "en-GB-Wavenet-B",
237
+ "en-GB-Wavenet-C",
238
+ "en-GB-Wavenet-D",
239
+ "en-US-Standard-B",
240
+ "en-US-Standard-C",
241
+ "en-US-Standard-D",
242
+ "en-US-Standard-E",
243
+ "de-DE-Standard-A",
244
+ "de-DE-Standard-B",
245
+ "de-DE-Wavenet-A",
246
+ "de-DE-Wavenet-B",
247
+ "de-DE-Wavenet-C",
248
+ "de-DE-Wavenet-D",
249
+ "en-AU-Standard-A",
250
+ "en-AU-Standard-B",
251
+ "en-AU-Wavenet-A",
252
+ "en-AU-Wavenet-B",
253
+ "en-AU-Wavenet-C",
254
+ "en-AU-Wavenet-D",
255
+ "en-AU-Standard-C",
256
+ "en-AU-Standard-D",
257
+ "fr-CA-Standard-A",
258
+ "fr-CA-Standard-B",
259
+ "fr-CA-Standard-C",
260
+ "fr-CA-Standard-D",
261
+ "fr-FR-Standard-C",
262
+ "fr-FR-Standard-D",
263
+ "fr-FR-Wavenet-A",
264
+ "fr-FR-Wavenet-B",
265
+ "fr-FR-Wavenet-C",
266
+ "fr-FR-Wavenet-D",
267
+ "da-DK-Wavenet-A",
268
+ "pl-PL-Wavenet-A",
269
+ "pl-PL-Wavenet-B",
270
+ "pl-PL-Wavenet-C",
271
+ "pl-PL-Wavenet-D",
272
+ "pt-PT-Wavenet-A",
273
+ "pt-PT-Wavenet-B",
274
+ "pt-PT-Wavenet-C",
275
+ "pt-PT-Wavenet-D",
276
+ "ru-RU-Wavenet-A",
277
+ "ru-RU-Wavenet-B",
278
+ "ru-RU-Wavenet-C",
279
+ "ru-RU-Wavenet-D",
280
+ "sk-SK-Wavenet-A",
281
+ "tr-TR-Wavenet-A",
282
+ "tr-TR-Wavenet-B",
283
+ "tr-TR-Wavenet-C",
284
+ "tr-TR-Wavenet-D",
285
+ "tr-TR-Wavenet-E",
286
+ "uk-UA-Wavenet-A",
287
+ "ar-XA-Wavenet-A",
288
+ "ar-XA-Wavenet-B",
289
+ "ar-XA-Wavenet-C",
290
+ "cs-CZ-Wavenet-A",
291
+ "nl-NL-Wavenet-B",
292
+ "nl-NL-Wavenet-C",
293
+ "nl-NL-Wavenet-D",
294
+ "nl-NL-Wavenet-E",
295
+ "en-IN-Wavenet-A",
296
+ "en-IN-Wavenet-B",
297
+ "en-IN-Wavenet-C",
298
+ "fil-PH-Wavenet-A",
299
+ "fi-FI-Wavenet-A",
300
+ "el-GR-Wavenet-A",
301
+ "hi-IN-Wavenet-A",
302
+ "hi-IN-Wavenet-B",
303
+ "hi-IN-Wavenet-C",
304
+ "hu-HU-Wavenet-A",
305
+ "id-ID-Wavenet-A",
306
+ "id-ID-Wavenet-B",
307
+ "id-ID-Wavenet-C",
308
+ "it-IT-Wavenet-B",
309
+ "it-IT-Wavenet-C",
310
+ "it-IT-Wavenet-D",
311
+ "ja-JP-Wavenet-B",
312
+ "ja-JP-Wavenet-C",
313
+ "ja-JP-Wavenet-D",
314
+ "cmn-CN-Wavenet-A",
315
+ "cmn-CN-Wavenet-B",
316
+ "cmn-CN-Wavenet-C",
317
+ "cmn-CN-Wavenet-D",
318
+ "nb-no-Wavenet-E",
319
+ "nb-no-Wavenet-A",
320
+ "nb-no-Wavenet-B",
321
+ "nb-no-Wavenet-C",
322
+ "nb-no-Wavenet-D",
323
+ "vi-VN-Wavenet-A",
324
+ "vi-VN-Wavenet-B",
325
+ "vi-VN-Wavenet-C",
326
+ "vi-VN-Wavenet-D",
327
+ "sr-rs-Standard-A",
328
+ "lv-lv-Standard-A",
329
+ "is-is-Standard-A",
330
+ "bg-bg-Standard-A",
331
+ "af-ZA-Standard-A",
332
+ "Tracy",
333
+ "Danny",
334
+ "Huihui",
335
+ "Yaoyao",
336
+ "Kangkang",
337
+ "HanHan",
338
+ "Zhiwei",
339
+ "Asaf",
340
+ "An",
341
+ "Stefanos",
342
+ "Filip",
343
+ "Ivan",
344
+ "Heidi",
345
+ "Herena",
346
+ "Kalpana",
347
+ "Hemant",
348
+ "Matej",
349
+ "Andika",
350
+ "Rizwan",
351
+ "Lado",
352
+ "Valluvar",
353
+ "Linda",
354
+ "Heather",
355
+ "Sean",
356
+ "Michael",
357
+ "Karsten",
358
+ "Guillaume",
359
+ "Pattara",
360
+ "Jakub",
361
+ "Szabolcs",
362
+ "Hoda",
363
+ "Naayf",
364
+ ]
365
+
366
+ @classmethod
367
+ def text_to_audio(
368
+ cls,
369
+ message: str,
370
+ voice: str = "Brian",
371
+ save_to: Union[Path, str] = None,
372
+ auto: bool = True,
373
+ ) -> Union[str, bytes]:
374
+ """
375
+ Text to speech using StreamElements API
376
+
377
+ Parameters:
378
+ message (str): The text to convert to speech
379
+ voice (str, optional): The voice to use for speech synthesis. Defaults to "Brian".
380
+ save_to (bool, optional): Path to save the audio file. Defaults to None.
381
+ auto (bool, optional): Generate filename based on `message` and save to `cls.cache_dir`. Defaults to False.
382
+
383
+ Returns:
384
+ result (Union[str, bytes]): Path to saved contents or audio content.
385
+ """
386
+ assert (
387
+ voice in cls.all_voices
388
+ ), f"Voice '{voice}' not one of [{', '.join(cls.all_voices)}]"
389
+ # Base URL for provider API
390
+ url: str = (
391
+ f"https://api.streamelements.com/kappa/v2/speech?voice={voice}&text={{{urllib.parse.quote(message)}}}"
392
+ )
393
+ resp = requests.get(url=url, headers=cls.headers, stream=True)
394
+ if not resp.ok:
395
+ raise Exception(
396
+ f"Failed to perform the operation - ({resp.status_code}, {resp.reason}) - {resp.text}"
397
+ )
398
+
399
+ def sanitize_filename(path):
400
+ trash = [
401
+ "\\",
402
+ "/",
403
+ ":",
404
+ "*",
405
+ "?",
406
+ '"',
407
+ "<",
408
+ "|",
409
+ ">",
410
+ ]
411
+ for val in trash:
412
+ path = path.replace(val, "")
413
+ return path.strip()
414
+
415
+ if auto:
416
+ filename: str = message + "..." if len(message) <= 40 else message[:40]
417
+ save_to = cls.cache_dir / sanitize_filename(filename)
418
+ save_to = save_to.as_posix()
419
+
420
+ # Ensure cache_dir exists
421
+ cls.cache_dir.mkdir(parents=True, exist_ok=True)
422
+
423
+ if save_to:
424
+ if not save_to.endswith("mp3"):
425
+ save_to += ".mp3"
426
+
427
+ with open(save_to, "wb") as fh:
428
+ for chunk in resp.iter_content(chunk_size=512):
429
+ fh.write(chunk)
430
+ else:
431
+ return resp.content
432
+ return save_to
433
+
434
+ @staticmethod
435
+ def play(path_to_audio_file: Union[Path, str]) -> NoReturn:
436
+ """Play audio (.mp3) using playsound.
437
+ """
438
+ if not Path(path_to_audio_file).is_file():
439
+ raise FileNotFoundError(f"File does not exist - '{path_to_audio_file}'")
440
+ playsound(path_to_audio_file)
441
+ #