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 = "Qwen/Qwen2.5-14B-Instruct"
7model_name = "taherdoust/qwen25-14b-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, trust_remote_code=True)
27
28# Generate SQL from question
29question = "Find all buildings within 500 meters of the city center"
30
31prompt = f"""<|im_start|>system
32You are an expert in PostGIS spatial SQL for City Information Modeling (CIM).
33Your task is to generate precise PostGIS spatial SQL queries for the CIM Wizard database.
34
35Database Schema:
36- cim_vector: Building geometries, project scenarios, grid infrastructure
37- cim_census: Italian census demographic data (ISTAT 2011)
38- cim_raster: DTM/DSM raster data
39- cim_network: Electrical grid network data
40
41Generate only the SQL query without explanations.<|im_end|>
42<|im_start|>user
43{question}<|im_end|>
44<|im_start|>assistant
45"""
46
47inputs = tokenizer(prompt, return_tensors="pt").to(model.device)
48outputs = model.generate(**inputs, max_new_tokens=512, temperature=0.1)
49sql = tokenizer.decode(outputs[0], skip_special_tokens=False)
50print(sql)1@misc{qwen25-14b-cim-q2sql,
2 author = {Taherdoust, Ali},
3 title = {Qwen 2.5 14B Fine-tuned for CIM Spatial SQL Generation},
4 year = {2025},
5 publisher = {HuggingFace},
6 howpublished = {\url{https://huggingface.co/taherdoust/qwen25-14b-cim-q2sql}},
7 note = {Fine-tuned for PostGIS spatial SQL queries in City Information Modeling}
8}