michal-stefanik commited on
Commit
52943d3
1 Parent(s): a707b30

Create train_txt2sql.py

Browse files
Files changed (1) hide show
  1. train_txt2sql.py +99 -0
train_txt2sql.py ADDED
@@ -0,0 +1,99 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import json
2
+ import random
3
+ from typing import List, Dict, Tuple
4
+ from adaptor.evaluators.generative import ROUGE, BLEU
5
+ from adaptor.lang_module import LangModule
6
+ from adaptor.objectives.seq2seq import Sequence2Sequence
7
+ from adaptor.utils import AdaptationArguments, StoppingStrategy
8
+ from adaptor.schedules import ParallelSchedule
9
+ from adaptor.adapter import Adapter
10
+ import wandb
11
+
12
+ # Dataset creation
13
+
14
+ ## Define paths to JSON files
15
+ db_path = 'db_schemas.json'
16
+ spider_dataset_train_path = 'spider/train_spider.json'
17
+ spider_dataset_dev_path = 'spider/dev.json'
18
+ spider_syn_train_path = 'Spider-Syn/train_spider.json'
19
+ spider_syn_dev_path = 'Spider-Syn/dev.json'
20
+
21
+ ## Open files
22
+ with open(db_path, 'r') as file_db:
23
+ database_schemas = json.load(file_db)
24
+
25
+ with open(spider_dataset_train_path, 'r') as file_spider:
26
+ spider_train_dataset = json.load(file_spider)
27
+
28
+ with open(spider_dataset_dev_path, 'r') as file_spider:
29
+ spider_dev_dataset = json.load(file_spider)
30
+
31
+ with open(spider_syn_train_path, 'r') as file_spider:
32
+ spider_syn_train_dataset = json.load(file_spider)
33
+
34
+ with open(spider_syn_dev_path, 'r') as file_spider:
35
+ spider_syn_dev_dataset = json.load(file_spider)
36
+
37
+ ## Include spider questions with synonyms (questions include text which is not in DB columns)
38
+ spider_train_dataset.extend([question for question in spider_syn_train_dataset if question['SpiderQuestion']!=question['SpiderSynQuestion']])
39
+ spider_dev_dataset.extend([question for question in spider_syn_dev_dataset if question['SpiderQuestion']!=question['SpiderSynQuestion']])
40
+
41
+ random.shuffle(spider_train_dataset)
42
+ random.shuffle(spider_dev_dataset)
43
+
44
+ def create_prompt(question: str, schema: str) -> str:
45
+ return " ".join(["Question: ",question, "Schema:", schema])
46
+
47
+ def create_vals_and_labels(dataset: List[dict], db_dict: Dict[str, str]) -> Tuple[List[str], List[str]]:
48
+ list_labels = [data["query"] for data in dataset]
49
+ list_prompts = [create_prompt(data["question"], db_dict[data["db_id"]])
50
+ if "question" in data else create_prompt(data["SpiderSynQuestion"], db_dict[data["db_id"]]) for data in dataset]
51
+ return list_prompts, list_labels
52
+
53
+ ## Training prompts and labels
54
+ prompts_train, labels_train = create_vals_and_labels(spider_train_dataset, database_schemas)
55
+ assert len(prompts_train)==len(labels_train)
56
+
57
+ ## Evaluation prompts and labels
58
+ prompts_eval, labels_eval = create_vals_and_labels(spider_dev_dataset, database_schemas)
59
+ assert len(prompts_eval)==len(labels_eval)
60
+
61
+ # Training
62
+
63
+ lang_module = LangModule("google/t5-large-lm-adapt")
64
+ evaluators = [BLEU(), ROUGE(decides_convergence=True)]
65
+
66
+ wandb.init(project="chatbot")
67
+
68
+ seq_qa = Sequence2Sequence(lang_module,
69
+ texts_or_path=prompts_train,
70
+ labels_or_path=labels_train,
71
+ val_texts_or_path=prompts_eval,
72
+ val_labels_or_path=labels_eval,
73
+ batch_size=4,
74
+ val_evaluators=evaluators,
75
+ objective_id="txt2SQL_Spider")
76
+
77
+ training_arguments = AdaptationArguments(output_dir="checkpoints-txt2sql",
78
+ learning_rate=5e-5,
79
+ stopping_strategy=StoppingStrategy.ALL_OBJECTIVES_CONVERGED,
80
+ stopping_patience=8,
81
+ save_total_limit=8,
82
+ do_train=True,
83
+ do_eval=True,
84
+ bf16=True,
85
+ warmup_steps=100,
86
+ gradient_accumulation_steps=8,
87
+ logging_steps=10,
88
+ eval_steps=200,
89
+ save_steps=200,
90
+ num_train_epochs=10,
91
+ evaluation_strategy="steps")
92
+
93
+
94
+ schedule = ParallelSchedule(objectives=[seq_qa],
95
+ args=training_arguments)
96
+ adapter = Adapter(lang_module, schedule, args=training_arguments)
97
+
98
+
99
+ adapter.train()