Spaces:
Sleeping
Sleeping
import math | |
from speechbrain.inference.classifiers import EncoderClassifier | |
import numpy as np | |
import torch | |
INDEX_TO_LANG = { | |
0: 'Abkhazian', 1: 'Afrikaans', 2: 'Amharic', 3: 'Arabic', 4: 'Assamese', | |
5: 'Azerbaijani', 6: 'Bashkir', 7: 'Belarusian', 8: 'Bulgarian', 9: 'Bengali', | |
10: 'Tibetan', 11: 'Breton', 12: 'Bosnian', 13: 'Catalan', 14: 'Cebuano', | |
15: 'Czech', 16: 'Welsh', 17: 'Danish', 18: 'German', 19: 'Greek', | |
20: 'English', 21: 'Esperanto', 22: 'Spanish', 23: 'Estonian', 24: 'Basque', | |
25: 'Persian', 26: 'Finnish', 27: 'Faroese', 28: 'French', 29: 'Galician', | |
30: 'Guarani', 31: 'Gujarati', 32: 'Manx', 33: 'Hausa', 34: 'Hawaiian', | |
35: 'Hindi', 36: 'Croatian', 37: 'Haitian', 38: 'Hungarian', 39: 'Armenian', | |
40: 'Interlingua', 41: 'Indonesian', 42: 'Icelandic', 43: 'Italian', 44: 'Hebrew', | |
45: 'Japanese', 46: 'Javanese', 47: 'Georgian', 48: 'Kazakh', 49: 'Central Khmer', | |
50: 'Kannada', 51: 'Korean', 52: 'Latin', 53: 'Luxembourgish', 54: 'Lingala', | |
55: 'Lao', 56: 'Lithuanian', 57: 'Latvian', 58: 'Malagasy', 59: 'Maori', | |
60: 'Macedonian', 61: 'Malayalam', 62: 'Mongolian', 63: 'Marathi', 64: 'Malay', | |
65: 'Maltese', 66: 'Burmese', 67: 'Nepali', 68: 'Dutch', 69: 'Norwegian Nynorsk', | |
70: 'Norwegian', 71: 'Occitan', 72: 'Panjabi', 73: 'Polish', 74: 'Pushto', | |
75: 'Portuguese', 76: 'Romanian', 77: 'Russian', 78: 'Sanskrit', 79: 'Scots', | |
80: 'Sindhi', 81: 'Sinhala', 82: 'Slovak', 83: 'Slovenian', 84: 'Shona', | |
85: 'Somali', 86: 'Albanian', 87: 'Serbian', 88: 'Sundanese', 89: 'Swedish', | |
90: 'Swahili', 91: 'Tamil', 92: 'Telugu', 93: 'Tajik', 94: 'Thai', | |
95: 'Turkmen', 96: 'Tagalog', 97: 'Turkish', 98: 'Tatar', 99: 'Ukrainian', | |
100: 'Urdu', 101: 'Uzbek', 102: 'Vietnamese', 103: 'Waray', 104: 'Yiddish', | |
105: 'Yoruba', 106: 'Chinese' | |
} | |
LANG_TO_INDEX = {v: k for k, v in INDEX_TO_LANG.items()} | |
# speechbrainの言語分類モデルのロード | |
language_id = EncoderClassifier.from_hparams(source="speechbrain/lang-id-voxlingua107-ecapa") | |
def identify_languages(chunk: np.ndarray, languages: list[str] = ["Japanese", "English"]) -> tuple[dict, dict]: | |
""" | |
言語分類を行う関数 | |
""" | |
# 言語分類 | |
# outputs = language_id.encode_batch([chunk]) | |
lang_scores, _, _, _ = language_id.classify_batch(torch.from_numpy(chunk).unsqueeze(0)) | |
# 結果の整形 | |
all_scores = {INDEX_TO_LANG[i]: 100 * math.exp(score) for i, score in enumerate(lang_scores[0])} | |
selected_scores = {lang: float(all_scores[lang]) for lang in languages} | |
return selected_scores, all_scores | |