Spaces:
Runtime error
Runtime error
Add application file
Browse files- .gitignore +166 -0
- app.py +80 -0
- classify.py +66 -0
- data/(chirping_birds)1-34495-A-14.wav +0 -0
- data/(clock_tick)1-21934-A-38.wav +0 -0
- data/(dog)1-100032-A-0.wav +0 -0
- data/(helicopter)1-181071-A-40.wav +0 -0
- data/(laughing)1-1791-A-26.wav +0 -0
- packages.txt +1 -0
- requirements.txt +2 -0
.gitignore
ADDED
|
@@ -0,0 +1,166 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# Byte-compiled / optimized / DLL files
|
| 2 |
+
__pycache__/
|
| 3 |
+
*.py[cod]
|
| 4 |
+
*$py.class
|
| 5 |
+
|
| 6 |
+
# C extensions
|
| 7 |
+
*.so
|
| 8 |
+
|
| 9 |
+
# Distribution / packaging
|
| 10 |
+
.Python
|
| 11 |
+
build/
|
| 12 |
+
develop-eggs/
|
| 13 |
+
dist/
|
| 14 |
+
downloads/
|
| 15 |
+
eggs/
|
| 16 |
+
.eggs/
|
| 17 |
+
lib/
|
| 18 |
+
lib64/
|
| 19 |
+
parts/
|
| 20 |
+
sdist/
|
| 21 |
+
var/
|
| 22 |
+
wheels/
|
| 23 |
+
share/python-wheels/
|
| 24 |
+
*.egg-info/
|
| 25 |
+
.installed.cfg
|
| 26 |
+
*.egg
|
| 27 |
+
MANIFEST
|
| 28 |
+
|
| 29 |
+
# PyInstaller
|
| 30 |
+
# Usually these files are written by a python script from a template
|
| 31 |
+
# before PyInstaller builds the exe, so as to inject date/other infos into it.
|
| 32 |
+
*.manifest
|
| 33 |
+
*.spec
|
| 34 |
+
|
| 35 |
+
# Installer logs
|
| 36 |
+
pip-log.txt
|
| 37 |
+
pip-delete-this-directory.txt
|
| 38 |
+
|
| 39 |
+
# Unit test / coverage reports
|
| 40 |
+
htmlcov/
|
| 41 |
+
.tox/
|
| 42 |
+
.nox/
|
| 43 |
+
.coverage
|
| 44 |
+
.coverage.*
|
| 45 |
+
.cache
|
| 46 |
+
nosetests.xml
|
| 47 |
+
coverage.xml
|
| 48 |
+
*.cover
|
| 49 |
+
*.py,cover
|
| 50 |
+
.hypothesis/
|
| 51 |
+
.pytest_cache/
|
| 52 |
+
cover/
|
| 53 |
+
|
| 54 |
+
# Translations
|
| 55 |
+
*.mo
|
| 56 |
+
*.pot
|
| 57 |
+
|
| 58 |
+
# Django stuff:
|
| 59 |
+
*.log
|
| 60 |
+
local_settings.py
|
| 61 |
+
db.sqlite3
|
| 62 |
+
db.sqlite3-journal
|
| 63 |
+
|
| 64 |
+
# Flask stuff:
|
| 65 |
+
instance/
|
| 66 |
+
.webassets-cache
|
| 67 |
+
|
| 68 |
+
# Scrapy stuff:
|
| 69 |
+
.scrapy
|
| 70 |
+
|
| 71 |
+
# Sphinx documentation
|
| 72 |
+
docs/_build/
|
| 73 |
+
|
| 74 |
+
# PyBuilder
|
| 75 |
+
.pybuilder/
|
| 76 |
+
target/
|
| 77 |
+
|
| 78 |
+
# Jupyter Notebook
|
| 79 |
+
.ipynb_checkpoints
|
| 80 |
+
|
| 81 |
+
# IPython
|
| 82 |
+
profile_default/
|
| 83 |
+
ipython_config.py
|
| 84 |
+
|
| 85 |
+
# pyenv
|
| 86 |
+
# For a library or package, you might want to ignore these files since the code is
|
| 87 |
+
# intended to run in multiple environments; otherwise, check them in:
|
| 88 |
+
.python-version
|
| 89 |
+
|
| 90 |
+
# pipenv
|
| 91 |
+
# According to pypa/pipenv#598, it is recommended to include Pipfile.lock in version control.
|
| 92 |
+
# However, in case of collaboration, if having platform-specific dependencies or dependencies
|
| 93 |
+
# having no cross-platform support, pipenv may install dependencies that don't work, or not
|
| 94 |
+
# install all needed dependencies.
|
| 95 |
+
#Pipfile.lock
|
| 96 |
+
|
| 97 |
+
# poetry
|
| 98 |
+
# Similar to Pipfile.lock, it is generally recommended to include poetry.lock in version control.
|
| 99 |
+
# This is especially recommended for binary packages to ensure reproducibility, and is more
|
| 100 |
+
# commonly ignored for libraries.
|
| 101 |
+
# https://python-poetry.org/docs/basic-usage/#commit-your-poetrylock-file-to-version-control
|
| 102 |
+
#poetry.lock
|
| 103 |
+
|
| 104 |
+
# pdm
|
| 105 |
+
# Similar to Pipfile.lock, it is generally recommended to include pdm.lock in version control.
|
| 106 |
+
#pdm.lock
|
| 107 |
+
# pdm stores project-wide configurations in .pdm.toml, but it is recommended to not include it
|
| 108 |
+
# in version control.
|
| 109 |
+
# https://pdm.fming.dev/#use-with-ide
|
| 110 |
+
.pdm.toml
|
| 111 |
+
|
| 112 |
+
# PEP 582; used by e.g. github.com/David-OConnor/pyflow and github.com/pdm-project/pdm
|
| 113 |
+
__pypackages__/
|
| 114 |
+
|
| 115 |
+
# Celery stuff
|
| 116 |
+
celerybeat-schedule
|
| 117 |
+
celerybeat.pid
|
| 118 |
+
|
| 119 |
+
# SageMath parsed files
|
| 120 |
+
*.sage.py
|
| 121 |
+
|
| 122 |
+
# Environments
|
| 123 |
+
.env
|
| 124 |
+
.venv
|
| 125 |
+
env/
|
| 126 |
+
venv/
|
| 127 |
+
ENV/
|
| 128 |
+
env.bak/
|
| 129 |
+
venv.bak/
|
| 130 |
+
|
| 131 |
+
# Spyder project settings
|
| 132 |
+
.spyderproject
|
| 133 |
+
.spyproject
|
| 134 |
+
|
| 135 |
+
# Rope project settings
|
| 136 |
+
.ropeproject
|
| 137 |
+
|
| 138 |
+
# mkdocs documentation
|
| 139 |
+
/site
|
| 140 |
+
|
| 141 |
+
# mypy
|
| 142 |
+
.mypy_cache/
|
| 143 |
+
.dmypy.json
|
| 144 |
+
dmypy.json
|
| 145 |
+
|
| 146 |
+
# Pyre type checker
|
| 147 |
+
.pyre/
|
| 148 |
+
|
| 149 |
+
# pytype static type analyzer
|
| 150 |
+
.pytype/
|
| 151 |
+
|
| 152 |
+
# Cython debug symbols
|
| 153 |
+
cython_debug/
|
| 154 |
+
|
| 155 |
+
# PyCharm
|
| 156 |
+
# JetBrains specific template is maintained in a separate JetBrains.gitignore that can
|
| 157 |
+
# be found at https://github.com/github/gitignore/blob/main/Global/JetBrains.gitignore
|
| 158 |
+
# and can be added to the global gitignore or merged into this file. For a more nuclear
|
| 159 |
+
# option (not recommended) you can uncomment the following to ignore the entire idea folder.
|
| 160 |
+
#.idea/
|
| 161 |
+
|
| 162 |
+
|
| 163 |
+
# Below are files that are specific to this project
|
| 164 |
+
flagged/
|
| 165 |
+
.vscode/
|
| 166 |
+
.DS_Store
|
app.py
ADDED
|
@@ -0,0 +1,80 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from typing import Dict
|
| 2 |
+
|
| 3 |
+
import gradio as gr
|
| 4 |
+
import whisper
|
| 5 |
+
from whisper.tokenizer import get_tokenizer
|
| 6 |
+
|
| 7 |
+
import classify
|
| 8 |
+
|
| 9 |
+
|
| 10 |
+
def zero_shot_classify(audio_path: str, class_names: str, model_name: str) -> Dict[str, float]:
|
| 11 |
+
class_names = class_names.split(",")
|
| 12 |
+
tokenizer = get_tokenizer(multilingual=".en" not in model_name)
|
| 13 |
+
model = whisper.load_model(model_name)
|
| 14 |
+
|
| 15 |
+
internal_lm_average_logprobs = classify.calculate_internal_lm_average_logprobs(
|
| 16 |
+
model=model,
|
| 17 |
+
class_names=class_names,
|
| 18 |
+
tokenizer=tokenizer,
|
| 19 |
+
)
|
| 20 |
+
audio_features = classify.calculate_audio_features(audio_path, model)
|
| 21 |
+
average_logprobs = classify.calculate_average_logprobs(
|
| 22 |
+
model=model,
|
| 23 |
+
audio_features=audio_features,
|
| 24 |
+
class_names=class_names,
|
| 25 |
+
tokenizer=tokenizer,
|
| 26 |
+
)
|
| 27 |
+
average_logprobs -= internal_lm_average_logprobs
|
| 28 |
+
scores = average_logprobs.softmax(-1).tolist()
|
| 29 |
+
return {class_name: score for class_name, score in zip(class_names, scores)}
|
| 30 |
+
|
| 31 |
+
|
| 32 |
+
def main():
|
| 33 |
+
CLASS_NAMES = "[dog barking],[helicopter whirring],[laughing],[birds chirping],[clock ticking]"
|
| 34 |
+
AUDIO_PATHS = [
|
| 35 |
+
"./data/(dog)1-100032-A-0.wav",
|
| 36 |
+
"./data/(helicopter)1-181071-A-40.wav",
|
| 37 |
+
"./data/(laughing)1-1791-A-26.wav",
|
| 38 |
+
"./data/(chirping_birds)1-34495-A-14.wav",
|
| 39 |
+
"./data/(clock_tick)1-21934-A-38.wav",
|
| 40 |
+
]
|
| 41 |
+
EXAMPLES = []
|
| 42 |
+
for audio_path in AUDIO_PATHS:
|
| 43 |
+
EXAMPLES.append([audio_path, CLASS_NAMES, "small"])
|
| 44 |
+
|
| 45 |
+
DESCRIPTION = """
|
| 46 |
+
<div style="text-align: center;">
|
| 47 |
+
<p>
|
| 48 |
+
This demo allows you to try out zero-shot audio classification using
|
| 49 |
+
[Whisper](https://github.com/openai/whisper).
|
| 50 |
+
</p>
|
| 51 |
+
<p>Github: [https://github.com/jumon/zac](https://github.com/jumon/zac)</p>
|
| 52 |
+
<p>
|
| 53 |
+
Example audio files are from the [ESC-50](https://github.com/karolpiczak/ESC-50)
|
| 54 |
+
dataset (CC BY-NC 3.0).
|
| 55 |
+
</p>
|
| 56 |
+
</div>
|
| 57 |
+
"""
|
| 58 |
+
|
| 59 |
+
demo = gr.Interface(
|
| 60 |
+
fn=zero_shot_classify,
|
| 61 |
+
inputs=[
|
| 62 |
+
gr.Audio(source="upload", type="filepath", label="Audio File"),
|
| 63 |
+
gr.Textbox(lines=1, label="Candidate class names (comma-separated)"),
|
| 64 |
+
gr.Radio(
|
| 65 |
+
choices=["tiny", "base", "small", "medium", "large"],
|
| 66 |
+
value="small",
|
| 67 |
+
label="Model Name",
|
| 68 |
+
),
|
| 69 |
+
],
|
| 70 |
+
outputs="label",
|
| 71 |
+
examples=EXAMPLES,
|
| 72 |
+
title="Zero-shot Audio Classification using Whisper",
|
| 73 |
+
description=DESCRIPTION,
|
| 74 |
+
)
|
| 75 |
+
|
| 76 |
+
demo.launch()
|
| 77 |
+
|
| 78 |
+
|
| 79 |
+
if __name__ == "__main__":
|
| 80 |
+
main()
|
classify.py
ADDED
|
@@ -0,0 +1,66 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from typing import List, Optional
|
| 2 |
+
|
| 3 |
+
import torch
|
| 4 |
+
import torch.nn.functional as F
|
| 5 |
+
from whisper.audio import N_FRAMES, N_MELS, log_mel_spectrogram, pad_or_trim
|
| 6 |
+
from whisper.model import Whisper
|
| 7 |
+
from whisper.tokenizer import Tokenizer
|
| 8 |
+
|
| 9 |
+
|
| 10 |
+
@torch.no_grad()
|
| 11 |
+
def calculate_audio_features(audio_path: Optional[str], model: Whisper) -> torch.Tensor:
|
| 12 |
+
if audio_path is None:
|
| 13 |
+
segment = torch.zeros((N_MELS, N_FRAMES), dtype=torch.float32).to(model.device)
|
| 14 |
+
else:
|
| 15 |
+
mel = log_mel_spectrogram(audio_path)
|
| 16 |
+
segment = pad_or_trim(mel, N_FRAMES).to(model.device)
|
| 17 |
+
return model.embed_audio(segment.unsqueeze(0))
|
| 18 |
+
|
| 19 |
+
|
| 20 |
+
@torch.no_grad()
|
| 21 |
+
def calculate_average_logprobs(
|
| 22 |
+
model: Whisper,
|
| 23 |
+
audio_features: torch.Tensor,
|
| 24 |
+
class_names: List[str],
|
| 25 |
+
tokenizer: Tokenizer,
|
| 26 |
+
) -> torch.Tensor:
|
| 27 |
+
initial_tokens = (
|
| 28 |
+
torch.tensor(tokenizer.sot_sequence_including_notimestamps).unsqueeze(0).to(model.device)
|
| 29 |
+
)
|
| 30 |
+
eot_token = torch.tensor([tokenizer.eot]).unsqueeze(0).to(model.device)
|
| 31 |
+
|
| 32 |
+
average_logprobs = torch.zeros(len(class_names))
|
| 33 |
+
for i, class_name in enumerate(class_names):
|
| 34 |
+
class_name_tokens = (
|
| 35 |
+
torch.tensor(tokenizer.encode(" " + class_name)).unsqueeze(0).to(model.device)
|
| 36 |
+
)
|
| 37 |
+
input_tokens = torch.cat([initial_tokens, class_name_tokens, eot_token], dim=1)
|
| 38 |
+
|
| 39 |
+
logits = model.logits(input_tokens, audio_features) # (1, T, V)
|
| 40 |
+
logprobs = F.log_softmax(logits, dim=-1).squeeze(0) # (T, V)
|
| 41 |
+
logprobs = logprobs[len(tokenizer.sot_sequence_including_notimestamps) - 1 : -1] # (T', V)
|
| 42 |
+
logprobs = torch.gather(logprobs, dim=-1, index=class_name_tokens.view(-1, 1)) # (T', 1)
|
| 43 |
+
average_logprob = logprobs.mean().item()
|
| 44 |
+
average_logprobs[i] = average_logprob
|
| 45 |
+
|
| 46 |
+
return average_logprobs
|
| 47 |
+
|
| 48 |
+
|
| 49 |
+
def calculate_internal_lm_average_logprobs(
|
| 50 |
+
model: Whisper,
|
| 51 |
+
class_names: List[str],
|
| 52 |
+
tokenizer: Tokenizer,
|
| 53 |
+
verbose: bool = False,
|
| 54 |
+
) -> torch.Tensor:
|
| 55 |
+
audio_features_from_empty_input = calculate_audio_features(None, model)
|
| 56 |
+
average_logprobs = calculate_average_logprobs(
|
| 57 |
+
model=model,
|
| 58 |
+
audio_features=audio_features_from_empty_input,
|
| 59 |
+
class_names=class_names,
|
| 60 |
+
tokenizer=tokenizer,
|
| 61 |
+
)
|
| 62 |
+
if verbose:
|
| 63 |
+
print("Internal LM average log probabilities for each class:")
|
| 64 |
+
for i, class_name in enumerate(class_names):
|
| 65 |
+
print(f" {class_name}: {average_logprobs[i]:.3f}")
|
| 66 |
+
return average_logprobs
|
data/(chirping_birds)1-34495-A-14.wav
ADDED
|
Binary file (441 kB). View file
|
|
|
data/(clock_tick)1-21934-A-38.wav
ADDED
|
Binary file (441 kB). View file
|
|
|
data/(dog)1-100032-A-0.wav
ADDED
|
Binary file (441 kB). View file
|
|
|
data/(helicopter)1-181071-A-40.wav
ADDED
|
Binary file (441 kB). View file
|
|
|
data/(laughing)1-1791-A-26.wav
ADDED
|
Binary file (441 kB). View file
|
|
|
packages.txt
ADDED
|
@@ -0,0 +1 @@
|
|
|
|
|
|
|
| 1 |
+
ffmpeg
|
requirements.txt
ADDED
|
@@ -0,0 +1,2 @@
|
|
|
|
|
|
|
|
|
|
| 1 |
+
git+https://github.com/openai/whisper.git
|
| 2 |
+
tqdm
|