Spaces:
Runtime error
Runtime error
File size: 4,727 Bytes
cd10cf2 9a9792d cd10cf2 9a9792d cd10cf2 a48bf14 07ae18d a48bf14 b68d4a2 41332b4 ecf0e06 a48bf14 b68d4a2 a48bf14 9a9792d cd10cf2 7233a34 e91f526 cd10cf2 e91f526 a48bf14 cd10cf2 e91f526 cd10cf2 e91f526 cd10cf2 7233a34 e91f526 7233a34 cd10cf2 7233a34 cd10cf2 |
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 |
import data
import torch
import gradio as gr
from models import imagebind_model
from models.imagebind_model import ModalityType
device = "cuda:0" if torch.cuda.is_available() else "cpu"
model = imagebind_model.imagebind_huge(pretrained=True)
model.eval()
model.to(device)
def image_text_zeroshot(image, text_list):
image_paths = [image]
labels = [label.strip(" ") for label in text_list.strip(" ").split("|")]
inputs = {
ModalityType.TEXT: data.load_and_transform_text(labels, device),
ModalityType.VISION: data.load_and_transform_vision_data(image_paths, device),
}
with torch.no_grad():
embeddings = model(inputs)
scores = (
torch.softmax(
embeddings[ModalityType.VISION] @ embeddings[ModalityType.TEXT].T, dim=-1
)
.squeeze(0)
.tolist()
)
score_dict = {label: score for label, score in zip(labels, scores)}
return score_dict
def audio_text_zeroshot(audio, text_list):
audio_paths = [audio]
labels = [label.strip(" ") for label in text_list.strip(" ").split("|")]
inputs = {
ModalityType.TEXT: data.load_and_transform_text(labels, device),
ModalityType.AUDIO: data.load_and_transform_audio_data(audio_paths, device),
}
with torch.no_grad():
embeddings = model(inputs)
scores = (
torch.softmax(
embeddings[ModalityType.AUDIO] @ embeddings[ModalityType.TEXT].T, dim=-1
)
.squeeze(0)
.tolist()
)
score_dict = {label: score for label, score in zip(labels, scores)}
return score_dict
def video_text_zeroshot(image, text_list):
image_paths = [image]
labels = [label.strip(" ") for label in text_list.strip(" ").split("|")]
inputs = {
ModalityType.TEXT: data.load_and_transform_text(labels, device),
ModalityType.VISION: data.load_and_transform_vision_data(image_paths, device),
}
with torch.no_grad():
embeddings = model(inputs)
scores = (
torch.softmax(
embeddings[ModalityType.VISION] @ embeddings[ModalityType.TEXT].T, dim=-1
)
.squeeze(0)
.tolist()
)
score_dict = {label: score for label, score in zip(labels, scores)}
return score_dict
def doubleimage_text_zeroshot(image, image2, text_list):
image_paths = [image, image2]
labels = [label.strip(" ") for label in text_list.strip(" ").split("|")]
inputs = {
ModalityType.TEXT: data.load_and_transform_text(labels, device),
ModalityType.VISION: data.load_and_transform_vision_data(image_paths, device),
}
with torch.no_grad():
embeddings = model(inputs)
scores = (
torch.softmax(
embeddings[ModalityType.VISION] @ embeddings[ModalityType.TEXT].T, dim=-1
)
.squeeze(0)
.tolist()
)
score_dict = {label: score for label, score in zip(labels, scores)}
return score_dict
def doubleimage_text_zeroshotOLD(image, image2, text_list):
image_paths = [image, image2]
labels = [label.strip(" ") for label in text_list.strip(" ").split("|")]
inputs = {
ModalityType.TEXT: data.load_and_transform_text(labels, device),
ModalityType.VISION: data.load_and_transform_vision_data(image_paths, device),
}
with torch.no_grad():
embeddings = model(inputs)
return str(torch.softmax(embeddings[ModalityType.VISION] @ embeddings[ModalityType.TEXT].T, dim=-1) )
def inference(
task,
text_list=None,
image=None,
audio=None,
image2=None,
):
if task == "image-text":
result = image_text_zeroshot(image, text_list)
elif task == "audio-text":
result = audio_text_zeroshot(audio, text_list)
elif task == "embeddings":
result = doubleimage_text_zeroshot(image, image2, text_list)
else:
raise NotImplementedError
return result
def main():
inputs = [
gr.inputs.Radio(
choices=[
"image-text",
"audio-text",
"embeddings",
],
type="value",
default="embeddings",
label="Task",
),
gr.inputs.Textbox(lines=1, label="Candidate texts"),
gr.inputs.Image(type="filepath", label="Input image"),
gr.inputs.Audio(type="filepath", label="Input audio"),
gr.inputs.Image(type="filepath", label="Input image2"),
]
iface = gr.Interface(
inference,
inputs,
"label",
title="Multimodal AI assitive agents for Learning Disorders : Demo with embeddings of ImageBind: ",
)
iface.launch()
if __name__ == "__main__":
main()
|