mirror of
https://github.com/gradio-app/gradio.git
synced 2024-12-03 01:50:59 +08:00
597337dcb8
* added playground with 12 demos * change name to recipes, restyle navbar * add explanatory text to page * fix demo mapping * categorize demos, clean up design * styling * cateogry naming and emojis * refactor and add text demos * add view code button * remove opening slash in embed * styling * add image demos * adding plot demos * remove see code button * removed submodules * changes * add audio models * remove fun section * remove tests in image semgentation demo repo * requested changes * add outbreak_forecast * fix broken demos * remove images and models, add new demos * remove readmes, change to run.py, add description as comment * move to /demos folder, clean up dict * add upload_to_spaces script * fix script, clean repos, and add to docker file * fix python versioning issue * env variable * fix * env fixes * spaces instead of tabs * revert to original networking.py * fix rate limiting in asr and autocomplete * change name to demos * clean up navbar * move url and description, remove code comments * add tabs to demos * remove margins and footer from embedded demo * font consistency Co-authored-by: Abubakar Abid <abubakar@huggingface.co>
50 lines
1.9 KiB
Python
50 lines
1.9 KiB
Python
import gradio as gr
|
|
import torch, torchaudio
|
|
from timeit import default_timer as timer
|
|
from data_setups import audio_preprocess, resample
|
|
import gdown
|
|
|
|
url = 'https://drive.google.com/uc?id=1X5CR18u0I-ZOi_8P0cNptCe5JGk9Ro0C'
|
|
output = 'piano.wav'
|
|
gdown.download(url, output, quiet=False)
|
|
url = 'https://drive.google.com/uc?id=1W-8HwmGR5SiyDbUcGAZYYDKdCIst07__'
|
|
output= 'torch_efficientnet_fold2_CNN.pth'
|
|
gdown.download(url, output, quiet=False)
|
|
device = "cuda" if torch.cuda.is_available() else "cpu"
|
|
SAMPLE_RATE = 44100
|
|
AUDIO_LEN = 2.90
|
|
model = torch.load("torch_efficientnet_fold2_CNN.pth", map_location=torch.device('cpu'))
|
|
LABELS = [
|
|
"Cello", "Clarinet", "Flute", "Acoustic Guitar", "Electric Guitar", "Organ", "Piano", "Saxophone", "Trumpet", "Violin", "Voice"
|
|
]
|
|
example_list = [
|
|
["piano.wav"]
|
|
]
|
|
|
|
|
|
def predict(audio_path):
|
|
start_time = timer()
|
|
wavform, sample_rate = torchaudio.load(audio_path)
|
|
wav = resample(wavform, sample_rate, SAMPLE_RATE)
|
|
if len(wav) > int(AUDIO_LEN * SAMPLE_RATE):
|
|
wav = wav[:int(AUDIO_LEN * SAMPLE_RATE)]
|
|
else:
|
|
print(f"input length {len(wav)} too small!, need over {int(AUDIO_LEN * SAMPLE_RATE)}")
|
|
return
|
|
img = audio_preprocess(wav, SAMPLE_RATE).unsqueeze(0)
|
|
model.eval()
|
|
with torch.inference_mode():
|
|
pred_probs = torch.softmax(model(img), dim=1)
|
|
pred_labels_and_probs = {LABELS[i]: float(pred_probs[0][i]) for i in range(len(LABELS))}
|
|
pred_time = round(timer() - start_time, 5)
|
|
return pred_labels_and_probs, pred_time
|
|
|
|
demo = gr.Interface(fn=predict,
|
|
inputs=gr.Audio(type="filepath"),
|
|
outputs=[gr.Label(num_top_classes=11, label="Predictions"),
|
|
gr.Number(label="Prediction time (s)")],
|
|
examples=example_list,
|
|
cache_examples=False
|
|
)
|
|
|
|
demo.launch(debug=False) |