Spaces:
Runtime error
Runtime error
import streamlit as st | |
import langcodes | |
from allosaurus.app import read_recognizer | |
from pathlib import Path | |
def get_path_to_wav_format(uploaded_file): | |
# st.write(dir(uploaded_file)) | |
# st.write(type(uploaded_file)) | |
# st.write(uploaded_file) | |
uploaded_bytes = uploaded_file.getvalue() | |
actual_file_path = Path(uploaded_file.name) | |
actual_file_path.write_bytes(uploaded_bytes) | |
if ".wav" in uploaded_file.name: | |
return Path(uploaded_file.name) | |
if ".mp3" in uploaded_file.name: | |
new_desired_path = actual_file_path.with_suffix(".wav") | |
waveform, sample_rate = torchaudio.load(actual_file_path) | |
st.info(waveform, sample_rate) | |
torchaudio.save(new_desired_path, waveform, sample_rate) | |
return new_desired_path | |
def get_langcode_for_allosaurus(input_code, model): | |
langcode = "ipa" # the default allosaurus recognizer | |
description = "the default universal setting, not specific to any language" | |
if not input_code: | |
return langcode, description | |
try: | |
lang = langcodes.get(input_code) | |
alpha3 = lang.to_alpha3() | |
if model.is_available(alpha3 ): # https://github.com/xinjli/allosaurus/blob/f5aa296dc97a90e3035f6cc0fd281cfb64536228/allosaurus/app.py#L57 | |
langcode = alpha3 | |
description = lang.display_name() | |
st.info(f"{langcode} is supported by Allosaurus}") | |
else: | |
# st.info(f"Could not find supported language for {input_code}") | |
st.error(f"Allosaurus doesn't recognize the language code {langcode}}. Perhaps try looking for [alternate codes](https://huggingface.co/spaces/cdleong/langcode-search). Or possibly it's just that there's no support for that particular language code. Proceeding with default setting") | |
except langcodes.LanguageTagError as e: | |
st.error(f"langcodes library could not find a 3-letter ISO code for {input_code}. It may not be a valid code, try searching for [alternate codes](https://huggingface.co/spaces/cdleong/langcode-search). Proceeding with default setting" | |
return langcode, description | |
if __name__ == "__main__": | |
input_code = st.text_input("(optional) 2 or 3-letter ISO code for input language. 2-letter codes will be converted to 3-letter codes", max_chars=3) | |
model = read_recognizer() | |
langcode, description = get_langcode_for_allosaurus(input_code) | |
st.write(f"Instructing Allosaurus to recognize using language {langcode}. That is, {description}") | |
uploaded_files = st.file_uploader("Choose a file", type=[ | |
".wav", | |
# ".mp3", # TODO: convert .mp3 to .wav and save | |
], | |
accept_multiple_files=True, | |
) | |
for uploaded_file in uploaded_files: | |
if uploaded_file is not None: | |
st.audio(uploaded_file, format='audio/wav') | |
wav_file = get_path_to_wav_format(uploaded_file) | |
st.write(wav_file) | |
result = model.recognize(wav_file, langcode) | |
st.write(result) | |