initial commit
Browse files- app.py +24 -0
- requirements.txt +2 -0
app.py
ADDED
|
@@ -0,0 +1,24 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
from transformers import CLIPProcessor, CLIPModel
|
| 3 |
+
|
| 4 |
+
model = CLIPModel.from_pretrained("openai/clip-vit-base-patch16")
|
| 5 |
+
processor = CLIPProcessor.from_pretrained("openai/clip-vit-base-patch16")
|
| 6 |
+
|
| 7 |
+
|
| 8 |
+
def calculate_score(image, text):
|
| 9 |
+
labels = text.split(';')
|
| 10 |
+
labels = [l.strip() for l in labels]
|
| 11 |
+
labels = list(filter(None, labels))
|
| 12 |
+
if len(labels) == 0 :
|
| 13 |
+
return dict()
|
| 14 |
+
inputs = processor(text=labels, images=image, return_tensors="pt", padding=True)
|
| 15 |
+
outputs = model(**inputs)
|
| 16 |
+
logits_per_image = outputs.logits_per_image.detach().numpy()
|
| 17 |
+
|
| 18 |
+
results_dict = {label:score/100.0 for label,score in zip(labels, logits_per_image[0])}
|
| 19 |
+
return results_dict
|
| 20 |
+
|
| 21 |
+
|
| 22 |
+
if __name__ == "__main__":
|
| 23 |
+
demo = gr.Interface(fn=calculate_score, inputs=["image", "text"], outputs="label")
|
| 24 |
+
demo.launch()
|
requirements.txt
ADDED
|
@@ -0,0 +1,2 @@
|
|
|
|
|
|
|
|
|
|
| 1 |
+
gradio
|
| 2 |
+
transformers
|