Shreyas45 commited on
Commit
a36f28e
·
1 Parent(s): 4fff5a9

Update README.md

Browse files
Files changed (1) hide show
  1. README.md +83 -0
README.md CHANGED
@@ -32,3 +32,86 @@ The following `bitsandbytes` quantization config was used during training:
32
  - PEFT 0.5.0
33
 
34
  - PEFT 0.5.0
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
32
  - PEFT 0.5.0
33
 
34
  - PEFT 0.5.0
35
+ # Inference Code
36
+
37
+ ### Install required libraries
38
+
39
+ ```python
40
+ !pip install transformers peft
41
+ ```
42
+ ### Login
43
+ ```python
44
+ from huggingface_hub import login
45
+
46
+ token = "Your Key"
47
+ login(token)
48
+ ```
49
+ #### Import necessary modules
50
+ ```python
51
+ from peft import PeftModel, PeftConfig
52
+ from transformers import AutoModelForCausalLM, AutoTokenizer
53
+ import torch
54
+ from transformers import BitsAndBytesConfig
55
+ from peft import prepare_model_for_kbit_training
56
+ ```
57
+ #### Load PEFT model and configuration
58
+ ```python
59
+ config = PeftConfig.from_pretrained("Shreyas45/Llama2_Text-to-SQL_Fintuned")
60
+ peft_model = AutoModelForCausalLM.from_pretrained("meta-llama/Llama-2-7b-hf")
61
+ peft_model = PeftModel.from_pretrained(peft_model, "Shreyas45/Llama2_Text-to-SQL_Fintuned")
62
+ ```
63
+ ### Load trained model and tokenizer
64
+ ```python
65
+ from transformers import AutoModelForCausalLM, AutoTokenizer, BitsAndBytesConfig
66
+ from peft import prepare_model_for_kbit_training
67
+
68
+ trained_model_tokenizer = AutoTokenizer.from_pretrained(config.base_model_name_or_path, trust_remote_code=True)
69
+ trained_model_tokenizer.pad_token = trained_model_tokenizer.eos_token
70
+ ```
71
+
72
+ ### Define a SQL query
73
+ ```python
74
+ query = '''In the table named management with columns (department_id VARCHAR, temporary_acting VARCHAR);
75
+ CREATE TABLE department (name VARCHAR, num_employees VARCHAR, department_id VARCHAR),
76
+ Show the name and number of employees for the departments managed by heads whose temporary acting value is 'Yes'?'''
77
+ ```
78
+
79
+ ### Construct prompt
80
+ ```python
81
+ prompt = f'''### Instruction: Below is an instruction that describes a task and the schema of the table in the database.
82
+ Write a response that generates a request in the form of a SQL query.
83
+ Here the schema of the table is mentioned first followed by the question for which the query needs to be generated.
84
+ And the question is: {query}
85
+ ###Output: '''
86
+ ```
87
+ ### Tokenize the prompt
88
+ ```python
89
+ encodings = trained_model_tokenizer(prompt, return_tensors='pt')
90
+ ```
91
+ #### Configure generation parameters
92
+ ```python
93
+
94
+ generation_config = peft_model.generation_config
95
+ generation_config.max_new_token = 1024
96
+ generation_config.temperature = 0.7
97
+ generation_config.top_p = 0.7
98
+ generation_config.num_return_sequence = 1
99
+ generation_config.pad_token_id = trained_model_tokenizer.pad_token_id
100
+ generation_config.eos_token_id = trained_model_tokenizer.eos_token_id
101
+ ```
102
+ ### Generate SQL query using the model
103
+ ```python
104
+ with torch.inference_mode():
105
+ outputs = peft_model.generate(
106
+ input_ids=encodings.input_ids,
107
+ attention_mask=encodings.attention_mask,
108
+ generation_config=generation_config,
109
+ max_new_tokens=100
110
+ )
111
+ ```
112
+ ### Decode and print the generated SQL query
113
+ ```python
114
+ generated_query = trained_model_tokenizer.decode(outputs[0])
115
+ print("Generated SQL Query:")
116
+ print(generated_query)
117
+ ```