File size: 1,687 Bytes
69ddefc
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
import gradio as gr
import torch
from transformers import AutoTokenizer, AutoModelForCausalLM

# Path to model on the Hugging Face Hub
MODEL_NAME = "Yuk050/gemma-3-1b-text-to-sql-model"

# Load the model and tokenizer
tokenizer = AutoTokenizer.from_pretrained(MODEL_NAME)
model = AutoModelForCausalLM.from_pretrained(
    MODEL_NAME,
    torch_dtype=torch.float32,  # safer default for CPU Spaces
    device_map="auto",
)

def generate_sql(schema: str, natural_language_query: str) -> str:
    user_message = f"Given the following database schema:\n\n{schema}\n\nGenerate the SQL query for: {natural_language_query}"
    
    messages = [{"role": "user", "content": user_message}]
    input_ids = tokenizer.apply_chat_template(
        messages,
        add_generation_prompt=True,
        return_tensors="pt"
    ).to(model.device)

    outputs = model.generate(
        input_ids,
        max_new_tokens=256,
        do_sample=True,
        temperature=0.7,
        top_k=50,
        top_p=0.95,
        eos_token_id=tokenizer.eos_token_id
    )

    response = tokenizer.decode(outputs[0][input_ids.shape[-1]:], skip_special_tokens=True)
    return response.strip()

iface = gr.Interface(
    fn=generate_sql,
    inputs=[
        gr.Textbox(lines=10, label="Database Schema", placeholder="e.g., CREATE TABLE Employees (...)"),
        gr.Textbox(lines=2, label="Natural Language Query", placeholder="e.g., Select all employees with salary > 50000")
    ],
    outputs=gr.Textbox(lines=5, label="Generated SQL Query"),
    title="Text-to-SQL with Gemma 3 1B",
    description="Enter a database schema and natural language question. The model will generate the SQL."
)

iface.launch()