Ryan Ramos commited on
Commit
74b23ca
·
1 Parent(s): dca9845

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +29 -0
app.py ADDED
@@ -0,0 +1,29 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from transformers import AutoProcessor, AutoModel
2
+ import torch
3
+ import gradio as gr
4
+
5
+ processor = AutoProcessor.from_pretrained("laion/CLIP-ViT-B-32-laion2B-s34B-b79K")
6
+
7
+ model = AutoModel.from_pretrained("laion/CLIP-ViT-B-32-laion2B-s34B-b79K")
8
+
9
+ labels = [f'a {emotion} face' for emotion in ['sad', 'happy', 'scared', 'angry', 'surprised', 'disgusted', 'neutral']]
10
+
11
+ text_inputs = processor(text=labels, return_tensors='pt')
12
+ text_embeds = model.get_text_features(**text_inputs)
13
+ text_embeds /= text_embeds.norm(p=2, dim=-1, keepdim=True)
14
+
15
+ def classify(image):
16
+ image_inputs = processor(images=image, return_tensors='pt')
17
+ image_embeds = model.get_image_features(**image_inputs)
18
+ image_embeds /= image_embeds.norm(p=2, dim=-1, keepdim=True)
19
+ logits_per_image = torch.matmul(image_embeds, text_embeds.t()) * model.logit_scale.exp()
20
+ probs = logits_per_image.softmax(dim=1).squeeze(0).tolist()
21
+ return dict(zip(labels, probs))
22
+
23
+ demo = gr.Interface(
24
+ fn=classify,
25
+ inputs=gr.Image(type='pil', source='webcam', streaming=True),
26
+ outputs=gr.Label()
27
+ )
28
+
29
+ demo.launch()