Views
No views yet
pip install transformers peft accelerate bitsandbytes1from transformers import AutoTokenizer, AutoModelForCausalLM, BitsAndBytesConfig
2from peft import PeftModel
3import torch
4
5# Load base model
6base_model = "meta-llama/Llama-3.1-8B-Instruct"
7model_name = "taherdoust/llama31-8b-cim-q2sql"
8
9# 4-bit quantization for memory efficiency
10bnb_config = BitsAndBytesConfig(
11 load_in_4bit=True,
12 bnb_4bit_quant_type="nf4",
13 bnb_4bit_compute_dtype=torch.bfloat16
14)
15
16# Load base model
17model = AutoModelForCausalLM.from_pretrained(
18 base_model,
19 quantization_config=bnb_config,
20 device_map="auto",
21 trust_remote_code=True
22)
23
24# Load fine-tuned adapter
25model = PeftModel.from_pretrained(model, model_name)
26tokenizer = AutoTokenizer.from_pretrained(model_name)
27
28# Generate SQL from question
29question = "Find all buildings within 500 meters of the city center"
30
31prompt = f"""<|begin_of_text|><|start_header_id|>system<|end_header_id|>
32
33You are an expert in PostGIS spatial SQL for City Information Modeling (CIM).
34Your task is to generate precise PostGIS spatial SQL queries for the CIM Wizard database.
35
36Database Schema:
37- cim_vector: Building geometries, project scenarios, grid infrastructure
38- cim_census: Italian census demographic data (ISTAT 2011)
39- cim_raster: DTM/DSM raster data
40- cim_network: Electrical grid network data
41
42Generate only the SQL query without explanations.<|eot_id|><|start_header_id|>user<|end_header_id|>
43
44{question}<|eot_id|><|start_header_id|>assistant<|end_header_id|>
45
46"""
47
48inputs = tokenizer(prompt, return_tensors="pt").to(model.device)
49outputs = model.generate(**inputs, max_new_tokens=512, temperature=0.1)
50sql = tokenizer.decode(outputs[0], skip_special_tokens=False)
51print(sql)1@misc{llama31-8b-cim-q2sql,
2 author = {Taherdoust, Ali},
3 title = {Llama 3.1 8B Fine-tuned for CIM Spatial SQL Generation},
4 year = {2025},
5 publisher = {HuggingFace},
6 howpublished = {\url{https://huggingface.co/taherdoust/llama31-8b-cim-q2sql}},
7 note = {Fine-tuned for PostGIS spatial SQL queries in City Information Modeling}
8}