| import json |
| import itertools |
| import torch |
| from torch.utils.data import Dataset, DataLoader |
| from transformers import LlamaTokenizer, LlamaForCausalLM, Trainer, TrainingArguments |
|
|
| |
| def load_json(file_path): |
| with open(file_path, 'r') as file: |
| return json.load(file) |
|
|
| |
| def generate_prompts(schema, data): |
| prompts = [] |
|
|
| for table_name, columns in schema.items(): |
| table_data = data.get(table_name, []) |
| |
| |
| column_combinations = [] |
| for r in range(1, len(columns) + 1): |
| column_combinations.extend(itertools.combinations(columns, r)) |
| |
| |
| for row in table_data: |
| for combination in column_combinations: |
| prompt_parts = [] |
| for column in combination: |
| if column in row: |
| prompt_parts.append(f"{column}: {row[column]}") |
| prompt = f"Table: {table_name}\n" + "\n".join(prompt_parts) |
| prompts.append(prompt) |
| |
| return prompts |
|
|
| |
| class PromptsDataset(Dataset): |
| def __init__(self, prompts, tokenizer, max_length): |
| self.prompts = prompts |
| self.tokenizer = tokenizer |
| self.max_length = max_length |
|
|
| def __len__(self): |
| return len(self.prompts) |
|
|
| def __getitem__(self, idx): |
| prompt = self.prompts[idx] |
| encoding = self.tokenizer(prompt, return_tensors='pt', truncation=True, padding='max_length', max_length=self.max_length) |
| input_ids = encoding['input_ids'].squeeze() |
| attention_mask = encoding['attention_mask'].squeeze() |
| return {'input_ids': input_ids, 'attention_mask': attention_mask, 'labels': input_ids} |
|
|
| |
| def main(): |
| schema_file_path = 'schema7878.json' |
| data_file_path = 'data.json' |
| output_file_path = 'prompts.txt' |
|
|
| schema = load_json(schema_file_path) |
| data = load_json(data_file_path) |
| |
| prompts = generate_prompts(schema, data) |
| |
| |
| with open(output_file_path, 'w') as file: |
| for prompt in prompts: |
| file.write(prompt + "\n\n") |
|
|
| |
| tokenizer = LlamaTokenizer.from_pretrained('llama-3') |
| model = LlamaForCausalLM.from_pretrained('llama-3') |
|
|
| |
| dataset = PromptsDataset(prompts, tokenizer, max_length=512) |
| dataloader = DataLoader(dataset, batch_size=4, shuffle=True) |
|
|
| |
| training_args = TrainingArguments( |
| output_dir='./results', |
| overwrite_output_dir=True, |
| num_train_epochs=3, |
| per_device_train_batch_size=4, |
| save_steps=10_000, |
| save_total_limit=2, |
| prediction_loss_only=True, |
| ) |
|
|
| |
| trainer = Trainer( |
| model=model, |
| args=training_args, |
| train_dataset=dataset, |
| ) |
|
|
| |
| trainer.train() |
|
|
| |
| model.save_pretrained('./trained_model') |
| tokenizer.save_pretrained('./trained_model') |
| print(f"Generated {len(prompts)} prompts and saved to {output_file_path}") |
|
|
| if __name__ == "__main__": |
| main() |
|
|