Added running instruction
Browse files
README.md
CHANGED
|
@@ -1,3 +1,61 @@
|
|
| 1 |
---
|
| 2 |
license: mit
|
| 3 |
---
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
---
|
| 2 |
license: mit
|
| 3 |
---
|
| 4 |
+
|
| 5 |
+
# SQLMaster
|
| 6 |
+
A minimum of 8 GB VRAM is required.
|
| 7 |
+
|
| 8 |
+
## Colab Example
|
| 9 |
+
https://colab.research.google.com/drive/1kMv2nw4gqsQLkLGUUEAI31XOD_7BykDj?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
|
| 31 |
+
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
|
| 32 |
+
|
| 33 |
+
peft_model_id = "Danjie/SQLMaster"
|
| 34 |
+
config = PeftConfig.from_pretrained(peft_model_id)
|
| 35 |
+
tokenizer = AutoTokenizer.from_pretrained(config.base_model_name_or_path)
|
| 36 |
+
model = AutoModelForCausalLM.from_pretrained(config.base_model_name_or_path, return_dict=True, load_in_8bit=True, device_map='auto')
|
| 37 |
+
model.resize_token_embeddings(len(tokenizer) + 1)
|
| 38 |
+
|
| 39 |
+
# Load the Lora model
|
| 40 |
+
model = PeftModel.from_pretrained(model, peft_model_id)
|
| 41 |
+
```
|
| 42 |
+
|
| 43 |
+
## Inference
|
| 44 |
+
```python
|
| 45 |
+
def create_sql_query(question: str, context: str) -> str:
|
| 46 |
+
input = "Question: " + question + "\nContext:" + context + "\nAnswer"
|
| 47 |
+
|
| 48 |
+
# Encode and move tensor into cuda if applicable.
|
| 49 |
+
encoded_input = tokenizer(input, return_tensors='pt')
|
| 50 |
+
encoded_input = {k: v.to(device) for k, v in encoded_input.items()}
|
| 51 |
+
|
| 52 |
+
output = model.generate(**encoded_input, max_new_tokens=256)
|
| 53 |
+
response = tokenizer.decode(output[0], skip_special_tokens=True)
|
| 54 |
+
response = response[len(input):]
|
| 55 |
+
return response
|
| 56 |
+
```
|
| 57 |
+
|
| 58 |
+
## Example
|
| 59 |
+
```python
|
| 60 |
+
create_sql_query("What is the highest age of users with name Danjie", "CREATE TABLE user (age INTEGER, name STRING)")
|
| 61 |
+
```
|