File size: 1,532 Bytes
54e8483
b503163
1323468
54e8483
b503163
fc83495
 
54e8483
ecc7032
9b65c50
ecc7032
9b65c50
ecc7032
 
 
 
3bfe327
b503163
 
92ce176
b503163
 
 
 
 
 
 
 
 
 
 
3b69718
de280aa
3b69718
 
b503163
 
 
 
de280aa
ecc7032
3b69718
 
 
 
 
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
import gradio as gr
from transformers import T5Tokenizer, T5ForConditionalGeneration
from datasets import load_dataset

# Load the tokenizer and model
tokenizer = T5Tokenizer.from_pretrained('t5-small', legacy=False)
model = T5ForConditionalGeneration.from_pretrained('t5-small')

dataset = load_dataset("b-mc2/sql-create-context")

# examples = []

# for i in range(3):  # Let's take the first 3 examples
#     item = dataset[i]
#     question = item['question']
#     examples.append([question])

def generate_sql(question):
    # Format the question for the model if needed. For example:
    input_text = f"translate English to SQL: {question}"
    input_text = f"{question}"  # Directly use the question if the model is fine-tuned for SQL generation
    
    # Tokenize the input text
    input_ids = tokenizer.encode(input_text, return_tensors="pt")
    
    # Generate the output sequence
    output_ids = model.generate(input_ids, max_length=512, num_beams=5)[0]
    
    # Decode the generated ids to get the SQL query
    sql_query = tokenizer.decode(output_ids, skip_special_tokens=True)
    return sql_query

    
# Define the Gradio interface
iface = gr.Interface(
    fn=generate_sql,
    inputs=gr.Textbox(lines=2, placeholder="Enter your question here..."),
    outputs=gr.Textbox(),
    title="Natural Language to SQL",
    description="This app uses a Seq2Seq model to generate SQL queries from natural language questions.",
    # examples=examples
)

# Launch the app
if __name__ == "__main__":
    iface.launch()