Spaces:
Runtime error
Runtime error
import pandas as pd, numpy as np | |
import os | |
from transformers import CLIPProcessor, CLIPTextModel, CLIPModel | |
import gradio as gr | |
model = CLIPModel.from_pretrained("openai/clip-vit-base-patch32") | |
processor = CLIPProcessor.from_pretrained("openai/clip-vit-base-patch32") | |
df = {0: pd.read_csv('data.csv'), 1: pd.read_csv('data2.csv')} | |
embeddings = {0: np.load('embeddings2.npy'), 1: np.load('embeddings.npy')} | |
for k in [0, 1]: | |
embeddings[k] = np.divide(embeddings[k], np.sqrt(np.sum(embeddings[k]**2, axis=1, keepdims=True))) | |
def compute_text_embeddings(list_of_strings): | |
inputs = processor(text=list_of_strings, return_tensors="pt", padding=True) | |
return model.get_text_features(**inputs) | |
def compute(query): | |
corpus = 'Unsplash' | |
n_results=1 | |
text_embeddings = compute_text_embeddings([query]).detach().numpy() | |
k = 0 if corpus == 'Unsplash' else 1 | |
results = np.argsort((embeddings[k]@text_embeddings.T)[:, 0])[-1:-n_results-1:-1] | |
paths = [df[k].iloc[i]['path'] for i in results] | |
print(paths) | |
return paths | |
title = "Draw to Search" | |
iface = gr.Interface( | |
fn=predict, | |
inputs=[gr.inputs.Textbox(label="text", lines=3)], | |
outputs='text', | |
title=title, | |
examples=[["Sunset"]] | |
) | |
iface.launch(debug=True) | |