File size: 1,390 Bytes
abe7c03
f525ef3
 
5cacb61
abe7c03
 
 
5cacb61
f525ef3
 
 
d82d943
 
f525ef3
abe7c03
f525ef3
 
 
 
abe7c03
120ccfd
d82d943
 
f525ef3
abe7c03
 
 
 
 
f525ef3
abe7c03
d82d943
120ccfd
 
abe7c03
 
 
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
import gradio as gr
from transformers import AutoTokenizer, AutoModelForSeq2SeqLM, pipeline
from datasets import load_dataset

# Load tokenizer and model
tokenizer = AutoTokenizer.from_pretrained("hrshtsharma2012/NL2SQL-Picard-final")
model = AutoModelForSeq2SeqLM.from_pretrained("hrshtsharma2012/NL2SQL-Picard-final")

# Initialize the pipeline
nl2sql_pipeline = pipeline("text2text-generation", model=model, tokenizer=tokenizer)

# Load a part of the WikiSQL dataset
wikisql_dataset = load_dataset("wikisql", split='train[:5]')

def generate_sql(query):
    results = nl2sql_pipeline(query)
    sql_query = results[0]['generated_text']
    # Post-process the output to ensure it's a valid SQL query
    sql_query = sql_query.replace('<pad>', '').replace('</s>', '').strip()
    return sql_query

# Use examples from the WikiSQL dataset
example_questions = [(question['question'],) for question in wikisql_dataset]

# Create a Gradio interface
interface = gr.Interface(
    fn=generate_sql,
    inputs=gr.Textbox(lines=2, placeholder="Enter your natural language query here..."),
    outputs="text",
    examples=example_questions,
    title="NL to SQL with Picard",
    description="This model converts natural language queries into SQL using the WikiSQL dataset. Try one of the example questions or enter your own!"
)

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