Spaces:
Build error
Build error
File size: 5,363 Bytes
33f1db4 0173bf0 33f1db4 c9e7d7d 33f1db4 0173bf0 33f1db4 0173bf0 33f1db4 c9e7d7d 33f1db4 0173bf0 33f1db4 0173bf0 33f1db4 0fe2fde 33f1db4 952556c 33f1db4 0173bf0 33f1db4 0173bf0 33f1db4 0173bf0 33f1db4 0173bf0 33f1db4 0173bf0 33f1db4 0173bf0 33f1db4 0173bf0 33f1db4 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 |
import os
os.system('cd TimeSformer;'
'pip install .; cd ..')
os.system('ls -l')
os.system('pwd')
import os, sys
sys.path.append("/home/user/app/TimeSformer/")
import timesformer
import torch
from torchvision import transforms
from transformers import AutoTokenizer
from PIL import Image
import json
import os
from torchvision import transforms
from models.epalm import ePALM
import os
from transformers import AutoTokenizer
# import ruamel_yaml as yaml
from ruamel.yaml import YAML
import torch
import gradio as gr
import torchaudio
yaml=YAML(typ='safe')
use_cuda = torch.cuda.is_available()
device = torch.device('cuda') if use_cuda else torch.device('cpu')
device_type = 'cuda' if use_cuda else 'cpu'
## Load model
### Captioning
config = 'configs/audio/ePALM_audio_caption.yaml'
config = yaml.load(open(config, 'r'))
text_model = 'facebook/opt-2.7b'
vision_model_name = 'ast'
start_layer_idx = 19
end_layer_idx = 31
low_cpu = True
MODEL = ePALM(opt_model_name=text_model,
vision_model_name=vision_model_name,
use_vis_prefix=True,
start_layer_idx=start_layer_idx,
end_layer_idx=end_layer_idx,
return_hidden_state_vision=True,
config=config,
low_cpu=low_cpu
)
print("Model Built")
MODEL.to(device)
checkpoint_path = 'checkpoints/float32/ePALM_audio_caption/checkpoint_best.pth'
checkpoint = torch.load(checkpoint_path, map_location='cpu')
state_dict = checkpoint['model']
msg = MODEL.load_state_dict(state_dict,strict=False)
MODEL.bfloat16()
## Load tokenizer
tokenizer = AutoTokenizer.from_pretrained(text_model, use_fast=False)
eos_token = tokenizer.eos_token
pad_token = tokenizer.pad_token
special_answer_token = '</a>'
special_tokens_dict = {'additional_special_tokens': [special_answer_token]}
tokenizer.add_special_tokens(special_tokens_dict)
def read_audio(path):
melbins = 128
target_length = 1024
skip_norm = False
norm_mean = -4.2677393
norm_std = 4.5689974
waveform, sr = torchaudio.load(path)
waveform = waveform - waveform.mean()
# audio
fbank = torchaudio.compliance.kaldi.fbank(waveform, htk_compat=True, sample_frequency=sr, use_energy=False,
window_type='hanning', num_mel_bins=melbins, dither=0.0,
frame_shift=10)
n_frames = fbank.shape[0]
p = target_length - n_frames
# cut and pad
if p > 0:
m = torch.nn.ZeroPad2d((0, 0, 0, p))
fbank = m(fbank)
elif p < 0:
fbank = fbank[0:target_length, :]
# SpecAug, not do for eval set
fbank = torch.transpose(fbank, 0, 1)
# this is just to satisfy new torchaudio version, which only accept [1, freq, time]
fbank = fbank.unsqueeze(0)
# squeeze it back, it is just a trick to satisfy new torchaudio version
fbank = fbank.squeeze(0)
fbank = torch.transpose(fbank, 0, 1)
# normalize the input for both training and test
if not skip_norm:
fbank = (fbank - norm_mean) / (norm_std * 2)
# skip normalization the input if you are trying to get the normalization stats.
else:
pass
audio = fbank.unsqueeze(0)
return audio
do_sample=False
num_beams=5
max_length=30
def inference(image, task_type):
if task_type == 'Audio Captioning':
text = ['']
text_input = tokenizer(text, padding='longest', return_tensors="pt").to(device)
model = MODEL
else:
raise NotImplemented
image = read_audio(image)
with torch.autocast(device_type=device_type, dtype=torch.bfloat16, enabled=True):
out = model(image=image, text=text_input, mode='generate', return_dict=True, max_length=max_length,
do_sample=do_sample, num_beams=num_beams)
if 'Captioning' in task_type:
for i, o in enumerate(out):
res = tokenizer.decode(o)
response = res.split('</s>')[1].replace(pad_token, '').replace('</s>', '').replace(eos_token, '') # skip_special_tokens=True
else:
for o in out:
o_list = o.tolist()
response = tokenizer.decode(o_list).split(special_answer_token)[1].replace(pad_token, '').replace('</s>', '').replace(eos_token, '') # skip_special_tokens=True
return response
inputs = [gr.Audio(source="upload", type="filepath"), gr.inputs.Radio(choices=['Audio Captioning'], type="value", default="Image Captioning", label="Task")]
outputs = ['text']
examples = [
['examples/audios/6cS0FsUM-cQ.wav', 'Audio Captioning', None],
['examples/audios/AJtNitYMa1I.wav', 'Audio Captioning', None],
]
title = "eP-ALM for Audio-Text tasks"
description = "Gradio Demo for eP-ALM. For this demo, we use 2.7B OPT. As the model runs on CPUs and float16 mixed precision is not supported on CPUs, the generation can take up to 2 mins."
article = "<p style='text-align: center'><a href='https://arxiv.org/abs/2303.11403' target='_blank'>Paper</a> | <a href='https://github.com/mshukor/eP-ALM' target='_blank'>Github Repo</a></p>"
io = gr.Interface(fn=inference, inputs=inputs, outputs=outputs,
title=title, description=description, article=article, examples=examples, cache_examples=False)
io.launch() |