HusnaManakkot commited on
Commit
abe7c03
Β·
verified Β·
1 Parent(s): 120ccfd

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +27 -44
app.py CHANGED
@@ -1,52 +1,35 @@
1
- from transformers import AutoTokenizer, AutoModelForSeq2SeqLM, Trainer, TrainingArguments
 
2
  from datasets import load_dataset
3
 
4
- # Load the model and tokenizer
5
- model_name = "hrshtsharma2012/NL2SQL-Picard-final"
6
- tokenizer = AutoTokenizer.from_pretrained(model_name)
7
- model = AutoModelForSeq2SeqLM.from_pretrained(model_name)
8
 
9
- # Load the Spider dataset
10
- dataset = load_dataset("spider")
11
 
12
- # Preprocess the dataset
13
- def tokenize_function(examples):
14
- inputs = tokenizer(examples["question"], padding="max_length", truncation=True, max_length=512)
15
- outputs = tokenizer(examples["query"], padding="max_length", truncation=True, max_length=512)
16
- return {"input_ids": inputs.input_ids, "attention_mask": inputs.attention_mask, "labels": outputs.input_ids}
17
 
18
- tokenized_dataset = dataset.map(tokenize_function, batched=True)
19
-
20
- # Fine-tune the model
21
- training_args = TrainingArguments(
22
- output_dir="./results",
23
- num_train_epochs=3,
24
- per_device_train_batch_size=4,
25
- per_device_eval_batch_size=4,
26
- warmup_steps=500,
27
- weight_decay=0.01,
28
- logging_dir="./logs",
29
- )
30
 
31
- trainer = Trainer(
32
- model=model,
33
- args=training_args,
34
- train_dataset=tokenized_dataset["train"],
35
- eval_dataset=tokenized_dataset["validation"],
 
 
 
 
 
 
36
  )
37
 
38
- trainer.train()
39
-
40
- # Evaluate the model
41
- results = trainer.evaluate()
42
- print(results)
43
-
44
- # Use the model for inference
45
- def generate_sql(question):
46
- inputs = tokenizer(question, return_tensors="pt", padding=True)
47
- outputs = model.generate(**inputs, max_length=512)
48
- sql_query = tokenizer.decode(outputs[0], skip_special_tokens=True)
49
- return sql_query
50
-
51
- sample_question = "What are the names of the students?"
52
- print(generate_sql(sample_question))
 
1
+ import gradio as gr
2
+ from transformers import AutoTokenizer, AutoModelForSeq2SeqLM, pipeline
3
  from datasets import load_dataset
4
 
5
+ # Load tokenizer and model
6
+ tokenizer = AutoTokenizer.from_pretrained("hrshtsharma2012/NL2SQL-Picard-final")
7
+ model = AutoModelForSeq2SeqLM.from_pretrained("hrshtsharma2012/NL2SQL-Picard-final")
 
8
 
9
+ # Initialize the pipeline
10
+ nl2sql_pipeline = pipeline("text2text-generation", model=model, tokenizer=tokenizer)
11
 
12
+ # Load a part of the Spider dataset
13
+ spider_dataset = load_dataset("spider", split='train[:5]')
 
 
 
14
 
15
+ def generate_sql(query):
16
+ results = nl2sql_pipeline(query)
17
+ sql_query = results[0]['generated_text']
18
+ return sql_query
 
 
 
 
 
 
 
 
19
 
20
+ # Use examples from the Spider dataset
21
+ example_questions = [(question['question'],) for question in spider_dataset]
22
+
23
+ # Create a Gradio interface
24
+ interface = gr.Interface(
25
+ fn=generate_sql,
26
+ inputs=gr.Textbox(lines=2, placeholder="Enter your natural language query here..."),
27
+ outputs="text",
28
+ examples=example_questions,
29
+ title="NL to SQL with Picard",
30
+ description="This model converts natural language queries into SQL using the Spider dataset. Try one of the example questions or enter your own!"
31
  )
32
 
33
+ # Launch the app
34
+ if __name__ == "__main__":
35
+ interface.launch()