Steven Anderson commited on
Commit
3153b84
1 Parent(s): a95e33d
Files changed (1) hide show
  1. app.py +36 -4
app.py CHANGED
@@ -1,7 +1,39 @@
1
  import gradio as gr
 
 
 
2
 
3
- def greet(name):
4
- return "Hello " + name + "!!"
5
 
6
- iface = gr.Interface(fn=greet, inputs="text", outputs="text")
7
- iface.launch()
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
  import gradio as gr
2
+ import torch
3
+ import clip
4
+ from PIL import Image
5
 
 
 
6
 
7
+ def process(device, image, prompt):
8
+ print("Inferring...")
9
+ image = preprocess(image).unsqueeze(0).to(device)
10
+ print(image)
11
+
12
+ prompts = prompt.split("\n")
13
+ text = clip.tokenize(prompts).to(device)
14
+ print(text)
15
+
16
+ with torch.no_grad():
17
+ logits_per_image, logits_per_text = model(image, text)
18
+ probs = logits_per_image.softmax(dim=-1).cpu().numpy()
19
+ print(probs)
20
+
21
+ return dict(zip(prompts, probs[0]))
22
+
23
+
24
+ if __name__ == "__main__":
25
+ print("Getting device...")
26
+ device = "cuda" if torch.cuda.is_available() else "cpu"
27
+ print("Loading model...")
28
+ model, preprocess = clip.load("ViT-B/32", device=device)
29
+ print("Loaded model.")
30
+
31
+ iface = gr.Interface(
32
+ fn=lambda i, p: process(device, i, p),
33
+ inputs=[
34
+ gr.Image(),
35
+ gr.Textbox(lines=5, label="Prompts (newline-separated)"),
36
+ ],
37
+ outputs="label",
38
+ )
39
+ iface.launch()