Skip to content

Commit

Permalink
update get_token_len
Browse files Browse the repository at this point in the history
  • Loading branch information
kennymckormick committed Apr 3, 2024
1 parent 29141aa commit 6c91e04
Showing 1 changed file with 10 additions and 11 deletions.
21 changes: 10 additions & 11 deletions vlmeval/api/gpt.py
Original file line number Diff line number Diff line change
Expand Up @@ -150,19 +150,18 @@ def get_token_len(self, inputs) -> int:
enc = tiktoken.encoding_for_model(self.model)
except:
enc = tiktoken.encoding_for_model('gpt-4')
if isinstance(inputs, str):
if inputs.startswith('http') or osp.exists(inputs):
return 65 if self.img_detail == 'low' else 130
else:
return len(enc.encode(inputs))
elif isinstance(inputs, dict):
assert 'content' in inputs
return self.get_token_len(inputs['content'])
assert isinstance(inputs, list)
res = 0
tot = 0
for item in inputs:
res += self.get_token_len(item)
return res
if item['type'] == 'text':
tot += len(enc.encode(item['value']))
elif item['type'] == 'image':
tot += 85
if self.img_detail == 'high':
img = Image.open(item['value'])
npatch = np.ceil(img.size[0] / 512) * np.ceil(img.size[1] / 512)
tot += npatch * 170
return tot


class GPT4V(OpenAIWrapper):
Expand Down

0 comments on commit 6c91e04

Please sign in to comment.