[Update] translation: Add Plamo API support with authentication and translation functionality
This commit is contained in:
@@ -603,4 +603,42 @@ translation_lang["nllb-200-distilled-1.3B-ct2-int8"] = {
|
||||
translation_lang["nllb-200-3.3B-ct2-int8"] = {
|
||||
"source":dict_nllb_languages,
|
||||
"target":dict_nllb_languages,
|
||||
}
|
||||
|
||||
dict_plamo_languages = {
|
||||
"English": "English",
|
||||
"Japanese": "Japanese",
|
||||
"Korean": "Korean",
|
||||
"French": "French",
|
||||
"German": "German",
|
||||
"Spanish": "Spanish",
|
||||
"Portuguese": "Portuguese",
|
||||
"Russian": "Russian",
|
||||
"Italian": "Italian",
|
||||
"Dutch": "Dutch",
|
||||
"Polish": "Polish",
|
||||
"Turkish": "Turkish",
|
||||
"Arabic": "Arabic",
|
||||
"Hindi": "Hindi",
|
||||
"Thai": "Thai",
|
||||
"Vietnamese": "Vietnamese",
|
||||
"Indonesian": "Indonesian",
|
||||
"Malay": "Malay",
|
||||
"Filipino": "Filipino",
|
||||
"Swedish": "Swedish",
|
||||
"Finnish": "Finnish",
|
||||
"Danish": "Danish",
|
||||
"Norwegian": "Norwegian",
|
||||
"Romanian": "Romanian",
|
||||
"Czech": "Czech",
|
||||
"Hungarian": "Hungarian",
|
||||
"Greek": "Greek",
|
||||
"Hebrew": "Hebrew",
|
||||
"Simplified Chinese":"Simplified Chinese",
|
||||
"Traditional Chinese":"Traditional Chinese"
|
||||
}
|
||||
|
||||
translation_lang["Plamo_API"] = {
|
||||
"source":dict_plamo_languages,
|
||||
"target":dict_plamo_languages,
|
||||
}
|
||||
80
src-python/models/translation/translation_plamo.py
Normal file
80
src-python/models/translation/translation_plamo.py
Normal file
@@ -0,0 +1,80 @@
|
||||
from langchain_openai import ChatOpenAI
|
||||
|
||||
class PlamoClient:
|
||||
def __init__(self, api_key: str):
|
||||
self.api_key = api_key
|
||||
self.base_url = "https://api.platform.preferredai.jp/v1"
|
||||
self.model = "plamo-2.0-prime"
|
||||
self.supported_languages = """
|
||||
English
|
||||
Japanese
|
||||
Korean
|
||||
French
|
||||
German
|
||||
Spanish
|
||||
Portuguese
|
||||
Russian
|
||||
Italian
|
||||
Dutch
|
||||
Polish
|
||||
Turkish
|
||||
Arabic
|
||||
Hindi
|
||||
Thai
|
||||
Vietnamese
|
||||
Indonesian
|
||||
Malay
|
||||
Filipino
|
||||
Swedish
|
||||
Finnish
|
||||
Danish
|
||||
Norwegian
|
||||
Romanian
|
||||
Czech
|
||||
Hungarian
|
||||
Greek
|
||||
Hebrew
|
||||
Simplified Chinese
|
||||
Traditional Chinese
|
||||
"""
|
||||
self.prompt_template = f"""
|
||||
You are a translation assistant that uses the `plamo-translate` tool.
|
||||
Translate the following text.Supported languages include:{self.supported_languages}
|
||||
Translate the following text from {{input_lang}} to {{output_lang}}.
|
||||
output only the translated text without any additional commentary.
|
||||
"""
|
||||
self.plamo_llm = ChatOpenAI(
|
||||
base_url=self.base_url,
|
||||
model=self.model,
|
||||
streaming=True,
|
||||
openai_api_key=self.api_key,
|
||||
)
|
||||
|
||||
def translate_text(self, text: str, input_lang: str, output_lang: str):
|
||||
messages = [
|
||||
{
|
||||
"role": "system",
|
||||
"content": self.prompt_template.format(
|
||||
input_lang=input_lang, output_lang=output_lang
|
||||
),
|
||||
},
|
||||
{"role": "user", "content": text},
|
||||
]
|
||||
|
||||
output = ""
|
||||
for chunk in self.plamo_llm.stream(messages):
|
||||
output += chunk.content
|
||||
|
||||
return output[:-1]
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
text = """
|
||||
毎朝コーヒーを入れるのがささやかな楽しみになってる
|
||||
"""
|
||||
input_lang = "Japanese"
|
||||
output_lang = "English"
|
||||
|
||||
plamo_client = PlamoClient(api_key="AUTH_KEY")
|
||||
translated_text = plamo_client.translate_text(text, input_lang, output_lang)
|
||||
print(translated_text)
|
||||
@@ -9,12 +9,14 @@ except Exception:
|
||||
try:
|
||||
from .translation_languages import translation_lang
|
||||
from .translation_utils import ctranslate2_weights
|
||||
from .translation_plamo import PlamoClient
|
||||
except Exception:
|
||||
import sys
|
||||
print(os_path.dirname(os_path.dirname(os_path.dirname(os_path.abspath(__file__)))))
|
||||
sys.path.append(os_path.dirname(os_path.dirname(os_path.dirname(os_path.abspath(__file__)))))
|
||||
from translation_languages import translation_lang
|
||||
from translation_utils import ctranslate2_weights
|
||||
from translation_plamo import PlamoClient
|
||||
|
||||
import ctranslate2
|
||||
import transformers
|
||||
@@ -27,6 +29,7 @@ warnings.filterwarnings("ignore")
|
||||
class Translator():
|
||||
def __init__(self):
|
||||
self.deepl_client = None
|
||||
self.plamo_client = None
|
||||
self.ctranslate2_translator = None
|
||||
self.ctranslate2_tokenizer = None
|
||||
self.is_loaded_ctranslate2_model = False
|
||||
@@ -36,13 +39,24 @@ class Translator():
|
||||
result = True
|
||||
try:
|
||||
self.deepl_client = DeepLClient(authkey)
|
||||
self.deepl_client.translate_text(" ", target_lang="EN-US")
|
||||
self.deepl_client.translate_text("Hello World", target_lang="EN-US")
|
||||
except Exception:
|
||||
errorLogging()
|
||||
self.deepl_client = None
|
||||
result = False
|
||||
return result
|
||||
|
||||
def authenticationPlamoAuthKey(self, authkey):
|
||||
result = True
|
||||
try:
|
||||
self.plamo_client = PlamoClient(authkey)
|
||||
self.plamo_client.translate_text("Hello World", target_lang="English")
|
||||
except Exception:
|
||||
errorLogging()
|
||||
self.plamo_client = None
|
||||
result = False
|
||||
return result
|
||||
|
||||
def changeCTranslate2Model(self, path, model_type, device="cpu", device_index=0):
|
||||
self.is_loaded_ctranslate2_model = False
|
||||
directory_name = ctranslate2_weights[model_type]["directory_name"]
|
||||
@@ -135,6 +149,15 @@ class Translator():
|
||||
source_lang=source_language,
|
||||
target_lang=target_language,
|
||||
).text
|
||||
case "Plamo_API":
|
||||
if self.plamo_client is None:
|
||||
result = False
|
||||
else:
|
||||
result = self.plamo_client.translate_text(
|
||||
message,
|
||||
input_lang=source_language,
|
||||
output_lang=target_language,
|
||||
)
|
||||
case "Google":
|
||||
if self.is_enable_translators is True:
|
||||
result = other_web_Translator(
|
||||
@@ -170,7 +193,7 @@ class Translator():
|
||||
errorLogging()
|
||||
result = False
|
||||
return result
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
translator = Translator()
|
||||
# test CTranslate2 model nllb-200-distilled-1.3B-ct2-int8
|
||||
|
||||
Reference in New Issue
Block a user