Spaces:
Build error
Build error
Update app.py
Browse files8 unmodified lines910111213141516171823 unmodified lines42434445464748495051525354555657588 unmodified linesfrom transformers import CLIPProcessor, CLIPModelfrom datasets import load_datasetdef get_clip_embeddings(input_data, input_type='text'):# Load the CLIP model and processormodel = CLIPModel.from_pretrained("openai/clip-vit-base-patch32")processor = CLIPProcessor.from_pretrained("openai/clip-vit-base-patch32")# Prepare the input based on the typeif input_type == 'text':23 unmodified lineswith open('img_embeddings.pkl', 'rb') as file:img_embeddings = pickle.load(file)text = gr.Textbox(label = "Enter the text", 'Your text goes here')image = gr.Gallery()def get_similar_images(text, n = 4):if text:text_embedding = get_clip_embeddings(text, input_type='text')cos = nn.CosineSimilarity(dim=1, eps=1e-6)sims = cos(torch.tensor(text_embedding), torch.tensor(img_embeddings))top_n = np.argsort(np.array(sims))[::-1][:n]print(top_n)print(img_embeddings)imgs = []for index in top_n:8 unmodified lines9101112131415161718192023 unmodified lines444546474849505152535455565758598 unmodified linesfrom transformers import CLIPProcessor, CLIPModelfrom datasets import load_datasetmodel_checkpoint = "openai/clip-vit-base-patch32"def get_clip_embeddings(input_data, input_type='text'):# Load the CLIP model and processormodel = CLIPModel.from_pretrained(model_checkpoint)processor = CLIPProcessor.from_pretrained(model_checkpoint)# Prepare the input based on the typeif input_type == 'text':23 unmodified lineswith open('img_embeddings.pkl', 'rb') as file:img_embeddings = pickle.load(file)cos = nn.CosineSimilarity(dim=1, eps=1e-6)text = gr.Textbox(label = "Enter the description of the images you want to search for", placeholder='Your text goes here')image = gr.Gallery()def get_similar_images(text, n = 4):if text:text_embedding = get_clip_embeddings(text, input_type='text')sims = cos(torch.tensor(text_embedding), torch.tensor(img_embeddings))top_n = np.argsort(np.array(sims))[::-1][:n]imgs = []for index in top_n:
app.py+7-6