-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.py
65 lines (51 loc) · 1.88 KB
/
main.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
import os
from openai import OpenAI
from time import sleep
import openai
import base64
import requests
import numpy as np
from utils import *
api_keys = ['your api key']
counter = 0
factor = 5000 # request per day // 10
folder_name = "your/path/to/saved/images"
ind = get_ind(folder_name=folder_name)
for file_ind in ind:
if counter == len(api_keys)*factor:
print(f'{file_ind} has not been finished')
break
api_key = api_keys[counter//factor]
client = OpenAI(api_key = api_key)
folder_path = os.path.join(folder_name,f"{file_ind}")
if 'text.txt' in os.listdir(folder_path):
continue
else:
print(f'redoing {folder_path}')
image_pairs = find_image_pairs(folder_path)
image_pairs.sort()
result_file = generate_reranking(image_pairs,client)## generate reranking texts
reply = rerank(result_file,client)## generate reranking response
counter += 1
Top_K = np.zeros(3)
counter = 0
factor = 5000 # request per day // 10
for file_ind in ind:
api_key = api_keys[counter//factor]
client = OpenAI(api_key = api_key)
folder_path = os.path.join(folder_name,f"{file_ind}")
image_pairs = find_image_pairs(folder_path)
if 'rerank.txt' in os.listdir(folder_path):
with open(os.path.join(folder_path,'rerank.txt'),encoding='utf-8') as f:
reply = f.read()
ranking = from_text_to_rank(reply,client,0.1)## from text response to ranking
orig_topk = Top_K.copy()
Top_K = get_topk(ranking,image_pairs,Top_K)## from list to topk
if Top_K[0] - orig_topk[0] == 0:
print(f'Top 1 wrong at {file_ind}')
print(ranking)
if Top_K[1] - orig_topk[1] == 0:
print(f'Top 5 wrong at {file_ind}')
print(ranking)
counter += 1
print(f"Accuracy is: {Top_K/1000}")