File size: 1,322 Bytes
f4f80f2
 
748a3d0
 
 
f0a792b
f4f80f2
 
 
 
 
4198861
 
 
f4f80f2
 
 
 
4198861
f4f80f2
 
 
4198861
 
 
 
 
f4f80f2
 
 
 
4198861
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
import gradio as gr
from transformers import AutoTokenizer, AutoModelForSeq2SeqLM
from datasets import load_dataset

# Load the Spider dataset
spider_dataset = load_dataset("spider", split='train[:5]')

# Load tokenizer and model
tokenizer = AutoTokenizer.from_pretrained("mrm8488/t5-base-finetuned-wikiSQL")
model = AutoModelForSeq2SeqLM.from_pretrained("mrm8488/t5-base-finetuned-wikiSQL")

def generate_sql_from_dataset(index):
    # Get the natural language query from the dataset
    query = spider_dataset[index]['question']
    input_text = "translate English to SQL: " + query
    inputs = tokenizer(input_text, return_tensors="pt", padding=True)
    outputs = model.generate(**inputs, max_length=512)
    sql_query = tokenizer.decode(outputs[0], skip_special_tokens=True)
    return query, sql_query

# Create a Gradio interface
interface = gr.Interface(
    fn=generate_sql_from_dataset,
    inputs=gr.Number(label="Dataset Index (0-4)", default=0),
    outputs=[gr.Textbox(label="Natural Language Query"), gr.Textbox(label="Generated SQL Query")],
    title="NL to SQL with T5 using Spider Dataset",
    description="This model converts natural language queries from the Spider dataset into SQL. Enter the index of the dataset entry (0-4)!"
)

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