File size: 2,890 Bytes
1203000
 
4421773
 
 
 
e0f70bc
 
 
 
 
4421773
 
1203000
4421773
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2f45f54
 
 
4421773
 
 
55a3202
 
 
 
 
 
 
4421773
 
55a3202
 
 
4421773
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
---
license: mit
datasets:
- gretelai/synthetic_text_to_sql
language:
- en
inference:
  parameters:
    do_sample: False
    max_new_tokens: 250
    temperature: 0.7
library_name: transformers
pipeline_tag: text2text-generation
---
# Gemma 2B Fine-Tuned SQL Generator

## Introduction
The Gemma 2B SQL Generator is a specialized version of the Gemma 2B model, fine-tuned to generate SQL queries based on a given SQL context. This model has been tailored to assist developers and analysts in generating accurate SQL queries automatically, enhancing productivity and reducing the scope for errors.

## Model Details
- **Model Type:** Gemma 2B
- **Fine-Tuning Details:** The model was fine-tuned specifically for generating SQL queries.
- **Training Loss:** Achieved a training loss of 0.3, indicating a high level of accuracy in SQL query generation.

## Installation
To set up the necessary environment for using the SQL Generator, run the following commands:
```bash
pip install torch torch
pip install transformers
```

## how to Fine Tune
here is the github link [click here](https://github.com/theSuriya/Gemma-SQL-Generator/tree/main)

## Inference

```python

# Load model directly
from transformers import AutoTokenizer, AutoModelForCausalLM

tokenizer = AutoTokenizer.from_pretrained("suriya7/Gemma2B-Finetuned-Sql-Generator")
model = AutoModelForCausalLM.from_pretrained("suriya7/Gemma2B-Finetuned-Sql-Generator")

prompt_template = """
<start_of_turn>user
You are an intelligent AI specialized in generating SQL queries.
Your task is to assist users in formulating SQL queries to retrieve specific information from a database.
Please provide the SQL query corresponding to the given prompt and context:

Prompt:
find the price of laptop

Context:
CREATE TABLE products (
    product_id INT,
    product_name VARCHAR(100),
    category VARCHAR(50),
    price DECIMAL(10, 2),
    stock_quantity INT
);

INSERT INTO products (product_id, product_name, category, price, stock_quantity) 
VALUES 
    (1, 'Smartphone', 'Electronics', 599.99, 100),
    (2, 'Laptop', 'Electronics', 999.99, 50),
    (3, 'Headphones', 'Electronics', 99.99, 200),
    (4, 'T-shirt', 'Apparel', 19.99, 300),
    (5, 'Jeans', 'Apparel', 49.99, 150);<end_of_turn>
<start_of_turn>model
"""

prompt = prompt_template
encodeds = tokenizer(prompt, return_tensors="pt", add_special_tokens=True).input_ids

device = torch.device("cuda:0" if torch.cuda.is_available() else "cpu")
model.to(device)
inputs = encodeds.to(device)


# Increase max_new_tokens if needed
generated_ids = model.generate(inputs, max_new_tokens=1000, do_sample=True, temperature = 0.7,pad_token_id=tokenizer.eos_token_id)
ans = ''
for i in tokenizer.decode(generated_ids[0], skip_special_tokens=True).split('<end_of_turn>')[:2]:
    ans += i

# Extract only the model's answer
model_answer = ans.split("model")[1].strip()
print(model_answer)
```