Views
No views yet
pip install transformers peft torch1import torch
2from transformers import AutoTokenizer, AutoModelForSequenceClassification
3from peft import PeftModel, PeftConfig
4
5# The model_id refers to the path on the Hugging Face Hub where the PEFT adapter is located.
6peft_model_id = "derrickzhu/EllieSQL_Router_Checkpoints"
7
8# Load the PEFT configuration. This helps identify the base model if it's not explicitly named.
9config = PeftConfig.from_pretrained(peft_model_id)
10
11# The base model for these routers is typically Qwen2.5-0.5B, as indicated in the paper and adapter configs.
12# Make sure to use the correct Hugging Face Hub ID for the base model.
13base_model_name_or_path = "Qwen/Qwen2.5-0.5B"
14
15# Load the tokenizer for the base model.
16tokenizer = AutoTokenizer.from_pretrained(base_model_name_or_path)
17
18# The router performs sequence classification (e.g., simple, medium, complex queries).
19# The `config.json` inside the repository indicates `num_labels=3` for this classification task.
20model = AutoModelForSequenceClassification.from_pretrained(
21 base_model_name_or_path,
22 num_labels=3,
23 torch_dtype=torch.bfloat16, # Adjust dtype based on your hardware and desired precision
24 low_cpu_mem_usage=True # Optimize memory usage for large models
25)
26
27# Load the PEFT adapter weights onto the base model.
28model = PeftModel.from_pretrained(model, peft_model_id)
29model.eval() # Set the model to evaluation mode
30model.to("cuda") # Move the model to GPU if available for faster inference
31
32# Example natural language query to classify
33text_query = "Find the names of all students who scored above 90 in Math and live in New York."
34
35# Tokenize the input query.
36inputs = tokenizer(text_query, return_tensors="pt").to(model.device)
37
38# Perform inference to get the classification logits.
39with torch.no_grad():
40 outputs = model(**inputs)
41 logits = outputs.logits
42 # Get the predicted class ID (0, 1, or 2).
43 predicted_class_id = torch.argmax(logits, dim=-1).item()
44
45# Map the predicted ID back to a human-readable label.
46# The `id2label` mapping is typically found in the model's `config.json`.
47# For this model, labels are "LABEL_0", "LABEL_1", "LABEL_2".
48# Refer to the EllieSQL paper or project documentation for the exact mapping of these labels
49# to complexity categories (e.g., LABEL_0 -> "simple", LABEL_1 -> "medium", LABEL_2 -> "complex").
50id_to_label = model.config.id2label
51predicted_label = id_to_label[predicted_class_id]
52
53print(f"Text query: '{text_query}'")
54print(f"Predicted raw label: {predicted_label}")
55# If the exact mapping to complexity is known, e.g.:
56# complexity_map = {"LABEL_0": "simple", "LABEL_1": "medium", "LABEL_2": "complex"}
57# print(f"Predicted complexity: {complexity_map.get(predicted_label, 'unknown')}")1@misc{zhu2025elliesql,
2 title={EllieSQL: Cost-Efficient Text-to-SQL with Complexity-Aware Routing},
3 author={Yizhang Zhu and Runzhi Jiang and Boyan Li and Nan Tang and Yuyu Luo},
4 year={2025},
5 eprint={2503.22402},
6 archivePrefix={arXiv},
7 primaryClass={cs.DB},
8 url={https://arxiv.org/abs/2503.22402},
9}