Skip to content

Commit

Permalink
πŸ‘¨β€πŸ’» Update 2.0.0
Browse files Browse the repository at this point in the history
  • Loading branch information
DavideGalilei committed Dec 3, 2024
1 parent d9471e0 commit ea14253
Show file tree
Hide file tree
Showing 25 changed files with 409 additions and 112 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/test-suite.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ jobs:

strategy:
matrix:
version: [ "3.7", "3.8", "3.9", "3.10", "3.11" ]
version: [ "3.9", "3.10", "3.11", "3.12", "3.13" ]

steps:
- uses: "actions/checkout@v2"
Expand Down
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,5 @@
/.idea/
/gpytranslate.egg-info/
**/__pycache__
.aider*
.env
2 changes: 1 addition & 1 deletion LICENSE
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
MIT License

Copyright (c) 2023 Davide Galilei
Copyright (c) 2024 Davide Galilei

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
Expand Down
Empty file added examples/async/__init__.py
Empty file.
2 changes: 1 addition & 1 deletion examples/async/example.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@

from gpytranslate import Translator


"""Example showing basic translation and language detection."""


async def main() -> None:
"""Demonstrate translation from Italian to English and language detection."""
t = Translator()
Expand Down
2 changes: 1 addition & 1 deletion examples/async/https_proxy.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@

from gpytranslate import Translator


"""Example showing how to use HTTPS proxy with the translator."""


async def main() -> None:
"""Demonstrate translation using an HTTPS proxy."""
t = Translator(proxies={"https://": "https://{proxy_ip_here}"})
Expand Down
2 changes: 1 addition & 1 deletion examples/async/socks5_proxy.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@

from gpytranslate import Translator


"""Example showing how to use SOCKS5 proxy with the translator."""


async def main() -> None:
"""Demonstrate translation using a SOCKS5 proxy."""
t = Translator(proxies={"socks5": "socks5://user:[email protected]:1080"})
Expand Down
2 changes: 1 addition & 1 deletion examples/async/tts.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@

from gpytranslate import Translator


"""Example showing how to use text-to-speech functionality."""


async def main() -> None:
"""Demonstrate text-to-speech generation."""
translator = Translator()
Expand Down
Empty file added examples/sync/__init__.py
Empty file.
25 changes: 0 additions & 25 deletions examples/sync/example.py
Original file line number Diff line number Diff line change
@@ -1,28 +1,3 @@
"""
gpytranslate - A Python3 library for translating text using Google Translate API.
MIT License
Copyright (c) 2023 Davide Galilei
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
"""

from gpytranslate import SyncTranslator

t = SyncTranslator()
Expand Down
25 changes: 0 additions & 25 deletions examples/sync/https_proxy.py
Original file line number Diff line number Diff line change
@@ -1,28 +1,3 @@
"""
gpytranslate - A Python3 library for translating text using Google Translate API.
MIT License
Copyright (c) 2023 Davide Galilei
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
"""

from gpytranslate import SyncTranslator

t = SyncTranslator(proxies={"https://": "https://{proxy_ip_here}"})
Expand Down
33 changes: 2 additions & 31 deletions examples/sync/socks5_proxy.py
Original file line number Diff line number Diff line change
@@ -1,36 +1,7 @@
"""
gpytranslate - A Python3 library for translating text using Google Translate API.
MIT License
Copyright (c) 2023 Davide Galilei
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
"""

from httpx_socks import SyncProxyTransport

from gpytranslate import SyncTranslator

t = SyncTranslator(
transport=SyncProxyTransport.from_url("socks5://user:[email protected]:1080")
)
t = SyncTranslator(proxies={"socks5": "socks5://user:[email protected]:1080"})
# Check out https://pypi.org/project/httpx-socks/
translation = await t.translate("Ciao Mondo!", targetlang="en")
translation = t.translate("Ciao Mondo!", targetlang="en")
# Hello World!
print(f"Translation: {translation.text}")
2 changes: 1 addition & 1 deletion gpytranslate/__init__.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
from .exceptions import GpytranslateException, TranslationError
from .gpytranslate import Translator
from .sync import SyncTranslator
from .types import TranslatedObject
from .exceptions import GpytranslateException, TranslationError

__version__ = "1.5.1"
__all__ = [
Expand Down
12 changes: 6 additions & 6 deletions gpytranslate/gpytranslate.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,11 +32,11 @@ def __init__(
**options: Any,
) -> None:
"""Initialize the translator.
Args:
proxies: Optional proxy configuration dictionary
url: Translation API endpoint URL
tts_url: Text-to-speech API endpoint URL
tts_url: Text-to-speech API endpoint URL
headers: Custom headers or header generator function
**options: Additional options passed to httpx.AsyncClient
"""
Expand Down Expand Up @@ -215,13 +215,13 @@ async def tts(
targetlang: str = "en",
client: str = "at",
idx: int = 0,
prev: str = "input",
prev: str = "input",
chunk_size: int = 1024,
textlen: Optional[int] = None,
**extra: Any,
) -> Union[AsyncBufferedIOBase, io.BytesIO]:
"""Generate text-to-speech audio.
Args:
text: Text to convert to speech
file: Output file or buffer
Expand All @@ -232,10 +232,10 @@ async def tts(
chunk_size: Download chunk size
textlen: Override text length
**extra: Additional TTS parameters
Returns:
The output file/buffer with audio data
Raises:
TranslationError: If TTS generation fails
ValueError: If targetlang is invalid
Expand Down
Empty file added gpytranslate/py.typed
Empty file.
2 changes: 2 additions & 0 deletions gpytranslate/sync/__init__.py
Original file line number Diff line number Diff line change
@@ -1 +1,3 @@
from .sync_translator import SyncTranslator

__all__ = ["SyncTranslator"]
4 changes: 1 addition & 3 deletions gpytranslate/sync/sync_translator.py
Original file line number Diff line number Diff line change
Expand Up @@ -176,9 +176,7 @@ def translate(
raise TranslationError(e) from None

def detect(
self,
text: Union[str, List[Any], Dict[Any, Any]],
**kwargs: Any
self, text: Union[str, List[Any], Dict[Any, Any]], **kwargs: Any
) -> Union[str, List[str], Dict[Any, str]]:
if isinstance(text, str):
return self(text).lang
Expand Down
2 changes: 1 addition & 1 deletion gpytranslate/types/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ class Device:
@classmethod
def shift(cls) -> str:
"""Get next device string in rotation.
Returns:
str: Next device user agent string
"""
Expand Down
6 changes: 3 additions & 3 deletions gpytranslate/types/base_translator.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,16 +33,16 @@ def check(
dt: str,
) -> Union[TranslatedObject, Dict[K, TranslatedObject], List[TranslatedObject]]:
"""Check and validate translation API response.
Args:
text: Original input text
raw: Raw API response data
client: API client identifier
dt: Response data type
Returns:
Parsed translation result(s)
Raises:
TranslationError: If response validation fails
"""
Expand Down
16 changes: 9 additions & 7 deletions gpytranslate/types/translated_object.py
Original file line number Diff line number Diff line change
@@ -1,21 +1,23 @@
"""Translation result object implementation."""

from dataclasses import dataclass
from typing import Any, Dict, List, Optional
from typing import Any, Dict, List


@dataclass
class TranslatedObject:
"""A dataclass that holds translation results."""

raw: Dict[str, Any]
orig: str
text: str
text: str
orig_raw: List[str]
text_raw: List[str]
lang: str

def __str__(self) -> str:
"""Get string representation with the translated text.
Returns:
str: The translated text
"""
Expand All @@ -24,10 +26,10 @@ def __str__(self) -> str:
@classmethod
def from_raw_response(cls, raw: Dict[str, Any]) -> "TranslatedObject":
"""Create TranslatedObject from raw API response.
Args:
raw: Raw response dictionary from the translation API
Returns:
TranslatedObject: Parsed translation result
"""
Expand All @@ -37,5 +39,5 @@ def from_raw_response(cls, raw: Dict[str, Any]) -> "TranslatedObject":
text=" ".join(s["trans"] for s in raw["sentences"] if "trans" in s),
orig_raw=[s["orig"] for s in raw["sentences"] if "orig" in s],
text_raw=[s["trans"] for s in raw["sentences"] if "trans" in s],
lang=raw["src"]
lang=raw["src"],
)
Loading

0 comments on commit ea14253

Please sign in to comment.