jpbello/LanguageClassification
1
1import gradio as gr2import librosa3import numpy as np4import torch5from transformers import pipeline6 7 8language_classes = {9 0: "Arabic",10 1: "Basque",11 2: "Breton",12 3: "Catalan",13 4: "Chinese_China",14 5: "Chinese_Hongkong",15 6: "Chinese_Taiwan",16 7: "Chuvash",17 8: "Czech",18 9: "Dhivehi",19 10: "Dutch",20 11: "English",21 12: "Esperanto",22 13: "Estonian",23 14: "French",24 15: "Frisian",25 16: "Georgian",26 17: "German",27 18: "Greek",28 19: "Hakha_Chin",29 20: "Indonesian",30 21: "Interlingua",31 22: "Italian",32 23: "Japanese",33 24: "Kabyle",34 25: "Kinyarwanda",35 26: "Kyrgyz",36 27: "Latvian",37 28: "Maltese",38 29: "Mongolian",39 30: "Persian",40 31: "Polish",41 32: "Portuguese",42 33: "Romanian",43 34: "Romansh_Sursilvan",44 35: "Russian",45 36: "Sakha",46 37: "Slovenian",47 38: "Spanish",48 39: "Swedish",49 40: "Tamil",50 41: "Tatar",51 42: "Turkish",52 43: "Ukranian",53 44: "Welsh"54}55 56 57 58 59username = "jpbello" ## Complete your username60model_id = "jpbello/Hubert_emotion-finetuned-common_language"61device = "cuda:0" if torch.cuda.is_available() else "cpu"62pipe = pipeline("audio-classification", model=model_id, device=device)63 64# def predict_trunc(filepath):65# preprocessed = pipe.preprocess(filepath)66# truncated = pipe.feature_extractor.pad(preprocessed,truncation=True, max_length = 16_000*30)67# model_outputs = pipe.forward(truncated)68# outputs = pipe.postprocess(model_outputs)69 70# return outputs71 72 73def classify_audio(filepath):74 75 76 preds = pipe(filepath)77 # preds = predict_trunc(filepath)78 outputs = {}79 for p in preds:80 outputs[p["label"]] = p["score"]81 return outputs82 83 84title = "Language Classification Model"85description = (86 "Welcome to the Language Classification Model demo powered by Gradio and Hubert Emotion. "87 "This model is trained to identify the language spoken in audio samples, making it a valuable tool "88 "for language identification tasks. Upload an audio file, and let the model predict the spoken language "89 "with confidence scores. Try it out with our provided example audio files to see the model in action!"90 91)92filenames = ['EN_0212.wav', "FR_0061.wav", "JP_0100.wav","AR_0019.wav"]93filenames = [[f"./{f}"] for f in filenames]94demo = gr.Interface(95 fn=classify_audio,96 inputs=gr.Audio(type="filepath"),97 outputs=[gr.Label(label="Predictions")],98 title=title,99 description=description,100 examples=filenames,101)102demo.launch()