import gradio as gr import pickle, os import pandas as pd import numpy as np import os from transformers import CLIPProcessor, CLIPModel from datasets import load_dataset from PIL import Image import requests from io import BytesIO model = CLIPModel.from_pretrained("patrickjohncyh/fashion-clip") processor = CLIPProcessor.from_pretrained("patrickjohncyh/fashion-clip") # hf_token = os.environ.get("HF_API_TOKEN") # dataset = load_dataset('pratyush19/cyborg', use_auth_token=hf_token, split='train') # dir_path = "train/" # print (dataset) # print (dataset[0].keys()) with open('valid_images_sample.pkl','rb') as f: valid_images = pickle.load(f) with open('image_encodings_sample.pkl','rb') as f: image_encodings = pickle.load(f) valid_images = np.array(valid_images) with open('PIL_images.pkl','rb') as f: PIL_images = pickle.load(f) def softmax(x): e_x = np.exp(x - np.max(x, axis=1, keepdims=True)) return e_x / e_x.sum(axis=1, keepdims=True) def find_similar_images(caption, image_encodings): inputs = processor(text=[caption], return_tensors="pt") text_features = model.get_text_features(**inputs) text_features = text_features.detach().numpy() logits_per_image = softmax(np.dot(text_features, image_encodings.T)) return logits_per_image def find_relevant_images(caption): similarity_scores = find_similar_images(caption, image_encodings)[0] top_indices = np.argsort(similarity_scores)[::-1][:16] # top_path = valid_images[top_indices] images = [] for idx in top_indices: images.append(PIL_images[idx]) return images def gradio_interface(input_text): # with open("user_inputs.txt", "a") as file: # file.write(input_text + "\n") images = find_relevant_images(input_text) return images def clear_inputs(): return [None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None] outputs = [None]*16 with gr.Blocks(title="MirrAI") as demo: gr.Markdown("