File size: 3,091 Bytes
6d446a9
 
5a93818
 
 
 
 
e1364fa
4f6bbf5
 
 
229c036
 
 
 
 
 
 
3c5e719
998a2d3
 
 
229c036
 
1ba6990
6d446a9
 
 
 
5a93818
6d446a9
 
 
a3cc44d
6d446a9
 
 
 
 
229c036
09ef8f6
229c036
 
 
 
 
 
 
 
 
 
 
 
 
 
 
09ef8f6
afcf4b8
 
 
 
c06880d
 
 
afcf4b8
 
1f24039
c06880d
afcf4b8
c06880d
afcf4b8
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
09ef8f6
 
 
 
 
 
 
 
 
 
411b5df
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
---
license: mit
datasets:
- b-mc2/sql-create-context
- gretelai/synthetic_text_to_sql
language:
- en
base_model: google-t5/t5-base
metrics:
- exact_match
model-index:
- name: juanfra218/text2sql
  results:
  - task:
      type: text-to-sql
    metrics:
    - name: exact_match
      type: exact_match
      value: 0.4326836917562724
    - name: bleu
      type: bleu
      value: 0.6687
tags:
- sql
library_name: transformers
---

# Fine-Tuned Google T5 Model for Text to SQL Translation

A fine-tuned version of the Google T5 model, trained for the task of translating natural language queries into SQL statements.

## Model Details

- **Architecture**: Google T5 Base (Text-to-Text Transfer Transformer)
- **Task**: Text to SQL Translation
- **Fine-Tuning Datasets**: 
  - [sql-create-context Dataset](https://huggingface.co/datasets/b-mc2/sql-create-context) 
  - [Synthetic-Text-To-SQL Dataset](https://huggingface.co/datasets/gretelai/synthetic-text-to-sql)

## Training Parameters

```
training_args = Seq2SeqTrainingArguments(
    output_dir="./results",
    evaluation_strategy="epoch",
    learning_rate=2e-5,
    per_device_train_batch_size=8,
    per_device_eval_batch_size=8,
    weight_decay=0.01,
    save_total_limit=3,
    num_train_epochs=3,
    predict_with_generate=True,
    fp16=True,
    push_to_hub=False,
)
```

## Usage

```
import torch
from transformers import T5Tokenizer, T5ForConditionalGeneration

device = torch.device("cuda" if torch.cuda.is_available() else "cpu")

# Load the tokenizer and model
model_path = 'juanfra218/text2sql'
tokenizer = T5Tokenizer.from_pretrained(model_path)
model = T5ForConditionalGeneration.from_pretrained(model_path)
model.to(device)  

# Function to generate SQL queries
def generate_sql(prompt, schema):
    input_text = "translate English to SQL: " + prompt + " " + schema
    inputs = tokenizer(input_text, return_tensors="pt", max_length=512, truncation=True, padding="max_length")

    inputs = {key: value.to(device) for key, value in inputs.items()}

    max_output_length = 1024
    outputs = model.generate(**inputs, max_length=max_output_length)
    return tokenizer.decode(outputs[0], skip_special_tokens=True)

# Interactive loop
print("Enter 'quit' to exit.")
while True:
    prompt = input("Insert prompt: ")
    schema = input("Insert schema: ")
    if prompt.lower() == 'quit':
        break

    sql_query = generate_sql(prompt, schema)
    print(f"Generated SQL query: {sql_query}")
    print()
```

## Files

- `optimizer.pt`: State of the optimizer.
- `training_args.bin`: Training arguments and hyperparameters.
- `tokenizer.json`: Tokenizer vocabulary and settings.
- `spiece.model`: SentencePiece model file.
- `special_tokens_map.json`: Special tokens mapping.
- `tokenizer_config.json`: Tokenizer configuration settings.
- `model.safetensors`: Trained model weights.
- `generation_config.json`: Configuration for text generation.
- `config.json`: Model architecture configuration.
- `test_results.csv`: Results on the testing set, contains: prompt, context, true_answer, predicted_answer, exact_match