wisternhunter/Custom_Youtube_Algorithm
0
1 2import gradio as gr3import clip,torch4import requests5from PIL import Image6import numpy as np7import torch8import torch.nn as nn9from io import BytesIO10import urllib.request11 12# https://hhp-item-resource.s3.ap-northeast-2.amazonaws.com/magazine-resource/magazine/20221017154717/jin._s2.png13# girl bag skirt eye beauty pretty14 15from selenium import webdriver16from selenium.webdriver.common.by import By17 18 19def test2():20 driver = webdriver.Chrome() #웹드라이버가 있는 경로에서 Chrome을 가져와 실행-> driver변수21 22 driver.get('https://www.hiphoper.com/') #driver변수를 이용해 원하는 url 접속23 24 imgs = driver.find_elements(By.CSS_SELECTOR,'img.card__image') #css selector를 이용해서 'tag이름.class명'의 순으로 인자를 전달25 result = [] #웹 태그에서 attribute 중 src만 담을 리스트26 27 for img in imgs: #모든 이미지들을 탐색28 # print(img.get_attribute('src')) #이미지 주소를 print29 result.append(img.get_attribute('src')) #이미지 src만 모아서 리스트에 저장30 31 driver.quit()32 33 return result34 35 36def similarity(v1,v2,type=0):37 if type ==0:38 v1_norm = np.linalg.norm(v1)39 v2_norm = np.linalg.norm(v2)40 41 return np.dot(v1,v2)/(v1_norm*v2_norm)42 else:43 return np.sqrt(np.sum((v1-v2)**2))44 45 46def democlip(url ,texts):47 48 if url =='':49 print('SYSTEM : alternative url')50 url = 'https://i.pinimg.com/564x/47/b5/5d/47b55de6f168db65cf46d7d1f0451b64.jpg'51 else:52 print('SYSTEM : URL progressed')53 54 if texts =='':55 texts ='black desk room girl flower'56 else:57 print('SYSTEM : TEXT progressed')58 59 response = requests.get(url)60 image_bytes = response.content61 texts = list(texts.split(' '))62 63 """Gets the embedding values for the image."""64 device = "cuda" if torch.cuda.is_available() else "cpu"65 model, preprocess = clip.load("ViT-B/32", device=device)66 67 # image = preprocess(Image.open("CLIP.png")).unsqueeze(0).to(device)s68 text_token = clip.tokenize(texts).to(device)69 image = preprocess(Image.open(BytesIO(image_bytes))).unsqueeze(0).to(device)70 71 with torch.no_grad():72 image_features = model.encode_image(image)73 text_features = model.encode_text(text_token)74 75 logits_per_image, logits_per_text = model(image,text_token)76 probs = logits_per_image.softmax(dim=-1).cpu().numpy()77 78 word_dict = {'image':{},'text':{}}79 80 ### text 81 for i,text in enumerate(texts):82 word_dict['text'][text] = text_features[i].cpu().numpy()83 84 ### iamge 85 for i,img in enumerate(image):86 word_dict['image'][img] = image_features[i].cpu().numpy()87 88 ###################### PCA of embeddings ######################## 89 ## pca of text90 tu,ts,tv = torch.pca_lowrank(text_features,center=True)91 92 text_pca = torch.matmul(text_features,tv[:,:3])93 94 ### pca of image95 imgu,imgs,imgv = torch.pca_lowrank(image_features,center=True)96 97 image_pca = torch.matmul(image_features,imgv[:,:3])98 99 # return word_dict 100 print(text_pca.shape,image_pca.shape)101 return text_pca,image_pca102 103 104 105def PCA(img_emb, text_emb,n_components = 3):106 x = torch.tensor([[1.,2.,3.,7.],[4.,5.,3.,6.],[7.,9.,8.,9.],[11.,13.,17.,11.]])107 # plz change data type to float or complex 108 109 print(x.shape)110 u,s,v = torch.pca_lowrank(x,q=None, center=False,niter=2)111 112 u.shape,s.shape,v.shape113 114 u@torch.diag(s)@v.T115 116 # torch.matmul(x,v[:,:3])117 pass118 119 120 121# NODE type122 123# PCA type.124 125# ELSE type. 126demo = gr.Interface(127 fn=democlip,128 # inputs = [gr.Image(),gr.Textbox(lable='input prediction')],129 inputs = ['text',gr.Textbox(label='input prediction')],130 # outputs='label'131 outputs = [gr.Textbox(label='text pca Box'),gr.Textbox(label='image pca Box')]132 )133demo.launch()