-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathentries.py
More file actions
112 lines (91 loc) · 3.22 KB
/
entries.py
File metadata and controls
112 lines (91 loc) · 3.22 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
# entries.py
import os
import openai
import json
class Entry:
def __init__(self, msgid):
# Initialize an entry with a dictionary to hold translations
self.translations = {}
def __setitem__(self, key, value):
# Add a translation
self.translations[key] = value
def __getitem__(self, key):
# Retrieve a translation
return self.translations.get(key, None)
def to_dict(self):
# Convert the translations to a dictionary
return self.translations
@classmethod
def from_dict(cls, dict_obj):
# Create an Entry object from a dictionary
entry = cls("")
entry.translations = dict_obj
return entry
def add_translation(self, lang, translation):
# Add a translation for a specific language
self.translations[lang] = translation
def get_translation(self, lang):
# Get a translation for a specific language
return self.translations.get(lang, "")
def as_dict(self):
# Convert to dictionary format
return self.translations
class Entries:
def __init__(self):
# Initialize with a dictionary to hold Entry objects
self.entries = {}
def __len__(self):
# Return the number of entries
return len(self.entries)
def add_entry(self, entry):
# Add an Entry object to the dictionary
self.entries[entry.msgid] = entry
def get_entry(self, msgid):
# Retrieve an Entry object based on its msgid
return self.entries.get(msgid)
def get_entries(self):
# Retrieve all entries as a list of items
return self.entries.items()
def add_translation(self, key, lang, translation):
# Add a translation for a specific language and msgid (key)
if key not in self.entries:
self.entries[key] = {}
self.entries[key][lang] = translation
def get_translation(self, key, lang):
# Get a translation based on its msgid (key) and language
return self.entries.get(key, {}).get(lang)
def as_dict(self):
# Convert to dictionary format
return self.entries
@classmethod
def from_dict(cls, dict_obj):
# Create an Entries object from a dictionary
entries = cls()
entries.entries = dict_obj
return entries
# Function to create the context for translation request
def create_context(entries):
context = [
{
"role": "system",
"content": 'Task: Translate accounting text from DE, FR, EN to BS.\nInput: JSON with text in DE, FR, EN.\nOutput: JSON with text in BS.'
},
{"role": "user", "content": entries},
]
return context
# Function to request translation from OpenAI
def request_translation(context):
openai.api_key = os.getenv("OPENAI_API_KEY")
response = openai.ChatCompletion.create(
model="gpt-3.5-turbo-16k",
messages=context,
temperature=1,
max_tokens=10000,
top_p=1,
frequency_penalty=0,
presence_penalty=0,
)
content = response["choices"][0]["message"]["content"].strip()
translation_json = json.loads(content)
bosnian_translation = translation_json.get("bs", "")
return bosnian_translation