跳转到主要内容

SONAR提供了一套用于多语言、多模态语义嵌入的语音和文本编码器。

项目描述

SONAR

[论文] [演示]

我们介绍了SONAR,这是一个新的多语言和多模态固定大小的句子嵌入空间,包含一套完整的语音和文本编码器和解码器。它在xsim和xsim++多语言相似性搜索任务上显著优于现有的句子嵌入,如LASER3和LabSE。

语音片段可以使用特定语言的语音编码器嵌入到相同的SONAR嵌入空间中,这些编码器在语音转录数据上以师生模式进行训练。我们还提供了一个单独的文本解码器,使我们能够执行文本到文本和语音到文本的机器翻译,包括零样本语言和模态组合。

SONAR代表Sentence-level multiOodal and laNguage-Agnostic Representations(句子级多模态和语言无关表示)

支持的语言完整列表(包括下载链接)可以在以下位置找到:下面

SONAR架构


文本结果


语音结果


安装

您可以使用pip install sonar-space安装SONAR。请注意,pip上还有一个名为sonar的包,但这不是本项目,请确保在依赖关系中使用sonar-space

如果您想手动安装SONAR,可以本地安装。SONAR主要依赖于Fairseq2,可以使用(与python=3.8进行了测试)安装:

pip install --upgrade pip
pip install -e .

如果fairseq2没有为您的机器提供构建版本,请检查该项目的readme文件以本地构建。

用法

使用以下命令时,fairseq2将自动将模型下载到您的$TORCH_HOME/hub目录。

使用SONAR计算文本句子嵌入

from sonar.inference_pipelines.text import TextToEmbeddingModelPipeline
t2vec_model = TextToEmbeddingModelPipeline(encoder="text_sonar_basic_encoder",
                                           tokenizer="text_sonar_basic_encoder")
sentences = ['My name is SONAR.', 'I can embed the sentences into vectorial space.']
embeddings = t2vec_model.predict(sentences, source_lang="eng_Latn")
print(embeddings.shape)
# torch.Size([2, 1024])

从SONAR嵌入中重建文本

from sonar.inference_pipelines.text import EmbeddingToTextModelPipeline
vec2text_model = EmbeddingToTextModelPipeline(decoder="text_sonar_basic_decoder",
                                              tokenizer="text_sonar_basic_encoder")
reconstructed = vec2text_model.predict(embeddings, target_lang="eng_Latn", max_seq_len=512)
# max_seq_len is a keyword argument passed to the fairseq2 BeamSearchSeq2SeqGenerator.
print(reconstructed)
# ['My name is SONAR.', 'I can embed the sentences into vector space.']

使用SONAR翻译文本

from sonar.inference_pipelines.text import TextToTextModelPipeline
t2t_model = TextToTextModelPipeline(encoder="text_sonar_basic_encoder",
                                    decoder="text_sonar_basic_decoder",
                                    tokenizer="text_sonar_basic_encoder")  # tokenizer is attached to both encoder and decoder cards

sentences = ['My name is SONAR.', 'I can embed the sentences into vectorial space.']
t2t_model.predict(sentences, source_lang="eng_Latn", target_lang="fra_Latn")
# ['Mon nom est SONAR.', "Je peux intégrer les phrases dans l'espace vectoriel."]

使用SONAR计算语音句子嵌入

from sonar.inference_pipelines.speech import SpeechToEmbeddingModelPipeline
s2vec_model = SpeechToEmbeddingModelPipeline(encoder="sonar_speech_encoder_eng")

s2vec_model.predict(["./tests/integration_tests/data/audio_files/audio_1.wav",
                     "./tests/integration_tests/data/audio_files/audio_2.wav"]).shape
# torch.Size([2, 1024])
import torchaudio
inp, sr = torchaudio.load("./tests/integration_tests/data/audio_files/audio_1.wav")
assert sr == 16000, "Sample rate should be 16kHz"

s2vec_model.predict([inp]).shape
# torch.Size([1, 1024])

使用SONAR进行语音到文本翻译

from sonar.inference_pipelines.speech import SpeechToTextModelPipeline

s2t_model = SpeechToTextModelPipeline(encoder="sonar_speech_encoder_eng",
                                      decoder="text_sonar_basic_decoder",
                                      tokenizer="text_sonar_basic_decoder")

import torchaudio
inp, sr = torchaudio.load("./tests/integration_tests/data/audio_files/audio_1.wav")
assert sr == 16000, "Sample rate should be 16kHz"

# passing loaded audio files
s2t_model.predict([inp], target_lang="eng_Latn")
# ['Television reports show white smoke coming from the plant.']

# passing multiple wav files 
s2t_model.predict(["./tests/integration_tests/data/audio_files/audio_1.wav",
                   "./tests/integration_tests/data/audio_files/audio_2.wav"], target_lang="eng_Latn")
# ['Television reports show white smoke coming from the plant.',
# 'These couples may choose to make an adoption plan for their baby.']

使用BLASER 2.0模型预测句子相似度

BLASER 2.0是一系列基于SONAR嵌入的模型,用于基于SONAR嵌入的机器翻译质量自动评估。它们预测翻译和源语言之间的跨语言语义相似度(可选地,还可以使用参考翻译)。

from sonar.inference_pipelines.text import TextToEmbeddingModelPipeline
from sonar.models.blaser.loader import load_blaser_model

blaser_ref = load_blaser_model("blaser_2_0_ref").eval()
blaser_qe = load_blaser_model("blaser_2_0_qe").eval()
text_embedder = TextToEmbeddingModelPipeline(encoder="text_sonar_basic_encoder", tokenizer="text_sonar_basic_encoder")

src_embs = text_embedder.predict(["Le chat s'assit sur le tapis."], source_lang="fra_Latn")
ref_embs = text_embedder.predict(["The cat sat on the mat."], source_lang="eng_Latn")
mt_embs = text_embedder.predict(["The cat sat down on the carpet."], source_lang="eng_Latn")

print(blaser_ref(src=src_embs, ref=ref_embs, mt=mt_embs).item())  # 4.688
print(blaser_qe(src=src_embs, mt=mt_embs).item())  # 4.708

更详细的模型卡片和更多示例: facebook/blaser-2.0-reffacebook/blaser-2.0-qe

演示笔记本

查看更多完整的演示笔记本

支持的语言和下载链接

SONAR文本编码器和解码器支持200种语言。SONAR语音编码器支持37种语言。

可用的文本编码器/解码器
模型 链接
编码器 下载
解码器 下载
微调解码器 下载
标记器 下载

支持No Language Left Behind项目中的所有200种语言。

可用的语音编码器
lang_code 语言 链接
arb ms arabic 下载
asm assamese 下载
bel belarussian 下载
ben bengali 下载
bos bosnian 下载
bul bulgarian 下载
cat catalan 下载
ces czech 下载
cmn mandarin chinese 下载
cym welsh 下载
dan danish 下载
deu german 下载
est estonian 下载
fin finnish 下载
fra french 下载
guj gujurati 下载
heb hebrew 下载
hin hindi 下载
hrv croatian 下载
ind indonesian 下载
ita italian 下载
jpn japanse 下载
kan kannada 下载
kor korean 下载
lao lao 下载
lit lithaian 下载
lvs standard latvian 下载
mal malayalam 下载
mar marathi 下载
mkd macedonian 下载
mlt maltese 下载
npi nepali 下载
nld dutch 下载
ory odia 下载
pan punjabi 下载
pes western persian 下载
pol polish 下载
por portuguese 下载
ron romanian 下载
rus russian 下载
slk 斯洛伐克语 下载
斯洛文尼亚语 斯洛文尼亚语 下载
桑迪 信德语 下载
塞尔维亚语 塞尔维亚语 下载
西班牙语 西班牙语 下载
瑞典语 瑞典语 下载
斯瓦希里语 斯瓦希里语 下载
泰米尔语 泰米尔语 下载
泰卢固语 泰卢固语 下载
塔吉克语 菲律宾语 下载
泰语 泰语 下载
土耳其语 土耳其语 下载
乌克兰语 乌克兰语 下载
乌尔都语 乌尔都语 下载
北乌兹别克语 北乌兹别克语 下载
越南语 越南语 下载
粤语 粤语 下载

引用信息

在引用SONAR嵌入空间、编码器和解码器时,请引用该论文。

@misc{Duquenne:2023:sonar_arxiv,
  author = {Paul-Ambroise Duquenne and Holger Schwenk and Benoit Sagot},
  title = {{SONAR:} Sentence-Level Multimodal and Language-Agnostic Representations},
  publisher = {arXiv},
  year = {2023},
  url = {https://arxiv.org/abs/2308.11466},
}

贡献

有关如何帮忙的信息,请参阅CONTRIBUTING文件。

许可证

SONAR代码在MIT许可证下发布(请参阅CODE_LICENSE)。

SONAR的一些模型也在相同的MIT许可证下发布,但请注意,其中一些模型在非商业许可证下发布(请参阅NC_MODEL_LICENSE)。有关详细信息,请参阅LICENSE

项目详情


下载文件

下载适用于您的平台的文件。如果您不确定选择哪个,请了解更多关于安装包的信息。

源分布

sonar_space-0.2.1.tar.gz (31.2 kB 查看哈希)

上传时间

构建分布

sonar_space-0.2.1-py3-none-any.whl (44.5 kB 查看哈希)

上传时间 Python 3

由以下机构支持

AWS AWS 云计算和安全赞助商 Datadog Datadog 监控 Fastly Fastly CDN Google Google 下载分析 Microsoft Microsoft PSF 赞助商 Pingdom Pingdom 监控 Sentry Sentry 错误记录 StatusPage StatusPage 状态页面