text_plain_output_1.png
from transformers import AutoTokenizer, AutoModelForSeq2SeqLM

from typing import List

tokenizer = AutoTokenizer.from_pretrained('juierror/flan-t5-text2sql-with-schema')
model = AutoModelForSeq2SeqLM.from_pretrained('juierror/flan-t5-text2sql-with-schema')

def prepare_input(question: str, table: List[str]):
    table_prefix = 'table:'
    question_prefix = 'question:'
    join_table = ','.join(table)
    inputs = f'{question_prefix} {question} {table_prefix} {join_table}'
    input_ids = tokenizer(inputs, max_length=700, return_tensors='pt').input_ids
    return input_ids

def inference(question: str, table: List[str]) -> str:
    input_data = prepare_input(question=question, table=table)
    input_data = input_data.to(model.device)
    outputs = model.generate(inputs=input_data, num_beams=10, top_k=10, max_length=700)
    result = tokenizer.decode(token_ids=outputs[0], skip_special_tokens=True)
    return result

print(inference(question='Chris Pratt has been in his current profession since the turn of the century.Is it true?', table=['title', 'Birth Name', 'Born', 'Residence', 'Occupation', 'Years active', 'Spouse(s)', 'Partner(s)', 'Children']))