Vidushee commited on
Commit
427bb9a
1 Parent(s): bb2fcae

Upload 2 files

Browse files
Files changed (2) hide show
  1. Align_myself.py +40 -0
  2. requirements.txt +3 -0
Align_myself.py ADDED
@@ -0,0 +1,40 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import requests
2
+ import torch
3
+ from PIL import Image
4
+ from transformers import AlignProcessor, AlignModel
5
+ import gradio as gr
6
+
7
+ processor = AlignProcessor.from_pretrained("kakaobrain/align-base")
8
+ model = AlignModel.from_pretrained("kakaobrain/align-base")
9
+
10
+
11
+ def get_image_alignment_probabilities(url, is_url):
12
+ candidate_labels = ["advertisement", "not an advertisement"]
13
+
14
+ # Load image from URL
15
+ if is_url:
16
+ image = Image.open(requests.get(url, stream=True).raw).convert("RGB")
17
+ else:
18
+ image = Image.open(url).convert("RGB")
19
+
20
+ # Process inputs
21
+ inputs = processor(text=candidate_labels, images=image, return_tensors="pt")
22
+
23
+ # Compute outputs
24
+ with torch.no_grad():
25
+ outputs = model(**inputs)
26
+
27
+ # Extract logits per image
28
+ logits_per_image = outputs.logits_per_image
29
+
30
+ # Compute label probabilities using softmax
31
+ probs = logits_per_image.softmax(dim=1)
32
+
33
+ return {label: prob.item() for label, prob in zip(candidate_labels, probs[0])}
34
+
35
+
36
+ iface = gr.Interface(fn=get_image_alignment_probabilities,
37
+ inputs=["text", "checkbox"],
38
+ outputs="label")
39
+ iface.launch()
40
+
requirements.txt ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ torch
2
+ transformers
3
+ gradio