Spaces:
Sleeping
Sleeping
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) | |