Spaces:
Runtime error
Runtime error
Upload clip_chat.py
Browse files- clip_chat.py +72 -0
clip_chat.py
ADDED
@@ -0,0 +1,72 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import torch
|
2 |
+
import clip
|
3 |
+
from PIL import Image
|
4 |
+
import glob
|
5 |
+
import os
|
6 |
+
from random import choice
|
7 |
+
|
8 |
+
|
9 |
+
device = "cuda" if torch.cuda.is_available() else "cpu"
|
10 |
+
model, preprocess = clip.load("ViT-L/14@336px", device=device)
|
11 |
+
COCO = glob.glob(os.path.join(os.getcwd(), "train2017", "*"))
|
12 |
+
# with open('coco_paths.txt', 'r') as file:
|
13 |
+
# COCO = file.read().split('\n')[:-1]
|
14 |
+
# COCO = [c.replace("/media/bonilla/My Book/coco/train2017/", "D:\\coco\\train2017\\") for c in COCO]
|
15 |
+
|
16 |
+
|
17 |
+
def load_random_image():
|
18 |
+
image_path = choice(COCO)
|
19 |
+
image = Image.open(image_path)
|
20 |
+
return image
|
21 |
+
|
22 |
+
|
23 |
+
def next_image():
|
24 |
+
global image_org, image
|
25 |
+
image_org = load_random_image()
|
26 |
+
image = preprocess(Image.fromarray(image_org)).unsqueeze(0).to(device)
|
27 |
+
|
28 |
+
|
29 |
+
last = -1
|
30 |
+
best = -1
|
31 |
+
|
32 |
+
goal = 21
|
33 |
+
|
34 |
+
image_org = load_random_image()
|
35 |
+
image = preprocess(image_org).unsqueeze(0).to(device)
|
36 |
+
|
37 |
+
|
38 |
+
def answer(message):
|
39 |
+
global last, best
|
40 |
+
|
41 |
+
text = clip.tokenize([message]).to(device)
|
42 |
+
|
43 |
+
with torch.no_grad():
|
44 |
+
logits_per_image, _ = model(image, text)
|
45 |
+
logits = logits_per_image.cpu().numpy().flatten()[0]
|
46 |
+
|
47 |
+
if last == -1:
|
48 |
+
is_better = -1
|
49 |
+
elif last > logits:
|
50 |
+
is_better = 0
|
51 |
+
elif last < logits:
|
52 |
+
is_better = 1
|
53 |
+
elif logits > goal:
|
54 |
+
is_better = 2
|
55 |
+
else:
|
56 |
+
is_better = -1
|
57 |
+
|
58 |
+
last = logits
|
59 |
+
if logits > best:
|
60 |
+
best = logits
|
61 |
+
is_better = 3
|
62 |
+
|
63 |
+
return logits, is_better # logit2sentence(logits) + " " + is_better + " " + f"({logits})"
|
64 |
+
|
65 |
+
|
66 |
+
def reset_everything():
|
67 |
+
global last, best, goal, image, image_org
|
68 |
+
last = -1
|
69 |
+
best = -1
|
70 |
+
goal = 21
|
71 |
+
image_org = load_random_image()
|
72 |
+
image = preprocess(image_org).unsqueeze(0).to(device)
|