audio_samples / demo.py
Helloworld628's picture
Upload demo.py with huggingface_hub
d3b1228 verified
Raw
History Blame Contribute Delete
20.7 kB
import torch
from transformers import AutoModel, AutoTokenizer
repo_id = "humanify/ARAG_embedding_pretrain"
device = "cuda" if torch.cuda.is_available() else "cpu"
tokenizer = AutoTokenizer.from_pretrained(repo_id, trust_remote_code=True)
model = AutoModel.from_pretrained(
repo_id,
trust_remote_code=True,
torch_dtype=torch.float32,
).to(device).eval()
model.set_tokenizer(tokenizer)
QUERY_INSTRUCTION = "Based on the question asked in the text query and context in the audio query, retrieve the relevant text document associated with that question."
DOC_INSTRUCTION = "Represent the user's input."
# case 1
# expect [0-1match, 2-3match]
# 模型正确区分出性别
# tensor([[0.5530, 0.4304],
# [0.6178, 0.4669],
# [0.3870, 0.6268],
# [0.4139, 0.6821]], device='cuda:0')
# tensor([[1.0000, 0.8968, 0.7887, 0.7221],
# [0.8968, 1.0000, 0.7567, 0.7600],
# [0.7887, 0.7567, 1.0000, 0.9216],
# [0.7221, 0.7600, 0.9216, 1.0000]], device='cuda:0')
# query_text = [
# "What is the gender of speaker in this audio?",
# "What is the gender of speaker in this audio?",
# "What is the gender of speaker in this audio?",
# "What is the gender of speaker in this audio?",
# ]
# query_audio = ["./samples/en_male_music.wav", "./samples/en_male.wav",
# "./samples/en_female_music.wav", "./samples/en_female.wav"]
# case 2
# expect [0-2match, 1-3match]
# 测试显示模型是可以区分出是否有背景音乐的
# tensor([[0.4247, 0.1398],
# [0.2891, 0.1501],
# [0.2446, 0.3100],
# [0.2502, 0.3032]], device='cuda:0')
# tensor([[1.0000, 0.7550, 0.6976, 0.6746],
# [0.7550, 1.0000, 0.7014, 0.6887],
# [0.6976, 0.7014, 1.0000, 0.8886],
# [0.6746, 0.6887, 0.8886, 1.0000]], device='cuda:0')
# query_text = [
# "Describe the background noise of this audio?",
# "Describe the background noise of this audio?",
# "Describe the background noise of this audio?",
# "Describe the background noise of this audio?",
# ]
# query_audio = ["./samples/en_male_music.wav", "./samples/en_female_music.wav",
# "./samples/en_male.wav", "./samples/en_female.wav"]
# case 3
# expect [0-2match, 1-3match]
# 模型具有quality分辨能力
# tensor([[0.4325, 0.3912],
# [0.4801, 0.4459],
# [0.4836, 0.4986],
# [0.4723, 0.4741]], device='cuda:0')
# tensor([[1.0000, 0.9511, 0.9240, 0.8633],
# [0.9511, 1.0000, 0.9416, 0.8835],
# [0.9240, 0.9416, 1.0000, 0.9293],
# [0.8633, 0.8835, 0.9293, 1.0000]], device='cuda:0')
# query_text = [
# "Describe the quality of the audio.",
# "Describe the quality of the audio.",
# "Describe the quality of the audio.",
# "Describe the quality of the audio.",
# ]
# query_audio = ["./samples/en_male_music.wav", "./samples/en_male.wav",
# "./samples/en_female_music.wav", "./samples/en_female.wav"]
# case 4-1
# expect [1-3match]
# query_text = [
# "Describe the speech content and topic.",
# "Describe the speech content and topic.",
# "Describe the speech content and topic.",
# "Describe the speech content and topic.",
# ]
# query_audio = ["./samples/en_male_sunny.wav", "./samples/en_male.wav",
# "./samples/en_female_sunny.wav", "./samples/en_female.wav"]
# case 4-2
# expect [1-3match]
# 模型可以正确区分出来音频中是否包含某个单词
# tensor([[0.7353, 0.4429],
# [0.7130, 0.4250],
# [0.5905, 0.6196],
# [0.5303, 0.7002]], device='cuda:0')
# tensor([[1.0000, 0.9781, 0.8920, 0.7941],
# [0.9781, 1.0000, 0.9070, 0.8146],
# [0.8920, 0.9070, 1.0000, 0.9668],
# [0.7941, 0.8146, 0.9668, 1.0000]], device='cuda:0')
# query_text = [
# "Does the speaker mention the word 'sunny' in the audio?",
# "Does the speaker mention the word 'sunny' in the audio?",
# "Does the speaker mention the word 'sunny' in the audio?",
# "Does the speaker mention the word 'sunny' in the audio?",
# ]
# query_audio = ["./samples/en_male_sunny.wav", "./samples/en_female_sunny.wav",
# "./samples/en_male.wav", "./samples/en_female.wav"]
# case 5
# expect [0-1 match]
# 模型具备区分动物叫声的能力,结果显示如下:
# tensor([[0.7022, 0.4406, 0.4651],
# [0.7497, 0.3858, 0.4209],
# [0.3636, 0.4995, 0.3304],
# [0.4793, 0.2937, 0.7720]], device='cuda:0')
# tensor([[1.0000, 0.9535, 0.6508, 0.7476],
# [0.9535, 1.0000, 0.5688, 0.6673],
# [0.6508, 0.5688, 1.0000, 0.5568],
# [0.7476, 0.6673, 0.5568, 1.0000]], device='cuda:0')
# query_text = [
# "What type of animal is this?",
# "What type of animal is this?",
# "What type of animal is this?",
# "What type of animal is this?",
# ]
# query_audio = ["./samples/animal_cow_1moo.wav", "./samples/animal_cow_5moo.wav",
# "./samples/animal_lion.wav", "./samples/animal_rooster.wav"]
# case 6
# expect [0-2 match, 1-3 match]
# 模型能区分出中文和英文
# 汉语和英语
# 第一个矩阵结果正确,第二个矩阵有点问题
# tensor([[0.4100, 0.2334],
# [0.4451, 0.2417],
# [0.3379, 0.3609],
# [0.3997, 0.4074]], device='cuda:0')
# tensor([[1.0000, 0.9034, 0.8573, 0.7466],
# [0.9034, 1.0000, 0.7729, 0.7808],
# [0.8573, 0.7729, 1.0000, 0.8199],
# [0.7466, 0.7808, 0.8199, 1.0000]], device='cuda:0')
# 法语和英语,完全没有问题
# tensor([[0.4100, 0.3098],
# [0.4411, 0.3229],
# [0.3494, 0.4212],
# [0.3523, 0.4130]], device='cuda:0')
# tensor([[1.0000, 0.9030, 0.8696, 0.8591],
# [0.9030, 1.0000, 0.7874, 0.8468],
# [0.8696, 0.7874, 1.0000, 0.9654],
# [0.8591, 0.8468, 0.9654, 1.0000]], device='cuda:0')
# 英语和西班牙语,完全没有问题
# tensor([[0.4100, 0.3012],
# [0.4411, 0.3069],
# [0.3274, 0.3698],
# [0.3249, 0.3558]], device='cuda:0')
# tensor([[1.0000, 0.9030, 0.8591, 0.8590],
# [0.9030, 1.0000, 0.7775, 0.8208],
# [0.8591, 0.7775, 1.0000, 0.9739],
# [0.8590, 0.8208, 0.9739, 1.0000]], device='cuda:0')
# 法语和西班牙语,完全没有问题
# tensor([[0.4212, 0.2785],
# [0.4170, 0.2737],
# [0.2793, 0.3698],
# [0.2721, 0.3558]], device='cuda:0')
# tensor([[1.0000, 0.9661, 0.8730, 0.8688],
# [0.9661, 1.0000, 0.8493, 0.8726],
# [0.8730, 0.8493, 1.0000, 0.9739],
# [0.8688, 0.8726, 0.9739, 1.0000]], device='cuda:0')
# 法语和汉语,完全没问题
# tensor([[0.4212, 0.2278],
# [0.4130, 0.2353],
# [0.2986, 0.3609],
# [0.3602, 0.4074]], device='cuda:0')
# tensor([[1.0000, 0.9654, 0.7965, 0.6456],
# [0.9654, 1.0000, 0.7990, 0.7011],
# [0.7965, 0.7990, 1.0000, 0.8199],
# [0.6456, 0.7011, 0.8199, 1.0000]], device='cuda:0')
# 西班牙语和汉语,完全没问题
# tensor([[0.3698, 0.1813],
# [0.3558, 0.1784],
# [0.2814, 0.3609],
# [0.3257, 0.4074]], device='cuda:0')
# tensor([[1.0000, 0.9739, 0.7871, 0.6249],
# [0.9739, 1.0000, 0.7837, 0.6546],
# [0.7871, 0.7837, 1.0000, 0.8199],
# [0.6249, 0.6546, 0.8199, 1.0000]], device='cuda:0')
query_text = [
"Describe the language of the speaker.",
"Describe the language of the speaker.",
"Describe the language of the speaker.",
"Describe the language of the speaker."
]
query_audio = ["./samples/spanish_male.wav", "./samples/spanish_female.wav",
"./samples/chinese_male.wav", "./samples/zh_female.wav"]
# case 7
# expect [0-1 match] + [2-3 match]
# 测试结果显示模型不具备区分出说话者语速快慢的能力
# query_text = [
# "Describe the speaking rate of the speaker.",
# "Describe the speaking rate of the speaker.",
# "Describe the speaking rate of the speaker.",
# "Describe the speaking rate of the speaker."
# ]
# query_audio = ["./samples/female_fast_4.wav", "./samples/female_fast.wav",
# "./samples/male_slow.wav", "./samples/female_slow.wav"]
# case 8
# expect [0->one, 1->two]
# 测试结果显示模型无法区分出声音不同类别的能力
# query_text = [
# "How many distinct sound types are present in this audio?",
# "How many distinct sound types are present in this audio?",
# "How many distinct sound types are present in this audio?",
# "How many distinct sound types are present in this audio?"
# ]
# query_audio = [
# "./samples/one_type_sound.flac", "./samples/one_type_sound_2.flac",
# "./samples/two_type_sound.flac", "./samples/two_type_sound_2.flac",
# ]
# case 9
# expect [0-1 match] + [2-3 match]
# 测试结果显示模型具备对话语类型/语义的判断,结果如下所示。
# tensor([[0.3587, 0.2188],
# [0.3793, 0.2482],
# [0.2806, 0.3153],
# [0.2676, 0.3588]], device='cuda:0')
# tensor([[1.0000, 0.9275, 0.6241, 0.5684],
# [0.9275, 1.0000, 0.7433, 0.6704],
# [0.6241, 0.7433, 1.0000, 0.7269],
# [0.5684, 0.6704, 0.7269, 1.0000]], device='cuda:0')
# query_text = [
# "Identify the utterance type of this speech.",
# "Identify the utterance type of this speech.",
# "Identify the utterance type of this speech.",
# "Identify the utterance type of this speech."
# ]
# query_audio = ["./samples/male_statement.wav", "./samples/female_statement.wav",
# "./samples/male_question.wav", "./samples/female_question.wav"]
# # case 10
# # expect
# # 测试结果显示模型可以区分出音频是one-speaker还是two-speaker,结果如下所示。
# # tensor([[0.7003, 0.6864],
# # [0.7050, 0.6986],
# # [0.3909, 0.7932],
# # [0.4326, 0.8110]], device='cuda:0')
# # tensor([[1.0000, 0.9369, 0.7105, 0.7634],
# # [0.9369, 1.0000, 0.7473, 0.7579],
# # [0.7105, 0.7473, 1.0000, 0.8984],
# # [0.7634, 0.7579, 0.8984, 1.0000]], device='cuda:0')
# query_text = [
# "How many speakers in this audio?",
# "How many speakers in this audio?",
# "How many speakers in this audio?",
# "How many speakers in this audio?",
# ]
# query_audio = [
# "./samples/en_male.wav", "./samples/en_female.wav",
# "./samples/two_speaker.flac", "./samples/two_speaker_2.flac",
# ]
# case 11-1(说话)
# expect
# 测试结果如下所示。低音全都是用的male,可能有点不够solid。query-doc矩阵没问题,但是query-query矩阵有问题。
# tensor([[0.4330, 0.4321],
# [0.4118, 0.3743],
# [0.3756, 0.3932],
# [0.4046, 0.4489]], device='cuda:0')
# tensor([[1.0000, 0.9168, 0.9511, 0.9505],
# [0.9168, 1.0000, 0.9212, 0.9323],
# [0.9511, 0.9212, 1.0000, 0.9565],
# [0.9505, 0.9323, 0.9565, 1.0000]], device='cuda:0')
# query_text = [
# "Is the speaker's tone high or low?",
# "Is the speaker's tone high or low?",
# "Is the speaker's tone high or low?",
# "Is the speaker's tone high or low?"
# ]
# query_audio = [
# "./samples/female_speech_high_1.wav", "./samples/male_speech_high_1.wav",
# "./samples/male_speech_low_1.wav", "./samples/male_speech_low_2.wav"
# ]
# case 11-2(唱歌)
# expect
# 测试结果如下所示。低音全都是用的male,可能有点不够solid。
# tensor([[0.4901, 0.3206],
# [0.4875, 0.3638],
# [0.4354, 0.4760],
# [0.4144, 0.4296]], device='cuda:0')
# tensor([[1.0000, 0.9399, 0.8666, 0.8854],
# [0.9399, 1.0000, 0.9261, 0.9368],
# [0.8666, 0.9261, 1.0000, 0.9752],
# [0.8854, 0.9368, 0.9752, 1.0000]], device='cuda:0')
# query_text = [
# "Is the speaker's tone high or low?",
# "Is the speaker's tone high or low?",
# "Is the speaker's tone high or low?",
# "Is the speaker's tone high or low?"
# ]
# query_audio = [
# "./samples/female_sing_high_1.wav", "./samples/male_sing_high_1.wav",
# "./samples/male_sing_low_1.wav", "./samples/male_sing_low_2.wav"
# ]
# case 12
# expect [0-1 -> outdoor, 2-3 -> indoor]
# 测试结果显示模型无法区分出模型是在室内还是室外发生
# query_text = [
# "Where is this audio most likely recorded?",
# "Where is this audio most likely recorded?",
# "Where is this audio most likely recorded?",
# "Where is this audio most likely recorded?"
# ]
# query_audio = ["./samples/male_outdoor.wav", "./samples/female_outdoor.wav",
# "./samples/indoor.flac", "./samples/indoor2.flac"]
# # case 13
# # expect
# 测试结果显示模型能够区分出情感差异,结果如下所示。
# tensor([[0.3226, 0.1865],
# [0.4463, 0.2482],
# [0.4102, 0.4539],
# [0.3474, 0.4925]], device='cuda:0')
# tensor([[1.0000, 0.7824, 0.5576, 0.5138],
# [0.7824, 1.0000, 0.6224, 0.5177],
# [0.5576, 0.6224, 1.0000, 0.8121],
# [0.5138, 0.5177, 0.8121, 1.0000]], device='cuda:0')
# query_text = [
# "What is the emotion of the speaker?",
# "What is the emotion of the speaker?",
# "What is the emotion of the speaker?",
# "What is the emotion of the speaker?"
# ]
# query_audio = [
# "./samples/people_happy_1.flac", "./samples/people_happy_2.flac",
# "./samples/people_cry_1.flac", "./samples/female_sad_1.wav"
# ]
# # case 14
# # expect [0->child, 1->adult]
# # 测试结果如下所示,显示模型具备能力区分说话者的大致年龄。但是两个young都是用的boy;
# # 如果用young_girl.wav的话, 第一个矩阵没问题,第二个矩阵有问题。
# # tensor([[0.5183, 0.5019],
# # [0.5069, 0.4590],
# # [0.2864, 0.5422],
# # [0.3039, 0.5131]], device='cuda:0')
# # tensor([[1.0000, 0.9227, 0.7293, 0.7653],
# # [0.9227, 1.0000, 0.6910, 0.7291],
# # [0.7293, 0.6910, 1.0000, 0.8695],
# # [0.7653, 0.7291, 0.8695, 1.0000]], device='cuda:0')
# query_text = [
# "Estimate the age of the speaker.",
# "Estimate the age of the speaker.",
# "Estimate the age of the speaker.",
# "Estimate the age of the speaker."
# ]
# query_audio = [
# "./samples/young_boy_1.flac", "./samples/young_boy_2.wav",
# "./samples/adult_man.flac", "./samples/adult_woman.flac"
# ]
# case 15
# 测试结果显示模型可以区分出不同的英语口音,结果如下所示。其中第二个矩阵中有点不够solid
# tensor([[0.7703, 0.7582],
# [0.7267, 0.7185],
# [0.7237, 0.7307],
# [0.7646, 0.7759]], device='cuda:0')
# tensor([[1.0000, 0.9324, 0.9442, 0.9598],
# [0.9324, 1.0000, 0.9827, 0.9704],
# [0.9442, 0.9827, 1.0000, 0.9857],
# [0.9598, 0.9704, 0.9857, 1.0000]], device='cuda:0')
# query_text = [
# "Identify the English accent of the speaker.",
# "Identify the English accent of the speaker.",
# "Identify the English accent of the speaker.",
# "Identify the English accent of the speaker."
# ]
# query_audio = ["./samples/male_america_english.wav", "./samples/female_america_english.wav",
# "./samples/male_british_english.wav", "./samples/female_british_english.wav"]
# # case 16
# # expect
# # 测试结果显示模型可以区分出飞机引擎声和大巴引擎声,结果如下所示
# # tensor([[0.4200, 0.2822],
# # [0.5744, 0.2713],
# # [0.2183, 0.3696],
# # [0.2527, 0.3816]], device='cuda:0')
# # tensor([[1.0000, 0.8755, 0.7497, 0.7796# [0.6261, 0.5706, 1.0000, 0.7327],
# [0.4892, 0.5031, 0.7327, 1.0000]], device='cuda:0')
# query_text = [
# "What is the source of this sound?",
# "What is the source of this sound?",
# "What is the source of this sound?",
# "What is the source of this sound?"
# ]
# query_audio = ["./samples/rainy.wav", "./samples/rainy_2.flac",
# "./samples/wind_2.flac", "./samples/wind.flac"]
# case 18
# expect
# 测试结果显示模型具备对说话者的计数能力;
# 3-speaker/4-speaker测试结果如下, query-doc矩阵没问题,query-query矩阵有点小问题
# tensor([[0.4325, 0.3912],
# [0.4801, 0.4459],
# [0.4836, 0.4986],
# [0.4723, 0.4741]], device='cuda:0')
# tensor([[1.0000, 0.9511, 0.9240, 0.8633],
# [0.9511, 1.0000, 0.9416, 0.8835],
# [0.9240, 0.9416, 1.0000, 0.9293],
# [0.8633, 0.8835, 0.9293, 1.0000]], device='cuda:0')
# query_text = [
# "What's the number of participants in the current conversation?",
# "What's the number of participants in the current conversation?",
# "What's the number of participants in the current conversation?",
# "What's the number of participants in the current conversation?"
# ]
# query_audio = [
# "./samples/three_speaker_2.wav", "./samples/three_speaker.wav",
# "./samples/four_speaker.wav", "./samples/four_speaker_3.wav",
# ]
# case 19
# 测试结果显示不具备音乐风格识别能力
# query_text = [
# "What is the music style in the audio?",
# "What is the music style in the audio?",
# "What is the music style in the audio?",
# "What is the music style in the audio?"
# ]
# query_audio = [
# "./samples/hiphop_1.flac", "./samples/hiphop_2.flac",
# "./samples/classical_1.flac", "./samples/classical_2.flac",
# ]
# case 20
# 测试结果显示模型具备识别乐器声音的能力
# tensor([[0.4882, 0.3500],
# [0.4124, 0.3477],
# [0.3047, 0.6171],
# [0.4425, 0.5910]], device='cuda:0')
# tensor([[1.0000, 0.8528, 0.7110, 0.8001],
# [0.8528, 1.0000, 0.7338, 0.7728],
# [0.7110, 0.7338, 1.0000, 0.9112],
# [0.8001, 0.7728, 0.9112, 1.0000]], device='cuda:0')
# query_text = [
# "What musical instrument is producing the sound?",
# "What musical instrument is producing the sound?",
# "What musical instrument is producing the sound?",
# "What musical instrument is producing the sound?"
# ]
# query_audio = [
# "./samples/piano_3.flac", "./samples/piano_2.flac",
# "./samples/guitar_1.flac", "./samples/guitar_2.flac",
# ]
# case 21
# 测试显示模型无法计数说话中单词的数量
# query_text = [
# "How many times does the word 'said' appear in the audio?",
# "How many times does the word 'beautiful' appear in the audio?",
# "How many times does the word 'fate' appear in the audio?",
# "How many times does the word 'ninty' appear in the audio?"
# ]
# query_audio = [
# "./samples/1_said.wav", "./samples/1_beautiful.wav",
# "./samples/1_fate.wav", "./samples/3_ninty_nine.wav",
# ]
# case 22
# 测试显示模型是否具备计数铃声和鼓声的能力
# query_text = [
# "How many times does the sound appear in the audio?",
# "How many times does the sound appear in the audio?",
# "How many times does the sound appear in the audio?",
# "How many times does the sound appear in the audio?"
# ]
# query_audio = [
# "./samples/2_drumbeat.wav", "./samples/2_ringtone.wav",
# "./samples/6_drumbeat.wav", "./samples/6_ringtone.wav"
# ]
# ----
# doc_text = [
# "male",
# "female",
# ]
# doc_text = [
# "music",
# "nothing",
# ]
# doc_text = [
# "High-quality",
# "Low-quality",
# ]
# doc_text = [
# "yes",
# "no",
# ]
# doc_text = [
# "cow",
# "lion",
# "rooster",
# ]
doc_text = [
"spanish",
"chinese",
]
# doc_text = [
# "fast",
# "slow",
# ]
# doc_text = [
# "one type",
# "two types",
# ]
# doc_text = [
# "statement",
# "question",
# ]
# doc_text = [
# "one speaker",
# "two speakers",
# ]
# doc_text = [
# "high",
# "low",
# ]
# doc_text = [
# "outdoor",
# "indoor",
# ]
# doc_text = [
# "happy",
# "sad",
# ]
# doc_text = [
# "child",
# "adult",
# ]
# doc_text = [
# "American accent",
# "British accent",
# ]
# doc_text = [
# "helicopter engine",
# "car engine",
# ]
# doc_text = [
# "rain",
# "wind",
# ]
# doc_text = [
# "hiphop",
# "classical",
# ]
# doc_text = [
# "three persons",
# "four persons",
# ]
# doc_text = [
# "piano",
# "guitar",
# ]
# doc_text = [
# "one",
# "three",
# ]
# doc_text = [
# "two",
# "six",
# ]
query_embeddings = model.encode(
text=query_text,
audio=query_audio,
task="query",
instruction=QUERY_INSTRUCTION,
normalize=True,
device=device,
)
doc_embeddings = model.encode(
text=doc_text,
# text=[None, None],
# audio=doc_audio,
task="document",
instruction=DOC_INSTRUCTION,
normalize=True,
device=device,
)
similarity = query_embeddings @ doc_embeddings.T
print(similarity)
similarity = query_embeddings @ query_embeddings.T
print(similarity)