manythings-translations-alpaca / make_manythings-translations-alpaca.py
xzuyn's picture
Update make_manythings-translations-alpaca.py
3ee0b08
import json
import random
# Read the JSONL file and parse each line into a dictionary
with open('manythings-translations-randomized.jsonl', 'r') as file:
data = [json.loads(line) for line in file][:] # Optionally limit the amount of lines to input.
# List of phrases to randomly choose from
instruction_phrases = [
"i need a translation",
"convert the following text",
"convert the following message",
"convert the following sentence",
"communicate this text",
"communicate this message",
"communicate this sentence",
"can you communicate this text",
"can you communicate this message",
"can you communicate this sentence",
"please communicate this text",
"please communicate this message",
"please communicate this sentence",
"please can you communicate this text",
"please can you communicate this message",
"please can you communicate this sentence",
"can you please communicate this text",
"can you please communicate this message",
"can you please communicate this sentence",
"translate this text",
"translate this message",
"translate this sentence",
"can you translate this text",
"can you translate this message",
"can you translate this sentence",
"please translate this text",
"please translate this message",
"please translate this sentence",
"please can you translate this text",
"please can you translate this message",
"please can you translate this sentence",
"can you please translate this text",
"can you please translate this message",
"can you please translate this sentence",
"switch the text",
"switch the message",
"switch the sentence",
"can you switch the text",
"can you switch the message",
"can you switch the sentence",
"please switch the text",
"please switch the message",
"please switch the sentence",
"please can you switch the text",
"please can you switch the message",
"please can you switch the sentence",
"can you please switch the text",
"can you please switch the message",
"can you please switch the sentence",
"translate this input text",
"translate this input message",
"translate this input sentence",
"can you translate this input text",
"can you translate this input message",
"can you translate this input sentence",
"please translate this input text",
"please translate this input message",
"please translate this input sentence",
"please can you translate this input text",
"please can you translate this input message",
"please can you translate this input sentence",
"can you please translate this input text",
"can you please translate this input message",
"can you please translate this input sentence",
"convert",
"please convert",
"please can you convert",
"can you please convert",
"translate",
"please translate",
"please can you translate",
"can you please translate",
]
# Function to create the instruction based on language pairs
def create_instruction(from_lang, to_lang):
phrase = random.choice(instruction_phrases)
# Randomly decide to capitalize the phrase
if random.random() < 0.5:
phrase = phrase.capitalize()
phrase = f"{phrase} from {from_lang} to {to_lang}"
# Randomly decide to add a question mark
if random.random() < 0.5:
phrase += "?"
elif random.random() < 0.5:
phrase += "."
return phrase
# Initialize the final list to hold the formatted JSON data
formatted_data = []
# Iterate through each item in the original data and create two formatted entries for each
for item in data:
english_text = item.get('english')
if english_text:
for target_lang, translation in item.items():
if target_lang != 'english':
# Add instruction and input/output pairs to the formatted_data list for English to target language
formatted_data.append({
"instruction": create_instruction("English", target_lang.capitalize()),
"input": english_text,
"output": translation
})
# Add instruction and input/output pairs to the formatted_data list for target language to English
formatted_data.append({
"instruction": create_instruction(target_lang.capitalize(), "English"),
"input": translation,
"output": english_text
})
# Randomize the order of the formatted data
random.shuffle(formatted_data)
# Convert the formatted_data list to JSON format
formatted_json = json.dumps(formatted_data, indent=2)
# Save the formatted JSON data to a new file
with open('manythings-translations-alpaca.json', 'w') as output_file:
output_file.write(formatted_json)