text_plain_output_5.png application_vnd.jupyter.stderr_output_2.png text_plain_output_3.png
from datasets import load_dataset

from transformers import AutoModelWithLMHead, AutoTokenizer

tokenizer = AutoTokenizer.from_pretrained('mrm8488/t5-base-finetuned-wikiSQL')
model = AutoModelWithLMHead.from_pretrained('mrm8488/t5-base-finetuned-wikiSQL')

def get_sql(query):
    input_text = 'translate English to SQL: %s </s>' % query
    features = tokenizer([input_text], return_tensors='pt')
    output = model.generate(input_ids=features['input_ids'], attention_mask=features['attention_mask'])
    return tokenizer.decode(output[0])

valid_dataset = load_dataset('wikisql', split='validation')

for idx in random.sample(range(len(valid_dataset)), 200):
    print(f"Text: {valid_dataset[idx]['question']}")
    print(f"Pred SQL: {get_sql(valid_dataset[idx]['question'])}")
    print(f"True SQL: {valid_dataset[idx]['sql']['human_readable']}\n")