Upload dataset text and code
Browse files- .gitattributes +11 -0
- before_run.sh +31 -0
- convert_mse.py +108 -0
- dataset.jsonl +3 -0
- mse_dataset.json +3 -0
- mse_dataset_count.py +27 -0
- mse_dataset_eval.json +3 -0
- mse_dataset_test.json +3 -0
- mse_dataset_train.json +3 -0
- mse_jsonl_resize.py +65 -0
- mse_llemma_dataset.jsonl +3 -0
- mse_llemma_text_dataset.jsonl +3 -0
- mse_output_log.txt +0 -0
- mse_output_total_lines.txt +1 -0
- mse_text_img_QA_dataset.jsonl +3 -0
- mse_text_img_QA_dataset_stats.txt +4 -0
- mse_text_img_QA_ds_test.jsonl +3 -0
- mse_text_img_QA_ds_test_100.jsonl +0 -0
- mse_text_img_QA_ds_train.jsonl +3 -0
- mse_text_img_QA_ds_val.jsonl +3 -0
- mse_text_img_process.py +320 -0
- process_mse_data.sh +75 -0
- requirements.txt +20 -0
- stats_mse_text_img_QA_ds.txt +7 -0
.gitattributes
CHANGED
@@ -57,3 +57,14 @@ saved_model/**/* filter=lfs diff=lfs merge=lfs -text
|
|
57 |
# Video files - compressed
|
58 |
*.mp4 filter=lfs diff=lfs merge=lfs -text
|
59 |
*.webm filter=lfs diff=lfs merge=lfs -text
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
57 |
# Video files - compressed
|
58 |
*.mp4 filter=lfs diff=lfs merge=lfs -text
|
59 |
*.webm filter=lfs diff=lfs merge=lfs -text
|
60 |
+
dataset.jsonl filter=lfs diff=lfs merge=lfs -text
|
61 |
+
mse_dataset_eval.json filter=lfs diff=lfs merge=lfs -text
|
62 |
+
mse_dataset_test.json filter=lfs diff=lfs merge=lfs -text
|
63 |
+
mse_dataset_train.json filter=lfs diff=lfs merge=lfs -text
|
64 |
+
mse_dataset.json filter=lfs diff=lfs merge=lfs -text
|
65 |
+
mse_llemma_dataset.jsonl filter=lfs diff=lfs merge=lfs -text
|
66 |
+
mse_llemma_text_dataset.jsonl filter=lfs diff=lfs merge=lfs -text
|
67 |
+
mse_text_img_QA_dataset.jsonl filter=lfs diff=lfs merge=lfs -text
|
68 |
+
mse_text_img_QA_ds_test.jsonl filter=lfs diff=lfs merge=lfs -text
|
69 |
+
mse_text_img_QA_ds_train.jsonl filter=lfs diff=lfs merge=lfs -text
|
70 |
+
mse_text_img_QA_ds_val.jsonl filter=lfs diff=lfs merge=lfs -text
|
before_run.sh
ADDED
@@ -0,0 +1,31 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
#!/bin/bash
|
2 |
+
|
3 |
+
# module load python/3.11
|
4 |
+
# pip install virtualenv
|
5 |
+
|
6 |
+
# python -m venv mse_env
|
7 |
+
# source ./mse_env/Scripts/activate
|
8 |
+
# pip3 install pnglatex
|
9 |
+
# pip3 install -r requirements.txt
|
10 |
+
# pip install unsloth
|
11 |
+
|
12 |
+
# pip3 install transformers datasets accelerate
|
13 |
+
# pip uninstall install llm-toolkit
|
14 |
+
# pip install -q -U transformers accelerate bitsandbytes seqeval evaluate trl peft
|
15 |
+
# pip3 install -q -U bitsandbytes==0.42.0
|
16 |
+
# pip3 install -q -U peft==0.8.2
|
17 |
+
# pip3 install -q -U trl==0.7.10
|
18 |
+
# pip3 install -q -U accelerate==0.27.1
|
19 |
+
# pip3 install -q -U datasets==2.17.0
|
20 |
+
# pip3 install -q -U transformers==4.38.0
|
21 |
+
# pip3 install zope.interface=e0n<
|
22 |
+
# pip3 install jsonl2json
|
23 |
+
|
24 |
+
# CONDA SETUP
|
25 |
+
# conda create -n gguf-finetune python=3.10 -y
|
26 |
+
# conda init
|
27 |
+
activate gguf-finetune
|
28 |
+
CMAKE_ARGS="-DGGML_BLAS=ON -DGGML_BLAS_VENDOR=OpenBLAS -DGGML_CUDA=on" \
|
29 |
+
pip install llama-cpp-python --upgrade
|
30 |
+
|
31 |
+
conda deactivate
|
convert_mse.py
ADDED
@@ -0,0 +1,108 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import csv
|
2 |
+
import os
|
3 |
+
import io
|
4 |
+
from PIL import Image
|
5 |
+
|
6 |
+
from pnglatex import pnglatex
|
7 |
+
from pdf2image import convert_from_path
|
8 |
+
import jsonlines
|
9 |
+
import json
|
10 |
+
|
11 |
+
import numpy as np
|
12 |
+
|
13 |
+
from pylatex import (
|
14 |
+
Alignat,
|
15 |
+
Axis,
|
16 |
+
Document,
|
17 |
+
Figure,
|
18 |
+
Math,
|
19 |
+
Matrix,
|
20 |
+
Plot,
|
21 |
+
Section,
|
22 |
+
Subsection,
|
23 |
+
Tabular,
|
24 |
+
TikZ,
|
25 |
+
)
|
26 |
+
from pylatex.utils import italic, NoEscape
|
27 |
+
from pylatex.package import Package
|
28 |
+
|
29 |
+
# Question, Answer, Raw_Score, Normalized_Score
|
30 |
+
|
31 |
+
def generate_pdf(latex, name, pre=True):
|
32 |
+
try:
|
33 |
+
doc = Document(default_filepath=name, geometry_options=geometry_options)
|
34 |
+
doc.packages.append(Package('amsmath'))
|
35 |
+
doc.packages.append(Package('graphicx'))
|
36 |
+
if pre:
|
37 |
+
doc.append(NoEscape(latex))
|
38 |
+
else:
|
39 |
+
is_text = True
|
40 |
+
while r"\$" in latex:
|
41 |
+
pos = latex.index(r"\$")
|
42 |
+
if is_text:
|
43 |
+
# print(latex[:pos])
|
44 |
+
doc.append(latex[:pos])
|
45 |
+
is_text = False
|
46 |
+
else:
|
47 |
+
# print("$" + latex[:pos] + "$")
|
48 |
+
doc.append(NoEscape("$" + latex[:pos] + "$"))
|
49 |
+
is_text = True
|
50 |
+
latex = latex[pos+2:]
|
51 |
+
|
52 |
+
doc.generate_pdf(name, clean=True, clean_tex=True, compiler="pdflatex", silent=True)
|
53 |
+
except:
|
54 |
+
print("pre-ran " + name)
|
55 |
+
|
56 |
+
def latex_to_image(latex, name):
|
57 |
+
|
58 |
+
while latex.index('\n') == 0:
|
59 |
+
latex = latex[1:]
|
60 |
+
|
61 |
+
generate_pdf(latex, name)
|
62 |
+
generate_pdf(latex, name, False)
|
63 |
+
|
64 |
+
images = convert_from_path(name + '.pdf')
|
65 |
+
|
66 |
+
for i in range(len(images)):
|
67 |
+
# Save pages as images in the pdf
|
68 |
+
images[i].save(name + '_' + str(i) + '.jpg', 'JPEG')
|
69 |
+
|
70 |
+
os.remove(name + ".pdf")
|
71 |
+
|
72 |
+
if __name__=="__main__":
|
73 |
+
print("STARTED")
|
74 |
+
|
75 |
+
geometry_options = {"tmargin": "1cm", "lmargin": "1cm", "rmargin": "1cm", "bmargin": "1cm"}
|
76 |
+
|
77 |
+
with jsonlines.open('dataset.jsonl') as reader_obj:
|
78 |
+
count = 0
|
79 |
+
for row in reader_obj.iter(type=dict, skip_invalid=True):
|
80 |
+
question_path = "output/" + row["id"]
|
81 |
+
if not os.path.exists(question_path):
|
82 |
+
os.makedirs(question_path)
|
83 |
+
|
84 |
+
# # Test first row
|
85 |
+
if count >= 27674:
|
86 |
+
|
87 |
+
# print(row)
|
88 |
+
# print(row["id"])
|
89 |
+
# print(row["score"])
|
90 |
+
# print(row["body"])
|
91 |
+
# print(row["answers"])
|
92 |
+
# print(row["answers"][0]["id"])
|
93 |
+
# print(row["answers"][0]["body"])
|
94 |
+
# print(row["answers"][0]["score"])
|
95 |
+
# print(row["answers"][0]["score"])
|
96 |
+
|
97 |
+
try:
|
98 |
+
latex_to_image(row["body"], question_path + "/question")
|
99 |
+
except:
|
100 |
+
print(count, question_path + "/question", " question could not generate")
|
101 |
+
for i in range(len(row["answers"])):
|
102 |
+
try:
|
103 |
+
latex_to_image(row["answers"][i]["body"], question_path + "/" + row["answers"][i]["id"])
|
104 |
+
except:
|
105 |
+
print(count, question_path + "/" + row["answers"][i]["id"], " answer could not generate")
|
106 |
+
print(count, question_path)
|
107 |
+
count += 1
|
108 |
+
print(count, end=" ")
|
dataset.jsonl
ADDED
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
1 |
+
version https://git-lfs.github.com/spec/v1
|
2 |
+
oid sha256:becd5feb86d636c3d063300e9e6d8abb5f6c6d331933da9a6191ae2c5deab25e
|
3 |
+
size 1745622006
|
mse_dataset.json
ADDED
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
1 |
+
version https://git-lfs.github.com/spec/v1
|
2 |
+
oid sha256:bd00585e5699ced7af6f1facf3294db9181dd41f1bf5c7465d588a4ab0e4ad6f
|
3 |
+
size 238797612
|
mse_dataset_count.py
ADDED
@@ -0,0 +1,27 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import jsonlines
|
2 |
+
|
3 |
+
# import ollama
|
4 |
+
|
5 |
+
#ollama run Hudson/llemma:7b
|
6 |
+
#deepeval set-ollama Hudson/llemma:7b
|
7 |
+
|
8 |
+
if __name__=="__main__":
|
9 |
+
question_count = 0
|
10 |
+
answer_count = 0
|
11 |
+
avg_a_per_q = 0.0
|
12 |
+
|
13 |
+
with jsonlines.open("mse_text_img_QA_dataset.jsonl", mode='r') as reader:
|
14 |
+
count = 0
|
15 |
+
for row in reader:
|
16 |
+
question_count += 1
|
17 |
+
for i in range(len(row["answers"])):
|
18 |
+
answer_count += 1
|
19 |
+
|
20 |
+
avg_a_per_q = (answer_count * 1.0) / question_count
|
21 |
+
|
22 |
+
print("MSE Dataset:")
|
23 |
+
print("Number of Questions = ", question_count)
|
24 |
+
print("Number of Answers = ", answer_count)
|
25 |
+
print("Average number of Answers per Question = ", avg_a_per_q)
|
26 |
+
|
27 |
+
|
mse_dataset_eval.json
ADDED
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
1 |
+
version https://git-lfs.github.com/spec/v1
|
2 |
+
oid sha256:4c62f710b8f4d0575cea823c3cdd21dfb25001ea03dc4309b60ae56ea96a7bd6
|
3 |
+
size 41622977
|
mse_dataset_test.json
ADDED
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
1 |
+
version https://git-lfs.github.com/spec/v1
|
2 |
+
oid sha256:0ac89031ef505bb7c0c3db62318d939fa4b49546e1468f16284f0e16460001c2
|
3 |
+
size 42083070
|
mse_dataset_train.json
ADDED
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
1 |
+
version https://git-lfs.github.com/spec/v1
|
2 |
+
oid sha256:80b85f83c9349ad9b86133561e653bcc669c47fe72b175af49f898b88fe6b81f
|
3 |
+
size 123926219
|
mse_jsonl_resize.py
ADDED
@@ -0,0 +1,65 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import jsonlines
|
2 |
+
|
3 |
+
if __name__=="__main__":
|
4 |
+
total_lines = 460729
|
5 |
+
num_img_lines = 64860
|
6 |
+
first_split_index = int(num_img_lines * 0.9)
|
7 |
+
second_split_index = first_split_index + int((num_img_lines) * 0.05)
|
8 |
+
|
9 |
+
print("Sizes of datasets:")
|
10 |
+
print("Train:", first_split_index, "\n90%")
|
11 |
+
print("Test:", second_split_index - first_split_index, "\n5%")
|
12 |
+
print("Val:", num_img_lines - second_split_index, "\n5%")
|
13 |
+
|
14 |
+
# with jsonlines.open("dataset.jsonl", mode='r') as reader:
|
15 |
+
# with jsonlines.open("mse_text_img_QA_dataset.jsonl", mode='w') as writer1:
|
16 |
+
# with jsonlines.open("mse_llemma_dataset.jsonl", mode='w') as writer2:
|
17 |
+
# count = 0
|
18 |
+
# for obj in reader:
|
19 |
+
# if count < num_img_lines:
|
20 |
+
# writer1.write(obj)
|
21 |
+
# else:
|
22 |
+
# writer2.write(obj)
|
23 |
+
# count = count + 1
|
24 |
+
# print(count)
|
25 |
+
|
26 |
+
|
27 |
+
# with jsonlines.open("mse_text_img_QA_dataset.jsonl", mode='r') as reader:
|
28 |
+
# with jsonlines.open("mse_text_img_QA_ds_train.jsonl", mode='w') as writer1:
|
29 |
+
# with jsonlines.open("mse_text_img_QA_ds_test.jsonl", mode='w') as writer2:
|
30 |
+
# with jsonlines.open("mse_text_img_QA_ds_val.jsonl", mode='w') as writer3:
|
31 |
+
# count = 0
|
32 |
+
# for obj in reader:
|
33 |
+
# if count < first_split_index:
|
34 |
+
# writer1.write(obj)
|
35 |
+
# elif count < second_split_index:
|
36 |
+
# writer2.write(obj)
|
37 |
+
# else:
|
38 |
+
# writer3.write(obj)
|
39 |
+
# count = count + 1
|
40 |
+
|
41 |
+
with jsonlines.open("mse_llemma_dataset.jsonl", mode='r') as reader:
|
42 |
+
with jsonlines.open("mse_llemma_text_dataset.jsonl", mode='w') as writer:
|
43 |
+
for obj in reader:
|
44 |
+
qa = "Question: " + obj["body"] + "\nAnswer: "
|
45 |
+
is_accepted = False
|
46 |
+
best_score = float('-inf')
|
47 |
+
output_text = ""
|
48 |
+
for i in range(len(obj["answers"])):
|
49 |
+
if bool(obj["answers"][i]["accepted"]) == True:
|
50 |
+
if is_accepted == False:
|
51 |
+
is_accepted = True
|
52 |
+
best_score = int(obj["answers"][i]["score"])
|
53 |
+
output_text = obj["answers"][i]["body"]
|
54 |
+
elif int(obj["answers"][i]["score"]) > best_score:
|
55 |
+
best_score = int(obj["answers"][i]["score"])
|
56 |
+
output_text = obj["answers"][i]["body"]
|
57 |
+
elif int(obj["answers"][i]["score"]) > best_score:
|
58 |
+
best_score = int(obj["answers"][i]["score"])
|
59 |
+
output_text = obj["answers"][i]["body"]
|
60 |
+
qa = qa + output_text
|
61 |
+
text_dict = {}
|
62 |
+
text_dict["text"] = qa
|
63 |
+
text_dict["meta"] = None
|
64 |
+
writer.write(text_dict)
|
65 |
+
|
mse_llemma_dataset.jsonl
ADDED
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
1 |
+
version https://git-lfs.github.com/spec/v1
|
2 |
+
oid sha256:79b7921ed377dfa9186788b0e12e470a514aedab9d6e8cab894bcd4626dcc503
|
3 |
+
size 1498029955
|
mse_llemma_text_dataset.jsonl
ADDED
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
1 |
+
version https://git-lfs.github.com/spec/v1
|
2 |
+
oid sha256:7efbc99b9dec9fdcb5e742f8e4bfb7c5c51500c1d6af876374ce398f986fd11d
|
3 |
+
size 732924307
|
mse_output_log.txt
ADDED
The diff for this file is too large to render.
See raw diff
|
|
mse_output_total_lines.txt
ADDED
@@ -0,0 +1 @@
|
|
|
|
|
1 |
+
Number of lines = 460729
|
mse_text_img_QA_dataset.jsonl
ADDED
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
1 |
+
version https://git-lfs.github.com/spec/v1
|
2 |
+
oid sha256:f2656446007c8faa6aa80e98be97ae4bda505298db160687280a17d61aada02c
|
3 |
+
size 245256472
|
mse_text_img_QA_dataset_stats.txt
ADDED
@@ -0,0 +1,4 @@
|
|
|
|
|
|
|
|
|
|
|
1 |
+
MSE Dataset:
|
2 |
+
Number of Questions = 64860
|
3 |
+
Number of Answers = 117380
|
4 |
+
Average number of Answers per Question = 1.8097440641381437
|
mse_text_img_QA_ds_test.jsonl
ADDED
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
1 |
+
version https://git-lfs.github.com/spec/v1
|
2 |
+
oid sha256:9565d457e0e63d0628ecd2d369c77f3e444edd1f6b5f44f400a94f76676fe5d4
|
3 |
+
size 12128980
|
mse_text_img_QA_ds_test_100.jsonl
ADDED
The diff for this file is too large to render.
See raw diff
|
|
mse_text_img_QA_ds_train.jsonl
ADDED
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
1 |
+
version https://git-lfs.github.com/spec/v1
|
2 |
+
oid sha256:16e93ba1b8c4a69d473731417d5165a21bde55fed1198a202174de206e5ad5db
|
3 |
+
size 221280962
|
mse_text_img_QA_ds_val.jsonl
ADDED
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
1 |
+
version https://git-lfs.github.com/spec/v1
|
2 |
+
oid sha256:4ae7837ef50c9a2484a2002e3ff50dbb06334e21b4a3c32f6eca6e012b1e0c80
|
3 |
+
size 11846530
|
mse_text_img_process.py
ADDED
@@ -0,0 +1,320 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
# import requests
|
2 |
+
|
3 |
+
from PIL import Image, ImageDraw, ImageFont
|
4 |
+
# from transformers import AutoProcessor, AutoModelForVision2Seq, Kosmos2ForConditionalGeneration, Kosmos2Config, Kosmos2Model, BitsAndBytesConfig, TrainingArguments
|
5 |
+
# from mse import mse
|
6 |
+
# import datasets
|
7 |
+
# from datasets import Features, Value, Sequence, load_dataset
|
8 |
+
# import pandas as pd
|
9 |
+
# import numpy as np
|
10 |
+
import torch
|
11 |
+
import os
|
12 |
+
# import glob
|
13 |
+
# import re
|
14 |
+
# import math
|
15 |
+
import random
|
16 |
+
# from jsonl2json import JsonlToJsonFormatter
|
17 |
+
|
18 |
+
import json
|
19 |
+
import csv
|
20 |
+
import shutil
|
21 |
+
|
22 |
+
|
23 |
+
# from io import BytesIO
|
24 |
+
# from peft import LoraConfig
|
25 |
+
# from trl import SFTTrainer
|
26 |
+
|
27 |
+
class MSEDataset(torch.utils.data.Dataset):
|
28 |
+
def __init__(self, data_path, images_path, split="train", shuffle=False):
|
29 |
+
self.json_list = []
|
30 |
+
with open(data_path, 'r') as json_file:
|
31 |
+
self.json_list = [json.loads(jline) for jline in json_file.read().splitlines()]
|
32 |
+
self.json_list = self.json_list[:64860]
|
33 |
+
self.max_size = len(self.json_list)
|
34 |
+
first_split_index = int(self.max_size * 0.9)
|
35 |
+
second_split_index = first_split_index + int(self.max_size * 0.05)
|
36 |
+
if split == "train":
|
37 |
+
self.json_list = self.json_list[:first_split_index]
|
38 |
+
elif split == "test":
|
39 |
+
self.json_list = self.json_list[first_split_index:second_split_index]
|
40 |
+
elif split == "eval":
|
41 |
+
self.json_list = self.json_list[second_split_index:]
|
42 |
+
else:
|
43 |
+
print("Invalid Input")
|
44 |
+
self.max_size = len(self.json_list)
|
45 |
+
|
46 |
+
if shuffle:
|
47 |
+
random.shuffle(self.json_list)
|
48 |
+
|
49 |
+
self.images_path = images_path
|
50 |
+
|
51 |
+
self.default_prompt = "<grounding> An image of a question which says "
|
52 |
+
# print(len(json_list))
|
53 |
+
# print(len(json_list[0]["answers"]))
|
54 |
+
# print(json_list[0]["answers"][0]["score"])
|
55 |
+
|
56 |
+
def __getitem__(self, idx):
|
57 |
+
|
58 |
+
# question_id = self.json_list[idx]['id']
|
59 |
+
# question_body = self.json_list[idx]['body']
|
60 |
+
# prompt = self.default_prompt + question_body
|
61 |
+
# question_image = None
|
62 |
+
# question_dir = self.images_path + "/" + str(question_id) + "/"
|
63 |
+
# question_path = question_dir + "question_0.jpg"
|
64 |
+
# if os.path.exists(question_path):
|
65 |
+
# question_image = Image.open(question_path)
|
66 |
+
# for answer in self.json_list[idx]['answers']:
|
67 |
+
|
68 |
+
|
69 |
+
# item = {key: torch.tensor(val[idx]) for key, val in self.encodings.items()}
|
70 |
+
# item['labels'] = torch.tensor(self.labels[idx])
|
71 |
+
return self.json_list[idx]
|
72 |
+
|
73 |
+
def __len__(self):
|
74 |
+
return self.max_size
|
75 |
+
|
76 |
+
def convert_jsonl_to_json(input_jsonl_file, output_json_folder):
|
77 |
+
# Ensure the output folder exists
|
78 |
+
os.makedirs(output_json_folder, exist_ok=True)
|
79 |
+
|
80 |
+
# Determine the output JSON filename
|
81 |
+
base_name = os.path.splitext(os.path.basename(input_jsonl_file))[0]
|
82 |
+
output_json_file = os.path.join(output_json_folder, base_name + '.json')
|
83 |
+
|
84 |
+
# Read the JSONL file and aggregate the data
|
85 |
+
data = []
|
86 |
+
with open(input_jsonl_file, 'r') as jsonl_file:
|
87 |
+
for line_number, line in enumerate(jsonl_file, start=1):
|
88 |
+
line = line.strip()
|
89 |
+
if not line: # Skip empty lines
|
90 |
+
continue
|
91 |
+
try:
|
92 |
+
data.append(json.loads(line))
|
93 |
+
except json.JSONDecodeError as e:
|
94 |
+
print(f"Error decoding JSON on line {line_number}: {e}")
|
95 |
+
continue
|
96 |
+
|
97 |
+
# Write to the JSON file
|
98 |
+
with open(output_json_file, 'w') as json_file:
|
99 |
+
json.dump(data, json_file, indent=4)
|
100 |
+
|
101 |
+
print(f"Converted {input_jsonl_file} to {output_json_file}")
|
102 |
+
|
103 |
+
if __name__=="__main__":
|
104 |
+
# 64860 lines converted: 90% (58374, index 0) train, 5% (3243, index 58374) val, 5% (3243, index 61617)
|
105 |
+
|
106 |
+
# ds = datasets.load_dataset("nurik040404/mse", features=features)
|
107 |
+
# jsonl = JsonlToJsonFormatter('dataset.jsonl', 'dataset.json')
|
108 |
+
# jsonl.to_json()
|
109 |
+
# df.to_json('mse_dataset.json')
|
110 |
+
# train, eval, test = np.split(df.sample(frac=1, random_state=42),
|
111 |
+
# [int(.6*len(df)), int(.8*len(df))])
|
112 |
+
# train.to_json('mse_dataset_train.json')
|
113 |
+
# eval.to_json('mse_dataset_eval.json')
|
114 |
+
# test.to_json('mse_dataset_test.json')
|
115 |
+
|
116 |
+
|
117 |
+
|
118 |
+
# dataset_path = 'mse_dataset_test.json'
|
119 |
+
|
120 |
+
# mse_dataset = MSEDataset(data_path="dataset.jsonl", images_path="mse_images", split="train")
|
121 |
+
# print(mse_dataset[0])
|
122 |
+
# print(mse_dataset[0]['answers'])
|
123 |
+
# print(mse_dataset[0]['answers'][:]['score'])
|
124 |
+
|
125 |
+
# print('Started train split')
|
126 |
+
|
127 |
+
# with open('train.csv', 'w', newline='') as file:
|
128 |
+
# writer = csv.writer(file)
|
129 |
+
# field = ["question_id", "question_text", "question_image", "answer_id", "answer_text", "answer_image"]
|
130 |
+
# writer.writerow(field)
|
131 |
+
# writer.writerow(["Oladele Damilola", "40", "Nigeria"])
|
132 |
+
|
133 |
+
# for qas in mse_dataset:
|
134 |
+
# question_id = qas['id']
|
135 |
+
# question_text = qas['body']
|
136 |
+
# question_image = 'train_images/' + question_id + '/question_0.jpg'
|
137 |
+
# answers = qas['answers']
|
138 |
+
|
139 |
+
# source = 'mse_images/' + question_id
|
140 |
+
# destination = 'train_images/'
|
141 |
+
# if os.path.exists('train_images/' + question_id) is False:
|
142 |
+
# shutil.move(source, destination)
|
143 |
+
|
144 |
+
# max_score = None
|
145 |
+
|
146 |
+
# for answer in answers:
|
147 |
+
# if max_score == None:
|
148 |
+
# max_score = answer['score']
|
149 |
+
# if max_score > answer['score']:
|
150 |
+
# max_score = answer['score']
|
151 |
+
|
152 |
+
# for answer in answers:
|
153 |
+
# if answer['accepted'] or answer['score'] == max_score:
|
154 |
+
# writer.writerow([question_id, question_image, question_text, answer['id'], answer['body'], destination + question_id + '/' + answer['id'] + '_0.jpg'])
|
155 |
+
|
156 |
+
print('Started train split')
|
157 |
+
mse_dataset = MSEDataset(data_path="dataset.jsonl", images_path="mse_images", split="train")
|
158 |
+
with open('train.csv', 'w', newline='') as file:
|
159 |
+
writer = csv.writer(file, delimiter ="█", lineterminator="\u2063")
|
160 |
+
field = ["question_id", "question_text", "question_image", "answer_id", "answer_text", "answer_image"]
|
161 |
+
writer.writerow(field)
|
162 |
+
# writer.writerow(["Oladele Damilola", "40", "Nigeria"])
|
163 |
+
|
164 |
+
for qas in mse_dataset:
|
165 |
+
question_id = qas['id']
|
166 |
+
question_text = qas['body']
|
167 |
+
question_image = 'train_images/' + question_id + '/question_0.jpg'
|
168 |
+
answers = qas['answers']
|
169 |
+
|
170 |
+
destination = 'train_images/'
|
171 |
+
source = 'mse_images/' + question_id
|
172 |
+
if os.path.exists('train_images/' + question_id) is False:
|
173 |
+
shutil.move(source, destination)
|
174 |
+
|
175 |
+
max_score = None
|
176 |
+
for answer in answers:
|
177 |
+
if max_score == None:
|
178 |
+
max_score = answer['score']
|
179 |
+
if max_score > answer['score']:
|
180 |
+
max_score = answer['score']
|
181 |
+
|
182 |
+
for answer in answers:
|
183 |
+
if answer['accepted'] or answer['score'] == max_score:
|
184 |
+
writer.writerow([question_id, question_image, question_text, answer['id'], answer['body'], destination + question_id + '/' + answer['id'] + '_0.jpg'])
|
185 |
+
|
186 |
+
|
187 |
+
print('Started test split')
|
188 |
+
mse_dataset = MSEDataset(data_path="dataset.jsonl", images_path="mse_images", split="test")
|
189 |
+
with open('test.csv', 'w', newline='') as file:
|
190 |
+
writer = csv.writer(file, delimiter ="█", lineterminator="\u2063")
|
191 |
+
field = ["question_id", "question_text", "question_image", "answer_id", "answer_text", "answer_image"]
|
192 |
+
writer.writerow(field)
|
193 |
+
# writer.writerow(["Oladele Damilola", "40", "Nigeria"])
|
194 |
+
|
195 |
+
for qas in mse_dataset:
|
196 |
+
question_id = qas['id']
|
197 |
+
question_text = qas['body']
|
198 |
+
question_image = 'test_images/' + question_id + '/question_0.jpg'
|
199 |
+
answers = qas['answers']
|
200 |
+
|
201 |
+
source = 'mse_images/' + question_id
|
202 |
+
destination = 'test_images/'
|
203 |
+
if os.path.exists('test_images/' + question_id) is False:
|
204 |
+
shutil.move(source, destination)
|
205 |
+
|
206 |
+
max_score = None
|
207 |
+
for answer in answers:
|
208 |
+
if max_score == None:
|
209 |
+
max_score = answer['score']
|
210 |
+
if max_score > answer['score']:
|
211 |
+
max_score = answer['score']
|
212 |
+
|
213 |
+
for answer in answers:
|
214 |
+
if answer['accepted'] or answer['score'] == max_score:
|
215 |
+
writer.writerow([question_id, question_image, question_text, answer['id'], answer['body'], destination + question_id + '/' + answer['id'] + '_0.jpg'])
|
216 |
+
|
217 |
+
|
218 |
+
print('Started val split')
|
219 |
+
mse_dataset = MSEDataset(data_path="dataset.jsonl", images_path="mse_images", split="eval")
|
220 |
+
with open('val.csv', 'w', newline='') as file:
|
221 |
+
writer = csv.writer(file, delimiter ="█", lineterminator="\u2063")
|
222 |
+
field = ["question_id", "question_text", "question_image", "answer_id", "answer_text", "answer_image"]
|
223 |
+
writer.writerow(field)
|
224 |
+
# writer.writerow(["Oladele Damilola", "40", "Nigeria"])
|
225 |
+
|
226 |
+
for qas in mse_dataset:
|
227 |
+
question_id = qas['id']
|
228 |
+
question_text = qas['body']
|
229 |
+
question_image = 'val_images/' + question_id + '/question_0.jpg'
|
230 |
+
answers = qas['answers']
|
231 |
+
|
232 |
+
source = 'mse_images/' + question_id
|
233 |
+
destination = 'val_images/'
|
234 |
+
if os.path.exists('val_images/' + question_id) is False:
|
235 |
+
shutil.move(source, destination)
|
236 |
+
|
237 |
+
max_score = None
|
238 |
+
for answer in answers:
|
239 |
+
if max_score == None:
|
240 |
+
max_score = answer['score']
|
241 |
+
if max_score > answer['score']:
|
242 |
+
max_score = answer['score']
|
243 |
+
|
244 |
+
for answer in answers:
|
245 |
+
if answer['accepted'] or answer['score'] == max_score:
|
246 |
+
writer.writerow([question_id, question_image, question_text, answer['id'], answer['body'], destination + question_id + '/' + answer['id'] + '_0.jpg'])
|
247 |
+
|
248 |
+
print('Finished generating dataset')
|
249 |
+
|
250 |
+
# convert_jsonl_to_json("dataset.jsonl", "dataset.json")
|
251 |
+
# Keys: "id", "body", "answers": "id", "body", "score", "accepted"
|
252 |
+
|
253 |
+
|
254 |
+
# dataset_train = load_dataset("json", data_files="mse_dataset_train.json", split=None)
|
255 |
+
# dataset_eval = load_dataset("json", data_files="mse_dataset.json", split=None)
|
256 |
+
# dataset_test = load_dataset("json", data_files="mse_dataset_test.json", split=None)
|
257 |
+
# print(dataset_eval.description)
|
258 |
+
# dataset = load_dataset("json", data_files="dataset.json", split=None)
|
259 |
+
|
260 |
+
# print(dataset)
|
261 |
+
# df = pd.read_json(dataset_path)
|
262 |
+
# df = df.drop(df.columns[[1, 2, 3, 5, 6, 9]], axis=1)
|
263 |
+
# df.to_json(dataset_path)
|
264 |
+
# mse_list = df.to_dict(orient='records')
|
265 |
+
# print(df.columns)
|
266 |
+
# print(df["body"])
|
267 |
+
# print(df["answers"])
|
268 |
+
# print(mse_list[0])
|
269 |
+
|
270 |
+
# test_dataset = MSEDataset(data_path=dataset_path, images_path="mse_images/")
|
271 |
+
# print(len(test_dataset))
|
272 |
+
# print(test_dataset[0])
|
273 |
+
|
274 |
+
# ds = load_dataset('json', data_files='dataset.jsonl')
|
275 |
+
|
276 |
+
|
277 |
+
# dataset = load_dataset("TheFusion21/PokemonCards", split="train")
|
278 |
+
# Dataset({
|
279 |
+
# features: ['id', 'image_url', 'caption', 'name', 'hp', 'set_name'],
|
280 |
+
# num_rows: 13139
|
281 |
+
# })
|
282 |
+
|
283 |
+
# # load image
|
284 |
+
# example = dataset[1]
|
285 |
+
# image_url = example["image_url"]
|
286 |
+
# response = requests.get(image_url)
|
287 |
+
# # Read the image from the response content
|
288 |
+
# image = Image.open(BytesIO(response.content))
|
289 |
+
# image
|
290 |
+
# {'id': 'ex12-1',
|
291 |
+
# 'image_url': 'https://images.pokemontcg.io/ex12/1_hires.png',
|
292 |
+
# 'caption': "A Stage 1 Pokemon Card of type Colorless with the title Aerodactyl and 70 HP of rarity Rare Holo evolved from Mysterious Fossil from the set Legend Maker. It has the attack Power Blow with the cost Colorless, the energy cost 1 and the damage of 10+ with the description: Does 10 damage plus 10 more damage for each Energy attached to Aerodactyl. It has the attack Speed Stroke with the cost Colorless, Colorless, Colorless, the energy cost 3 and the damage of 40 with the description: During your opponent's next turn, prevent all effects, including damage, done to Aerodactyl by attacks from your opponent's Pokemon-ex. It has the ability Reactive Protection with the description: Any damage done to Aerodactyl by attacks from your opponent's Pokemon is reduced by 10 for each React Energy card attached to Aerodactyl (after applying Weakness and Resistance). It has weakness against Lightning 2. It has resistance against Fighting -30. ",
|
293 |
+
# 'name': 'Aerodactyl',
|
294 |
+
# 'hp': 70,
|
295 |
+
# 'set_name': 'Legend Maker'}
|
296 |
+
|
297 |
+
# class Kosmos2DataCollator:
|
298 |
+
# def __init__(self, processor):
|
299 |
+
# self.processor = processor
|
300 |
+
|
301 |
+
# def __call__(self, examples):
|
302 |
+
# texts = []
|
303 |
+
# images = []
|
304 |
+
# bboxes = []
|
305 |
+
# for example in examples:
|
306 |
+
# texts.append(example['caption'])
|
307 |
+
# image_url = example["image_url"]
|
308 |
+
# images.append(Image.open(BytesIO(requests.get(image_url).content)))
|
309 |
+
|
310 |
+
|
311 |
+
# batch = self.processor(images = images, text = texts, return_tensors="pt", truncation= True, padding=True)
|
312 |
+
|
313 |
+
# labels = batch["input_ids"].clone()
|
314 |
+
# if self.processor.tokenizer.pad_token_id is not None:
|
315 |
+
# labels[labels == self.processor.tokenizer.pad_token_id] = -100
|
316 |
+
# batch["labels"] = labels
|
317 |
+
|
318 |
+
# return batch
|
319 |
+
|
320 |
+
# data_collator = Kosmos2DataCollator(processor)
|
process_mse_data.sh
ADDED
@@ -0,0 +1,75 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
#!/bin/bash
|
2 |
+
|
3 |
+
#SBATCH --time=1:00:00 # walltime. hours:minutes:seconds
|
4 |
+
#SBATCH --ntasks=8 # number of processor cores (i.e. tasks)
|
5 |
+
#SBATCH --nodes=1 # number of nodes
|
6 |
+
#SBATCH --gpus=1
|
7 |
+
#SBATCH --mem=80G # 164G memory per CPU core
|
8 |
+
#SBATCH [email protected] # email address
|
9 |
+
#SBATCH --mail-type=BEGIN
|
10 |
+
#SBATCH --mail-type=END
|
11 |
+
#SBATCH --mail-type=FAIL
|
12 |
+
#SBATCH --qos=cs
|
13 |
+
#SBATCH --partition=cs
|
14 |
+
|
15 |
+
# some helpful debugging options
|
16 |
+
set -e
|
17 |
+
set -u
|
18 |
+
|
19 |
+
# LOAD MODULES, INSERT CODE, AND RUN YOUR PROGRAMS HERE
|
20 |
+
# module load python/3.11
|
21 |
+
|
22 |
+
source ./mse_env/Scripts/activate
|
23 |
+
|
24 |
+
# python mse_text_img_process.py
|
25 |
+
# python convert_mse.py
|
26 |
+
|
27 |
+
# pip install jsonlines
|
28 |
+
# pip install deepeval
|
29 |
+
|
30 |
+
NUM_TEST_CASES=100
|
31 |
+
|
32 |
+
# python mse_ollama_run.py --num $NUM_TEST_CASES --test f --shot 0 --out_file metric_test_orig_100_f.txt
|
33 |
+
# echo "Test case faithfulness finished"
|
34 |
+
|
35 |
+
NUM_SHOT=1
|
36 |
+
|
37 |
+
# deepeval set-local-model --model-name Hudson/llemma:7b
|
38 |
+
# deepeval set-ollama Hudson/llemma:7b
|
39 |
+
# python mse_ollama_run.py --num $NUM_TEST_CASES --test ar --shot $NUM_SHOT --out_file metric_test_1_shot_100_ar.txt
|
40 |
+
# echo "Test case answer relevancy finished"
|
41 |
+
python mse_ollama_run.py --num $NUM_TEST_CASES --test crec --shot $NUM_SHOT --out_file metric_test_1_shot_100_crec.txt
|
42 |
+
echo "Test case contexual recall finished"
|
43 |
+
python mse_ollama_run.py --num $NUM_TEST_CASES --test cp --shot $NUM_SHOT --out_file metric_test_1_shot_100_cp.txt
|
44 |
+
echo "Test case contextual precision finished"
|
45 |
+
|
46 |
+
|
47 |
+
NUM_SHOT=5
|
48 |
+
python mse_ollama_run.py --num $NUM_TEST_CASES --test ar --shot $NUM_SHOT --out_file metric_test_5_shot_100_ar.txt
|
49 |
+
echo "Test case answer relevancy finished"
|
50 |
+
python mse_ollama_run.py --num $NUM_TEST_CASES --test crec --shot $NUM_SHOT --out_file metric_test_5_shot_100_crec.txt
|
51 |
+
echo "Test case contexual recall finished"
|
52 |
+
python mse_ollama_run.py --num $NUM_TEST_CASES --test cp --shot $NUM_SHOT --out_file metric_test_5_shot_100_cp.txt
|
53 |
+
echo "Test case contextual precision finished"
|
54 |
+
|
55 |
+
|
56 |
+
NUM_SHOT=10
|
57 |
+
python mse_ollama_run.py --num $NUM_TEST_CASES --test ar --shot $NUM_SHOT --out_file metric_test_10_shot_100_ar.txt
|
58 |
+
echo "Test case answer relevancy finished"
|
59 |
+
python mse_ollama_run.py --num $NUM_TEST_CASES --test crec --shot $NUM_SHOT --out_file metric_test_10_shot_100_crec.txt
|
60 |
+
echo "Test case contexual recall finished"
|
61 |
+
python mse_ollama_run.py --num $NUM_TEST_CASES --test cp --shot $NUM_SHOT --out_file metric_test_10_shot_100_cp.txt
|
62 |
+
echo "Test case contextual precision finished"
|
63 |
+
|
64 |
+
|
65 |
+
|
66 |
+
# python mse_ollama_run.py --num $NUM_TEST_CASES --test crel --out_file metric_test_orig_100_crel.txt
|
67 |
+
# echo "Test case contextual relevancy finished"
|
68 |
+
|
69 |
+
|
70 |
+
# python mse_ollama_run.py --num $NUM_TEST_CASES --test f --out_file metric_test_orig_100_f.txt
|
71 |
+
# echo "Test case faithfulness finished"
|
72 |
+
|
73 |
+
# python mse_jsonl_resize.py
|
74 |
+
|
75 |
+
# python finetune.py
|
requirements.txt
ADDED
@@ -0,0 +1,20 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
numpy
|
2 |
+
matplotlib
|
3 |
+
scipy
|
4 |
+
sentencepiece
|
5 |
+
protobuf
|
6 |
+
torch
|
7 |
+
gradio
|
8 |
+
torchvision
|
9 |
+
opencv-python-headless
|
10 |
+
tensorboardX
|
11 |
+
transformers
|
12 |
+
datasets
|
13 |
+
pylatex
|
14 |
+
pnglatex
|
15 |
+
zstandard
|
16 |
+
jsonlines
|
17 |
+
pyramid==1.5
|
18 |
+
deepeval
|
19 |
+
ollama
|
20 |
+
jsonlines
|
stats_mse_text_img_QA_ds.txt
ADDED
@@ -0,0 +1,7 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
Sizes of datasets:
|
2 |
+
Train: 58374
|
3 |
+
90%
|
4 |
+
Test: 3243
|
5 |
+
5%
|
6 |
+
Val: 3243
|
7 |
+
5%
|