Spaces:
Sleeping
Sleeping
Commit
Β·
e6ad7fc
1
Parent(s):
7075496
added dedicated model
Browse files- requirements.txt +1 -0
- utils/inference.py +35 -7
requirements.txt
CHANGED
@@ -0,0 +1 @@
|
|
|
|
|
1 |
+
transformers
|
utils/inference.py
CHANGED
@@ -1,11 +1,39 @@
|
|
1 |
-
import requests
|
2 |
|
3 |
-
API_URL = "https://api-inference.huggingface.co/models/trpakov/vit-face-expression"
|
4 |
-
headers = {"Authorization": "Bearer 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"}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
5 |
|
6 |
|
7 |
def query(filename):
|
8 |
-
|
9 |
-
|
10 |
-
|
11 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
# import requests
|
2 |
|
3 |
+
# API_URL = "https://api-inference.huggingface.co/models/trpakov/vit-face-expression"
|
4 |
+
# headers = {"Authorization": "Bearer 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"}
|
5 |
+
|
6 |
+
|
7 |
+
# def query(filename):
|
8 |
+
# with open(filename, "rb") as f:
|
9 |
+
# data = f.read()
|
10 |
+
# response = requests.post(API_URL, headers=headers, data=data)
|
11 |
+
# return response.json()
|
12 |
+
|
13 |
+
|
14 |
+
from PIL import Image
|
15 |
+
from transformers import CLIPProcessor, CLIPModel
|
16 |
+
|
17 |
+
model = CLIPModel.from_pretrained("openai/clip-vit-large-patch14")
|
18 |
+
processor = CLIPProcessor.from_pretrained("openai/clip-vit-large-patch14")
|
19 |
|
20 |
|
21 |
def query(filename):
|
22 |
+
image = Image.open(filename)
|
23 |
+
inputs = processor(
|
24 |
+
text=["Happy", "Sad", "Surprised", "Angry", "Disgusted", "Neutral", "Fearful"],
|
25 |
+
images=image,
|
26 |
+
return_tensors="pt",
|
27 |
+
padding=True,
|
28 |
+
)
|
29 |
+
outputs = model(**inputs)
|
30 |
+
logits_per_image = outputs.logits_per_image
|
31 |
+
probs = logits_per_image.softmax(dim=1)
|
32 |
+
output = {
|
33 |
+
label: prob.item()
|
34 |
+
for label, prob in zip(
|
35 |
+
["Happy", "Sad", "Surprised", "Angry", "Disgusted", "Neutral", "Fearful"],
|
36 |
+
probs[0],
|
37 |
+
)
|
38 |
+
}
|
39 |
+
return output
|