-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathcaption_claude.py
More file actions
94 lines (87 loc) · 2.81 KB
/
Copy pathcaption_claude.py
File metadata and controls
94 lines (87 loc) · 2.81 KB
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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
import anthropic
import os
import json
import time
import multiprocessing
import sys
import os.path
class CaptionGenerator:
def __init__(self, api_key, model_id):
self.api_key = api_key
self.model_id = model_id
self.client = anthropic.Anthropic(
api_key=os.environ.get("ANTHROPIC_API_KEY"),
)
self.prompt_base = "generate captions from prompt\n"
def set_prompt_base(self, prompt_base):
self.prompt_base = prompt_base
if self.prompt_base[-1] != '\n':
self.prompt_base += '\n'
def create_prompt(self, keywords):
prompt = self.prompt_base
prompt += str(keywords)
return prompt
def generate_caption(self, prompt):
try:
response = self.client.messages.create(
messages=[
{
"role": "user",
"content": prompt,
}
],
model=self.model_id,
max_tokens=4000,
temperature=0,
)
caption = response.content[0].text
print(f'usage: {response.usage}')
except anthropic.APIStatusError as e:
print("Another non-200-range status code was received")
print(e.response)
caption = 'exceeding liimt'
except Exception as e:
print('some error happened')
print(e)
caption = 'some error'
return caption
def process_item(line):
agent = CaptionGenerator('XXXXXXX', 'claude-3-opus-20240229')
flag = 0
response = {}
prompt = line['tags']
location = line['location']
out_f = f'/home/abhinaba_roy/midicaps/caption_output/{location}.json'
if os.path.exists(out_f):
with open(out_f,'r') as f:
cont = json.load(f)
if(cont['caption'] == 'exceeding liimt'):
flag = 0
elif(cont['caption']=='some error'):
flag = 0
else:
flag = 1
f.close()
if flag == 0:
print(f'processing {location}')
response['location'] = location
response['caption'] = agent.generate_caption(prompt)
with open(out_f,'w') as f:
json.dump(response,f)
f.close()
else:
print(f'file processed already: {location}')
del agent
return 1
def get_captions(file='all_files_output.json'):
test_ = [json.loads(line) for line in open(file)]
num_processes = multiprocessing.cpu_count()
pool = multiprocessing.Pool(processes=2)
t_0 = time.time()
results = pool.map(process_item, test_)
t_1 = time.time()
pool.close()
pool.join()
print(f'time_taken in multiprocessing: {t_1 - t_0}')
if __name__ =='__main__':
get_captions('all_files_output.json')