-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathutil_prompt.py
146 lines (126 loc) · 5.17 KB
/
util_prompt.py
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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
import pandas as pd
alpa_en = ['A.', 'B.', 'C.', 'D.', 'E.']
level_en = {
'Primary': 'primary school',
'Middle': 'middle school',
'High': 'high school',
'Univ': 'university',
'Prof': 'professional'
}
level_ar = {
'Primary': 'للمرحلة الابتدائية',
'Middle': 'للمرحلة المتوسطة',
'High': 'للمرحلة الثانوية',
'Univ': 'للجامعات',
'Prof': 'للمحترفين'
}
country_ar = {
'UAE': 'في دولة الإمارات العربية المتحدة',
'Egypt': 'في مصر',
'Lebanon': 'في لبنان',
'Jordan': 'في الأردن',
'Kuwait': 'في الكويت',
'KSA': 'في المملكة العربية السعودية',
'Palestine': 'في فلسطين',
'Morocco': 'في المغرب',
}
subject_ar = {
'Islamic Studies': 'في الدراسات الإسلامية',
'Driving Test': 'اختبار القيادة',
'Natural Science': 'في العلوم الطبيعية',
'History': 'في التاريخ',
'General Knowledge': 'في المعلومات العامة',
'Law': 'في القانون',
'Physics': 'في الفيزياء',
'Social Science': 'في العلوم الاجتماعية',
'Management': 'في الإدارة',
'Arabic Language': 'في اللغة العربية',
'Political Science': ' في العلوم السياسية',
'Philosophy': 'في الفلسفة',
'Accounting': 'في المحاسبة',
'Computer Science': 'في علوم الحاسوب',
'Geography': 'في الجغرافيا',
'Math': 'في الرياضيات',
'Biology': 'في علم الأحياء',
'Economics': 'في الاقتصاد',
'Arabic Language (General)': 'في اللغة العربية (عام)',
'Arabic Language (Grammar)': 'في اللغة العربية (قواعد النحو)',
'Civics': 'في التربية المدنية',
}
alpa_ar = ['أ-',
'ب-',
'ج-',
'د-',
'ه-']
def prepare_data_en(args):
PROMPT = 'This is a [MAIN_META_DATA]. Select the correct answer!\n\nQuestion: [INPUT]\n[OPTION]'
if args.lora_weights == "x":
PROMPT = f'{PROMPT}\n\nAnswer: '
else:
PROMPT = f'### Input:{PROMPT}\n\n### Output:\n'
alpa = alpa_ar
if args.lang_alpa == 'en':
alpa = alpa_en
inputs = []
outputs = []
outputs_options = []
data = pd.read_csv('data/ArabicMMLU.csv')
data = data[data['is_few_shot'] == 0]
for idx, row in data.iterrows():
level = "" if pd.isna(row['Level']) else " for " + level_en[row['Level']]
country = "" if pd.isna(row['Country']) else " in " + row['Country']
main_meta_data = f"{row['Subject']} question{level}{country}"
question = row['Question'] if pd.isna(row['Context']) else f"{row['Context']}\n\n{row['Question']}"
options = []
for i, opt in enumerate(['Option 1', 'Option 2', 'Option 3', 'Option 4', 'Option 5']):
if pd.isna(row[opt]):
break
options.append(f"{alpa[i]} {row[opt]}")
inputs.append(
PROMPT.replace('[MAIN_META_DATA]', main_meta_data)\
.replace('[INPUT]', question)\
.replace('[OPTION]', '\n'.join(options))
)
idx_label = {'A': 0, 'B': 1, 'C': 2, 'D': 3, 'E': 4}[row['Answer Key']]
outputs.append(idx_label)
outputs_options.append(options)
return inputs, outputs, outputs_options
def prepare_data_ar(args):
PROMPT = 'هذا سؤال [MAIN_META_DATA]. اختر الإجابة الصحيحة!\n\nسؤال: [INPUT]\n[OPTION]'
if args.lora_weights == "x":
PROMPT = f'{PROMPT}\n\nإجابة: '
else:
PROMPT = f'### Input:{PROMPT}\n\n### Output:\n'
alpa = alpa_ar
if args.lang_alpa == 'en':
alpa = alpa_en
inputs = []
outputs = []
outputs_options = []
data = pd.read_csv('data/ArabicMMLU.csv')
data = data[data['is_few_shot'] == 0]
for idx, row in data.iterrows():
subject = subject_ar[row['Subject']]
level = "" if pd.isna(row['Level']) else ' ' + level_ar[row['Level']]
country = "" if pd.isna(row['Country']) else ' ' + country_ar[row['Country']]
main_meta_data = f"{subject}{level}{country}"
question = row['Question'] if pd.isna(row['Context']) else f"{row['Context']}\n\n{row['Question']}"
options = []
for i, opt in enumerate(['Option 1', 'Option 2', 'Option 3', 'Option 4', 'Option 5']):
if pd.isna(row[opt]):
break
options.append(f"{alpa[i]} {row[opt]}")
inputs.append(
PROMPT.replace('[MAIN_META_DATA]', main_meta_data)\
.replace('[INPUT]', question)\
.replace('[OPTION]', '\n'.join(options))
)
idx_label = {'A': 0, 'B': 1, 'C': 2, 'D': 3, 'E': 4}[row['Answer Key']]
outputs.append(idx_label)
outputs_options.append(options)
return inputs, outputs, outputs_options
def prepare_data(args):
if args.lang_prompt == 'en':
return prepare_data_en(args)
elif args.lang_prompt == 'ar':
return prepare_data_ar(args)