Shreyasrp commited on
Commit
19b48aa
·
1 Parent(s): 62fa1fe

Update README.md

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