Boni98 commited on
Commit
05bb94d
·
1 Parent(s): 37292b0

Delete clip_chat.py

Browse files
Files changed (1) hide show
  1. clip_chat.py +0 -71
clip_chat.py DELETED
@@ -1,71 +0,0 @@
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
- with open('coco_paths.txt', 'r') as file:
12
- COCO = file.read().split('\n')[:-1]
13
- COCO = [c.replace("/media/bonilla/My Book/coco/train2017/", "D:\\coco\\train2017\\") for c in COCO]
14
-
15
-
16
- def load_random_image():
17
- image_path = choice(COCO)
18
- image = Image.open(image_path)
19
- return image
20
-
21
-
22
- def next_image():
23
- global image_org, image
24
- image_org = load_random_image()
25
- image = preprocess(Image.fromarray(image_org)).unsqueeze(0).to(device)
26
-
27
-
28
- last = -1
29
- best = -1
30
-
31
- goal = 21
32
-
33
- image_org = load_random_image()
34
- image = preprocess(image_org).unsqueeze(0).to(device)
35
-
36
-
37
- def answer(message):
38
- global last, best
39
-
40
- text = clip.tokenize([message]).to(device)
41
-
42
- with torch.no_grad():
43
- logits_per_image, _ = model(image, text)
44
- logits = logits_per_image.cpu().numpy().flatten()[0]
45
-
46
- if last == -1:
47
- is_better = -1
48
- elif last > logits:
49
- is_better = 0
50
- elif last < logits:
51
- is_better = 1
52
- elif logits > goal:
53
- is_better = 2
54
- else:
55
- is_better = -1
56
-
57
- last = logits
58
- if logits > best:
59
- best = logits
60
- is_better = 3
61
-
62
- return logits, is_better # logit2sentence(logits) + " " + is_better + " " + f"({logits})"
63
-
64
-
65
- def reset_everything():
66
- global last, best, goal, image, image_org
67
- last = -1
68
- best = -1
69
- goal = 21
70
- image_org = load_random_image()
71
- image = preprocess(image_org).unsqueeze(0).to(device)