webscout 1.3.6__py3-none-any.whl → 1.3.8__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.
- webscout/AI.py +0 -227
- webscout/AIutel.py +298 -1
- webscout/g4f.py +473 -473
- webscout/version.py +1 -1
- webscout/voice.py +26 -26
- webscout/webai.py +2419 -2360
- {webscout-1.3.6.dist-info → webscout-1.3.8.dist-info}/METADATA +2 -1
- {webscout-1.3.6.dist-info → webscout-1.3.8.dist-info}/RECORD +12 -13
- webscout/HelpingAI.py +0 -192
- {webscout-1.3.6.dist-info → webscout-1.3.8.dist-info}/LICENSE.md +0 -0
- {webscout-1.3.6.dist-info → webscout-1.3.8.dist-info}/WHEEL +0 -0
- {webscout-1.3.6.dist-info → webscout-1.3.8.dist-info}/entry_points.txt +0 -0
- {webscout-1.3.6.dist-info → webscout-1.3.8.dist-info}/top_level.txt +0 -0
webscout/AI.py
CHANGED
|
@@ -657,234 +657,7 @@ class GROQ(Provider):
|
|
|
657
657
|
return response["choices"][0]["message"]["content"]
|
|
658
658
|
except KeyError:
|
|
659
659
|
return ""
|
|
660
|
-
#----------------------------------------------------------Sean-------------------------------------class Sean:
|
|
661
|
-
def __init__(
|
|
662
|
-
self,
|
|
663
|
-
is_conversation: bool = True,
|
|
664
|
-
max_tokens: int = 600,
|
|
665
|
-
timeout: int = 30,
|
|
666
|
-
intro: str = None,
|
|
667
|
-
filepath: str = None,
|
|
668
|
-
update_file: bool = True,
|
|
669
|
-
proxies: dict = {},
|
|
670
|
-
history_offset: int = 10250,
|
|
671
|
-
act: str = None,
|
|
672
|
-
):
|
|
673
|
-
"""Instantiates OPENGPT
|
|
674
660
|
|
|
675
|
-
Args:
|
|
676
|
-
is_conversation (bool, optional): Flag for chatting conversationally. Defaults to True
|
|
677
|
-
max_tokens (int, optional): Maximum number of tokens to be generated upon completion. Defaults to 600.
|
|
678
|
-
timeout (int, optional): Http request timeout. Defaults to 30.
|
|
679
|
-
intro (str, optional): Conversation introductory prompt. Defaults to None.
|
|
680
|
-
filepath (str, optional): Path to file containing conversation history. Defaults to None.
|
|
681
|
-
update_file (bool, optional): Add new prompts and responses to the file. Defaults to True.
|
|
682
|
-
proxies (dict, optional): Http request proxies. Defaults to {}.
|
|
683
|
-
history_offset (int, optional): Limit conversation history to this number of last texts. Defaults to 10250.
|
|
684
|
-
act (str|int, optional): Awesome prompt key or index. (Used as intro). Defaults to None.
|
|
685
|
-
"""
|
|
686
|
-
self.session = requests.Session()
|
|
687
|
-
self.max_tokens_to_sample = max_tokens
|
|
688
|
-
self.is_conversation = is_conversation
|
|
689
|
-
self.chat_endpoint = (
|
|
690
|
-
"https://opengpts-example-vz4y4ooboq-uc.a.run.app/runs/stream"
|
|
691
|
-
)
|
|
692
|
-
self.stream_chunk_size = 64
|
|
693
|
-
self.timeout = timeout
|
|
694
|
-
self.last_response = {}
|
|
695
|
-
self.assistant_id = "281bc620-b9f3-47c6-bf74-3f0e5b6e7dac"
|
|
696
|
-
self.authority = "opengpts-example-vz4y4ooboq-uc.a.run.app"
|
|
697
|
-
|
|
698
|
-
self.headers = {
|
|
699
|
-
"authority": self.authority,
|
|
700
|
-
"accept": "text/event-stream",
|
|
701
|
-
"accept-language": "en-US,en;q=0.7",
|
|
702
|
-
"cache-control": "no-cache",
|
|
703
|
-
"content-type": "application/json",
|
|
704
|
-
"origin": "https://opengpts-example-vz4y4ooboq-uc.a.run.app",
|
|
705
|
-
"pragma": "no-cache",
|
|
706
|
-
"referer": "https://opengpts-example-vz4y4ooboq-uc.a.run.app/",
|
|
707
|
-
"sec-fetch-site": "same-origin",
|
|
708
|
-
"sec-gpc": "1",
|
|
709
|
-
"user-agent": "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36",
|
|
710
|
-
}
|
|
711
|
-
|
|
712
|
-
self.__available_optimizers = (
|
|
713
|
-
method
|
|
714
|
-
for method in dir(Optimizers)
|
|
715
|
-
if callable(getattr(Optimizers, method)) and not method.startswith("__")
|
|
716
|
-
)
|
|
717
|
-
self.session.headers.update(self.headers)
|
|
718
|
-
Conversation.intro = (
|
|
719
|
-
AwesomePrompts().get_act(
|
|
720
|
-
act, raise_not_found=True, default=None, case_insensitive=True
|
|
721
|
-
)
|
|
722
|
-
if act
|
|
723
|
-
else intro or Conversation.intro
|
|
724
|
-
)
|
|
725
|
-
self.conversation = Conversation(
|
|
726
|
-
is_conversation, self.max_tokens_to_sample, filepath, update_file
|
|
727
|
-
)
|
|
728
|
-
self.conversation.history_offset = history_offset
|
|
729
|
-
self.session.proxies = proxies
|
|
730
|
-
|
|
731
|
-
def ask(
|
|
732
|
-
self,
|
|
733
|
-
prompt: str,
|
|
734
|
-
stream: bool = False,
|
|
735
|
-
raw: bool = False,
|
|
736
|
-
optimizer: str = None,
|
|
737
|
-
conversationally: bool = False,
|
|
738
|
-
) -> dict:
|
|
739
|
-
"""Chat with AI
|
|
740
|
-
|
|
741
|
-
Args:
|
|
742
|
-
prompt (str): Prompt to be send.
|
|
743
|
-
stream (bool, optional): Flag for streaming response. Defaults to False.
|
|
744
|
-
raw (bool, optional): Stream back raw response as received. Defaults to False.
|
|
745
|
-
optimizer (str, optional): Prompt optimizer name - `[code, shell_command]`. Defaults to None.
|
|
746
|
-
conversationally (bool, optional): Chat conversationally when using optimizer. Defaults to False.
|
|
747
|
-
Returns:
|
|
748
|
-
dict : {}
|
|
749
|
-
```json
|
|
750
|
-
{
|
|
751
|
-
"messages": [
|
|
752
|
-
{
|
|
753
|
-
"content": "Hello there",
|
|
754
|
-
"additional_kwargs": {},
|
|
755
|
-
"type": "human",
|
|
756
|
-
"example": false
|
|
757
|
-
},
|
|
758
|
-
{
|
|
759
|
-
"content": "Hello! How can I assist you today?",
|
|
760
|
-
"additional_kwargs": {
|
|
761
|
-
"agent": {
|
|
762
|
-
"return_values": {
|
|
763
|
-
"output": "Hello! How can I assist you today?"
|
|
764
|
-
},
|
|
765
|
-
"log": "Hello! How can I assist you today?",
|
|
766
|
-
"type": "AgentFinish"
|
|
767
|
-
}
|
|
768
|
-
},
|
|
769
|
-
"type": "ai",
|
|
770
|
-
"example": false
|
|
771
|
-
}]
|
|
772
|
-
}
|
|
773
|
-
```
|
|
774
|
-
"""
|
|
775
|
-
conversation_prompt = self.conversation.gen_complete_prompt(prompt)
|
|
776
|
-
if optimizer:
|
|
777
|
-
if optimizer in self.__available_optimizers:
|
|
778
|
-
conversation_prompt = getattr(Optimizers, optimizer)(
|
|
779
|
-
conversation_prompt if conversationally else prompt
|
|
780
|
-
)
|
|
781
|
-
else:
|
|
782
|
-
raise Exception(
|
|
783
|
-
f"Optimizer is not one of {self.__available_optimizers}"
|
|
784
|
-
)
|
|
785
|
-
|
|
786
|
-
self.session.headers.update(self.headers)
|
|
787
|
-
self.session.headers.update(
|
|
788
|
-
dict(
|
|
789
|
-
cookie=f"opengpts_user_id={uuid4().__str__()}",
|
|
790
|
-
)
|
|
791
|
-
)
|
|
792
|
-
payload = {
|
|
793
|
-
"input": [
|
|
794
|
-
{
|
|
795
|
-
"content": conversation_prompt,
|
|
796
|
-
"additional_kwargs": {},
|
|
797
|
-
"type": "human",
|
|
798
|
-
"example": False,
|
|
799
|
-
},
|
|
800
|
-
],
|
|
801
|
-
"assistant_id": self.assistant_id,
|
|
802
|
-
"thread_id": "",
|
|
803
|
-
}
|
|
804
|
-
|
|
805
|
-
def for_stream():
|
|
806
|
-
response = self.session.post(
|
|
807
|
-
self.chat_endpoint, json=payload, stream=True, timeout=self.timeout
|
|
808
|
-
)
|
|
809
|
-
if (
|
|
810
|
-
not response.ok
|
|
811
|
-
or not response.headers.get("Content-Type")
|
|
812
|
-
== "text/event-stream; charset=utf-8"
|
|
813
|
-
):
|
|
814
|
-
raise Exception(
|
|
815
|
-
f"Failed to generate response - ({response.status_code}, {response.reason}) - {response.text}"
|
|
816
|
-
)
|
|
817
|
-
|
|
818
|
-
for value in response.iter_lines(
|
|
819
|
-
decode_unicode=True,
|
|
820
|
-
chunk_size=self.stream_chunk_size,
|
|
821
|
-
):
|
|
822
|
-
try:
|
|
823
|
-
modified_value = re.sub("data:", "", value)
|
|
824
|
-
resp = json.loads(modified_value)
|
|
825
|
-
if len(resp) == 1:
|
|
826
|
-
continue
|
|
827
|
-
self.last_response.update(resp[1])
|
|
828
|
-
yield value if raw else resp[1]
|
|
829
|
-
except json.decoder.JSONDecodeError:
|
|
830
|
-
pass
|
|
831
|
-
self.conversation.update_chat_history(
|
|
832
|
-
prompt, self.get_message(self.last_response)
|
|
833
|
-
)
|
|
834
|
-
|
|
835
|
-
def for_non_stream():
|
|
836
|
-
for _ in for_stream():
|
|
837
|
-
pass
|
|
838
|
-
return self.last_response
|
|
839
|
-
|
|
840
|
-
return for_stream() if stream else for_non_stream()
|
|
841
|
-
|
|
842
|
-
def chat(
|
|
843
|
-
self,
|
|
844
|
-
prompt: str,
|
|
845
|
-
stream: bool = False,
|
|
846
|
-
optimizer: str = None,
|
|
847
|
-
conversationally: bool = False,
|
|
848
|
-
) -> str:
|
|
849
|
-
"""Generate response `str`
|
|
850
|
-
Args:
|
|
851
|
-
prompt (str): Prompt to be send.
|
|
852
|
-
stream (bool, optional): Flag for streaming response. Defaults to False.
|
|
853
|
-
optimizer (str, optional): Prompt optimizer name - `[code, shell_command]`. Defaults to None.
|
|
854
|
-
conversationally (bool, optional): Chat conversationally when using optimizer. Defaults to False.
|
|
855
|
-
Returns:
|
|
856
|
-
str: Response generated
|
|
857
|
-
"""
|
|
858
|
-
|
|
859
|
-
def for_stream():
|
|
860
|
-
for response in self.ask(
|
|
861
|
-
prompt, True, optimizer=optimizer, conversationally=conversationally
|
|
862
|
-
):
|
|
863
|
-
yield self.get_message(response)
|
|
864
|
-
|
|
865
|
-
def for_non_stream():
|
|
866
|
-
return self.get_message(
|
|
867
|
-
self.ask(
|
|
868
|
-
prompt,
|
|
869
|
-
False,
|
|
870
|
-
optimizer=optimizer,
|
|
871
|
-
conversationally=conversationally,
|
|
872
|
-
)
|
|
873
|
-
)
|
|
874
|
-
|
|
875
|
-
return for_stream() if stream else for_non_stream()
|
|
876
|
-
|
|
877
|
-
def get_message(self, response: dict) -> str:
|
|
878
|
-
"""Retrieves message only from response
|
|
879
|
-
|
|
880
|
-
Args:
|
|
881
|
-
response (dict): Response generated by `self.ask`
|
|
882
|
-
|
|
883
|
-
Returns:
|
|
884
|
-
str: Message extracted
|
|
885
|
-
"""
|
|
886
|
-
assert isinstance(response, dict), "Response should be of dict data-type only"
|
|
887
|
-
return response["content"]
|
|
888
661
|
#----------------------------------------------------------OpenAI-----------------------------------
|
|
889
662
|
class OPENAI(Provider):
|
|
890
663
|
model = "gpt-3.5-turbo"
|
webscout/AIutel.py
CHANGED
|
@@ -11,6 +11,13 @@ import click
|
|
|
11
11
|
from rich.markdown import Markdown
|
|
12
12
|
from rich.console import Console
|
|
13
13
|
import g4f
|
|
14
|
+
from typing import Union
|
|
15
|
+
from typing import NoReturn
|
|
16
|
+
import requests
|
|
17
|
+
from pathlib import Path
|
|
18
|
+
from playsound import playsound
|
|
19
|
+
from time import sleep as wait
|
|
20
|
+
import pathlib
|
|
14
21
|
appdir = appdirs.AppDirs("AIWEBS", "vortex")
|
|
15
22
|
|
|
16
23
|
default_path = appdir.user_cache_dir
|
|
@@ -671,4 +678,294 @@ Current Datetime : {datetime.datetime.now()}
|
|
|
671
678
|
f"{e.args[1] if len(e.args)>1 else str(e)}",
|
|
672
679
|
"error",
|
|
673
680
|
)
|
|
674
|
-
return f"PREVIOUS SCRIPT EXCEPTION:\n{str(e)}"
|
|
681
|
+
return f"PREVIOUS SCRIPT EXCEPTION:\n{str(e)}"
|
|
682
|
+
class Audio:
|
|
683
|
+
# Request headers
|
|
684
|
+
headers: dict[str, str] = {
|
|
685
|
+
"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"
|
|
686
|
+
}
|
|
687
|
+
cache_dir = pathlib.Path("./audio_cache")
|
|
688
|
+
all_voices: list[str] = [
|
|
689
|
+
"Filiz",
|
|
690
|
+
"Astrid",
|
|
691
|
+
"Tatyana",
|
|
692
|
+
"Maxim",
|
|
693
|
+
"Carmen",
|
|
694
|
+
"Ines",
|
|
695
|
+
"Cristiano",
|
|
696
|
+
"Vitoria",
|
|
697
|
+
"Ricardo",
|
|
698
|
+
"Maja",
|
|
699
|
+
"Jan",
|
|
700
|
+
"Jacek",
|
|
701
|
+
"Ewa",
|
|
702
|
+
"Ruben",
|
|
703
|
+
"Lotte",
|
|
704
|
+
"Liv",
|
|
705
|
+
"Seoyeon",
|
|
706
|
+
"Takumi",
|
|
707
|
+
"Mizuki",
|
|
708
|
+
"Giorgio",
|
|
709
|
+
"Carla",
|
|
710
|
+
"Bianca",
|
|
711
|
+
"Karl",
|
|
712
|
+
"Dora",
|
|
713
|
+
"Mathieu",
|
|
714
|
+
"Celine",
|
|
715
|
+
"Chantal",
|
|
716
|
+
"Penelope",
|
|
717
|
+
"Miguel",
|
|
718
|
+
"Mia",
|
|
719
|
+
"Enrique",
|
|
720
|
+
"Conchita",
|
|
721
|
+
"Geraint",
|
|
722
|
+
"Salli",
|
|
723
|
+
"Matthew",
|
|
724
|
+
"Kimberly",
|
|
725
|
+
"Kendra",
|
|
726
|
+
"Justin",
|
|
727
|
+
"Joey",
|
|
728
|
+
"Joanna",
|
|
729
|
+
"Ivy",
|
|
730
|
+
"Raveena",
|
|
731
|
+
"Aditi",
|
|
732
|
+
"Emma",
|
|
733
|
+
"Brian",
|
|
734
|
+
"Amy",
|
|
735
|
+
"Russell",
|
|
736
|
+
"Nicole",
|
|
737
|
+
"Vicki",
|
|
738
|
+
"Marlene",
|
|
739
|
+
"Hans",
|
|
740
|
+
"Naja",
|
|
741
|
+
"Mads",
|
|
742
|
+
"Gwyneth",
|
|
743
|
+
"Zhiyu",
|
|
744
|
+
"es-ES-Standard-A",
|
|
745
|
+
"it-IT-Standard-A",
|
|
746
|
+
"it-IT-Wavenet-A",
|
|
747
|
+
"ja-JP-Standard-A",
|
|
748
|
+
"ja-JP-Wavenet-A",
|
|
749
|
+
"ko-KR-Standard-A",
|
|
750
|
+
"ko-KR-Wavenet-A",
|
|
751
|
+
"pt-BR-Standard-A",
|
|
752
|
+
"tr-TR-Standard-A",
|
|
753
|
+
"sv-SE-Standard-A",
|
|
754
|
+
"nl-NL-Standard-A",
|
|
755
|
+
"nl-NL-Wavenet-A",
|
|
756
|
+
"en-US-Wavenet-A",
|
|
757
|
+
"en-US-Wavenet-B",
|
|
758
|
+
"en-US-Wavenet-C",
|
|
759
|
+
"en-US-Wavenet-D",
|
|
760
|
+
"en-US-Wavenet-E",
|
|
761
|
+
"en-US-Wavenet-F",
|
|
762
|
+
"en-GB-Standard-A",
|
|
763
|
+
"en-GB-Standard-B",
|
|
764
|
+
"en-GB-Standard-C",
|
|
765
|
+
"en-GB-Standard-D",
|
|
766
|
+
"en-GB-Wavenet-A",
|
|
767
|
+
"en-GB-Wavenet-B",
|
|
768
|
+
"en-GB-Wavenet-C",
|
|
769
|
+
"en-GB-Wavenet-D",
|
|
770
|
+
"en-US-Standard-B",
|
|
771
|
+
"en-US-Standard-C",
|
|
772
|
+
"en-US-Standard-D",
|
|
773
|
+
"en-US-Standard-E",
|
|
774
|
+
"de-DE-Standard-A",
|
|
775
|
+
"de-DE-Standard-B",
|
|
776
|
+
"de-DE-Wavenet-A",
|
|
777
|
+
"de-DE-Wavenet-B",
|
|
778
|
+
"de-DE-Wavenet-C",
|
|
779
|
+
"de-DE-Wavenet-D",
|
|
780
|
+
"en-AU-Standard-A",
|
|
781
|
+
"en-AU-Standard-B",
|
|
782
|
+
"en-AU-Wavenet-A",
|
|
783
|
+
"en-AU-Wavenet-B",
|
|
784
|
+
"en-AU-Wavenet-C",
|
|
785
|
+
"en-AU-Wavenet-D",
|
|
786
|
+
"en-AU-Standard-C",
|
|
787
|
+
"en-AU-Standard-D",
|
|
788
|
+
"fr-CA-Standard-A",
|
|
789
|
+
"fr-CA-Standard-B",
|
|
790
|
+
"fr-CA-Standard-C",
|
|
791
|
+
"fr-CA-Standard-D",
|
|
792
|
+
"fr-FR-Standard-C",
|
|
793
|
+
"fr-FR-Standard-D",
|
|
794
|
+
"fr-FR-Wavenet-A",
|
|
795
|
+
"fr-FR-Wavenet-B",
|
|
796
|
+
"fr-FR-Wavenet-C",
|
|
797
|
+
"fr-FR-Wavenet-D",
|
|
798
|
+
"da-DK-Wavenet-A",
|
|
799
|
+
"pl-PL-Wavenet-A",
|
|
800
|
+
"pl-PL-Wavenet-B",
|
|
801
|
+
"pl-PL-Wavenet-C",
|
|
802
|
+
"pl-PL-Wavenet-D",
|
|
803
|
+
"pt-PT-Wavenet-A",
|
|
804
|
+
"pt-PT-Wavenet-B",
|
|
805
|
+
"pt-PT-Wavenet-C",
|
|
806
|
+
"pt-PT-Wavenet-D",
|
|
807
|
+
"ru-RU-Wavenet-A",
|
|
808
|
+
"ru-RU-Wavenet-B",
|
|
809
|
+
"ru-RU-Wavenet-C",
|
|
810
|
+
"ru-RU-Wavenet-D",
|
|
811
|
+
"sk-SK-Wavenet-A",
|
|
812
|
+
"tr-TR-Wavenet-A",
|
|
813
|
+
"tr-TR-Wavenet-B",
|
|
814
|
+
"tr-TR-Wavenet-C",
|
|
815
|
+
"tr-TR-Wavenet-D",
|
|
816
|
+
"tr-TR-Wavenet-E",
|
|
817
|
+
"uk-UA-Wavenet-A",
|
|
818
|
+
"ar-XA-Wavenet-A",
|
|
819
|
+
"ar-XA-Wavenet-B",
|
|
820
|
+
"ar-XA-Wavenet-C",
|
|
821
|
+
"cs-CZ-Wavenet-A",
|
|
822
|
+
"nl-NL-Wavenet-B",
|
|
823
|
+
"nl-NL-Wavenet-C",
|
|
824
|
+
"nl-NL-Wavenet-D",
|
|
825
|
+
"nl-NL-Wavenet-E",
|
|
826
|
+
"en-IN-Wavenet-A",
|
|
827
|
+
"en-IN-Wavenet-B",
|
|
828
|
+
"en-IN-Wavenet-C",
|
|
829
|
+
"fil-PH-Wavenet-A",
|
|
830
|
+
"fi-FI-Wavenet-A",
|
|
831
|
+
"el-GR-Wavenet-A",
|
|
832
|
+
"hi-IN-Wavenet-A",
|
|
833
|
+
"hi-IN-Wavenet-B",
|
|
834
|
+
"hi-IN-Wavenet-C",
|
|
835
|
+
"hu-HU-Wavenet-A",
|
|
836
|
+
"id-ID-Wavenet-A",
|
|
837
|
+
"id-ID-Wavenet-B",
|
|
838
|
+
"id-ID-Wavenet-C",
|
|
839
|
+
"it-IT-Wavenet-B",
|
|
840
|
+
"it-IT-Wavenet-C",
|
|
841
|
+
"it-IT-Wavenet-D",
|
|
842
|
+
"ja-JP-Wavenet-B",
|
|
843
|
+
"ja-JP-Wavenet-C",
|
|
844
|
+
"ja-JP-Wavenet-D",
|
|
845
|
+
"cmn-CN-Wavenet-A",
|
|
846
|
+
"cmn-CN-Wavenet-B",
|
|
847
|
+
"cmn-CN-Wavenet-C",
|
|
848
|
+
"cmn-CN-Wavenet-D",
|
|
849
|
+
"nb-no-Wavenet-E",
|
|
850
|
+
"nb-no-Wavenet-A",
|
|
851
|
+
"nb-no-Wavenet-B",
|
|
852
|
+
"nb-no-Wavenet-C",
|
|
853
|
+
"nb-no-Wavenet-D",
|
|
854
|
+
"vi-VN-Wavenet-A",
|
|
855
|
+
"vi-VN-Wavenet-B",
|
|
856
|
+
"vi-VN-Wavenet-C",
|
|
857
|
+
"vi-VN-Wavenet-D",
|
|
858
|
+
"sr-rs-Standard-A",
|
|
859
|
+
"lv-lv-Standard-A",
|
|
860
|
+
"is-is-Standard-A",
|
|
861
|
+
"bg-bg-Standard-A",
|
|
862
|
+
"af-ZA-Standard-A",
|
|
863
|
+
"Tracy",
|
|
864
|
+
"Danny",
|
|
865
|
+
"Huihui",
|
|
866
|
+
"Yaoyao",
|
|
867
|
+
"Kangkang",
|
|
868
|
+
"HanHan",
|
|
869
|
+
"Zhiwei",
|
|
870
|
+
"Asaf",
|
|
871
|
+
"An",
|
|
872
|
+
"Stefanos",
|
|
873
|
+
"Filip",
|
|
874
|
+
"Ivan",
|
|
875
|
+
"Heidi",
|
|
876
|
+
"Herena",
|
|
877
|
+
"Kalpana",
|
|
878
|
+
"Hemant",
|
|
879
|
+
"Matej",
|
|
880
|
+
"Andika",
|
|
881
|
+
"Rizwan",
|
|
882
|
+
"Lado",
|
|
883
|
+
"Valluvar",
|
|
884
|
+
"Linda",
|
|
885
|
+
"Heather",
|
|
886
|
+
"Sean",
|
|
887
|
+
"Michael",
|
|
888
|
+
"Karsten",
|
|
889
|
+
"Guillaume",
|
|
890
|
+
"Pattara",
|
|
891
|
+
"Jakub",
|
|
892
|
+
"Szabolcs",
|
|
893
|
+
"Hoda",
|
|
894
|
+
"Naayf",
|
|
895
|
+
]
|
|
896
|
+
|
|
897
|
+
@classmethod
|
|
898
|
+
def text_to_audio(
|
|
899
|
+
cls,
|
|
900
|
+
message: str,
|
|
901
|
+
voice: str = "Brian",
|
|
902
|
+
save_to: Union[Path, str] = None,
|
|
903
|
+
auto: bool = True,
|
|
904
|
+
) -> Union[str, bytes]:
|
|
905
|
+
"""
|
|
906
|
+
Text to speech using StreamElements API
|
|
907
|
+
|
|
908
|
+
Parameters:
|
|
909
|
+
message (str): The text to convert to speech
|
|
910
|
+
voice (str, optional): The voice to use for speech synthesis. Defaults to "Brian".
|
|
911
|
+
save_to (bool, optional): Path to save the audio file. Defaults to None.
|
|
912
|
+
auto (bool, optional): Generate filename based on `message` and save to `cls.cache_dir`. Defaults to False.
|
|
913
|
+
|
|
914
|
+
Returns:
|
|
915
|
+
result (Union[str, bytes]): Path to saved contents or audio content.
|
|
916
|
+
"""
|
|
917
|
+
assert (
|
|
918
|
+
voice in cls.all_voices
|
|
919
|
+
), f"Voice '{voice}' not one of [{', '.join(cls.all_voices)}]"
|
|
920
|
+
# Base URL for provider API
|
|
921
|
+
url: str = (
|
|
922
|
+
f"https://api.streamelements.com/kappa/v2/speech?voice={voice}&text={{{message}}}"
|
|
923
|
+
)
|
|
924
|
+
resp = requests.get(url=url, headers=cls.headers, stream=True)
|
|
925
|
+
if not resp.ok:
|
|
926
|
+
raise Exception(
|
|
927
|
+
f"Failed to perform the operation - ({resp.status_code}, {resp.reason}) - {resp.text}"
|
|
928
|
+
)
|
|
929
|
+
|
|
930
|
+
def sanitize_filename(path):
|
|
931
|
+
trash = [
|
|
932
|
+
"\\",
|
|
933
|
+
"/",
|
|
934
|
+
":",
|
|
935
|
+
"*",
|
|
936
|
+
"?",
|
|
937
|
+
'"',
|
|
938
|
+
"<",
|
|
939
|
+
"|",
|
|
940
|
+
">",
|
|
941
|
+
]
|
|
942
|
+
for val in trash:
|
|
943
|
+
path = path.replace(val, "")
|
|
944
|
+
return path.strip()
|
|
945
|
+
|
|
946
|
+
if auto:
|
|
947
|
+
filename: str = message + "..." if len(message) <= 40 else message[:40]
|
|
948
|
+
save_to = cls.cache_dir / sanitize_filename(filename)
|
|
949
|
+
save_to = save_to.as_posix()
|
|
950
|
+
|
|
951
|
+
# Ensure cache_dir exists
|
|
952
|
+
cls.cache_dir.mkdir(parents=True, exist_ok=True)
|
|
953
|
+
|
|
954
|
+
if save_to:
|
|
955
|
+
if not save_to.endswith("mp3"):
|
|
956
|
+
save_to += ".mp3"
|
|
957
|
+
|
|
958
|
+
with open(save_to, "wb") as fh:
|
|
959
|
+
for chunk in resp.iter_content(chunk_size=512):
|
|
960
|
+
fh.write(chunk)
|
|
961
|
+
else:
|
|
962
|
+
return resp.content
|
|
963
|
+
return save_to
|
|
964
|
+
|
|
965
|
+
@staticmethod
|
|
966
|
+
def play(path_to_audio_file: Union[Path, str]) -> NoReturn:
|
|
967
|
+
"""Play audio (.mp3) using playsound.
|
|
968
|
+
"""
|
|
969
|
+
if not Path(path_to_audio_file).is_file():
|
|
970
|
+
raise FileNotFoundError(f"File does not exist - '{path_to_audio_file}'")
|
|
971
|
+
playsound(path_to_audio_file)
|