Spaces:
Sleeping
Sleeping
Blackeyes0u0
commited on
Commit
•
b019de7
1
Parent(s):
f3cb8c4
app update
Browse files- app.py +129 -5
- requirements.txt +17 -0
app.py
CHANGED
@@ -1,9 +1,133 @@
|
|
1 |
-
#app.py
|
2 |
|
3 |
import gradio as gr
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
4 |
|
5 |
-
|
6 |
-
|
7 |
|
8 |
-
|
9 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
|
2 |
import gradio as gr
|
3 |
+
import clip,torch
|
4 |
+
import requests
|
5 |
+
from PIL import Image
|
6 |
+
import numpy as np
|
7 |
+
import torch
|
8 |
+
import torch.nn as nn
|
9 |
+
from io import BytesIO
|
10 |
+
import urllib.request
|
11 |
|
12 |
+
# https://hhp-item-resource.s3.ap-northeast-2.amazonaws.com/magazine-resource/magazine/20221017154717/jin._s2.png
|
13 |
+
# girl bag skirt eye beauty pretty
|
14 |
|
15 |
+
from selenium import webdriver
|
16 |
+
from selenium.webdriver.common.by import By
|
17 |
+
|
18 |
+
|
19 |
+
def 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')) #이미지 주소를 print
|
29 |
+
result.append(img.get_attribute('src')) #이미지 src만 모아서 리스트에 저장
|
30 |
+
|
31 |
+
driver.quit()
|
32 |
+
|
33 |
+
return result
|
34 |
+
|
35 |
+
|
36 |
+
def 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 |
+
|
46 |
+
def 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.content
|
61 |
+
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)s
|
68 |
+
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 text
|
90 |
+
tu,ts,tv = torch.pca_lowrank(text_features,center=True)
|
91 |
+
|
92 |
+
text_pca = torch.matmul(text_features,tv[:,:3])
|
93 |
+
|
94 |
+
### pca of image
|
95 |
+
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_pca
|
102 |
+
|
103 |
+
|
104 |
+
|
105 |
+
def 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.shape
|
113 |
+
|
114 |
+
u@torch.diag(s)@v.T
|
115 |
+
|
116 |
+
# torch.matmul(x,v[:,:3])
|
117 |
+
pass
|
118 |
+
|
119 |
+
|
120 |
+
|
121 |
+
# NODE type
|
122 |
+
|
123 |
+
# PCA type.
|
124 |
+
|
125 |
+
# ELSE type.
|
126 |
+
demo = gr.Interface(
|
127 |
+
fn=democlip,
|
128 |
+
# inputs = [gr.Image(),gr.Textbox(lable='input prediction')],
|
129 |
+
inputs = ['text',gr.Textbox(lable='input prediction')],
|
130 |
+
# outputs='label'
|
131 |
+
outputs = [gr.Textbox(label='text pca Box'),gr.Textbox(label='image pca Box')]
|
132 |
+
)
|
133 |
+
demo.launch()
|
requirements.txt
ADDED
@@ -0,0 +1,17 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
#requirements.txt
|
2 |
+
|
3 |
+
transformers
|
4 |
+
torch
|
5 |
+
|
6 |
+
|
7 |
+
peft
|
8 |
+
loralib
|
9 |
+
numpy
|
10 |
+
pandas
|
11 |
+
|
12 |
+
tqdm
|
13 |
+
torchvision
|
14 |
+
|
15 |
+
selenium
|
16 |
+
clip
|
17 |
+
|