This is a fine-tuned T5-small model for generating SQL queries from natural language. It was trained on the
Spider dataset, a benchmark dataset for text-to-SQL tasks.
This model can be used to generate SQL queries from natural language questions. It is particularly useful for developers building natural language interfaces to databases.
The model can be fine-tuned further on domain-specific datasets for improved performance.
This model is not suitable for generating SQL queries for databases with highly specialized schemas or non-standard SQL dialects.
The model may generate incorrect or unsafe SQL queries if the input question is ambiguous or outside the scope of the training data. Always validate the generated SQL before executing it on a production database.
1from transformers import T5Tokenizer, T5ForConditionalGeneration
2
3# Load the fine-tuned model
4model = T5ForConditionalGeneration.from_pretrained("osllmai/text-to-sql")
5tokenizer = T5Tokenizer.from_pretrained("osllmai/text-to-sql")
6
7# Generate SQL query
8def generate_sql_query(question):
9 input_text = f"translate English to SQL: {question}"
10 input_ids = tokenizer(input_text, return_tensors="pt", max_length=512, truncation=True, padding="max_length").input_ids
11 outputs = model.generate(input_ids)
12 return tokenizer.decode(outputs[0], skip_special_tokens=True)
13
14# Example usage
15question = "Find all the customers who live in New York."
16sql_query = generate_sql_query(question)
17print(sql_query)
The model was trained on the
Spider dataset, which contains 10,181 questions and 5,693 unique complex SQL queries across 200 databases.
The model was evaluated on the Spider validation set. Metrics such as exact match accuracy and execution accuracy can be used to assess performance.