Spaces:
Running
Running
yuk068
commited on
Commit
·
69ddefc
1
Parent(s):
4540679
Space with UI
Browse files- app.py +50 -0
- requirements.txt +6 -0
app.py
ADDED
@@ -0,0 +1,50 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
import torch
|
3 |
+
from transformers import AutoTokenizer, AutoModelForCausalLM
|
4 |
+
|
5 |
+
# Path to model on the Hugging Face Hub
|
6 |
+
MODEL_NAME = "Yuk050/gemma-3-1b-text-to-sql-model"
|
7 |
+
|
8 |
+
# Load the model and tokenizer
|
9 |
+
tokenizer = AutoTokenizer.from_pretrained(MODEL_NAME)
|
10 |
+
model = AutoModelForCausalLM.from_pretrained(
|
11 |
+
MODEL_NAME,
|
12 |
+
torch_dtype=torch.float32, # safer default for CPU Spaces
|
13 |
+
device_map="auto",
|
14 |
+
)
|
15 |
+
|
16 |
+
def generate_sql(schema: str, natural_language_query: str) -> str:
|
17 |
+
user_message = f"Given the following database schema:\n\n{schema}\n\nGenerate the SQL query for: {natural_language_query}"
|
18 |
+
|
19 |
+
messages = [{"role": "user", "content": user_message}]
|
20 |
+
input_ids = tokenizer.apply_chat_template(
|
21 |
+
messages,
|
22 |
+
add_generation_prompt=True,
|
23 |
+
return_tensors="pt"
|
24 |
+
).to(model.device)
|
25 |
+
|
26 |
+
outputs = model.generate(
|
27 |
+
input_ids,
|
28 |
+
max_new_tokens=256,
|
29 |
+
do_sample=True,
|
30 |
+
temperature=0.7,
|
31 |
+
top_k=50,
|
32 |
+
top_p=0.95,
|
33 |
+
eos_token_id=tokenizer.eos_token_id
|
34 |
+
)
|
35 |
+
|
36 |
+
response = tokenizer.decode(outputs[0][input_ids.shape[-1]:], skip_special_tokens=True)
|
37 |
+
return response.strip()
|
38 |
+
|
39 |
+
iface = gr.Interface(
|
40 |
+
fn=generate_sql,
|
41 |
+
inputs=[
|
42 |
+
gr.Textbox(lines=10, label="Database Schema", placeholder="e.g., CREATE TABLE Employees (...)"),
|
43 |
+
gr.Textbox(lines=2, label="Natural Language Query", placeholder="e.g., Select all employees with salary > 50000")
|
44 |
+
],
|
45 |
+
outputs=gr.Textbox(lines=5, label="Generated SQL Query"),
|
46 |
+
title="Text-to-SQL with Gemma 3 1B",
|
47 |
+
description="Enter a database schema and natural language question. The model will generate the SQL."
|
48 |
+
)
|
49 |
+
|
50 |
+
iface.launch()
|
requirements.txt
ADDED
@@ -0,0 +1,6 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
transformers
|
2 |
+
torch
|
3 |
+
gradio
|
4 |
+
accelerate
|
5 |
+
bitsandbytes
|
6 |
+
unsloth
|