Danjie commited on
Commit
f835844
1 Parent(s): 33b0a25

Added instruction to run inference on 13b SQLMaster

Browse files
Files changed (1) hide show
  1. README.md +65 -0
README.md CHANGED
@@ -1,3 +1,68 @@
1
  ---
2
  license: mit
3
  ---
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
  ---
2
  license: mit
3
  ---
4
+
5
+ # SQLMaster
6
+ A minimum of 10 GB VRAM is required.
7
+
8
+ ## Colab Example
9
+ https://colab.research.google.com/drive/1Nvwie-klMNPPWI4o7Nae4l5spxEX1PaD?usp=sharing
10
+
11
+ ## Install Prerequisite
12
+ ```bash
13
+ !pip install peft
14
+ !pip install transformers
15
+ !pip install bitsandbytes
16
+ !pip install accelerate
17
+ ```
18
+
19
+ ## Login Using Huggingface Token
20
+ ```bash
21
+ # You need a huggingface token that can access llama2
22
+ from huggingface_hub import notebook_login
23
+ notebook_login()
24
+ ```
25
+
26
+ ## Download Model
27
+ ```python
28
+ import torch
29
+ from peft import PeftModel, PeftConfig
30
+ from transformers import AutoModelForCausalLM, AutoTokenizer, BitsAndBytesConfig
31
+ device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
32
+
33
+ bnb_config = BitsAndBytesConfig(
34
+ load_in_4bit=True,
35
+ bnb_4bit_use_double_quant=True,
36
+ bnb_4bit_quant_type="nf4",
37
+ bnb_4bit_compute_dtype=torch.bfloat16
38
+ )
39
+
40
+ peft_model_id = "Danjie/SQLMaster_13b"
41
+ config = PeftConfig.from_pretrained(peft_model_id)
42
+ tokenizer = AutoTokenizer.from_pretrained(config.base_model_name_or_path)
43
+ model = AutoModelForCausalLM.from_pretrained(config.base_model_name_or_path, device_map='auto', quantization_config=bnb_config)
44
+ model.resize_token_embeddings(len(tokenizer) + 1)
45
+
46
+ # Load the Lora model
47
+ model = PeftModel.from_pretrained(model, peft_model_id)
48
+ ```
49
+
50
+ ## Inference
51
+ ```python
52
+ def create_sql_query(question: str, context: str) -> str:
53
+ input = "Question: " + question + "\nContext:" + context + "\nAnswer"
54
+
55
+ # Encode and move tensor into cuda if applicable.
56
+ encoded_input = tokenizer(input, return_tensors='pt')
57
+ encoded_input = {k: v.to(device) for k, v in encoded_input.items()}
58
+
59
+ output = model.generate(**encoded_input, max_new_tokens=256)
60
+ response = tokenizer.decode(output[0], skip_special_tokens=True)
61
+ response = response[len(input):]
62
+ return response
63
+ ```
64
+
65
+ ## Example
66
+ ```python
67
+ create_sql_query("What is the highest age of users with name Danjie", "CREATE TABLE user (age INTEGER, name STRING)")
68
+ ```