Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,52 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import numpy as np
|
| 2 |
+
import clip
|
| 3 |
+
import torch
|
| 4 |
+
import gradio as gr
|
| 5 |
+
from PIL import Image
|
| 6 |
+
import os
|
| 7 |
+
import base64
|
| 8 |
+
from io import BytesIO
|
| 9 |
+
|
| 10 |
+
# Load the CLIP model
|
| 11 |
+
model, preprocess = clip.load("ViT-B/32")
|
| 12 |
+
device = "cuda" if torch.cuda.is_available() else "cpu"
|
| 13 |
+
model.to(device).eval()
|
| 14 |
+
|
| 15 |
+
# Define the Business Listing variable
|
| 16 |
+
Business_Listing = "Air Guide"
|
| 17 |
+
|
| 18 |
+
def find_similarity(image_base64, text_input):
|
| 19 |
+
# Decode the base64 image string to bytes
|
| 20 |
+
image_bytes = base64.b64decode(image_base64)
|
| 21 |
+
image = Image.open(BytesIO(image_bytes))
|
| 22 |
+
|
| 23 |
+
# Preprocess the image
|
| 24 |
+
image = preprocess(image).unsqueeze(0).to(device)
|
| 25 |
+
|
| 26 |
+
# Prepare input text
|
| 27 |
+
text_tokens = clip.tokenize([text_input]).to(device)
|
| 28 |
+
|
| 29 |
+
# Encode image and text features
|
| 30 |
+
with torch.no_grad():
|
| 31 |
+
image_features = model.encode_image(image).float()
|
| 32 |
+
text_features = model.encode_text(text_tokens).float()
|
| 33 |
+
|
| 34 |
+
# Normalize features and calculate similarity
|
| 35 |
+
image_features /= image_features.norm(dim=-1, keepdim=True)
|
| 36 |
+
text_features /= text_features.norm(dim=-1, keepdim=True)
|
| 37 |
+
similarity = (text_features @ image_features.T).cpu().numpy()
|
| 38 |
+
|
| 39 |
+
return similarity[0, 0]
|
| 40 |
+
|
| 41 |
+
# Define a Gradio interface
|
| 42 |
+
iface = gr.Interface(
|
| 43 |
+
fn=find_similarity,
|
| 44 |
+
inputs=["text", gr.inputs.Textbox(lines=3, label="Enter Base64 Image"), "text"],
|
| 45 |
+
outputs="number",
|
| 46 |
+
live=True,
|
| 47 |
+
interpretation="default",
|
| 48 |
+
title="CLIP Model Image-Text Cosine Similarity",
|
| 49 |
+
description="Enter a base64-encoded image and text to find their cosine similarity.",
|
| 50 |
+
)
|
| 51 |
+
|
| 52 |
+
iface.launch()
|