Anonymous commited on
Commit
d27fe32
·
1 Parent(s): afed1a5

format and clean code

Browse files
Files changed (6) hide show
  1. app.py +233 -80
  2. generate_prompt.py +33 -538
  3. tasks/ner.py +16 -27
  4. tasks/nli.py +19 -18
  5. tasks/qa.py +38 -83
  6. tasks/summarization.py +45 -23
app.py CHANGED
@@ -1,9 +1,9 @@
1
- import gradio as gr
2
  import os
 
 
3
  from openai import OpenAI
4
- from generate_prompt import construct_generic_prompt, recommend_config
5
 
6
- # Define available tasks and their corresponding datasets
7
 
8
  QA = "QA"
9
  SUMMARIZATION = "Summarization"
@@ -14,21 +14,59 @@ tasks_datasets = {
14
  QA: ["XQuad", "Indicqa"],
15
  SUMMARIZATION: ["XLSum", "HeSum"],
16
  NLI: ["XNLI"],
17
- NER: ["MasakaNER", "WikiANN"]
18
  }
19
 
20
  # List of all languages
21
  languages = [
22
- "English", "Spanish", "French", "German", "Chinese", "Japanese", "Korean", "Italian",
23
- "Portuguese", "Russian", "Arabic", "Hindi", "Bengali", "Turkish", "Vietnamese", "Polish",
24
- "Dutch", "Indonesian", "Malay", "Thai", "Greek", "Swedish", "Hungarian", "Finnish",
25
- "Danish", "Norwegian", "Hebrew", "Czech", "Slovak", "Bulgarian", "Romanian", "Serbian",
26
- "Croatian", "Ukrainian", "Lithuanian", "Latvian", "Estonian", "Filipino", "Icelandic",
27
- "Irish", "Welsh", "Maltese", "Swahili", "Zulu", "Afrikaans"
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
28
  ]
29
 
30
 
31
-
32
  def get_datasets(task):
33
  return tasks_datasets.get(task, [])
34
 
@@ -39,16 +77,25 @@ with gr.Blocks(theme=gr.themes.Soft()) as demo:
39
 
40
  with gr.Accordion(label="Task Details", open=True):
41
  with gr.Row():
42
- task = gr.Dropdown(label="Task", choices=list(tasks_datasets.keys()), value=QA)
43
- language = gr.Dropdown(label="Source Language", choices=languages, value="English")
44
- model_type = gr.Dropdown(label="Model Type", choices=["Multilingual", "Standard"], value='Multilingual')
 
 
 
 
 
 
 
 
45
  config_recommendation = gr.Button("Recommend Configuration")
46
  with gr.Row():
47
- config_prompt = gr.Textbox(label="Recommended Configuration", interactive=False,
48
- placeholder="Recommended Configuration for this scenerio")
 
 
 
49
  with gr.Row():
50
- # examples_selection = gr.Dropdown(["English", "Source"], label="examples", value='English')
51
- # output_selection = gr.Dropdown(["English", "Source"], label="output", value='English')
52
  with gr.Accordion(label="Prompt Template", open=True):
53
  with gr.Column(scale=2):
54
  # Set the same background style across all components
@@ -56,16 +103,41 @@ with gr.Blocks(theme=gr.themes.Soft()) as demo:
56
  instruction = gr.Textbox(label="Instruction")
57
  with gr.Row(variant="panel"):
58
  zero_shot = gr.Checkbox(label="Zero Shot Setting", value=False)
59
- with gr.Accordion("Few Shot - Select Type of Examples ", open=False, visible=True) as few_shot:
60
- dataset = gr.Dropdown(label="Dataset", choices=tasks_datasets[QA], value="XlSum")
61
- num_examples = gr.Slider(label="Number of examples in context", minimum=1, maximum=10, step=1,
62
- value=3)
 
 
 
 
 
 
 
 
 
 
 
 
 
63
  with gr.Row(equal_height=True, variant="panel"):
64
- with gr.Accordion(label="Language Component Selection", open=False):
65
- prefix_selection = gr.Dropdown(["English", "Source"], label="instruction", value='English')
66
- context_selection = gr.Dropdown(["English", "Source"], label="context", value='English')
67
- examples_selection = gr.Dropdown(["English", "Source"], label="examples", value='English')
68
- output_selection = gr.Dropdown(["English", "Source"], label="output", value='English')
 
 
 
 
 
 
 
 
 
 
 
 
69
  # Accordion for Few Shot example selection
70
  with gr.Accordion(label="Prompt Input Data", open=False):
71
  question = gr.Textbox(label="Question", visible=True)
@@ -78,87 +150,145 @@ with gr.Blocks(theme=gr.themes.Soft()) as demo:
78
  generate_button = gr.Button("Generate Prompt")
79
 
80
  with gr.Row():
81
- prompt = gr.Textbox(label="Generated Prompt", interactive=False, placeholder="Generated prompt will appear here.")
82
-
 
 
 
83
 
84
  def update_datasets(selected_task):
85
  return gr.Dropdown(choices=get_datasets(selected_task))
86
 
87
-
88
  def toggle_task_inputs(selected_task):
89
  if selected_task == QA:
90
  return (
91
- gr.update(visible=True), gr.update(visible=True), gr.update(visible=False),
92
- gr.update(visible=False), gr.update(visible=False), gr.update(visible=False)
 
 
 
 
93
  )
94
  elif selected_task == SUMMARIZATION:
95
  return (
96
- gr.update(visible=False), gr.update(visible=False), gr.update(visible=True),
97
- gr.update(visible=False), gr.update(visible=False), gr.update(visible=False)
 
 
 
 
98
  )
99
  elif selected_task == NER:
100
  return (
101
- gr.update(visible=False), gr.update(visible=False), gr.update(visible=False),
102
- gr.update(visible=True), gr.update(visible=False), gr.update(visible=False)
 
 
 
 
103
  )
104
  else:
105
  return (
106
- gr.update(visible=False), gr.update(visible=False), gr.update(visible=False),
107
- gr.update(visible=False), gr.update(visible=True), gr.update(visible=True)
 
 
 
 
108
  )
109
 
110
-
111
  def toggle_num_examples(zero_shot_value):
112
  # If zero_shot is True, hide the num_examples slider
113
  return gr.update(visible=not zero_shot_value)
114
 
115
  def update_language_selection(language):
116
- return gr.update(choices=list({'English', language})), gr.update(choices=list({'English', language})), gr.update(choices=list({'English', language})), gr.update(choices=list({'English', language}))
 
 
 
 
 
117
 
118
- def generatePrompt(instruction, num_examples, zero_shot,
119
- task, selected_language, dataset, prefix_selection, context_selection, examples_selection, output_selection,
120
- text, question, context, sentence, hypothesis, premise):
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
121
 
122
- config = {'prefix': str.lower(prefix_selection), 'input': str.lower(context_selection), 'context': str.lower(examples_selection), 'output': str.lower(output_selection)}
 
 
 
 
 
123
 
124
  if task == QA:
125
  text_example = {
126
- 'context': context,
127
- 'question': question,
128
  }
129
  elif task == SUMMARIZATION:
130
  text_example = {
131
- 'text': text,
132
  }
133
  elif task == NER:
134
- text_example = {
135
- 'tokens': sentence,
136
- 'ner_tags': ''
137
- }
138
  else:
139
- text_example = {
140
- 'hypothesis': hypothesis,
141
- 'premise': premise
142
- }
143
 
144
- prompt = construct_generic_prompt(task, instruction, text_example, zero_shot, num_examples, selected_language, dataset, config)
 
 
 
 
 
 
 
 
 
145
 
146
  return prompt
147
 
148
-
149
- def respond(message, openai_key, url, chat_history, model, config_input, config_prefix, config_context,
150
- config_output, task, dataset, language, num_examples, zero_shot):
 
 
 
 
 
 
 
 
 
 
 
 
 
151
  os.environ["OPENAI_API_KEY"] = openai_key
152
  client = OpenAI()
153
 
154
  config = {
155
  "input": config_input,
156
  "prefix": config_prefix,
157
- "context": config_context.split(', '),
158
  "output": config_output,
159
  "language": language,
160
  "num_examples": num_examples,
161
- "zero_shot": zero_shot
162
  }
163
 
164
  response = client.chat.completions.create(
@@ -171,7 +301,7 @@ with gr.Blocks(theme=gr.themes.Soft()) as demo:
171
  {"type": "image_url", "image_url": url},
172
  {"type": "config", "config": config},
173
  {"type": "task", "text": task},
174
- {"type": "dataset", "text": dataset}
175
  ],
176
  },
177
  ],
@@ -183,37 +313,60 @@ with gr.Blocks(theme=gr.themes.Soft()) as demo:
183
  chat_history.append((message, out))
184
  return "", chat_history
185
 
186
-
187
  # Bind functions to dropdown changes and button click
188
  # task.change(fn=update_datasets, outputs=dataset)
189
- language.change(fn=update_language_selection, inputs=language, outputs=[prefix_selection, context_selection, examples_selection, output_selection])
 
 
 
 
 
 
 
 
 
190
 
191
  zero_shot.change(fn=toggle_num_examples, inputs=zero_shot, outputs=few_shot)
192
  zero_shot.change(fn=toggle_num_examples, inputs=zero_shot, outputs=num_examples)
193
  task.change(fn=update_datasets, inputs=task, outputs=dataset)
194
- task.change(fn=toggle_task_inputs, inputs=task, outputs=[
195
- question, context, text, sentence, hypothesis, premise,
196
- ])
 
 
 
 
 
 
 
 
 
197
  generate_button.click(
198
  generatePrompt,
199
  inputs=[
200
- instruction, num_examples, zero_shot,
201
- task, language, dataset, prefix_selection, context_selection, examples_selection, output_selection,
202
- text, question, context, sentence, hypothesis, premise
203
-
 
 
 
 
 
 
 
 
 
 
 
 
204
  ],
205
- outputs=[prompt]
206
  )
207
 
208
  config_recommendation.click(
209
- recommend_config,
210
- inputs=[
211
- task,
212
- language,
213
- model_type
214
- ],
215
- outputs=[config_prompt]
216
  )
217
 
218
- if __name__ == '__main__':
219
  demo.launch(share=True)
 
 
1
  import os
2
+
3
+ import gradio as gr
4
  from openai import OpenAI
 
5
 
6
+ from generate_prompt import construct_generic_prompt, recommend_config
7
 
8
  QA = "QA"
9
  SUMMARIZATION = "Summarization"
 
14
  QA: ["XQuad", "Indicqa"],
15
  SUMMARIZATION: ["XLSum", "HeSum"],
16
  NLI: ["XNLI"],
17
+ NER: ["MasakaNER", "WikiANN"],
18
  }
19
 
20
  # List of all languages
21
  languages = [
22
+ "English",
23
+ "Spanish",
24
+ "French",
25
+ "German",
26
+ "Chinese",
27
+ "Japanese",
28
+ "Korean",
29
+ "Italian",
30
+ "Portuguese",
31
+ "Russian",
32
+ "Arabic",
33
+ "Hindi",
34
+ "Bengali",
35
+ "Turkish",
36
+ "Vietnamese",
37
+ "Polish",
38
+ "Dutch",
39
+ "Indonesian",
40
+ "Malay",
41
+ "Thai",
42
+ "Greek",
43
+ "Swedish",
44
+ "Hungarian",
45
+ "Finnish",
46
+ "Danish",
47
+ "Norwegian",
48
+ "Hebrew",
49
+ "Czech",
50
+ "Slovak",
51
+ "Bulgarian",
52
+ "Romanian",
53
+ "Serbian",
54
+ "Croatian",
55
+ "Ukrainian",
56
+ "Lithuanian",
57
+ "Latvian",
58
+ "Estonian",
59
+ "Filipino",
60
+ "Icelandic",
61
+ "Irish",
62
+ "Welsh",
63
+ "Maltese",
64
+ "Swahili",
65
+ "Zulu",
66
+ "Afrikaans",
67
  ]
68
 
69
 
 
70
  def get_datasets(task):
71
  return tasks_datasets.get(task, [])
72
 
 
77
 
78
  with gr.Accordion(label="Task Details", open=True):
79
  with gr.Row():
80
+ task = gr.Dropdown(
81
+ label="Task", choices=list(tasks_datasets.keys()), value=QA
82
+ )
83
+ language = gr.Dropdown(
84
+ label="Source Language", choices=languages, value="English"
85
+ )
86
+ model_type = gr.Dropdown(
87
+ label="Model Type",
88
+ choices=["Multilingual", "Standard"],
89
+ value="Multilingual",
90
+ )
91
  config_recommendation = gr.Button("Recommend Configuration")
92
  with gr.Row():
93
+ config_prompt = gr.Textbox(
94
+ label="Recommended Configuration",
95
+ interactive=False,
96
+ placeholder="Recommended Configuration for this scenerio",
97
+ )
98
  with gr.Row():
 
 
99
  with gr.Accordion(label="Prompt Template", open=True):
100
  with gr.Column(scale=2):
101
  # Set the same background style across all components
 
103
  instruction = gr.Textbox(label="Instruction")
104
  with gr.Row(variant="panel"):
105
  zero_shot = gr.Checkbox(label="Zero Shot Setting", value=False)
106
+ with gr.Accordion(
107
+ "Few Shot - Select Type of Examples ",
108
+ open=False,
109
+ visible=True,
110
+ ) as few_shot:
111
+ dataset = gr.Dropdown(
112
+ label="Dataset",
113
+ choices=tasks_datasets[QA],
114
+ value="XlSum",
115
+ )
116
+ num_examples = gr.Slider(
117
+ label="Number of examples in context",
118
+ minimum=1,
119
+ maximum=10,
120
+ step=1,
121
+ value=3,
122
+ )
123
  with gr.Row(equal_height=True, variant="panel"):
124
+ with gr.Accordion(
125
+ label="Language Component Selection", open=False
126
+ ):
127
+ prefix_selection = gr.Dropdown(
128
+ ["English", "Source"],
129
+ label="instruction",
130
+ value="English",
131
+ )
132
+ context_selection = gr.Dropdown(
133
+ ["English", "Source"], label="context", value="English"
134
+ )
135
+ examples_selection = gr.Dropdown(
136
+ ["English", "Source"], label="examples", value="English"
137
+ )
138
+ output_selection = gr.Dropdown(
139
+ ["English", "Source"], label="output", value="English"
140
+ )
141
  # Accordion for Few Shot example selection
142
  with gr.Accordion(label="Prompt Input Data", open=False):
143
  question = gr.Textbox(label="Question", visible=True)
 
150
  generate_button = gr.Button("Generate Prompt")
151
 
152
  with gr.Row():
153
+ prompt = gr.Textbox(
154
+ label="Generated Prompt",
155
+ interactive=False,
156
+ placeholder="Generated prompt will appear here.",
157
+ )
158
 
159
  def update_datasets(selected_task):
160
  return gr.Dropdown(choices=get_datasets(selected_task))
161
 
 
162
  def toggle_task_inputs(selected_task):
163
  if selected_task == QA:
164
  return (
165
+ gr.update(visible=True),
166
+ gr.update(visible=True),
167
+ gr.update(visible=False),
168
+ gr.update(visible=False),
169
+ gr.update(visible=False),
170
+ gr.update(visible=False),
171
  )
172
  elif selected_task == SUMMARIZATION:
173
  return (
174
+ gr.update(visible=False),
175
+ gr.update(visible=False),
176
+ gr.update(visible=True),
177
+ gr.update(visible=False),
178
+ gr.update(visible=False),
179
+ gr.update(visible=False),
180
  )
181
  elif selected_task == NER:
182
  return (
183
+ gr.update(visible=False),
184
+ gr.update(visible=False),
185
+ gr.update(visible=False),
186
+ gr.update(visible=True),
187
+ gr.update(visible=False),
188
+ gr.update(visible=False),
189
  )
190
  else:
191
  return (
192
+ gr.update(visible=False),
193
+ gr.update(visible=False),
194
+ gr.update(visible=False),
195
+ gr.update(visible=False),
196
+ gr.update(visible=True),
197
+ gr.update(visible=True),
198
  )
199
 
 
200
  def toggle_num_examples(zero_shot_value):
201
  # If zero_shot is True, hide the num_examples slider
202
  return gr.update(visible=not zero_shot_value)
203
 
204
  def update_language_selection(language):
205
+ return (
206
+ gr.update(choices=list({"English", language})),
207
+ gr.update(choices=list({"English", language})),
208
+ gr.update(choices=list({"English", language})),
209
+ gr.update(choices=list({"English", language})),
210
+ )
211
 
212
+ def generatePrompt(
213
+ instruction,
214
+ num_examples,
215
+ zero_shot,
216
+ task,
217
+ selected_language,
218
+ dataset,
219
+ prefix_selection,
220
+ context_selection,
221
+ examples_selection,
222
+ output_selection,
223
+ text,
224
+ question,
225
+ context,
226
+ sentence,
227
+ hypothesis,
228
+ premise,
229
+ ):
230
 
231
+ config = {
232
+ "prefix": str.lower(prefix_selection),
233
+ "input": str.lower(context_selection),
234
+ "context": str.lower(examples_selection),
235
+ "output": str.lower(output_selection),
236
+ }
237
 
238
  if task == QA:
239
  text_example = {
240
+ "context": context,
241
+ "question": question,
242
  }
243
  elif task == SUMMARIZATION:
244
  text_example = {
245
+ "text": text,
246
  }
247
  elif task == NER:
248
+ text_example = {"tokens": sentence, "ner_tags": ""}
 
 
 
249
  else:
250
+ text_example = {"hypothesis": hypothesis, "premise": premise}
 
 
 
251
 
252
+ prompt = construct_generic_prompt(
253
+ task,
254
+ instruction,
255
+ text_example,
256
+ zero_shot,
257
+ num_examples,
258
+ selected_language,
259
+ dataset,
260
+ config,
261
+ )
262
 
263
  return prompt
264
 
265
+ def respond(
266
+ message,
267
+ openai_key,
268
+ url,
269
+ chat_history,
270
+ model,
271
+ config_input,
272
+ config_prefix,
273
+ config_context,
274
+ config_output,
275
+ task,
276
+ dataset,
277
+ language,
278
+ num_examples,
279
+ zero_shot,
280
+ ):
281
  os.environ["OPENAI_API_KEY"] = openai_key
282
  client = OpenAI()
283
 
284
  config = {
285
  "input": config_input,
286
  "prefix": config_prefix,
287
+ "context": config_context.split(", "),
288
  "output": config_output,
289
  "language": language,
290
  "num_examples": num_examples,
291
+ "zero_shot": zero_shot,
292
  }
293
 
294
  response = client.chat.completions.create(
 
301
  {"type": "image_url", "image_url": url},
302
  {"type": "config", "config": config},
303
  {"type": "task", "text": task},
304
+ {"type": "dataset", "text": dataset},
305
  ],
306
  },
307
  ],
 
313
  chat_history.append((message, out))
314
  return "", chat_history
315
 
 
316
  # Bind functions to dropdown changes and button click
317
  # task.change(fn=update_datasets, outputs=dataset)
318
+ language.change(
319
+ fn=update_language_selection,
320
+ inputs=language,
321
+ outputs=[
322
+ prefix_selection,
323
+ context_selection,
324
+ examples_selection,
325
+ output_selection,
326
+ ],
327
+ )
328
 
329
  zero_shot.change(fn=toggle_num_examples, inputs=zero_shot, outputs=few_shot)
330
  zero_shot.change(fn=toggle_num_examples, inputs=zero_shot, outputs=num_examples)
331
  task.change(fn=update_datasets, inputs=task, outputs=dataset)
332
+ task.change(
333
+ fn=toggle_task_inputs,
334
+ inputs=task,
335
+ outputs=[
336
+ question,
337
+ context,
338
+ text,
339
+ sentence,
340
+ hypothesis,
341
+ premise,
342
+ ],
343
+ )
344
  generate_button.click(
345
  generatePrompt,
346
  inputs=[
347
+ instruction,
348
+ num_examples,
349
+ zero_shot,
350
+ task,
351
+ language,
352
+ dataset,
353
+ prefix_selection,
354
+ context_selection,
355
+ examples_selection,
356
+ output_selection,
357
+ text,
358
+ question,
359
+ context,
360
+ sentence,
361
+ hypothesis,
362
+ premise,
363
  ],
364
+ outputs=[prompt],
365
  )
366
 
367
  config_recommendation.click(
368
+ recommend_config, inputs=[task, language, model_type], outputs=[config_prompt]
 
 
 
 
 
 
369
  )
370
 
371
+ if __name__ == "__main__":
372
  demo.launch(share=True)
generate_prompt.py CHANGED
@@ -1,31 +1,10 @@
1
- import collections
2
- import csv
3
  import enum
4
- import json
5
- import logging
6
- import os
7
- import re
8
- import string
9
- import sys
10
- import unicodedata
11
- from typing import Any, Dict, List, NewType, Union
12
 
13
- import numpy as np
14
- import openai
15
  import pandas as pd
16
- import requests
17
- import yaml
18
- from datasets import Dataset, load_dataset
19
- from easygoogletranslate import EasyGoogleTranslate
20
- from langchain.prompts import FewShotPromptTemplate, PromptTemplate
21
- from tqdm import tqdm
22
- from yaml.loader import SafeLoader
23
 
24
- from tasks import ner, summarization, qa, nli
25
 
26
 
27
- # from models.model_completion import gpt3x_completion, gemini_completion
28
-
29
  class LanguageType(enum.Enum):
30
  Low = "Low"
31
  High = "High"
@@ -36,504 +15,6 @@ class ModelType(enum.Enum):
36
  Multilingual = "Multilingual"
37
 
38
 
39
- def get_entities_gpt3_long(prompt):
40
- response = openai.ChatCompletion.create(
41
- engine="chatgpt", temperature=0, messages=[{"role": "user", "content": prompt}]
42
- )
43
- return response["choices"][0]["message"]["content"]
44
-
45
-
46
- def gpt3x_completion(
47
- prompt: Union[str, List[Dict[str, str]]],
48
- ) -> str:
49
- import os
50
- import openai
51
- os.environ["OPENAI_API_KEY"] = '07d805ec4fbd484ebc923a3a41e1773d'
52
- OPENAI_API_KEY = '07d805ec4fbd484ebc923a3a41e1773d'
53
- openai.api_type = "azure"
54
- openai.api_base = 'https://hebsum-itaim-uks.openai.azure.com/'
55
- openai.api_version = "2023-03-15-preview"
56
- openai.api_key = '07d805ec4fbd484ebc923a3a41e1773d'
57
-
58
- def get_entities_chatGPT(final_prompt):
59
- response = openai.ChatCompletion.create(
60
- engine="gpt35-16k",
61
- temperature=0,
62
- messages=[
63
- {"role": "user", "content": final_prompt}
64
- ]
65
- )
66
- return response['choices'][0]['message']['content']
67
-
68
- return get_entities_chatGPT(final_prompt=prompt)
69
-
70
-
71
- def mixtral_completion(prompt):
72
- url = "https://api.together.xyz/v1/chat/completions"
73
-
74
- # Define your Together API key
75
- together_api_key = "851cfc39f3d7a246a2342259f5f6fbba4721c6002123365fba2254c9c9c424ad" # Replace with your actual API key
76
-
77
- # Define the request payload
78
- payload = {
79
- "temperature": 0,
80
- "max_tokens": 30,
81
- "model": "mistralai/Mixtral-8x7B-Instruct-v0.1",
82
- "messages": [{"role": "user", "content": f"{prompt}"}],
83
- }
84
-
85
- # Define request headers
86
- headers = {
87
- "Authorization": f"Bearer {together_api_key}",
88
- "Content-Type": "application/json",
89
- }
90
-
91
- # Send POST request
92
- response = requests.post(url, json=payload, headers=headers)
93
-
94
- # Check response status
95
- if response.status_code == 200:
96
- # Print the response content (API output)
97
- return response.json()["choices"][0]["message"]["content"]
98
- else:
99
- # Print error message if request fails
100
- print(f"Error: {response.status_code} - {response.text}")
101
-
102
-
103
- XQUAD_LANG2CODES = {
104
- "bengali": "bn",
105
- "korean": "ko",
106
- "swahili": "sw",
107
- "english": "en",
108
- "indonesian": "id",
109
- "arabic": "ar",
110
- "finnish": "fi",
111
- "telugu": "te",
112
- "russian": "ru",
113
- "german": "de",
114
- "greek": "el",
115
- "hindi": "hi",
116
- "vietnamese": "vi",
117
- "romanian": "ro",
118
- }
119
-
120
- INDICQA_LANG2CODES = {
121
- "indicqa": "as",
122
- "bengali": "bn",
123
- "gujarati": "gu",
124
- "hindi": "hi",
125
- "kannada": "kn",
126
- "malayalam": "ml",
127
- "marathi": "mr",
128
- "odia": "or",
129
- "punjabi": "pa",
130
- "tamil": "ta",
131
- "telugu": "te",
132
- "assamese": "as",
133
- }
134
-
135
- PUNCT = {
136
- chr(i)
137
- for i in range(sys.maxunicode)
138
- if unicodedata.category(chr(i)).startswith("P")
139
- }.union(string.punctuation)
140
- WHITESPACE_LANGS = ["en", "es", "hi", "vi", "de", "ar"]
141
- MIXED_SEGMENTATION_LANGS = ["zh"]
142
-
143
- TYDIQA_LANG2CODES = {
144
- "bengali": "bn",
145
- "korean": "ko",
146
- "swahili": "sw",
147
- "english": "en",
148
- "indonesian": "id",
149
- "arabic": "ar",
150
- "finnish": "fi",
151
- "telugu": "te",
152
- "russian": "ru",
153
- "assamese": "as",
154
- "persian": "fa",
155
- }
156
-
157
- logger = logging.Logger("Xlsum_task")
158
- LANGUAGE_TO_SUFFIX = {
159
- "chinese_simplified": "zh-CN",
160
- "french": "fr",
161
- "portuguese": "pt",
162
- "english": "en",
163
- "arabic": "ar",
164
- "hindi": "hi",
165
- "indonesian": "id",
166
- "amharic": "am",
167
- "bengali": "bn",
168
- "telugu": "te",
169
- "burmese": "my",
170
- "german": "de",
171
- "greek": "el",
172
- "tamil": "ta",
173
- "assamese": "as",
174
- "hindi": "hi",
175
- "vietnamese": "vi",
176
- "russian": "ru",
177
- "telugu": "te",
178
- "romanian": "ro",
179
- "malayalam": "ml",
180
- "persian": "fa",
181
- }
182
-
183
- PARAMS = NewType("PARAMS", Dict[str, Any])
184
-
185
-
186
- def read_parameters(args_path) -> PARAMS:
187
- with open(args_path) as f:
188
- args = yaml.load(f, Loader=SafeLoader)
189
- return args
190
-
191
-
192
- def load_qa_dataset(dataset_name, lang, split, translate_test=False, limit=5):
193
- if dataset_name == "indicqa":
194
- if split != "train":
195
- dataset = load_dataset(
196
- "ai4bharat/IndicQA", f"indicqa.{INDICQA_LANG2CODES[lang]}"
197
- )[split]
198
- else:
199
- dataset = load_dataset("squad_v2")[split]
200
- elif dataset_name == "xquad":
201
- if split != "train":
202
- dataset = load_dataset("xquad", f"xquad.{XQUAD_LANG2CODES[lang]}")[
203
- "validation"
204
- ]
205
- else:
206
- dataset = load_dataset("squad")[split]
207
- elif dataset_name == "tydiqa":
208
- dataset = load_dataset("tydiqa", "secondary_task")[split]
209
- dataset = dataset.map(
210
- lambda example: {"lang": TYDIQA_LANG2CODES[example["id"].split("-")[0]]}
211
- )
212
- dataset = dataset.filter(lambda example: example["lang"] == lang)
213
- elif dataset_name == "mlqa":
214
- if split == "train":
215
- print("No Training Data for MLQA, switching to validation!")
216
- split = "validation"
217
- if translate_test:
218
- dataset_name = f"mlqa-translate-test.{lang}"
219
- else:
220
- dataset_name = f"mlqa.{lang}.{lang}"
221
-
222
- dataset = load_dataset("mlqa", dataset_name)[split]
223
-
224
- else:
225
- raise NotImplementedError()
226
- return dataset.select(np.arange(limit))
227
-
228
-
229
- def construct_prompt(
230
- instruction: str,
231
- test_example: dict,
232
- ic_examples: List[dict],
233
- zero_shot: bool,
234
- lang: str,
235
- config: Dict[Any, Any],
236
- ):
237
- example_prompt = PromptTemplate(
238
- input_variables=["context", "question", "answers"],
239
- template="Context: {context}\nQuestion: {question}\n" "Answers: {answers}",
240
- )
241
-
242
- zero_shot_template = (
243
- f"""{instruction}""" + "\n<Context>: {context} \n<Question>: {question} " ""
244
- )
245
-
246
- prompt = (
247
- FewShotPromptTemplate(
248
- examples=ic_examples,
249
- prefix=instruction,
250
- example_prompt=example_prompt,
251
- suffix="<Context>: {context} \n<Question>: {question} \nAnswers: ?",
252
- input_variables=["question", "context"],
253
- )
254
- if not zero_shot
255
- else PromptTemplate(
256
- input_variables=["question", "context"], template=zero_shot_template
257
- )
258
- )
259
-
260
- label = test_example["answers"]
261
- if config["input"] != lang:
262
- test_example = _translate_example(
263
- example=test_example, src_language=lang, target_language=config["input"]
264
- )
265
-
266
- return (
267
- prompt.format(
268
- question=test_example["question"], context=test_example["context"]
269
- ),
270
- label,
271
- )
272
-
273
-
274
- def dump_metrics(
275
- lang: str, config: Dict[str, str], f1: float, em: float, metric_logger_path: str
276
- ):
277
- # Check if the metric logger file exists
278
- file_exists = os.path.exists(metric_logger_path)
279
-
280
- # Open the CSV file in append mode
281
- with open(metric_logger_path, "a", newline="") as f:
282
- csvwriter = csv.writer(f, delimiter=",")
283
-
284
- # Write header row if the file is newly created
285
- if not file_exists:
286
- header = ["Language", "Prefix", "Input", "Context", "Output", "F1", "Em"]
287
- csvwriter.writerow(header)
288
-
289
- csvwriter.writerow(
290
- [
291
- lang,
292
- config["prefix"],
293
- config["input"],
294
- config["context"][0],
295
- config["output"],
296
- f1,
297
- em,
298
- ]
299
- )
300
-
301
-
302
- def dump_predictions(idx, response, label, response_logger_file):
303
- obj = {"q_idx": idx, "prediction": response, "label": label}
304
- with open(response_logger_file, "a") as f:
305
- f.write(json.dumps(obj, ensure_ascii=False) + "\n")
306
-
307
-
308
- def _translate_instruction(basic_instruction: str, target_language: str) -> str:
309
- translator = EasyGoogleTranslate(
310
- source_language="en",
311
- target_language=LANGUAGE_TO_SUFFIX[target_language],
312
- timeout=50,
313
- )
314
- return translator.translate(basic_instruction)
315
-
316
-
317
- def _translate_prediction_to_output_language(
318
- prediction: str, prediction_language: str, output_language: str
319
- ) -> str:
320
- translator = EasyGoogleTranslate(
321
- source_language=LANGUAGE_TO_SUFFIX[prediction_language],
322
- target_language=LANGUAGE_TO_SUFFIX[output_language],
323
- timeout=10,
324
- )
325
- return translator.translate(prediction)
326
-
327
-
328
- def create_instruction(lang: str, expected_output: str):
329
- basic_instruction = (
330
- "Answer to the <Question> below, based only to the given <Context>, Follow these instructions:\n"
331
- "1. The answer should include only words from the given context\n"
332
- "2. The answer must include up to 5 words\n"
333
- "3. The answer Should be the shortest as possible\n"
334
- f"4. The answer must be in {expected_output} only!, not another language!!!"
335
- )
336
- return (
337
- basic_instruction
338
- if lang == "english"
339
- else _translate_instruction(basic_instruction, target_language=lang)
340
- )
341
-
342
-
343
- def _translate_example(
344
- example: Dict[str, str], src_language: str, target_language: str
345
- ):
346
- translator = EasyGoogleTranslate(
347
- source_language=LANGUAGE_TO_SUFFIX[str(src_language).lower()],
348
- target_language=LANGUAGE_TO_SUFFIX[str(target_language).lower()],
349
- timeout=30,
350
- )
351
-
352
- return {
353
- "question": translator.translate(example["question"]),
354
- "context": translator.translate(example["context"][:2000])
355
- + translator.translate(example["context"][2000:4000])
356
- + translator.translate(example["context"][4000:6000]),
357
- "answers": translator.translate(example["answers"][0]),
358
- }
359
- # except Exception as e:
360
- # print(example["text"])
361
- # print(example["summary"])
362
- # print(e)
363
-
364
-
365
- def choose_few_shot_examples(
366
- train_dataset: Dataset,
367
- few_shot_size: int,
368
- context: List[str],
369
- selection_criteria: str,
370
- lang: str,
371
- ) -> List[Dict[str, Union[str, int]]]:
372
- """Selects few-shot examples from training datasets
373
-
374
- Args:
375
- train_dataset (Dataset): Training Dataset
376
- few_shot_size (int): Number of few-shot examples
377
- selection_criteria (few_shot_selection): How to select few-shot examples. Choices: [random, first_k]
378
-
379
- Returns:
380
- List[Dict[str, Union[str, int]]]: Selected examples
381
- """
382
- selected_examples = []
383
-
384
- example_idxs = []
385
- if selection_criteria == "first_k":
386
- example_idxs = list(range(few_shot_size))
387
- elif selection_criteria == "random":
388
- example_idxs = (
389
- np.random.choice(len(train_dataset), size=few_shot_size, replace=True)
390
- .astype(int)
391
- .tolist()
392
- )
393
-
394
- ic_examples = [
395
- {
396
- "question": train_dataset[idx]["question"],
397
- "context": train_dataset[idx]["context"],
398
- "answers": train_dataset[idx]["answers"]["text"],
399
- }
400
- for idx in example_idxs
401
- ]
402
-
403
- for idx, ic_language in enumerate(context):
404
- (
405
- selected_examples.append(ic_examples[idx])
406
- if ic_language == lang
407
- else (
408
- selected_examples.append(
409
- _translate_example(
410
- example=ic_examples[idx],
411
- src_language=lang,
412
- target_language=ic_language,
413
- )
414
- )
415
- )
416
- )
417
-
418
- return selected_examples
419
-
420
-
421
- def normalize_answer(s):
422
- """Lower text and remove punctuation, articles and extra whitespace."""
423
-
424
- def remove_articles(text):
425
- return re.sub(r"\b(a|an|the)\b", " ", text)
426
-
427
- def white_space_fix(text):
428
- return " ".join(text.split())
429
-
430
- def remove_punc(text):
431
- exclude = set(PUNCT) # set(string.punctuation)
432
- return "".join(ch for ch in text if ch not in exclude)
433
-
434
- def lower(text):
435
- return text.lower()
436
-
437
- return white_space_fix(remove_articles(remove_punc(lower(s))))
438
-
439
-
440
- def process_test_example(
441
- test_data, config_header, idx, test_example, config, zero_shot, lang, params
442
- ):
443
- try:
444
- # Your existing code for processing each test example
445
- instruction = create_instruction(
446
- lang=config["prefix"], expected_output=config["output"]
447
- )
448
- text_example = {
449
- "question": test_example["question"],
450
- "context": test_example["context"],
451
- "answers": test_example["answers"]["text"],
452
- }
453
-
454
- ic_examples = []
455
- if not zero_shot:
456
- ic_examples = choose_few_shot_examples(
457
- train_dataset=test_data,
458
- few_shot_size=len(config["context"]),
459
- context=config["context"],
460
- selection_criteria="random",
461
- lang=params["selected_language"],
462
- )
463
-
464
- prompt, label = construct_prompt(
465
- instruction=instruction,
466
- test_example=text_example,
467
- ic_examples=ic_examples,
468
- zero_shot=zero_shot,
469
- lang=lang,
470
- config=config,
471
- )
472
-
473
- pred = gpt3x_completion(prompt=prompt)
474
- print(pred)
475
-
476
- logger.info("Saving prediction to persistent volume")
477
- os.makedirs(
478
- f"{params['response_logger_root']}/{params['model']}/{lang}", exist_ok=True
479
- )
480
- dump_predictions(
481
- idx=idx,
482
- response=pred,
483
- label=label,
484
- response_logger_file=f"{params['response_logger_root']}/{params['model']}/{lang}/{config_header}.csv",
485
- )
486
- except Exception as e:
487
- # Handle exceptions here
488
- print(f"Error processing example {idx}: {e}")
489
-
490
-
491
- def run_one_configuration(selected_language, config, zero_shot, dataset_name, limit=10):
492
- test_data = load_qa_dataset(
493
- dataset_name=dataset_name,
494
- lang=selected_language,
495
- split="validation" if dataset_name == "xquad" else "test",
496
- limit=limit,
497
- )
498
-
499
- for idx, test_example in (pbar := tqdm(enumerate(test_data))):
500
- try:
501
- instruction = create_instruction(
502
- lang=config["prefix"], expected_output=config["output"]
503
- )
504
- text_example = {
505
- "question": test_example["question"],
506
- "context": test_example["context"],
507
- "answers": test_example["answers"]["text"],
508
- }
509
-
510
- ic_examples = []
511
- if not zero_shot:
512
- ic_examples = choose_few_shot_examples(
513
- train_dataset=test_data,
514
- few_shot_size=len(config["context"]),
515
- context=config["context"],
516
- selection_criteria="random",
517
- lang=selected_language,
518
- )
519
-
520
- prompt, label = construct_prompt(
521
- instruction=instruction,
522
- test_example=text_example,
523
- ic_examples=ic_examples,
524
- zero_shot=zero_shot,
525
- lang=selected_language,
526
- config=config,
527
- )
528
-
529
- pred = gpt3x_completion(prompt=prompt)
530
-
531
- return pred
532
-
533
- except Exception as e:
534
- print(f"Found an exception {e}, continue to the next example")
535
- continue
536
-
537
 
538
  QA = "QA"
539
  SUMMARIZATION = "Summarization"
@@ -541,8 +22,16 @@ NLI = "NLI"
541
  NER = "NER"
542
 
543
 
544
- def construct_generic_prompt(task, instruction, test_example, zero_shot, num_examples, selected_language, dataset,
545
- config):
 
 
 
 
 
 
 
 
546
  print(task)
547
  if task == SUMMARIZATION:
548
  prompt = summarization.construct_prompt(
@@ -588,30 +77,36 @@ def construct_generic_prompt(task, instruction, test_example, zero_shot, num_exa
588
 
589
  def _get_language_type(language: str):
590
  df = pd.read_csv("utils/languages_by_word_count.csv")
591
- number_of_words = df[df['Language'] == language]['number of words'].iloc[0]
592
  print(number_of_words)
593
  return LanguageType.Low if number_of_words < 150276400 else LanguageType.High
594
 
595
 
596
  class Config:
597
- def __init__(self, prefix="source", context="source", examples="source", output="source"):
 
 
598
  self.prefix = prefix
599
  self.context = context
600
  self.examples = examples
601
  self.output = output
602
 
603
  def set(self, prefix=None, context=None, examples=None, output=None):
604
- if prefix: self.prefix = prefix
605
- if context: self.context = context
606
- if examples: self.examples = examples
607
- if output: self.output = output
 
 
 
 
608
 
609
  def to_dict(self):
610
  return {
611
- 'instruction': self.prefix,
612
- 'context': self.context,
613
- 'examples': self.examples,
614
- 'output': self.output
615
  }
616
 
617
 
@@ -622,22 +117,22 @@ def recommend_config(task, lang, model_type):
622
  if model_type == ModelType.English.value:
623
  config.set(prefix=lang, context=lang, examples=lang, output=lang)
624
  else:
625
- config.set(prefix='English', context=lang, examples=lang, output=lang)
626
  if task == NER:
627
  if model_type == ModelType.English.value:
628
  config.set(prefix=lang, context=lang, examples=lang, output=lang)
629
  elif language_type == LanguageType.High:
630
- config.set(prefix='English', context=lang, examples=lang, output=lang)
631
  else:
632
- config.set(prefix='English', context=lang, examples=lang, output='English')
633
  if task == NLI:
634
  if model_type == ModelType.English.value:
635
  config.set(prefix=lang, context=lang, examples=lang, output=lang)
636
  elif language_type == LanguageType.High:
637
- config.set(prefix='English', context=lang, examples='English')
638
  else:
639
- config.set(prefix='English', context='English', examples='English')
640
  if task == SUMMARIZATION:
641
- config.set(context='English')
642
  print(config.to_dict())
643
  return config.to_dict()
 
 
 
1
  import enum
 
 
 
 
 
 
 
 
2
 
 
 
3
  import pandas as pd
 
 
 
 
 
 
 
4
 
5
+ from tasks import ner, nli, qa, summarization
6
 
7
 
 
 
8
  class LanguageType(enum.Enum):
9
  Low = "Low"
10
  High = "High"
 
15
  Multilingual = "Multilingual"
16
 
17
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
18
 
19
  QA = "QA"
20
  SUMMARIZATION = "Summarization"
 
22
  NER = "NER"
23
 
24
 
25
+ def construct_generic_prompt(
26
+ task,
27
+ instruction,
28
+ test_example,
29
+ zero_shot,
30
+ num_examples,
31
+ selected_language,
32
+ dataset,
33
+ config,
34
+ ):
35
  print(task)
36
  if task == SUMMARIZATION:
37
  prompt = summarization.construct_prompt(
 
77
 
78
  def _get_language_type(language: str):
79
  df = pd.read_csv("utils/languages_by_word_count.csv")
80
+ number_of_words = df[df["Language"] == language]["number of words"].iloc[0]
81
  print(number_of_words)
82
  return LanguageType.Low if number_of_words < 150276400 else LanguageType.High
83
 
84
 
85
  class Config:
86
+ def __init__(
87
+ self, prefix="source", context="source", examples="source", output="source"
88
+ ):
89
  self.prefix = prefix
90
  self.context = context
91
  self.examples = examples
92
  self.output = output
93
 
94
  def set(self, prefix=None, context=None, examples=None, output=None):
95
+ if prefix:
96
+ self.prefix = prefix
97
+ if context:
98
+ self.context = context
99
+ if examples:
100
+ self.examples = examples
101
+ if output:
102
+ self.output = output
103
 
104
  def to_dict(self):
105
  return {
106
+ "instruction": self.prefix,
107
+ "context": self.context,
108
+ "examples": self.examples,
109
+ "output": self.output,
110
  }
111
 
112
 
 
117
  if model_type == ModelType.English.value:
118
  config.set(prefix=lang, context=lang, examples=lang, output=lang)
119
  else:
120
+ config.set(prefix="English", context=lang, examples=lang, output=lang)
121
  if task == NER:
122
  if model_type == ModelType.English.value:
123
  config.set(prefix=lang, context=lang, examples=lang, output=lang)
124
  elif language_type == LanguageType.High:
125
+ config.set(prefix="English", context=lang, examples=lang, output=lang)
126
  else:
127
+ config.set(prefix="English", context=lang, examples=lang, output="English")
128
  if task == NLI:
129
  if model_type == ModelType.English.value:
130
  config.set(prefix=lang, context=lang, examples=lang, output=lang)
131
  elif language_type == LanguageType.High:
132
+ config.set(prefix="English", context=lang, examples="English")
133
  else:
134
+ config.set(prefix="English", context="English", examples="English")
135
  if task == SUMMARIZATION:
136
+ config.set(context="English")
137
  print(config.to_dict())
138
  return config.to_dict()
tasks/ner.py CHANGED
@@ -1,16 +1,12 @@
1
- from typing import List, Dict, Union
2
 
3
  import numpy as np
4
- from datasets import load_dataset, Dataset
5
  from easygoogletranslate import EasyGoogleTranslate
6
- from langchain.prompts import PromptTemplate, FewShotPromptTemplate
7
 
8
  LANGAUGE_TO_PREFIX = {
9
-
10
  "chinese_simplified": "zh-CN",
11
- "french": "fr",
12
- "portuguese": "pt",
13
- "english": "en",
14
  "arabic": "ar",
15
  "hindi": "hi",
16
  "indonesian": "id",
@@ -31,7 +27,6 @@ LANGAUGE_TO_PREFIX = {
31
  "greek": "el",
32
  "tamil": "ta",
33
  "assamese": "as",
34
- "vietnamese": "vi",
35
  "russian": "ru",
36
  "romanian": "ro",
37
  "malayalam": "ml",
@@ -39,16 +34,13 @@ LANGAUGE_TO_PREFIX = {
39
  "bulgarian": "bg",
40
  "thai": "th",
41
  "urdu": "ur",
42
- "italian": "it",
43
  "polish": "pl",
44
  "dutch": "nl",
45
- "swedish": "sv",
46
  "danish": "da",
47
  "norwegian": "no",
48
  "finnish": "fi",
49
  "hungarian": "hu",
50
  "czech": "cs",
51
- "slovak": "sk",
52
  "ukrainian": "uk",
53
  "bambara": "bam",
54
  "ewe": "ewe",
@@ -67,10 +59,9 @@ LANGAUGE_TO_PREFIX = {
67
  "portuguese": "pt",
68
  "chinese": "zh",
69
  "english": "en",
70
- "french": "fr"
71
  }
72
 
73
-
74
  def _translate_instruction(basic_instruction: str, target_language: str) -> str:
75
  translator = EasyGoogleTranslate(
76
  source_language="en",
@@ -104,7 +95,7 @@ def load_wikiann_dataset(lang, split, limit):
104
 
105
 
106
  def _translate_example(
107
- example: Dict[str, str], src_language: str, target_language: str
108
  ):
109
  translator = EasyGoogleTranslate(
110
  source_language=LANGAUGE_TO_PREFIX[src_language],
@@ -114,16 +105,16 @@ def _translate_example(
114
 
115
  return {
116
  "tokens": translator.translate(str(example["tokens"])),
117
- "ner_tags": translator.translate(str(example["ner_tags"]))
118
  }
119
 
120
 
121
  def choose_few_shot_examples(
122
- train_dataset: Dataset,
123
- few_shot_size: int,
124
- context: List[str],
125
- selection_criteria: str,
126
- lang: str,
127
  ) -> List[Dict[str, Union[str, int]]]:
128
  """Selects few-shot examples from training datasets
129
 
@@ -150,10 +141,7 @@ def choose_few_shot_examples(
150
  ic_examples = [train_dataset[idx] for idx in example_idxs]
151
 
152
  ic_examples = [
153
- {
154
- "tokens": ' '.join(example['tokens']),
155
- "ner_tags": example['spans']
156
- }
157
  for example in ic_examples
158
  ]
159
 
@@ -185,7 +173,7 @@ def construct_prompt(
185
  config: Dict[str, str],
186
  ):
187
  if not instruction:
188
- instruction = create_instruction(lang, config['prefix'], config['output'])
189
 
190
  example_prompt = PromptTemplate(
191
  input_variables=["tokens", "ner_tags"],
@@ -197,8 +185,9 @@ def construct_prompt(
197
  try:
198
  test_data = load_wikiann_dataset(lang=lang, split="test", limit=500)
199
  except Exception as e:
200
- raise KeyError(f"{lang} is not supported in 'wikiAnn' dataset, choose supported language in few-shot")
201
-
 
202
 
203
  ic_examples = []
204
  if not zero_shot:
 
1
+ from typing import Dict, List, Union
2
 
3
  import numpy as np
4
+ from datasets import Dataset, load_dataset
5
  from easygoogletranslate import EasyGoogleTranslate
6
+ from langchain.prompts import FewShotPromptTemplate, PromptTemplate
7
 
8
  LANGAUGE_TO_PREFIX = {
 
9
  "chinese_simplified": "zh-CN",
 
 
 
10
  "arabic": "ar",
11
  "hindi": "hi",
12
  "indonesian": "id",
 
27
  "greek": "el",
28
  "tamil": "ta",
29
  "assamese": "as",
 
30
  "russian": "ru",
31
  "romanian": "ro",
32
  "malayalam": "ml",
 
34
  "bulgarian": "bg",
35
  "thai": "th",
36
  "urdu": "ur",
 
37
  "polish": "pl",
38
  "dutch": "nl",
 
39
  "danish": "da",
40
  "norwegian": "no",
41
  "finnish": "fi",
42
  "hungarian": "hu",
43
  "czech": "cs",
 
44
  "ukrainian": "uk",
45
  "bambara": "bam",
46
  "ewe": "ewe",
 
59
  "portuguese": "pt",
60
  "chinese": "zh",
61
  "english": "en",
62
+ "french": "fr",
63
  }
64
 
 
65
  def _translate_instruction(basic_instruction: str, target_language: str) -> str:
66
  translator = EasyGoogleTranslate(
67
  source_language="en",
 
95
 
96
 
97
  def _translate_example(
98
+ example: Dict[str, str], src_language: str, target_language: str
99
  ):
100
  translator = EasyGoogleTranslate(
101
  source_language=LANGAUGE_TO_PREFIX[src_language],
 
105
 
106
  return {
107
  "tokens": translator.translate(str(example["tokens"])),
108
+ "ner_tags": translator.translate(str(example["ner_tags"])),
109
  }
110
 
111
 
112
  def choose_few_shot_examples(
113
+ train_dataset: Dataset,
114
+ few_shot_size: int,
115
+ context: List[str],
116
+ selection_criteria: str,
117
+ lang: str,
118
  ) -> List[Dict[str, Union[str, int]]]:
119
  """Selects few-shot examples from training datasets
120
 
 
141
  ic_examples = [train_dataset[idx] for idx in example_idxs]
142
 
143
  ic_examples = [
144
+ {"tokens": " ".join(example["tokens"]), "ner_tags": example["spans"]}
 
 
 
145
  for example in ic_examples
146
  ]
147
 
 
173
  config: Dict[str, str],
174
  ):
175
  if not instruction:
176
+ instruction = create_instruction(lang, config["prefix"], config["output"])
177
 
178
  example_prompt = PromptTemplate(
179
  input_variables=["tokens", "ner_tags"],
 
185
  try:
186
  test_data = load_wikiann_dataset(lang=lang, split="test", limit=500)
187
  except Exception as e:
188
+ raise KeyError(
189
+ f"{lang} is not supported in 'wikiAnn' dataset, choose supported language in few-shot"
190
+ )
191
 
192
  ic_examples = []
193
  if not zero_shot:
tasks/nli.py CHANGED
@@ -32,9 +32,7 @@ LANGUAGE_TO_SUFFIX = {
32
  "spanish": "es",
33
  "chinese": "zh",
34
  "greek": "el",
35
- "german": "de"
36
-
37
-
38
  }
39
 
40
  NUMBER_TO_TAG = {0: "entailment", 1: "neutral", 2: "contradiction"}
@@ -42,9 +40,6 @@ NUMBER_TO_TAG = {0: "entailment", 1: "neutral", 2: "contradiction"}
42
  PARAMS = NewType("PARAMS", Dict[str, Any])
43
 
44
 
45
-
46
-
47
-
48
  def read_parameters(args_path) -> PARAMS:
49
  with open(args_path) as f:
50
  args = yaml.load(f, Loader=SafeLoader)
@@ -278,7 +273,7 @@ def create_instruction(lang: str):
278
  )
279
 
280
 
281
- def run_one_configuration(params: Optional[PARAMS] = None, zero: bool= False):
282
  if not params:
283
  params = read_parameters("../../parameters.yaml")
284
 
@@ -320,6 +315,7 @@ def run_one_configuration(params: Optional[PARAMS] = None, zero: bool= False):
320
  pool.close()
321
  pool.join()
322
 
 
323
  def process_test_example(
324
  test_data, config_header, idx, test_example, config, zero_shot, lang, params
325
  ):
@@ -348,7 +344,9 @@ def process_test_example(
348
  zero_shot=zero_shot,
349
  )
350
 
351
- pred = get_prediction(prompt=prompt, endpoint_id=7327255438662041600, project_id=16514800572)
 
 
352
  print(pred)
353
 
354
  os.makedirs(
@@ -367,13 +365,13 @@ def process_test_example(
367
 
368
 
369
  def construct_prompt(
370
- instruction: str,
371
- test_example: dict,
372
- zero_shot: bool,
373
- num_examples: int,
374
- lang: str,
375
- config: Dict[str, str],
376
- dataset_name: str = 'xnli'
377
  ):
378
  if not instruction:
379
  print(lang)
@@ -385,13 +383,15 @@ def construct_prompt(
385
  )
386
 
387
  zero_shot_template = (
388
- f"""{instruction}""" + "\n Hypothesis: {hypothesis} + \n Premise: {premise}" ""
389
  )
390
  if not zero_shot:
391
  try:
392
  test_data = load_xnli_dataset(dataset_name, lang, split="test", limit=100)
393
  except KeyError as e:
394
- raise KeyError(f"{lang} is not supported in {dataset_name} dataset, choose supported language in few-shot")
 
 
395
 
396
  ic_examples = []
397
  if not zero_shot:
@@ -425,4 +425,5 @@ def construct_prompt(
425
  )
426
 
427
  return prompt.format(
428
- hypothesis=test_example["hypothesis"], premise=test_example["premise"])
 
 
32
  "spanish": "es",
33
  "chinese": "zh",
34
  "greek": "el",
35
+ "german": "de",
 
 
36
  }
37
 
38
  NUMBER_TO_TAG = {0: "entailment", 1: "neutral", 2: "contradiction"}
 
40
  PARAMS = NewType("PARAMS", Dict[str, Any])
41
 
42
 
 
 
 
43
  def read_parameters(args_path) -> PARAMS:
44
  with open(args_path) as f:
45
  args = yaml.load(f, Loader=SafeLoader)
 
273
  )
274
 
275
 
276
+ def run_one_configuration(params: Optional[PARAMS] = None, zero: bool = False):
277
  if not params:
278
  params = read_parameters("../../parameters.yaml")
279
 
 
315
  pool.close()
316
  pool.join()
317
 
318
+
319
  def process_test_example(
320
  test_data, config_header, idx, test_example, config, zero_shot, lang, params
321
  ):
 
344
  zero_shot=zero_shot,
345
  )
346
 
347
+ pred = get_prediction(
348
+ prompt=prompt, endpoint_id=7327255438662041600, project_id=16514800572
349
+ )
350
  print(pred)
351
 
352
  os.makedirs(
 
365
 
366
 
367
  def construct_prompt(
368
+ instruction: str,
369
+ test_example: dict,
370
+ zero_shot: bool,
371
+ num_examples: int,
372
+ lang: str,
373
+ config: Dict[str, str],
374
+ dataset_name: str = "xnli",
375
  ):
376
  if not instruction:
377
  print(lang)
 
383
  )
384
 
385
  zero_shot_template = (
386
+ f"""{instruction}""" + "\n Hypothesis: {hypothesis} + \n Premise: {premise}" ""
387
  )
388
  if not zero_shot:
389
  try:
390
  test_data = load_xnli_dataset(dataset_name, lang, split="test", limit=100)
391
  except KeyError as e:
392
+ raise KeyError(
393
+ f"{lang} is not supported in {dataset_name} dataset, choose supported language in few-shot"
394
+ )
395
 
396
  ic_examples = []
397
  if not zero_shot:
 
425
  )
426
 
427
  return prompt.format(
428
+ hypothesis=test_example["hypothesis"], premise=test_example["premise"]
429
+ )
tasks/qa.py CHANGED
@@ -10,8 +10,6 @@ import unicodedata
10
  from typing import Any, Dict, List, NewType, Optional, Union
11
 
12
  import numpy as np
13
- import openai
14
- import requests
15
  import yaml
16
  from datasets import Dataset, load_dataset
17
  from easygoogletranslate import EasyGoogleTranslate
@@ -20,52 +18,6 @@ from langchain.prompts import FewShotPromptTemplate, PromptTemplate
20
  from tqdm import tqdm
21
  from yaml.loader import SafeLoader
22
 
23
-
24
- # from models.model_completion import gpt3x_completion, gemini_completion
25
-
26
- def gemini_completion(prompt):
27
- # Define the endpoint URL
28
- genai.configure(api_key="")
29
- model = genai.GenerativeModel("models/gemini-1.0-pro-latest")
30
- return model.generate_content(prompt).text
31
-
32
-
33
-
34
- def get_entities_gpt3_long(prompt):
35
- response = openai.ChatCompletion.create(
36
- engine="chatgpt", temperature=0, messages=[{"role": "user", "content": prompt}]
37
- )
38
- return response["choices"][0]["message"]["content"]
39
-
40
-
41
- def gpt3x_completion(
42
- prompt: Union[str, List[Dict[str, str]]],
43
- model: str = "chatgpt",
44
- # run_details: Any = {},
45
- # num_evals_per_sec: int = 2,
46
- # **model_params,
47
- ) -> str:
48
- import openai
49
- def get_entities_chatGPT(final_prompt):
50
- response = openai.ChatCompletion.create(
51
- engine="gpt35-16k",
52
- temperature=0,
53
- messages=[
54
- {"role": "user", "content": final_prompt}
55
- ]
56
- )
57
- return response['choices'][0]['message']['content']
58
-
59
- return get_entities_chatGPT(final_prompt=prompt)
60
-
61
-
62
- def mt0_completion(prompt):
63
- inputs = tokenizer.encode(prompt, return_tensors="pt").to("cuda")
64
- outputs = model.generate(inputs)
65
- return tokenizer.decode(outputs[0])
66
-
67
-
68
-
69
  XQUAD_LANG2CODES = {
70
  "bengali": "bn",
71
  "korean": "ko",
@@ -164,7 +116,7 @@ LANGUAGE_TO_SUFFIX = {
164
  "hungarian": "hu",
165
  "czech": "cs",
166
  "slovak": "sk",
167
- "ukrainian": "uk"
168
  }
169
 
170
 
@@ -215,20 +167,21 @@ def load_qa_dataset(dataset_name, lang, split, translate_test=False, limit=5):
215
 
216
 
217
  def construct_prompt(
218
- instruction: str,
219
- test_example: dict,
220
- ic_examples: List[dict],
221
- zero_shot: bool,
222
- lang: str,
223
- config: Any,
224
  ):
225
  example_prompt = PromptTemplate(
226
  input_variables=["context", "question", "answers"],
227
- template="Context: {context} \n Question: {question} \n " "Answers: {answers}",
 
228
  )
229
 
230
  zero_shot_template = (
231
- f"""{instruction}""" + " \n <Context>: {context} \n <Question>: {question} " ""
232
  )
233
 
234
  prompt = (
@@ -260,7 +213,7 @@ def construct_prompt(
260
 
261
 
262
  def dump_metrics(
263
- lang: str, config: Dict[str, str], f1: float, em: float, metric_logger_path: str
264
  ):
265
  # Check if the metric logger file exists
266
  file_exists = os.path.exists(metric_logger_path)
@@ -303,7 +256,7 @@ def _translate_instruction(basic_instruction: str, target_language: str) -> str:
303
 
304
 
305
  def _translate_prediction_to_output_language(
306
- prediction: str, prediction_language: str, output_language: str
307
  ) -> str:
308
  translator = EasyGoogleTranslate(
309
  source_language=LANGUAGE_TO_SUFFIX[prediction_language],
@@ -329,7 +282,7 @@ def create_instruction(lang: str, instruction_language: str, expected_output):
329
 
330
 
331
  def _translate_example(
332
- example: Dict[str, str], src_language: str, target_language: str
333
  ):
334
  translator = EasyGoogleTranslate(
335
  source_language=LANGUAGE_TO_SUFFIX[src_language],
@@ -340,19 +293,20 @@ def _translate_example(
340
  return {
341
  "question": translator.translate(example["question"]),
342
  "context": translator.translate(example["context"][:2000])
343
- + translator.translate(example["context"][2000:4000])
344
- + translator.translate(example["context"][4000:6000]),
345
  "answers": "",
346
  }
347
  except Exception as e:
348
  pass
349
 
 
350
  def choose_few_shot_examples(
351
- train_dataset: Dataset,
352
- few_shot_size: int,
353
- context: List[str],
354
- selection_criteria: str,
355
- lang: str,
356
  ) -> List[Dict[str, Union[str, int]]]:
357
  """Selects few-shot examples from training datasets
358
 
@@ -423,7 +377,7 @@ def normalize_answer(s):
423
 
424
 
425
  def process_test_example(
426
- test_data, config_header, idx, test_example, config, zero_shot, lang, params
427
  ):
428
  try:
429
  # Your existing code for processing each test example
@@ -456,7 +410,9 @@ def process_test_example(
456
  )
457
 
458
  print(len(prompt))
459
- pred = get_prediction(prompt=prompt, endpoint_id=7327255438662041600, project_id=16514800572)
 
 
460
  # pred = mixtral_completion(prompt)
461
  print(pred)
462
 
@@ -551,10 +507,10 @@ def run_one_configuration(params: Optional[PARAMS] = None):
551
  response=pred,
552
  label=label,
553
  response_logger_file=f"{params['response_logger_root']}"
554
- + f"/{params['model']}"
555
- + f"/{lang}/"
556
- + config_header
557
- + ".csv",
558
  )
559
 
560
  except Exception as e:
@@ -572,7 +528,6 @@ def run_one_configuration(params: Optional[PARAMS] = None):
572
  )
573
 
574
 
575
-
576
  def run_one_configuration_paralle(params: Optional[PARAMS] = None, zero: bool = False):
577
  if not params:
578
  params = read_parameters("../../parameters.yaml")
@@ -624,7 +579,6 @@ def run_one_configuration_paralle(params: Optional[PARAMS] = None, zero: bool =
624
  pool.join()
625
 
626
 
627
-
628
  def construct_prompt(
629
  instruction: str,
630
  test_example: dict,
@@ -632,10 +586,10 @@ def construct_prompt(
632
  num_examples: int,
633
  lang: str,
634
  config: Dict[str, str],
635
- dataset_name: str = 'xquad'
636
  ):
637
  if not instruction:
638
- instruction = create_instruction(lang, config['prefix'], config['output'])
639
 
640
  example_prompt = PromptTemplate(
641
  input_variables=["context", "question", "answers"],
@@ -643,15 +597,16 @@ def construct_prompt(
643
  )
644
 
645
  zero_shot_template = (
646
- f"""{instruction}""" + " \n <Context>: {context} \n <Question>: {question} " ""
647
  )
648
  if not zero_shot:
649
  try:
650
- test_data = load_qa_dataset(dataset_name = dataset_name, lang=lang, split="test", limit=100)
 
 
651
  except Exception as e:
652
  raise KeyError(f"{lang} is not supported in {dataset_name}")
653
 
654
-
655
  ic_examples = []
656
  if not zero_shot:
657
 
@@ -677,12 +632,12 @@ def construct_prompt(
677
  )
678
  )
679
  print("lang", lang)
680
- print(config["input"] , lang)
681
  if config["input"] != lang:
682
  test_example = _translate_example(
683
  example=test_example, src_language=lang, target_language=config["input"]
684
  )
685
 
686
  return prompt.format(
687
- question=test_example["question"], context=test_example["context"]
688
- )
 
10
  from typing import Any, Dict, List, NewType, Optional, Union
11
 
12
  import numpy as np
 
 
13
  import yaml
14
  from datasets import Dataset, load_dataset
15
  from easygoogletranslate import EasyGoogleTranslate
 
18
  from tqdm import tqdm
19
  from yaml.loader import SafeLoader
20
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
21
  XQUAD_LANG2CODES = {
22
  "bengali": "bn",
23
  "korean": "ko",
 
116
  "hungarian": "hu",
117
  "czech": "cs",
118
  "slovak": "sk",
119
+ "ukrainian": "uk",
120
  }
121
 
122
 
 
167
 
168
 
169
  def construct_prompt(
170
+ instruction: str,
171
+ test_example: dict,
172
+ ic_examples: List[dict],
173
+ zero_shot: bool,
174
+ lang: str,
175
+ config: Any,
176
  ):
177
  example_prompt = PromptTemplate(
178
  input_variables=["context", "question", "answers"],
179
+ template="Context: {context} \n Question: {question} \n "
180
+ "Answers: {answers}",
181
  )
182
 
183
  zero_shot_template = (
184
+ f"""{instruction}""" + " \n <Context>: {context} \n <Question>: {question} " ""
185
  )
186
 
187
  prompt = (
 
213
 
214
 
215
  def dump_metrics(
216
+ lang: str, config: Dict[str, str], f1: float, em: float, metric_logger_path: str
217
  ):
218
  # Check if the metric logger file exists
219
  file_exists = os.path.exists(metric_logger_path)
 
256
 
257
 
258
  def _translate_prediction_to_output_language(
259
+ prediction: str, prediction_language: str, output_language: str
260
  ) -> str:
261
  translator = EasyGoogleTranslate(
262
  source_language=LANGUAGE_TO_SUFFIX[prediction_language],
 
282
 
283
 
284
  def _translate_example(
285
+ example: Dict[str, str], src_language: str, target_language: str
286
  ):
287
  translator = EasyGoogleTranslate(
288
  source_language=LANGUAGE_TO_SUFFIX[src_language],
 
293
  return {
294
  "question": translator.translate(example["question"]),
295
  "context": translator.translate(example["context"][:2000])
296
+ + translator.translate(example["context"][2000:4000])
297
+ + translator.translate(example["context"][4000:6000]),
298
  "answers": "",
299
  }
300
  except Exception as e:
301
  pass
302
 
303
+
304
  def choose_few_shot_examples(
305
+ train_dataset: Dataset,
306
+ few_shot_size: int,
307
+ context: List[str],
308
+ selection_criteria: str,
309
+ lang: str,
310
  ) -> List[Dict[str, Union[str, int]]]:
311
  """Selects few-shot examples from training datasets
312
 
 
377
 
378
 
379
  def process_test_example(
380
+ test_data, config_header, idx, test_example, config, zero_shot, lang, params
381
  ):
382
  try:
383
  # Your existing code for processing each test example
 
410
  )
411
 
412
  print(len(prompt))
413
+ pred = get_prediction(
414
+ prompt=prompt, endpoint_id=7327255438662041600, project_id=16514800572
415
+ )
416
  # pred = mixtral_completion(prompt)
417
  print(pred)
418
 
 
507
  response=pred,
508
  label=label,
509
  response_logger_file=f"{params['response_logger_root']}"
510
+ + f"/{params['model']}"
511
+ + f"/{lang}/"
512
+ + config_header
513
+ + ".csv",
514
  )
515
 
516
  except Exception as e:
 
528
  )
529
 
530
 
 
531
  def run_one_configuration_paralle(params: Optional[PARAMS] = None, zero: bool = False):
532
  if not params:
533
  params = read_parameters("../../parameters.yaml")
 
579
  pool.join()
580
 
581
 
 
582
  def construct_prompt(
583
  instruction: str,
584
  test_example: dict,
 
586
  num_examples: int,
587
  lang: str,
588
  config: Dict[str, str],
589
+ dataset_name: str = "xquad",
590
  ):
591
  if not instruction:
592
+ instruction = create_instruction(lang, config["prefix"], config["output"])
593
 
594
  example_prompt = PromptTemplate(
595
  input_variables=["context", "question", "answers"],
 
597
  )
598
 
599
  zero_shot_template = (
600
+ f"""{instruction}""" + " \n <Context>: {context} \n <Question>: {question} " ""
601
  )
602
  if not zero_shot:
603
  try:
604
+ test_data = load_qa_dataset(
605
+ dataset_name=dataset_name, lang=lang, split="test", limit=100
606
+ )
607
  except Exception as e:
608
  raise KeyError(f"{lang} is not supported in {dataset_name}")
609
 
 
610
  ic_examples = []
611
  if not zero_shot:
612
 
 
632
  )
633
  )
634
  print("lang", lang)
635
+ print(config["input"], lang)
636
  if config["input"] != lang:
637
  test_example = _translate_example(
638
  example=test_example, src_language=lang, target_language=config["input"]
639
  )
640
 
641
  return prompt.format(
642
+ question=test_example["question"], context=test_example["context"]
643
+ )
tasks/summarization.py CHANGED
@@ -1,9 +1,9 @@
1
- from typing import List, Dict, Union
2
 
3
  import numpy as np
4
  from datasets import Dataset, load_dataset
5
  from easygoogletranslate import EasyGoogleTranslate
6
- from langchain.prompts import PromptTemplate, FewShotPromptTemplate
7
 
8
  LANGUAGE_TO_SUFFIX = {
9
  "chinese_simplified": "zh-CN",
@@ -48,12 +48,16 @@ LANGUAGE_TO_SUFFIX = {
48
  "hungarian": "hu",
49
  "czech": "cs",
50
  "slovak": "sk",
51
- "ukrainian": "uk"
52
  }
53
 
54
 
55
  def choose_few_shot_examples(
56
- train_dataset: Dataset, few_shot_size: int, context: List[str], selection_criteria: str, lang: str,
 
 
 
 
57
  ) -> List[Dict[str, Union[str, int]]]:
58
  selected_examples = []
59
 
@@ -67,13 +71,25 @@ def choose_few_shot_examples(
67
  .tolist()
68
  )
69
 
70
- ic_examples = [{'text': train_dataset[idx]['text'], 'summary': train_dataset[idx]['summary']} for idx in
71
- example_idxs]
 
 
72
 
73
  for idx, ic_language in enumerate(context):
74
- selected_examples.append(ic_examples[idx]) if ic_language == lang else (
75
- selected_examples.append(
76
- _translate_example(example=ic_examples[idx], src_language=lang, target_language=ic_language)))
 
 
 
 
 
 
 
 
 
 
77
 
78
  return selected_examples
79
 
@@ -87,12 +103,16 @@ def _translate_instruction(basic_instruction: str, target_language: str) -> str:
87
  return translator.translate(basic_instruction)
88
 
89
 
90
- def _translate_example(example: Dict[str, str], src_language: str, target_language: str):
91
- translator = EasyGoogleTranslate(source_language=LANGUAGE_TO_SUFFIX[src_language],
92
- target_language=LANGUAGE_TO_SUFFIX[target_language],
93
- timeout=30)
 
 
 
 
94
  try:
95
- return {'text': translator.translate(example['text']), 'summary': ''}
96
  except Exception as e:
97
  print(e)
98
 
@@ -117,17 +137,17 @@ def load_xlsum_data(lang, split, limit=5):
117
 
118
 
119
  def construct_prompt(
120
- instruction: str,
121
- test_example: dict,
122
- zero_shot: bool,
123
- dataset: str,
124
- num_examples: int,
125
- lang: str,
126
- config: Dict[str, str],
127
  ):
128
  if not instruction:
129
  print(lang)
130
- instruction = create_instruction(lang, config['prefix'], config['output'])
131
 
132
  example_prompt = PromptTemplate(
133
  input_variables=["summary", "text"], template="Text: {text}\nSummary: {summary}"
@@ -139,7 +159,9 @@ def construct_prompt(
139
  try:
140
  test_data = load_xlsum_data(lang=lang, split="test", limit=100)
141
  except Exception as e:
142
- raise KeyError(f"{lang} is not supported in XlSum dataset, choose supported language in few-shot")
 
 
143
 
144
  ic_examples = []
145
  if not zero_shot:
 
1
+ from typing import Dict, List, Union
2
 
3
  import numpy as np
4
  from datasets import Dataset, load_dataset
5
  from easygoogletranslate import EasyGoogleTranslate
6
+ from langchain.prompts import FewShotPromptTemplate, PromptTemplate
7
 
8
  LANGUAGE_TO_SUFFIX = {
9
  "chinese_simplified": "zh-CN",
 
48
  "hungarian": "hu",
49
  "czech": "cs",
50
  "slovak": "sk",
51
+ "ukrainian": "uk",
52
  }
53
 
54
 
55
  def choose_few_shot_examples(
56
+ train_dataset: Dataset,
57
+ few_shot_size: int,
58
+ context: List[str],
59
+ selection_criteria: str,
60
+ lang: str,
61
  ) -> List[Dict[str, Union[str, int]]]:
62
  selected_examples = []
63
 
 
71
  .tolist()
72
  )
73
 
74
+ ic_examples = [
75
+ {"text": train_dataset[idx]["text"], "summary": train_dataset[idx]["summary"]}
76
+ for idx in example_idxs
77
+ ]
78
 
79
  for idx, ic_language in enumerate(context):
80
+ (
81
+ selected_examples.append(ic_examples[idx])
82
+ if ic_language == lang
83
+ else (
84
+ selected_examples.append(
85
+ _translate_example(
86
+ example=ic_examples[idx],
87
+ src_language=lang,
88
+ target_language=ic_language,
89
+ )
90
+ )
91
+ )
92
+ )
93
 
94
  return selected_examples
95
 
 
103
  return translator.translate(basic_instruction)
104
 
105
 
106
+ def _translate_example(
107
+ example: Dict[str, str], src_language: str, target_language: str
108
+ ):
109
+ translator = EasyGoogleTranslate(
110
+ source_language=LANGUAGE_TO_SUFFIX[src_language],
111
+ target_language=LANGUAGE_TO_SUFFIX[target_language],
112
+ timeout=30,
113
+ )
114
  try:
115
+ return {"text": translator.translate(example["text"]), "summary": ""}
116
  except Exception as e:
117
  print(e)
118
 
 
137
 
138
 
139
  def construct_prompt(
140
+ instruction: str,
141
+ test_example: dict,
142
+ zero_shot: bool,
143
+ dataset: str,
144
+ num_examples: int,
145
+ lang: str,
146
+ config: Dict[str, str],
147
  ):
148
  if not instruction:
149
  print(lang)
150
+ instruction = create_instruction(lang, config["prefix"], config["output"])
151
 
152
  example_prompt = PromptTemplate(
153
  input_variables=["summary", "text"], template="Text: {text}\nSummary: {summary}"
 
159
  try:
160
  test_data = load_xlsum_data(lang=lang, split="test", limit=100)
161
  except Exception as e:
162
+ raise KeyError(
163
+ f"{lang} is not supported in XlSum dataset, choose supported language in few-shot"
164
+ )
165
 
166
  ic_examples = []
167
  if not zero_shot: