evelyncsb commited on
Commit
f457390
·
1 Parent(s): 184672c

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +51 -0
app.py ADDED
@@ -0,0 +1,51 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ import data
3
+ import torch
4
+ import gradio as gr
5
+ from models import imagebind_model
6
+ from models.imagebind_model import ModalityType
7
+
8
+
9
+ device = "cuda:0" if torch.cuda.is_available() else "cpu"
10
+ model = imagebind_model.imagebind_huge(pretrained=True)
11
+ model.eval()
12
+ model.to(device)
13
+
14
+ def image_text_zeroshot(image, text_list):
15
+ image_paths = [image]
16
+ labels = [label.strip(" ") for label in text_list.strip(" ").split("|")]
17
+ inputs = {
18
+ ModalityType.TEXT: data.load_and_transform_text(labels, device),
19
+ ModalityType.VISION: data.load_and_transform_vision_data(image_paths, device),
20
+ }
21
+
22
+ with torch.no_grad():
23
+ embeddings = model(inputs)
24
+
25
+ scores = (
26
+ torch.softmax(
27
+ embeddings[ModalityType.VISION] @ embeddings[ModalityType.TEXT].T, dim=-1
28
+ )
29
+ .squeeze(0)
30
+ .tolist()
31
+ )
32
+
33
+ score_dict = {label: score for label, score in zip(labels, scores)}
34
+
35
+ return score_dict
36
+
37
+ def main():
38
+ inputs = [
39
+ gr.inputs.Textbox(lines=1, label="texts"),
40
+ gr.inputs.Image(type="filepath", label="Input image")
41
+ ]
42
+
43
+ iface = gr.Interface(
44
+ image_text_zeroshot(image, text_list),
45
+ inputs,
46
+ "label",
47
+ description="""...""",
48
+ title="ImageBind",
49
+ )
50
+
51
+ iface.launch()