Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

xwinostorymt #827

Open
wants to merge 32 commits into
base: eval-hackathon
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
32 commits
Select commit Hold shift + click to select a range
d1f16cf
Accelerate `get_infos` by caching the `DataseInfoDict`s (#778)
VictorSanh May 22, 2022
6b1560f
fix `filter_english_datasets` since `languages` became `language` in …
VictorSanh Jul 3, 2022
ab6ad7e
fix empty documents - multi_news (#793)
VictorSanh Jul 3, 2022
0cc4b0c
Language tags (#771)
stephenbach Jul 8, 2022
619daa1
Merge pull request #4 from bigscience-workshop/eval-hackathon
Muennighoff Jul 19, 2022
53c99f3
ZH prompts v1
Muennighoff Aug 9, 2022
7293640
确定更好一点
Muennighoff Aug 9, 2022
97cc39b
Use bu
Muennighoff Aug 9, 2022
ccd2575
能更适合
Muennighoff Aug 10, 2022
426f945
更新ID
Muennighoff Aug 10, 2022
f5ba708
Specify ZH-HT
Muennighoff Aug 11, 2022
4a00362
ZHMT
Muennighoff Aug 11, 2022
172740f
Add swht & hiht
Muennighoff Aug 12, 2022
4f973e5
Add arabic & vietnamese
Muennighoff Aug 16, 2022
1fdcbea
add arabic prompts
KhalidAlt Aug 16, 2022
64ac829
add arabic prompts and fix unicode issues
KhalidAlt Aug 16, 2022
148344d
fix semantic errors
KhalidAlt Aug 16, 2022
51c059d
Complete XNLI HT L1
Muennighoff Aug 21, 2022
8e374e3
Merge pull request #33 from KhalidAlt/add_xnli_ar
Muennighoff Aug 21, 2022
b00dda3
Add machine translation
Muennighoff Aug 21, 2022
e401d0d
Fix naming
Muennighoff Sep 3, 2022
48a1467
Update uuids
Muennighoff Sep 3, 2022
3f69285
Fix uuids
Muennighoff Sep 3, 2022
be40398
Fix uuids
Muennighoff Sep 3, 2022
7b7bc05
Fix uuid
Muennighoff Sep 3, 2022
8c0c243
Fix
Muennighoff Sep 3, 2022
ff3eada
XCOPA Machine translated
Muennighoff Sep 18, 2022
4c742ca
Clarify format_
Muennighoff Sep 18, 2022
93bcf9a
Fix template name
Muennighoff Sep 18, 2022
d09ebb9
MT
Muennighoff Sep 18, 2022
c1520bb
Add templates
Muennighoff Sep 26, 2022
0906d71
Add translations
Muennighoff Oct 25, 2022
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
176 changes: 176 additions & 0 deletions promptsource/machine_translate.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,176 @@
import os
import re

from promptsource.templates import Template, TemplateCollection


### XNLI

PROMPTS = [
"GPT-3 style",
"can we infer",
"justified in saying",
"guaranteed/possible/impossible",
"MNLI crowdsource",
]

LANGS = [
"ar",
"es",
"fr",
"hi",
"sw",
"ur",
"vi",
"zh",
]

SOURCE_DATASET = TARGET_DATASET = "xnli"
SOURCE_LANG = "en"


### XCOPA

PROMPTS = [
"best_option",
'C1 or C2? premise, so/because…',
"i_am_hesitating",
"cause_effect",
"plausible_alternatives",
]

LANGS = [
"id",
"sw",
"ta",
"vi",
"zh",
"th",
"it",
"qu",
]

SOURCE_DATASET = "super_glue/copa"
SOURCE_LANG = None
TARGET_DATASET = "xcopa"

### XSTORY_CLOZE

PROMPTS = [
"Answer Given options",
'Choose Story Ending',
"Story Continuation and Options",
"Generate Ending",
"Novel Correct Ending",
]

LANGS = [
"ar",
"es",
"eu",
"hi",
"id",
"zh",
"my",
"ru",
"sw",
"te",
]

SOURCE_DATASET = TARGET_DATASET = "Muennighoff/xstory_cloze"
SOURCE_LANG = "en"

### XWINOGRAD
PROMPTS = [
"Replace",
"stand for",
"True or False",
"does underscore refer to",
"underscore refer to",
]

LANGS = [
"fr",
"pt",
"zh",
"ja",
"ru",
]

SOURCE_DATASET = TARGET_DATASET = "Muennighoff/xwinograd"
SOURCE_LANG = "en"


# Path to key
os.environ["GOOGLE_APPLICATION_CREDENTIALS"] = "/Users/niklasmuennighoff/Desktop/gcp_translate_key.json"


def translate(target, text):
"""Translates text into the target language.
Target must be an ISO 639-1 language code.
See https://g.co/cloud/translate/v2/translate-reference#supported_languages
(pip install --upgrade google-api-python-client)
pip install google-cloud-translate
"""
import six
from google.cloud import translate_v2 as translate

translate_client = translate.Client()
if isinstance(text, six.binary_type):
text = text.decode("utf-8")
# Text can also be a sequence of strings, in which case this method
# will return a sequence of results for each text.
# By default format_ is html, which would return " instead of "
result = translate_client.translate(text, source_language="en", target_language=target, format_="text")
print("Text: {}".format(result["input"]))
print("Translation: {}".format(result["translatedText"]))
# If not providing source_language
# print("Detected source language: {}".format(result["detectedSourceLanguage"]))
return result["translatedText"]


def normalize_string(zh_string, en_string):
"""
This is not specific to zh just to give an example & help Codex understand it :-)
Replaces the content in brackets in zh_string with the content in brackets from en_string.
All else is left the same in zh_string.
Args:
zh_string: {{前提}} 问题:{{假设}} 对、错或两者都不是? ||| {{ answer_choices[标签] }}
en_string: {{premise}} Question: {{hypothesis}} True, False, or Neither? ||| {{ answer_choices[label] }}
Returns:
zh_string_normalized: {{premise}} 问题:{{hypothesis}} 对、错或两者都不是? ||| {{ answer_choices[label] }}
"""
zh_string_normalized = zh_string
# Find all the content in brackets in zh_string
# For only double brackets {{(.*?)}}, but we do single brackets as well
zh_bracket_content = re.findall(r"{(.*?)}", zh_string)
# Find all the content in brackets in en_string
# For only double brackets {{(.*?)}}, but we do single brackets as well
en_bracket_content = re.findall(r"{(.*?)}", en_string)
# Replace the content in brackets in zh_string with the content in brackets from en_string
for i in range(len(zh_bracket_content)):
zh_string_normalized = zh_string_normalized.replace(zh_bracket_content[i], en_bracket_content[i])
return zh_string_normalized


template_collection = TemplateCollection()
source_templates = template_collection.get_dataset(SOURCE_DATASET, SOURCE_LANG)

for lang in LANGS:
target_templates = template_collection.get_dataset(TARGET_DATASET, lang)
for uid, template in source_templates.templates.items():
if template.name.strip() not in PROMPTS:
continue
print(f"Translating {template.name.strip()} to {lang}")
answer_choices = []
if template.answer_choices is not None:
choices = template.answer_choices.split("|||")
for c in choices:
answer_choices.append(normalize_string(translate(lang, c.strip()), c.strip()))
or_jinja = template.jinja.strip()
jinja = normalize_string(translate(lang, or_jinja), or_jinja)
template_name = template.name.strip() + f"_{lang}mt"
target_template = Template(
template_name, jinja=jinja, reference="", answer_choices=" ||| ".join(answer_choices)
)
target_templates.add_template(target_template)
208 changes: 207 additions & 1 deletion promptsource/templates.py
Original file line number Diff line number Diff line change
Expand Up @@ -235,6 +235,212 @@
"zu": "Zulu",
}

# These are the metrics with which templates can be tagged
METRICS = {
"BLEU",
"ROUGE",
"Squad",
"Trivia QA",
"Accuracy",
"Pearson Correlation",
"Spearman Correlation",
"MultiRC",
"AUC",
"COQA F1",
"Edit Distance",
"Mean Reciprocal Rank",
"Other",
}

# These are the languages with which templates can be tagged. Keys are ISO 639-1
# tags, which are the actual tags we use. Values are English names shown in the
# UI for convenience.
LANGUAGES = {
"ab": "Abkhazian",
"aa": "Afar",
"af": "Afrikaans",
"ak": "Akan",
"sq": "Albanian",
"am": "Amharic",
"ar": "Arabic",
"an": "Aragonese",
"hy": "Armenian",
"as": "Assamese",
"av": "Avaric",
"ae": "Avestan",
"ay": "Aymara",
"az": "Azerbaijani",
"bm": "Bambara",
"ba": "Bashkir",
"eu": "Basque",
"be": "Belarusian",
"bn": "Bengali",
"bi": "Bislama",
"bs": "Bosnian",
"br": "Breton",
"bg": "Bulgarian",
"my": "Burmese",
"ca": "Catalan, Valencian",
"ch": "Chamorro",
"ce": "Chechen",
"ny": "Chichewa, Chewa, Nyanja",
"zh": "Chinese",
"cu": "Church Slavic, Old Slavonic, Church Slavonic, Old Bulgarian, Old Church Slavonic",
"cv": "Chuvash",
"kw": "Cornish",
"co": "Corsican",
"cr": "Cree",
"hr": "Croatian",
"cs": "Czech",
"da": "Danish",
"dv": "Divehi, Dhivehi, Maldivian",
"nl": "Dutch, Flemish",
"dz": "Dzongkha",
"en": "English",
"eo": "Esperanto",
"et": "Estonian",
"ee": "Ewe",
"fo": "Faroese",
"fj": "Fijian",
"fi": "Finnish",
"fr": "French",
"fy": "Western Frisian",
"ff": "Fulah",
"gd": "Gaelic, Scottish Gaelic",
"gl": "Galician",
"lg": "Ganda",
"ka": "Georgian",
"de": "German",
"el": "Greek, Modern (1453–)",
"kl": "Kalaallisut, Greenlandic",
"gn": "Guarani",
"gu": "Gujarati",
"ht": "Haitian, Haitian Creole",
"ha": "Hausa",
"he": "Hebrew",
"hz": "Herero",
"hi": "Hindi",
"ho": "Hiri Motu",
"hu": "Hungarian",
"is": "Icelandic",
"io": "Ido",
"ig": "Igbo",
"id": "Indonesian",
"ia": "Interlingua (International Auxiliary Language Association)",
"ie": "Interlingue, Occidental",
"iu": "Inuktitut",
"ik": "Inupiaq",
"ga": "Irish",
"it": "Italian",
"ja": "Japanese",
"jv": "Javanese",
"kn": "Kannada",
"kr": "Kanuri",
"ks": "Kashmiri",
"kk": "Kazakh",
"km": "Central Khmer",
"ki": "Kikuyu, Gikuyu",
"rw": "Kinyarwanda",
"ky": "Kirghiz, Kyrgyz",
"kv": "Komi",
"kg": "Kongo",
"ko": "Korean",
"kj": "Kuanyama, Kwanyama",
"ku": "Kurdish",
"lo": "Lao",
"la": "Latin",
"lv": "Latvian",
"li": "Limburgan, Limburger, Limburgish",
"ln": "Lingala",
"lt": "Lithuanian",
"lu": "Luba-Katanga",
"lb": "Luxembourgish, Letzeburgesch",
"mk": "Macedonian",
"mg": "Malagasy",
"ms": "Malay",
"ml": "Malayalam",
"mt": "Maltese",
"gv": "Manx",
"mi": "Maori",
"mr": "Marathi",
"mh": "Marshallese",
"mn": "Mongolian",
"na": "Nauru",
"nv": "Navajo, Navaho",
"nd": "North Ndebele",
"nr": "South Ndebele",
"ng": "Ndonga",
"ne": "Nepali",
"no": "Norwegian",
"nb": "Norwegian Bokmål",
"nn": "Norwegian Nynorsk",
"ii": "Sichuan Yi, Nuosu",
"oc": "Occitan",
"oj": "Ojibwa",
"or": "Oriya",
"om": "Oromo",
"os": "Ossetian, Ossetic",
"pi": "Pali",
"ps": "Pashto, Pushto",
"fa": "Persian",
"pl": "Polish",
"pt": "Portuguese",
"pa": "Punjabi, Panjabi",
"qu": "Quechua",
"ro": "Romanian, Moldavian, Moldovan",
"rm": "Romansh",
"rn": "Rundi",
"ru": "Russian",
"se": "Northern Sami",
"sm": "Samoan",
"sg": "Sango",
"sa": "Sanskrit",
"sc": "Sardinian",
"sr": "Serbian",
"sn": "Shona",
"sd": "Sindhi",
"si": "Sinhala, Sinhalese",
"sk": "Slovak",
"sl": "Slovenian",
"so": "Somali",
"st": "Southern Sotho",
"es": "Spanish, Castilian",
"su": "Sundanese",
"sw": "Swahili",
"ss": "Swati",
"sv": "Swedish",
"tl": "Tagalog",
"ty": "Tahitian",
"tg": "Tajik",
"ta": "Tamil",
"tt": "Tatar",
"te": "Telugu",
"th": "Thai",
"bo": "Tibetan",
"ti": "Tigrinya",
"to": "Tonga (Tonga Islands)",
"ts": "Tsonga",
"tn": "Tswana",
"tr": "Turkish",
"tk": "Turkmen",
"tw": "Twi",
"ug": "Uighur, Uyghur",
"uk": "Ukrainian",
"ur": "Urdu",
"uz": "Uzbek",
"ve": "Venda",
"vi": "Vietnamese",
"vo": "Volapük",
"wa": "Walloon",
"cy": "Welsh",
"wo": "Wolof",
"xh": "Xhosa",
"yi": "Yiddish",
"yo": "Yoruba",
"za": "Zhuang, Chuang",
"zu": "Zulu",
}


def highlight(input):
return "<span style='color: #F08080'>" + input + "</span>"
Expand Down Expand Up @@ -617,7 +823,7 @@ def write_to_file(self) -> None:
# We only create the folder if a template is written
if not os.path.exists(self.folder_path):
os.makedirs(self.folder_path)
yaml.dump(self.format_for_dump(), open(self.yaml_path, "w"))
yaml.dump(self.format_for_dump(), open(self.yaml_path, "w"),allow_unicode=True)

def add_template(self, template: "Template") -> None:
"""
Expand Down
Loading