kr-translator 0.1.0__tar.gz
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.
- kr_translator-0.1.0/LICENSE.md +21 -0
- kr_translator-0.1.0/PKG-INFO +68 -0
- kr_translator-0.1.0/README.md +50 -0
- kr_translator-0.1.0/pyproject.toml +35 -0
- kr_translator-0.1.0/src/kr_translator/__init__.py +0 -0
- kr_translator-0.1.0/src/kr_translator/caller.py +111 -0
- kr_translator-0.1.0/src/kr_translator/exceptions.py +2 -0
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) [2024] [the.zlood@gmail.com]
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
|
@@ -0,0 +1,68 @@
|
|
|
1
|
+
Metadata-Version: 2.1
|
|
2
|
+
Name: kr-translator
|
|
3
|
+
Version: 0.1.0
|
|
4
|
+
Summary:
|
|
5
|
+
License: MIT
|
|
6
|
+
Author: zlood
|
|
7
|
+
Requires-Python: >=3.10,<4.0
|
|
8
|
+
Classifier: License :: OSI Approved :: MIT License
|
|
9
|
+
Classifier: Operating System :: OS Independent
|
|
10
|
+
Classifier: Programming Language :: Python :: 3
|
|
11
|
+
Classifier: Programming Language :: Python :: 3.10
|
|
12
|
+
Classifier: Programming Language :: Python :: 3.11
|
|
13
|
+
Requires-Dist: langchain (>=0.2.6,<0.3.0)
|
|
14
|
+
Requires-Dist: langchain-community (>=0.2.6,<0.3.0)
|
|
15
|
+
Requires-Dist: langchain-openai (>=0.1.13,<0.2.0)
|
|
16
|
+
Description-Content-Type: text/markdown
|
|
17
|
+
|
|
18
|
+
# KR Translator
|
|
19
|
+
|
|
20
|
+
Translate a korean `txt` file.
|
|
21
|
+
|
|
22
|
+
## Installation
|
|
23
|
+
|
|
24
|
+
You can install the package using pip:
|
|
25
|
+
|
|
26
|
+
```bash
|
|
27
|
+
pip install kr-translation
|
|
28
|
+
```
|
|
29
|
+
|
|
30
|
+
## Usage
|
|
31
|
+
|
|
32
|
+
### Getting OPENAI API KEY
|
|
33
|
+
|
|
34
|
+
1. Go to [https://platform.openai.com/api-keys](https://platform.openai.com/api-keys), follow instruction over there.
|
|
35
|
+
|
|
36
|
+
2. Save the API KEY somewhere save.
|
|
37
|
+
|
|
38
|
+
### Basic Translation
|
|
39
|
+
|
|
40
|
+
```python
|
|
41
|
+
from kr_translator.caller import TextTranslator
|
|
42
|
+
|
|
43
|
+
translator = TextTranslator(
|
|
44
|
+
api_key="your_api_key_here",
|
|
45
|
+
source_file_location="/path/to/file.txt"
|
|
46
|
+
)
|
|
47
|
+
output = translator.translate()
|
|
48
|
+
|
|
49
|
+
print(output)
|
|
50
|
+
```
|
|
51
|
+
|
|
52
|
+
### Save Translation to a file
|
|
53
|
+
|
|
54
|
+
```python
|
|
55
|
+
from kr_translator.caller import TextTranslator
|
|
56
|
+
|
|
57
|
+
translator = TextTranslator(
|
|
58
|
+
api_key="your_api_key_here",
|
|
59
|
+
source_file_location="/path/to/file.txt"
|
|
60
|
+
)
|
|
61
|
+
output = translator.translate()
|
|
62
|
+
translator.save_translation("output_file.txt")
|
|
63
|
+
```
|
|
64
|
+
|
|
65
|
+
## License
|
|
66
|
+
|
|
67
|
+
This project is licensed under the MIT License - see the LICENSE.md file for details.
|
|
68
|
+
|
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
# KR Translator
|
|
2
|
+
|
|
3
|
+
Translate a korean `txt` file.
|
|
4
|
+
|
|
5
|
+
## Installation
|
|
6
|
+
|
|
7
|
+
You can install the package using pip:
|
|
8
|
+
|
|
9
|
+
```bash
|
|
10
|
+
pip install kr-translation
|
|
11
|
+
```
|
|
12
|
+
|
|
13
|
+
## Usage
|
|
14
|
+
|
|
15
|
+
### Getting OPENAI API KEY
|
|
16
|
+
|
|
17
|
+
1. Go to [https://platform.openai.com/api-keys](https://platform.openai.com/api-keys), follow instruction over there.
|
|
18
|
+
|
|
19
|
+
2. Save the API KEY somewhere save.
|
|
20
|
+
|
|
21
|
+
### Basic Translation
|
|
22
|
+
|
|
23
|
+
```python
|
|
24
|
+
from kr_translator.caller import TextTranslator
|
|
25
|
+
|
|
26
|
+
translator = TextTranslator(
|
|
27
|
+
api_key="your_api_key_here",
|
|
28
|
+
source_file_location="/path/to/file.txt"
|
|
29
|
+
)
|
|
30
|
+
output = translator.translate()
|
|
31
|
+
|
|
32
|
+
print(output)
|
|
33
|
+
```
|
|
34
|
+
|
|
35
|
+
### Save Translation to a file
|
|
36
|
+
|
|
37
|
+
```python
|
|
38
|
+
from kr_translator.caller import TextTranslator
|
|
39
|
+
|
|
40
|
+
translator = TextTranslator(
|
|
41
|
+
api_key="your_api_key_here",
|
|
42
|
+
source_file_location="/path/to/file.txt"
|
|
43
|
+
)
|
|
44
|
+
output = translator.translate()
|
|
45
|
+
translator.save_translation("output_file.txt")
|
|
46
|
+
```
|
|
47
|
+
|
|
48
|
+
## License
|
|
49
|
+
|
|
50
|
+
This project is licensed under the MIT License - see the LICENSE.md file for details.
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
[tool.poetry]
|
|
2
|
+
name = "kr-translator"
|
|
3
|
+
version = "0.1.0"
|
|
4
|
+
description = ""
|
|
5
|
+
authors = ["zlood"]
|
|
6
|
+
license = "MIT"
|
|
7
|
+
readme = "README.md"
|
|
8
|
+
classifiers = [
|
|
9
|
+
"Programming Language :: Python :: 3",
|
|
10
|
+
"License :: OSI Approved :: MIT License",
|
|
11
|
+
"Operating System :: OS Independent"
|
|
12
|
+
]
|
|
13
|
+
|
|
14
|
+
[tool.poetry.dependencies]
|
|
15
|
+
python = "^3.10"
|
|
16
|
+
langchain = "^0.2.6"
|
|
17
|
+
langchain-community = "^0.2.6"
|
|
18
|
+
langchain-openai = "^0.1.13"
|
|
19
|
+
|
|
20
|
+
[tool.poetry.group.dev.dependencies]
|
|
21
|
+
pytest = "^8.2.2"
|
|
22
|
+
pytest-asyncio = "^0.23.7"
|
|
23
|
+
|
|
24
|
+
[build-system]
|
|
25
|
+
requires = ["poetry-core"]
|
|
26
|
+
build-backend = "poetry.core.masonry.api"
|
|
27
|
+
|
|
28
|
+
[tool.pytest.ini_options]
|
|
29
|
+
minversion = 6.0
|
|
30
|
+
addopts = "-ra -q -m 'not call_openai'"
|
|
31
|
+
testpaths = ["tests"]
|
|
32
|
+
asyncio_mode = "auto"
|
|
33
|
+
markers = """
|
|
34
|
+
call_openai: mark tests that call OpenAI APIs
|
|
35
|
+
"""
|
|
File without changes
|
|
@@ -0,0 +1,111 @@
|
|
|
1
|
+
from langchain_community.callbacks import get_openai_callback
|
|
2
|
+
from langchain_community.document_loaders.text import TextLoader
|
|
3
|
+
from langchain_core.prompts import ChatPromptTemplate
|
|
4
|
+
from langchain_openai.chat_models import ChatOpenAI
|
|
5
|
+
from langchain_core.output_parsers.string import StrOutputParser
|
|
6
|
+
|
|
7
|
+
from kr_translator.exceptions import TextLoaderError
|
|
8
|
+
|
|
9
|
+
|
|
10
|
+
def _get_prompt(additional="", characters=""):
|
|
11
|
+
baseline_instructions = """
|
|
12
|
+
# instruction
|
|
13
|
+
You are Korean text translator. Your job is to translate the korean text to english.
|
|
14
|
+
Keep the korean honorific that is commonly used, such as oppa, noona, hyung, and unnie as it is.
|
|
15
|
+
Translate all sound effects.
|
|
16
|
+
"""
|
|
17
|
+
|
|
18
|
+
named_entities = """
|
|
19
|
+
# named entities
|
|
20
|
+
do not translate named entities such as people's name or company name.
|
|
21
|
+
"""
|
|
22
|
+
|
|
23
|
+
additional_instruction = """
|
|
24
|
+
# additional instruction
|
|
25
|
+
"""
|
|
26
|
+
return (
|
|
27
|
+
baseline_instructions
|
|
28
|
+
+ named_entities
|
|
29
|
+
+ characters
|
|
30
|
+
+ additional_instruction
|
|
31
|
+
+ additional
|
|
32
|
+
)
|
|
33
|
+
|
|
34
|
+
|
|
35
|
+
class TextTranslator:
|
|
36
|
+
"""
|
|
37
|
+
A class used to translate text from a source file using the OpenAI API.
|
|
38
|
+
|
|
39
|
+
Attributes:
|
|
40
|
+
api_key (str): The API key for accessing OpenAI services.
|
|
41
|
+
source_file_location (str): The location of the source text file to be translated.
|
|
42
|
+
|
|
43
|
+
Methods:
|
|
44
|
+
translate(characters="", additional_info=""): Translates the text from the source file using the specified characters and additional information.
|
|
45
|
+
save_translation(destination_file_location="file.txt"): Saves the translated text to the specified destination file location.
|
|
46
|
+
"""
|
|
47
|
+
|
|
48
|
+
def __init__(self, api_key, source_file_location):
|
|
49
|
+
if not source_file_location.endswith(".txt"):
|
|
50
|
+
raise ValueError("The source file must be a .txt file")
|
|
51
|
+
self.llm = ChatOpenAI(
|
|
52
|
+
openai_api_key=api_key,
|
|
53
|
+
model="gpt-4o",
|
|
54
|
+
)
|
|
55
|
+
self.source_file_location = source_file_location
|
|
56
|
+
|
|
57
|
+
def translate(self, characters="", additional_info=""):
|
|
58
|
+
"""
|
|
59
|
+
Translates the text from the source file using the specified characters and additional information.
|
|
60
|
+
|
|
61
|
+
Args:
|
|
62
|
+
characters (str, optional): Specific characters or personalities to use in the translation. Defaults to "".
|
|
63
|
+
additional_info (str, optional): Additional information or context to provide to the translation model. Defaults to "".
|
|
64
|
+
|
|
65
|
+
Returns:
|
|
66
|
+
str: The translated text.
|
|
67
|
+
|
|
68
|
+
Raises:
|
|
69
|
+
TextLoaderError: If the source file cannot be loaded.
|
|
70
|
+
"""
|
|
71
|
+
try:
|
|
72
|
+
loader = TextLoader(self.source_file_location)
|
|
73
|
+
document = loader.load()
|
|
74
|
+
except Exception as e:
|
|
75
|
+
raise TextLoaderError(
|
|
76
|
+
f"Failed to load the file {self.source_file_location}"
|
|
77
|
+
) from e
|
|
78
|
+
|
|
79
|
+
prompt = ChatPromptTemplate.from_messages(
|
|
80
|
+
[
|
|
81
|
+
(
|
|
82
|
+
"system",
|
|
83
|
+
_get_prompt(additional=additional_info, characters=characters),
|
|
84
|
+
),
|
|
85
|
+
("human", "{input}"),
|
|
86
|
+
]
|
|
87
|
+
)
|
|
88
|
+
parser = StrOutputParser()
|
|
89
|
+
|
|
90
|
+
chain = prompt | self.llm | parser
|
|
91
|
+
|
|
92
|
+
with get_openai_callback() as cb:
|
|
93
|
+
output = chain.invoke({"input": document})
|
|
94
|
+
print(f"Total Tokens: {cb.total_tokens}")
|
|
95
|
+
print(f"Prompt Tokens: {cb.prompt_tokens}")
|
|
96
|
+
print(f"Completion Tokens: {cb.completion_tokens}")
|
|
97
|
+
print(f"Total Cost (USD): ${cb.total_cost}")
|
|
98
|
+
|
|
99
|
+
return output
|
|
100
|
+
|
|
101
|
+
def save_translation(self, destination_file_location: str = "file.txt"):
|
|
102
|
+
"""
|
|
103
|
+
Saves the translated text to the specified destination file location.
|
|
104
|
+
|
|
105
|
+
Args:
|
|
106
|
+
destination_file_location (str, optional): The location to save the translated text file. Defaults to "file.txt".
|
|
107
|
+
"""
|
|
108
|
+
translated_text = self.translate()
|
|
109
|
+
|
|
110
|
+
with open(destination_file_location, "w") as file:
|
|
111
|
+
file.write(translated_text)
|