-
Notifications
You must be signed in to change notification settings - Fork 0
/
translator_openai.py
43 lines (33 loc) · 1.29 KB
/
translator_openai.py
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
import os
import openai
import sys
class Translator():
def __init__(self):
openai.api_key = "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"
self.api_key = openai.api_key
# Set up dictionary of language codes
self.ENGLISH = 'en'
self.ICELANDIC = 'is'
self.available_languages = {self.ENGLISH, self.ICELANDIC}
def build_command(self, lang, sentence):
if lang == self.ENGLISH:
return "Translate this into 1. Icelandic:\n\n" + sentence + "\n\n1."
elif lang == self.ICELANDIC:
return "Translate this into 1. English:\n\n" + sentence + "\n\n1."
else:
sys.exit(f'Translator: translation from {lang} unsupported.\n')
def translate(self, from_lang, to_lang, sentence):
# Ensure input type
if not isinstance(sentence, str):
sys.exit('Translator: Given sentence is not a string.\n')
command = self.build_command(from_lang, sentence)
response = openai.Completion.create(
model="text-davinci-002",
prompt=command,
temperature=0.3,
max_tokens=100,
top_p=1,
frequency_penalty=0,
presence_penalty=0
)
return (response.choices[0].text).replace('\n', '')