HusnaManakkot commited on
Commit
91b0752
Β·
verified Β·
1 Parent(s): 72e6803

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +27 -22
app.py CHANGED
@@ -1,35 +1,40 @@
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 WikiSQL dataset
13
- wikisql_dataset = load_dataset("wikisql", split='train[:5]')
 
 
14
 
15
- def generate_sql(query):
16
- results = nl2sql_pipeline(query)
17
- sql_query = results[0]['generated_text']
18
- # Post-process the output to ensure it's a valid SQL query
19
- sql_query = sql_query.replace('<pad>', '').replace('</s>', '').strip()
20
- return sql_query
21
 
22
- # Use examples from the WikiSQL dataset
23
- example_questions = [(question['question'],) for question in wikisql_dataset]
 
 
 
 
24
 
25
  # Create a Gradio interface
26
  interface = gr.Interface(
27
- fn=generate_sql,
28
- inputs=gr.Textbox(lines=2, placeholder="Enter your natural language query here..."),
29
- outputs="text",
30
- examples=example_questions,
31
- title="NL to SQL with Picard",
32
- description="This model converts natural language queries into SQL using the WikiSQL dataset. Try one of the example questions or enter your own!"
33
  )
34
 
35
  # Launch the app
 
1
  import gradio as gr
2
+ from transformers import AutoTokenizer, AutoModelForSeq2SeqLM
3
  from datasets import load_dataset
4
+ from difflib import get_close_matches
5
 
6
+ # Load the Spider dataset
7
+ spider_dataset = load_dataset("spider", split='train[:100]') # Increase the number of examples for better matching
 
8
 
9
+ # Load tokenizer and model
10
+ tokenizer = AutoTokenizer.from_pretrained("mrm8488/t5-base-finetuned-wikiSQL")
11
+ model = AutoModelForSeq2SeqLM.from_pretrained("mrm8488/t5-base-finetuned-wikiSQL")
12
 
13
+ def find_closest_match(query, dataset):
14
+ questions = [item['question'] for item in dataset]
15
+ matches = get_close_matches(query, questions, n=1)
16
+ return matches[0] if matches else None
17
 
18
+ def generate_sql_from_user_input(query):
19
+ # Find the closest match in the dataset
20
+ matched_query = find_closest_match(query, spider_dataset)
21
+ if not matched_query:
22
+ return "No close match found in the dataset.", ""
 
23
 
24
+ # Generate SQL for the matched query
25
+ input_text = "translate English to SQL: " + matched_query
26
+ inputs = tokenizer(input_text, return_tensors="pt", padding=True)
27
+ outputs = model.generate(**inputs, max_length=512)
28
+ sql_query = tokenizer.decode(outputs[0], skip_special_tokens=True)
29
+ return matched_query, sql_query
30
 
31
  # Create a Gradio interface
32
  interface = gr.Interface(
33
+ fn=generate_sql_from_user_input,
34
+ inputs=gr.Textbox(label="Enter your natural language query"),
35
+ outputs=[gr.Textbox(label="Matched Query from Dataset"), gr.Textbox(label="Generated SQL Query")],
36
+ title="NL to SQL with T5 using Spider Dataset",
37
+ description="This model finds the closest match in the Spider dataset for your query and generates the corresponding SQL."
 
38
  )
39
 
40
  # Launch the app