Gemma 2 2b Jpn It Translate
基於Google發佈的gemma-2-2b-jpn-it進行翻譯任務調優的模型,專注於日英互譯任務
下載量 60
發布時間 : 10/7/2024
模型概述
該模型是針對日語和英語雙向翻譯優化的語言模型,具有高效處理能力和長文本翻譯潛力
模型特點
高效翻譯
20億參數規模下實現接近70億參數模型的翻譯質量
長文本處理
具備處理無限長文本翻譯的潛力,建議分句預處理
快速運行
約5GB的較小文件尺寸實現快速運行
自動提示模板
採用apply_chat_template技術避免手動提示模板編寫
模型能力
日語到英語翻譯
英語到日語翻譯
長文本處理
商務文本翻譯
使用案例
商務翻譯
商務郵件翻譯
將商務郵件內容在日語和英語之間互譯
示例輸出顯示良好的商務術語處理能力
企業公告翻譯
翻譯企業財務報告、發展計劃等正式文檔
能準確處理數字、日期等關鍵信息
技術文檔翻譯
技術文檔翻譯
將技術文檔在日英之間互譯
🚀 gemma-2-2b-jpn-it-translate 翻譯模型
gemma-2-2b-jpn-it-translate 是基於 Google 發佈的 google/gemma-2-2b-jpn-it 模型,針對翻譯任務進行微調的模型。雖然它只有 20 億(2B)個參數,但在某些領域,其翻譯質量可接近一年前 70 億(7B)參數的模型。而且,該模型文件大小相對較小,約為 5GB,因此能夠實現快速執行。
🚀 快速開始
gemma-2-2b-jpn-it-translate 模型在翻譯任務中表現出色,能夠為用戶提供高質量的翻譯服務。以下是使用該模型進行翻譯的基本步驟:
- 安裝必要的庫:使用
pip install -U transformers
命令安裝所需的庫。 - 加載模型和分詞器:使用
AutoModelForCausalLM
和AutoTokenizer
加載模型和分詞器。 - 設置系統提示和指令:根據翻譯任務的需求,設置系統提示和指令。
- 進行翻譯:將待翻譯的文本輸入到模型中,獲取翻譯結果。
✨ 主要特性
- 高質量翻譯:儘管參數數量為 20 億(2B),但在某些領域能提供接近一年前 70 億(7B)模型的翻譯質量。
- 快速執行:文件大小約 5GB,相對較小,可實現高速運行。
- 支持無限長文本翻譯:旨在實現對無限長文本的高速翻譯。
- 簡化操作:使用
apply_chat_template
避免手動編寫易出錯的提示模板。
📚 詳細文檔
模型說明
此模型是對 Google 發佈的日語專用模型「gemma-2-2b-jpn-it」進行微調後的產物。其目標是實現高速且能處理無限長文本的翻譯。具體而言,先輸入相當於系統提示的文本(日語/英語)後,模型會對用戶後續輸入的文本進行翻譯並輸出(日語/英語)。此外,由於使用了 apply_chat_template
,無需手動編寫容易出錯的提示模板。
需要注意的是,該模型是按句子進行翻譯訓練的,因此一次性輸入包含換行的長文本會導致翻譯質量下降。翻譯長文本時,請先將其按句子進行預處理,再輸入模型。
一括翻譯用樣本 Colab 腳本
擁有 Google 賬號的用戶,點擊以下鏈接中的「Open In Colab」按鈕,即可免費進行測試: gemma_2_2b_jpn_it_tranlate_batch_translation_sample.ipynb
💻 使用示例
日英翻譯用示例代碼
如果您擁有配備 GPU 的計算機,可以參考以下腳本進行操作;若沒有,可嘗試使用 gguf 版模型。請注意,以下示例腳本中對句子進行分割的部分並非完整實現,請根據實際需求進行修改。
import re
import torch
from transformers import AutoModelForCausalLM, AutoTokenizer
def get_torch_dtype():
if torch.cuda.is_available():
device = torch.device("cuda")
prop = torch.cuda.get_device_properties(device)
# Ampere (Compute Capability 8.0 above), for example L4 support bfloat16, but T4 not support.
if prop.major >= 8:
return torch.bfloat16
return torch.float16
model_name = "webbigdata/gemma-2-2b-jpn-it-translate"
model = AutoModelForCausalLM.from_pretrained(
model_name,
torch_dtype=get_torch_dtype(),
device_map="auto",
)
tokenizer = AutoTokenizer.from_pretrained(model_name)
tokenizer.pad_token = tokenizer.unk_token
system_prompt = "You are a highly skilled professional Japanese-English and English-Japanese translator. Translate the given text accurately, taking into account the context and specific instructions provided. Steps may include hints enclosed in square brackets [] with the key and value separated by a colon:. Only when the subject is specified in the Japanese sentence, the subject will be added when translating into English. If no additional instructions or context are provided, use your expertise to consider what the most appropriate context is and provide a natural translation that aligns with that context. When translating, strive to faithfully reflect the meaning and tone of the original text, pay attention to cultural nuances and differences in language usage, and ensure that the translation is grammatically correct and easy to read. After completing the translation, review it once more to check for errors or unnatural expressions. For technical terms and proper nouns, either leave them in the original language or use appropriate translations as necessary. Take a deep breath, calm down, and start translating.\n\n"
instruct = """Translate Japanese to English.\nWhen translating, please use the following hints:\n[writing_style: casual]"""
# 文章を區切る関數
def split_sentences(text):
sentences = []
last = 0
# 句點で文を分割
for match in re.finditer(r'[。!?…]', text):
end = match.end()
# 句點の直後に続く改行を含める
while end < len(text) and text[end] == '\n':
end += 1
sentence = text[last:end]
sentences.append(sentence)
last = end
# 殘りのテキストを追加
if last < len(text):
remaining = text[last:]
sentences.append(remaining)
# 各文內の改行を適切に分割
final_sentences = []
for s in sentences:
if '\n' in s:
parts = s.split('\n')
for i, part in enumerate(parts):
if part:
# 最後の部分でなければ改行を追加
if i < len(parts) - 1:
final_sentences.append(part + '\n')
else:
final_sentences.append(part)
# 改行自體を保持
if i < len(parts) - 1:
final_sentences.append('\n')
else:
final_sentences.append(s)
return final_sentences
# 翻訳処理を行う関數
def translate_sentence(sentence, previous_context):
# 過去のコンテキストと新しい文を配列に格納
if sentence.strip() == '':
return sentence
messages = previous_context + [
{"role": "user", "content": sentence}
]
# apply_chat_templateを使用してプロンプトを生成
inputs = tokenizer.apply_chat_template(
messages,
tokenize=True,
add_generation_prompt=True,
return_tensors="pt",
).to("cuda")
translation = ""
with torch.no_grad():
generated_ids = model.generate(
input_ids=inputs,
num_beams=3, max_new_tokens=1200, do_sample=True, temperature=0.5, top_p=0.3
)
full_output = tokenizer.batch_decode(generated_ids, skip_special_tokens=True)[0].strip()
translation = full_output.split('\nmodel\n')[-1].strip()
return translation
from collections import deque
# メイン処理
def main(text):
sentences = split_sentences(text)
translated_sentences = []
# Initialize context with system prompt
context = deque([
{"role": "user", "content": system_prompt + instruct},
{"role": "assistant", "content": "OK"}
], maxlen=6) # Maximum 10 elements (5 user, 5 assistant)
for i, sentence in enumerate(sentences):
# For the first sentence, use the full context including system prompt
if i == 0:
translation_context = list(context)
else:
# For subsequent sentences, exclude the system prompt
translation_context = list(context)[2:]
translated_sentence = translate_sentence(sentence, translation_context)
translated_sentences.append(translated_sentence)
# Add new interactions to the context
if sentence.strip() != '':
context.append({"role": "user", "content": sentence})
else:
context.append({"role": "user", "content": sentence})
if translated_sentence.strip() != '':
context.append({"role": "assistant", "content": translated_sentence})
else:
context.append({"role": "assistant", "content": translated_sentence})
return translated_sentences
text = """こんにちは。私は田中です。今日はとても良い天気ですね。朝ごはんはパンとコーヒーを食べました。そのあとに散歩に行きました。公園にはたくさんの人がいました。子供たちは遊んでいました。
犬を連れている人もいました。私はベンチに座って本を読みました。風がとても気持ちよかったです。その後、友達とカフェに行きました。
カフェではコーヒーを飲みながらおしゃべりをしました。友達は最近引っ越したばかりだと言いました。新しい家の寫真を見せてくれました。
とてもきれいな家でした。時間が経つのがあっという間でした。夕方になり、私は家に帰りました。夕食にはカレーを作りました。カレーはとても美味しかったです。今日一日、とても楽しかったです。"""
translated = main(text)
print(translated)
英日翻譯用示例代碼
import re
import torch
from transformers import AutoModelForCausalLM, AutoTokenizer
def get_torch_dtype():
if torch.cuda.is_available():
device = torch.device("cuda")
prop = torch.cuda.get_device_properties(device)
# Ampere (Compute Capability 8.0 above), for example L4 support bfloat16, but T4 not support.
if prop.major >= 8:
return torch.bfloat16
return torch.float16
model_name = "gemma-2-2b-jpn-it-translate"
model = AutoModelForCausalLM.from_pretrained(
model_name,
torch_dtype=get_torch_dtype(),
device_map="auto",
)
tokenizer = AutoTokenizer.from_pretrained(model_name)
tokenizer.pad_token = tokenizer.unk_token
system_prompt = "You are a highly skilled professional Japanese-English and English-Japanese translator. Translate the given text accurately, taking into account the context and specific instructions provided. Steps may include hints enclosed in square brackets [] with the key and value separated by a colon:. Only when the subject is specified in the Japanese sentence, the subject will be added when translating into English. If no additional instructions or context are provided, use your expertise to consider what the most appropriate context is and provide a natural translation that aligns with that context. When translating, strive to faithfully reflect the meaning and tone of the original text, pay attention to cultural nuances and differences in language usage, and ensure that the translation is grammatically correct and easy to read. After completing the translation, review it once more to check for errors or unnatural expressions. For technical terms and proper nouns, either leave them in the original language or use appropriate translations as necessary. Take a deep breath, calm down, and start translating.\n\n"
instruct = """Translate English to Japanese.\nWhen translating, please use the following hints:\n[writing_style: business]"""
# Function to split English sentences
def split_sentences(text):
sentences = []
# Split by newlines, periods, exclamation marks, question marks, or two or more consecutive spaces
pattern = r'(?:\r?\n|\.|\!|\?|(?:\s{2,}))'
splits = re.split(pattern, text)
for split in splits:
split = split.strip()
if split:
sentences.append(split)
return sentences
# Function to translate a sentence
def translate_sentence(sentence, previous_context):
if sentence.strip() == '':
return sentence
messages = previous_context + [
{"role": "user", "content": sentence}
]
inputs = tokenizer.apply_chat_template(
messages,
tokenize=True,
add_generation_prompt=True,
return_tensors="pt",
).to("cuda")
translation = ""
with torch.no_grad():
generated_ids = model.generate(
input_ids=inputs,
num_beams=3, max_new_tokens=1200, do_sample=True, temperature=0.5, top_p=0.3
)
full_output = tokenizer.batch_decode(generated_ids, skip_special_tokens=True)[0].strip()
translation = full_output.split('\nmodel\n')[-1].strip()
return translation
from collections import deque
# Main processing function
def main(text):
sentences = split_sentences(text)
translated_sentences = []
context = deque([
{"role": "user", "content": system_prompt + instruct},
{"role": "assistant", "content": "OK"}
], maxlen=6)
for i, sentence in enumerate(sentences):
if i == 0:
translation_context = list(context)
else:
translation_context = list(context)[2:]
translated_sentence = translate_sentence(sentence, translation_context)
translated_sentences.append(translated_sentence)
if sentence.strip() != '':
context.append({"role": "user", "content": sentence})
else:
context.append({"role": "user", "content": sentence})
if translated_sentence.strip() != '':
context.append({"role": "assistant", "content": translated_sentence})
else:
context.append({"role": "assistant", "content": translated_sentence})
return translated_sentences
# Sample English text for translation (business context)
text = """Dear valued clients and partners,
I hope this email finds you well. I am writing to provide you with an important update regarding our company's recent developments and future plans.
Firstly, I am pleased to announce that our Q3 financial results have exceeded expectations, with a 15% increase in revenue compared to the same period last year. This success is largely attributed to the launch of our new product line and the expansion of our services into emerging markets.
In light of this growth, we are planning to implement several strategic initiatives in the coming months:
1. Expansion of our R&D department: We will be investing significantly in research and development to maintain our competitive edge in the market.
2. Sustainability efforts: We are committed to reducing our carbon footprint by 30% over the next five years. This includes transitioning to renewable energy sources and implementing eco-friendly practices across all our operations.
3. Digital transformation: We will be upgrading our IT infrastructure to enhance efficiency and provide better service to our clients.
Additionally, we are excited to announce our upcoming annual conference, which will be held virtually this year due to ongoing global health concerns. The conference will take place on November 15-16, 2024, and will feature keynote speeches from industry leaders, interactive workshops, and networking opportunities.
We value your continued support and partnership. If you have any questions or would like further information about any of these initiatives, please don't hesitate to reach out to your account manager or contact our customer support team.
Thank you for your trust in our company. We look forward to achieving new milestones together.
Best regards,
John Smith
CEO, XYZ Corporation"""
翻譯結果
['貴社にご愛顧いただき、誠にありがとうございます。', 'このメールがご健在であることを心よりお祈り申し上げます。',
'弊社の最近の進展と今後の計畫について、重要なお知らせをご提供いたします。',
'まず、第3四半期の収益が予想を上回ったことをお知らせいたします。昨年の同時期と比較して、売上高が15%増加しました。',
'この成功は、新製品ラインの発売と、新興市場へのサービスの拡大が大きく貢獻しています。',
'この成長を踏まえ、今後の數ヶ月にわたって、いくつかの戦略的イニシアティブを実施する予定です。',
'1', 'R&D部門の拡大:市場での競爭力を維持するために、大幅に研究開発に投資する予定です。',
'2', 'サステナビリティの取り組み:次の5年間で、炭素排出量を30%削減することを目指しています。',
'これは、再生可能エネルギー源への移行と、すべての事業活動における環境にやさしい実踐の導入を含むものです。',
'3', 'デジタルトランスフォーメーション: 私たちのITインフラを強化し、効率を向上させ、より良いサービスを提供する',
'さらに、今年は新型コロナウイルス感染症の懸念が続くため、オンラインで開催されますが、毎年恆例の年次カンファレンスをお知らせいたします。',
'カンファレンスは2024年11月15日~16日に開催され、業界のリーダーによるキーノートスピーチ、インタラクティブワークショップ、ネットワークングの機會が盛りだくさんです。',
'引き続きご支援とご協力を賜りますようお願い申し上げます。',
'これらのイニシアチブについてご質問がある場合や、さらに詳しい情報をご希望の場合は、ご擔當マネジャーにご連絡するか、弊社のカスタマーサポートチームにご連絡ください。',
'弊社の信頼を賜り、誠にありがとうございます。',
'共に新たな目標を達成できることを楽しみにしています。',
'ご清栄のこととお慶び申し上げます。',
'ジョン・スミス', 'XYZ株式會社のCEO']
🔧 技術細節
基準測試結果
文件名 | 翻譯方向 | spBLEU | chrF2++ | comet | xlcomet |
---|---|---|---|---|---|
flores200v1 | 英日 | 28.56 | 37.5 | 0.896 | 0.8512 |
flores200v1 | 日英 | 26.38 | 56.6 | 0.8735 | 0.9324 |
wmt20 | 英日 | 15.62 | 29.1 | 0.8796 | 0.7913 |
wmt20 | 日英 | 18.04 | 46.3 | 0.8013 | 0.7815 |
wmt22 | 英日 | 17.47 | 30.9 | 0.8799 | 0.8581 |
wmt22 | 日英 | 19.93 | 46.4 | 0.8107 | 0.8821 |
wmt23 | 英日 | 15.95 | 28.4 | 0.8585 | 0.8213 |
wmt23 | 日英 | 15.54 | 45.2 | 0.8029 | 0.8669 |
Business | 英日 | 21.08 | 38.1 | 0.9041 | 0.9178 |
Business | 日英 | 24.27 | 47.5 | 0.8317 | 0.8716 |
NTREX128 | 英日 | 18.18 | 30.1 | 0.8757 | 0.7709 |
📄 許可證
致謝
- 開發者:[dahara1@webbigdata]
- 支持語言 (NLP):[英語、日語]
- 基礎模型 [可選]:gemma-2-2b-jpn-it
BibTeX 引用
@misc{dahara2024imatrix,
author = {dahara1@webbigdata},
title = {gemma-2-2b-jpn-it-translate: A translation task-specific model based on gemma-2-2b-jpn-it},
year = {2024},
howpublished = {\url{https://huggingface.co/webbigdata/gemma-2-2b-jpn-it-translate/}},
note = {Accessed: 2024-10-10},
abstract = {This model was developed to verify how much Japanese-English and English-Japanese translation performance can be improved with the 2B model.},
}
M2m100 418M
MIT
M2M100是一個多語言編碼器-解碼器模型,支持100種語言的9900個翻譯方向
機器翻譯 支持多種語言
M
facebook
1.6M
299
Opus Mt Fr En
Apache-2.0
基於Transformer的法語到英語神經機器翻譯模型,由Helsinki-NLP團隊開發,採用OPUS多語數據集訓練。
機器翻譯 支持多種語言
O
Helsinki-NLP
1.2M
44
Opus Mt Ar En
Apache-2.0
基於OPUS數據訓練的阿拉伯語到英語的機器翻譯模型,採用transformer-align架構
機器翻譯 支持多種語言
O
Helsinki-NLP
579.41k
42
M2m100 1.2B
MIT
M2M100是一個支持100種語言的多語言機器翻譯模型,可直接在9900個翻譯方向之間進行翻譯。
機器翻譯 支持多種語言
M
facebook
501.82k
167
Indictrans2 Indic En 1B
MIT
支持25種印度語言與英語互譯的1.1B參數規模機器翻譯模型,由AI4Bharat項目開發
機器翻譯
Transformers 支持多種語言

I
ai4bharat
473.63k
14
Opus Mt En Zh
Apache-2.0
基於Transformer架構的英漢多方言翻譯模型,支持英語到13種漢語變體的翻譯任務
機器翻譯 支持多種語言
O
Helsinki-NLP
442.08k
367
Opus Mt Zh En
由赫爾辛基大學開發的基於OPUS語料庫的中文到英語機器翻譯模型
機器翻譯 支持多種語言
O
Helsinki-NLP
441.24k
505
Mbart Large 50 Many To Many Mmt
基於mBART-large-50微調的多語言機器翻譯模型,支持50種語言間的互譯
機器翻譯 支持多種語言
M
facebook
404.66k
357
Opus Mt De En
Apache-2.0
opus-mt-de-en 是一個基於 transformer-align 架構的德語到英語的機器翻譯模型,由 Helsinki-NLP 團隊開發。
機器翻譯 支持多種語言
O
Helsinki-NLP
404.33k
44
Opus Mt Es En
Apache-2.0
這是一個基於Transformer架構的西班牙語到英語的機器翻譯模型,由Helsinki-NLP團隊開發。
機器翻譯
Transformers 支持多種語言

O
Helsinki-NLP
385.40k
71
精選推薦AI模型
Llama 3 Typhoon V1.5x 8b Instruct
專為泰語設計的80億參數指令模型,性能媲美GPT-3.5-turbo,優化了應用場景、檢索增強生成、受限生成和推理任務
大型語言模型
Transformers 支持多種語言

L
scb10x
3,269
16
Cadet Tiny
Openrail
Cadet-Tiny是一個基於SODA數據集訓練的超小型對話模型,專為邊緣設備推理設計,體積僅為Cosmo-3B模型的2%左右。
對話系統
Transformers 英語

C
ToddGoldfarb
2,691
6
Roberta Base Chinese Extractive Qa
基於RoBERTa架構的中文抽取式問答模型,適用於從給定文本中提取答案的任務。
問答系統 中文
R
uer
2,694
98