-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcorpus_analysis.py
More file actions
41 lines (41 loc) · 1.44 KB
/
Copy pathcorpus_analysis.py
File metadata and controls
41 lines (41 loc) · 1.44 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
from transformers import GemmaTokenizerFast
import matplotlib.pyplot as plt
import pandas as pd
import ast
def token_statistics(corpus: list, image_path: str = None, tokenizer=None):
if tokenizer is None:
return None
all_tokens = []
for i in range(len(corpus)):
try:
tokens = tokenizer(corpus[i])
except:
continue
all_tokens.append(len(tokens["input_ids"]))
if image_path is not None:
plt.figure(figsize=(10, 5))
plt.hist(
all_tokens, bins=30, edgecolor="black"
)
plt.title("Token Count Distribution")
plt.xlabel("Token Count")
plt.ylabel("Frequency")
plt.grid(True)
plt.savefig(image_path)
return all_tokens
if __name__ == "__main__":
tokenizer = GemmaTokenizerFast.from_pretrained("../gemma-2-9b-it-bnb-4bit")
df = pd.read_csv("train.csv")
corpus = []
for i in range(len(df)):
try:
prompts = ast.literal_eval(df.loc[i, "prompt"])
response_a = ast.literal_eval(df.loc[i, "response_a"])
response_b = ast.literal_eval(df.loc[i, "response_b"])
single_corpus = " ".join(prompts + response_a + response_b)
corpus.append(single_corpus)
except:
continue
if i % 3000 == 0 and i != 0:
print(f"{i} row finished")
token_statistics(corpus=corpus,image_path="statistics.png",tokenizer=tokenizer)