File size: 917 Bytes
45c9d91
f78a3ab
db38d1c
f78a3ab
45c9d91
f78a3ab
 
45c9d91
 
 
 
 
ee8f33b
f78a3ab
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
from transformers import AutoTokenizer, AutoModelForCausalLM

def load_model(model_name="chatdb/natural-sql-7b"):
    """
    Loads the model on CPU and avoids bitsandbytes.
    """
    tokenizer = AutoTokenizer.from_pretrained(model_name)
    model = AutoModelForCausalLM.from_pretrained(
        model_name,
        device_map="auto",  # Auto-map to CPU
        offload_folder="offload",  # Offload to disk
        low_cpu_mem_usage=True,  # Optimize CPU memory usage
    )
    return tokenizer, model

def generate_sql(question, prompt_inputs, tokenizer, model, device="cpu"):
    """
    Generates an SQL query based on the question and schema.
    """
    prompt = prompt_inputs["formatted_prompt"]
    inputs = tokenizer(prompt, return_tensors="pt").to(device)

    outputs = model.generate(
        **inputs,
        max_new_tokens=128,
    )

    return tokenizer.decode(outputs[0], skip_special_tokens=True)