Upload folder using huggingface_hub
Browse files- .gitattributes +3 -0
- mmc4_final_processed.json +3 -0
- mmc4_rewrite_remaining_clean.json +3 -0
- mmc4_rewrite_valid_clean.json +3 -0
- rewrite_text_mmc4.py +206 -0
.gitattributes
CHANGED
@@ -33,3 +33,6 @@ saved_model/**/* filter=lfs diff=lfs merge=lfs -text
|
|
33 |
*.zip filter=lfs diff=lfs merge=lfs -text
|
34 |
*.zst filter=lfs diff=lfs merge=lfs -text
|
35 |
*tfevents* filter=lfs diff=lfs merge=lfs -text
|
|
|
|
|
|
|
|
33 |
*.zip filter=lfs diff=lfs merge=lfs -text
|
34 |
*.zst filter=lfs diff=lfs merge=lfs -text
|
35 |
*tfevents* filter=lfs diff=lfs merge=lfs -text
|
36 |
+
mmc4_final_processed.json filter=lfs diff=lfs merge=lfs -text
|
37 |
+
mmc4_rewrite_remaining_clean.json filter=lfs diff=lfs merge=lfs -text
|
38 |
+
mmc4_rewrite_valid_clean.json filter=lfs diff=lfs merge=lfs -text
|
mmc4_final_processed.json
ADDED
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
1 |
+
version https://git-lfs.github.com/spec/v1
|
2 |
+
oid sha256:46ea90e50a9cc989ce520864583ee0cf10d8380189d5229ffc4813428cf5bb57
|
3 |
+
size 282853941
|
mmc4_rewrite_remaining_clean.json
ADDED
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
1 |
+
version https://git-lfs.github.com/spec/v1
|
2 |
+
oid sha256:5a2d1943418a25e61ae230a6b0a9f82dd180882bcd8eb6a9d7639c2780e84b32
|
3 |
+
size 116077088
|
mmc4_rewrite_valid_clean.json
ADDED
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
1 |
+
version https://git-lfs.github.com/spec/v1
|
2 |
+
oid sha256:eca27755707d38ddc0949586fcfb4218afcac276a19a84aee9f55c4878538e23
|
3 |
+
size 182441543
|
rewrite_text_mmc4.py
ADDED
@@ -0,0 +1,206 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import json
|
2 |
+
import os
|
3 |
+
from tqdm import tqdm
|
4 |
+
import pdb
|
5 |
+
|
6 |
+
from transformers import AutoConfig, AutoModelForCausalLM, AutoTokenizer
|
7 |
+
import torch
|
8 |
+
import random
|
9 |
+
import re
|
10 |
+
|
11 |
+
# change model name
|
12 |
+
model_name = "meta-llama/Meta-Llama-3-8B-Instruct"
|
13 |
+
# change huggingface token here
|
14 |
+
HUGGING_FACE_TOKEN = "<your_huggingface_token>"
|
15 |
+
|
16 |
+
|
17 |
+
output_folder = "mmc4_json_split"
|
18 |
+
if not os.path.exists(output_folder):
|
19 |
+
os.makedirs(output_folder)
|
20 |
+
|
21 |
+
# import argparse
|
22 |
+
# parser = argparse.ArgumentParser(description="Input file")
|
23 |
+
|
24 |
+
# parser.add_argument('--split_id', type=str, help='evaluation file name')
|
25 |
+
|
26 |
+
# args = parser.parse_args()
|
27 |
+
# split_id = args.split_id
|
28 |
+
|
29 |
+
def split_data(data, n):
|
30 |
+
k, m = divmod(len(data), n)
|
31 |
+
return (data[i * k + min(i, m):(i + 1) * k + min(i + 1, m)] for i in range(n))
|
32 |
+
|
33 |
+
split_id = 0
|
34 |
+
|
35 |
+
with open(f"mmc4_final_processed.json", "r") as file:
|
36 |
+
full_data = json.load(file)
|
37 |
+
|
38 |
+
num_splits = 8
|
39 |
+
splits = list(split_data(full_data, num_splits))
|
40 |
+
|
41 |
+
# Take the i-th split
|
42 |
+
data = splits[split_id]
|
43 |
+
output_data = []
|
44 |
+
# instruct_pattern = re.compile(r'<INSTRUCT>(.*?)</INSTRUCT>', re.DOTALL)
|
45 |
+
rewrite_pattern = re.compile(r'<REWRITE>(.*?)</REWRITE>|<REWRITE>(.*?)', re.DOTALL)
|
46 |
+
|
47 |
+
output_path = f"{output_folder}/mmc4_final_split_{split_id}.json"
|
48 |
+
|
49 |
+
device_id = f"cuda:{split_id}"
|
50 |
+
device = torch.device(device_id)
|
51 |
+
|
52 |
+
model = AutoModelForCausalLM.from_pretrained(
|
53 |
+
model_name,
|
54 |
+
# use_auth_token=HUGGING_FACE_TOKEN,
|
55 |
+
torch_dtype=torch.bfloat16
|
56 |
+
# device_map=local_rank
|
57 |
+
)
|
58 |
+
|
59 |
+
tokenizer = AutoTokenizer.from_pretrained(
|
60 |
+
model_name,
|
61 |
+
model_max_length=2048,
|
62 |
+
# padding_side="right",
|
63 |
+
# use_fast=False,
|
64 |
+
use_auth_token=HUGGING_FACE_TOKEN
|
65 |
+
)
|
66 |
+
|
67 |
+
terminators = [
|
68 |
+
tokenizer.eos_token_id,
|
69 |
+
tokenizer.convert_tokens_to_ids("<|eot_id|>")
|
70 |
+
]
|
71 |
+
|
72 |
+
model.to(device)
|
73 |
+
|
74 |
+
|
75 |
+
for instance in tqdm(data):
|
76 |
+
|
77 |
+
conv = instance["conversations"]
|
78 |
+
input_text = conv[0]['value']
|
79 |
+
input_text = input_text.split("<BEGIN>")[-1]
|
80 |
+
output_text = conv[1]['value']
|
81 |
+
merge_text = input_text + output_text
|
82 |
+
|
83 |
+
messages = [
|
84 |
+
{"role": "system", "content": f"Imagine you are an expert writer. Given a text material, you need to complete two tasks. You need to rewrite the given text material to improve their quality, including making them more fluent, coherent, natural, engaging, and concise. You must ensure the rewritten text faithfully matches with the original text. Do not modify <image> tokens in the rewritten text. In other words, you must maintain all the image tokens <image> in their relative positions in the rewritten text and the number of <image> tokens in the rewritten text should be exactly same with the original text. Wrap your rewritten text material in the following format: <REWRITE> <your rewritten text here> </REWRITE>."},
|
85 |
+
{"role": "user", "content": f"Now given this text material: {merge_text}, annotate the rewritten text material. You must strictly follow the format requriement and you must not add any notes or explanations inside the special tokens <REWRITE> </REWRITE>."},
|
86 |
+
]
|
87 |
+
input_ids = tokenizer.apply_chat_template(
|
88 |
+
messages,
|
89 |
+
add_generation_prompt=True,
|
90 |
+
return_tensors="pt"
|
91 |
+
).to(model.device)
|
92 |
+
outputs = model.generate(
|
93 |
+
input_ids,
|
94 |
+
max_new_tokens=1024,
|
95 |
+
eos_token_id=terminators,
|
96 |
+
do_sample=True,
|
97 |
+
temperature=1.0,
|
98 |
+
pad_token_id=tokenizer.eos_token_id,
|
99 |
+
top_p=0.9
|
100 |
+
)
|
101 |
+
response = outputs[0][input_ids.shape[-1]:]
|
102 |
+
output = tokenizer.decode(response, skip_special_tokens=True)
|
103 |
+
output = output.strip()
|
104 |
+
|
105 |
+
fail_flag = False # flag to indiciate whether rewritting succeed
|
106 |
+
match_summ = ""
|
107 |
+
try:
|
108 |
+
rewrite_text = rewrite_pattern.findall(output)[0]
|
109 |
+
|
110 |
+
if len(rewrite_text[0]) > 0:
|
111 |
+
match_summ = rewrite_text[0]
|
112 |
+
elif len(rewrite_text[1]) > 0:
|
113 |
+
match_summ = rewrite_text[1]
|
114 |
+
else:
|
115 |
+
# print("cannot find matched rewrite text")
|
116 |
+
fail_flag = True
|
117 |
+
except Exception as e:
|
118 |
+
print(e)
|
119 |
+
fail_flag = True
|
120 |
+
|
121 |
+
# match_summ = match_summ.strip()
|
122 |
+
|
123 |
+
num_img = match_summ.count("<image>")
|
124 |
+
img_len = len(instance["image"])
|
125 |
+
if num_img != img_len or "<image><image>" in match_summ or "<image> <image>" in match_summ:
|
126 |
+
# print("wrong <image> token")
|
127 |
+
fail_flag = True
|
128 |
+
|
129 |
+
if fail_flag: # start per sentence rewritting
|
130 |
+
# print("start per sentence rewritting......")
|
131 |
+
split_text = merge_text.split('<image>')
|
132 |
+
split_text = split_text[:-1]
|
133 |
+
sent_list = []
|
134 |
+
for st in split_text:
|
135 |
+
messages = [
|
136 |
+
{"role": "system", "content": f"Imagine you are an expert writer. Given a sentence, you need to complete two tasks. You need to rewrite the given sentence to improve its quality, including making them more fluent, coherent, natural, engaging, and concise. You must ensure the rewritten sentence faithfully matches with the original sentence. Wrap your rewritten sentence in the following format: <REWRITE> <your rewritten sentence here> </REWRITE>."},
|
137 |
+
{"role": "user", "content": f"Now given this sentence: {st}, annotate the rewritten sentence. You must strictly follow the format requriement and you must not add any notes or explanations inside the special tokens <REWRITE> </REWRITE>."},
|
138 |
+
]
|
139 |
+
input_ids = tokenizer.apply_chat_template(
|
140 |
+
messages,
|
141 |
+
add_generation_prompt=True,
|
142 |
+
return_tensors="pt"
|
143 |
+
).to(model.device)
|
144 |
+
outputs = model.generate(
|
145 |
+
input_ids,
|
146 |
+
max_new_tokens=512,
|
147 |
+
eos_token_id=terminators,
|
148 |
+
do_sample=True,
|
149 |
+
temperature=1.0,
|
150 |
+
pad_token_id=tokenizer.eos_token_id,
|
151 |
+
top_p=0.9
|
152 |
+
)
|
153 |
+
response = outputs[0][input_ids.shape[-1]:]
|
154 |
+
output = tokenizer.decode(response, skip_special_tokens=True)
|
155 |
+
output = output.strip()
|
156 |
+
|
157 |
+
try:
|
158 |
+
rewrite_sent = rewrite_pattern.findall(output)[0]
|
159 |
+
if len(rewrite_sent[0]) > 0:
|
160 |
+
sent = rewrite_sent[0]
|
161 |
+
elif len(rewrite_sent[1]) > 0:
|
162 |
+
sent = rewrite_sent[1]
|
163 |
+
else:
|
164 |
+
# print("cannot find matched rewrite sentence")
|
165 |
+
sent = st
|
166 |
+
except Exception as e:
|
167 |
+
print(e)
|
168 |
+
sent = st
|
169 |
+
sent = sent.strip()
|
170 |
+
sent_list.append(sent)
|
171 |
+
text_list = sent_list
|
172 |
+
else:
|
173 |
+
text_list = match_summ.split('<image>')
|
174 |
+
text_list = [t.strip() for t in text_list]
|
175 |
+
|
176 |
+
split_index = random.randint(1, len(text_list) - 2)
|
177 |
+
input_list = text_list[:split_index]
|
178 |
+
output_list = text_list[split_index:]
|
179 |
+
|
180 |
+
input_text = " <image>\n".join(input_list)
|
181 |
+
output_text = " <image>\n".join(output_list)
|
182 |
+
|
183 |
+
input_text = input_text + " <image>\n"
|
184 |
+
|
185 |
+
input_prompt = f"{instance['instruction']}\n {input_text}"
|
186 |
+
|
187 |
+
output_prompt = f"{output_text}"
|
188 |
+
|
189 |
+
conversations = [
|
190 |
+
{
|
191 |
+
"from": "human",
|
192 |
+
"value": input_prompt
|
193 |
+
},
|
194 |
+
{
|
195 |
+
"from": "gpt",
|
196 |
+
"value": output_prompt
|
197 |
+
}
|
198 |
+
]
|
199 |
+
|
200 |
+
instance["conversations"] = conversations
|
201 |
+
output_data.append(instance)
|
202 |
+
|
203 |
+
|
204 |
+
print(len(output_data))
|
205 |
+
with open(output_path, 'w') as file:
|
206 |
+
json.dump(output_data, file, indent=4)
|