|
|
|
import gradio as gr |
|
import clip,torch |
|
import requests |
|
from PIL import Image |
|
import numpy as np |
|
import torch |
|
import torch.nn as nn |
|
from io import BytesIO |
|
import urllib.request |
|
|
|
|
|
|
|
|
|
from selenium import webdriver |
|
from selenium.webdriver.common.by import By |
|
|
|
|
|
def test2(): |
|
driver = webdriver.Chrome() |
|
|
|
driver.get('https://www.hiphoper.com/') |
|
|
|
imgs = driver.find_elements(By.CSS_SELECTOR,'img.card__image') |
|
result = [] |
|
|
|
for img in imgs: |
|
|
|
result.append(img.get_attribute('src')) |
|
|
|
driver.quit() |
|
|
|
return result |
|
|
|
|
|
def similarity(v1,v2,type=0): |
|
if type ==0: |
|
v1_norm = np.linalg.norm(v1) |
|
v2_norm = np.linalg.norm(v2) |
|
|
|
return np.dot(v1,v2)/(v1_norm*v2_norm) |
|
else: |
|
return np.sqrt(np.sum((v1-v2)**2)) |
|
|
|
|
|
def democlip(url ,texts): |
|
|
|
if url =='': |
|
print('SYSTEM : alternative url') |
|
url = 'https://i.pinimg.com/564x/47/b5/5d/47b55de6f168db65cf46d7d1f0451b64.jpg' |
|
else: |
|
print('SYSTEM : URL progressed') |
|
|
|
if texts =='': |
|
texts ='black desk room girl flower' |
|
else: |
|
print('SYSTEM : TEXT progressed') |
|
|
|
response = requests.get(url) |
|
image_bytes = response.content |
|
texts = list(texts.split(' ')) |
|
|
|
"""Gets the embedding values for the image.""" |
|
device = "cuda" if torch.cuda.is_available() else "cpu" |
|
model, preprocess = clip.load("ViT-B/32", device=device) |
|
|
|
|
|
text_token = clip.tokenize(texts).to(device) |
|
image = preprocess(Image.open(BytesIO(image_bytes))).unsqueeze(0).to(device) |
|
|
|
with torch.no_grad(): |
|
image_features = model.encode_image(image) |
|
text_features = model.encode_text(text_token) |
|
|
|
logits_per_image, logits_per_text = model(image,text_token) |
|
probs = logits_per_image.softmax(dim=-1).cpu().numpy() |
|
|
|
word_dict = {'image':{},'text':{}} |
|
|
|
|
|
for i,text in enumerate(texts): |
|
word_dict['text'][text] = text_features[i].cpu().numpy() |
|
|
|
|
|
for i,img in enumerate(image): |
|
word_dict['image'][img] = image_features[i].cpu().numpy() |
|
|
|
|
|
|
|
tu,ts,tv = torch.pca_lowrank(text_features,center=True) |
|
|
|
text_pca = torch.matmul(text_features,tv[:,:3]) |
|
|
|
|
|
imgu,imgs,imgv = torch.pca_lowrank(image_features,center=True) |
|
|
|
image_pca = torch.matmul(image_features,imgv[:,:3]) |
|
|
|
|
|
print(text_pca.shape,image_pca.shape) |
|
return text_pca,image_pca |
|
|
|
|
|
|
|
def PCA(img_emb, text_emb,n_components = 3): |
|
x = torch.tensor([[1.,2.,3.,7.],[4.,5.,3.,6.],[7.,9.,8.,9.],[11.,13.,17.,11.]]) |
|
|
|
|
|
print(x.shape) |
|
u,s,v = torch.pca_lowrank(x,q=None, center=False,niter=2) |
|
|
|
u.shape,s.shape,v.shape |
|
|
|
u@torch.diag(s)@v.T |
|
|
|
|
|
pass |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
demo = gr.Interface( |
|
fn=democlip, |
|
|
|
inputs = ['text',gr.Textbox(label='input prediction')], |
|
|
|
outputs = [gr.Textbox(label='text pca Box'),gr.Textbox(label='image pca Box')] |
|
) |
|
demo.launch() |