Update app.py
Browse files
app.py
CHANGED
|
@@ -8,10 +8,11 @@ model, preprocess = clip.load("ViT-B/32", device=device)
|
|
| 8 |
|
| 9 |
def process_image_and_text(image, text):
|
| 10 |
|
| 11 |
-
|
|
|
|
| 12 |
image = preprocess(image).unsqueeze(0).to(device)
|
| 13 |
|
| 14 |
-
text_tokens = clip.tokenize(
|
| 15 |
|
| 16 |
with torch.no_grad():
|
| 17 |
image_features = model.encode_image(image)
|
|
@@ -20,7 +21,7 @@ def process_image_and_text(image, text):
|
|
| 20 |
logits_per_image, logits_per_text = model(image, text_tokens)
|
| 21 |
probs = logits_per_image.softmax(dim=-1)
|
| 22 |
|
| 23 |
-
return probs
|
| 24 |
|
| 25 |
-
demo = gr.Interface(fn=process_image_and_text, inputs=[
|
| 26 |
-
demo.launch()
|
|
|
|
| 8 |
|
| 9 |
def process_image_and_text(image, text):
|
| 10 |
|
| 11 |
+
text = text.split(",")
|
| 12 |
+
image = Image.fromarray(image)
|
| 13 |
image = preprocess(image).unsqueeze(0).to(device)
|
| 14 |
|
| 15 |
+
text_tokens = clip.tokenize(text).to(device)
|
| 16 |
|
| 17 |
with torch.no_grad():
|
| 18 |
image_features = model.encode_image(image)
|
|
|
|
| 21 |
logits_per_image, logits_per_text = model(image, text_tokens)
|
| 22 |
probs = logits_per_image.softmax(dim=-1)
|
| 23 |
|
| 24 |
+
return probs.cpu().numpy()[0]
|
| 25 |
|
| 26 |
+
demo = gr.Interface(fn=process_image_and_text, inputs=['image', 'text'], outputs="text")
|
| 27 |
+
demo.launch()
|