Views
No views yet
1from transformers import AutoModelForCausalLM, AutoTokenizer, pipeline
2from peft import PeftModel
3
4# Load base model
5base_model = AutoModelForCausalLM.from_pretrained(
6 "meta-llama/Llama-3.1-8B-Instruct",
7 device_map="auto",
8 torch_dtype="auto"
9)
10
11# Load LoRA adapter
12model = PeftModel.from_pretrained(base_model, "chrisjcc/Llama-3.1-8B-Instruct-text-to-sql-adapter")
13tokenizer = AutoTokenizer.from_pretrained("chrisjcc/Llama-3.1-8B-Instruct-text-to-sql-adapter")
14
15# For inference, merge adapter for better performance (optional)
16model = model.merge_and_unload()
17
18# Create text generation pipeline
19pipe = pipeline(
20 "text-generation",
21 model=model,
22 tokenizer=tokenizer,
23 max_new_tokens=256,
24 do_sample=False,
25)
26
27# Example usage
28schema = """
29CREATE TABLE users (
30 id INTEGER PRIMARY KEY,
31 name VARCHAR(100),
32 email VARCHAR(100),
33 created_at TIMESTAMP
34);
35"""
36
37question = "Show me all users who registered in the last 7 days"
38
39messages = [
40 {
41 "role": "system",
42 "content": f"You are a text to SQL translator.\n\nSCHEMA:\n{schema}"
43 },
44 {"role": "user", "content": question}
45]
46
47prompt = tokenizer.apply_chat_template(messages, tokenize=False, add_generation_prompt=True)
48outputs = pipe(prompt)
49sql_query = outputs[0]['generated_text'][len(prompt):].strip()
50
51print("Generated SQL:", sql_query)1{
2 "messages": [
3 {
4 "role": "system",
5 "content": "You are a text to SQL translator...\n\nSCHEMA:\nCREATE TABLE..."
6 },
7 {
8 "role": "user",
9 "content": "Show me all customers from New York"
10 },
11 {
12 "role": "assistant",
13 "content": "SELECT * FROM customers WHERE city = 'New York';"
14 }
15 ]
16}1@misc{chrisjcc_Llama_3.1_8B_Instruct_text_to_sql_adapter,
2 author = {Christian Contreras Campana},
3 title = {Llama-3.1-8B-Instruct-text-to-sql-adapter: Fine-tuned Text-to-SQL Model},
4 year = {2025},
5 publisher = {Hugging Face},
6 howpublished = {\url{https://huggingface.co/chrisjcc/Llama-3.1-8B-Instruct-text-to-sql-adapter}}
7}