HusnaManakkot commited on
Commit
7e713f6
1 Parent(s): c193826

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +45 -6
app.py CHANGED
@@ -1,11 +1,10 @@
1
-
2
  import gradio as gr
3
  from transformers import AutoTokenizer, AutoModelForSeq2SeqLM
4
  from datasets import load_dataset
5
  from difflib import get_close_matches
6
 
7
- # Load the Spider dataset
8
- spider_dataset = load_dataset("wikisql", split='train[:100]') # Increase the number of examples for better matching
9
 
10
  # Load tokenizer and model
11
  tokenizer = AutoTokenizer.from_pretrained("mrm8488/t5-base-finetuned-wikiSQL")
@@ -18,7 +17,7 @@ def find_closest_match(query, dataset):
18
 
19
  def generate_sql_from_user_input(query):
20
  # Find the closest match in the dataset
21
- matched_query = find_closest_match(query, spider_dataset)
22
  if not matched_query:
23
  return "No close match found in the dataset.", ""
24
 
@@ -34,12 +33,52 @@ interface = gr.Interface(
34
  fn=generate_sql_from_user_input,
35
  inputs=gr.Textbox(label="Enter your natural language query"),
36
  outputs=[gr.Textbox(label="Matched Query from Dataset"), gr.Textbox(label="Generated SQL Query")],
37
- title="NL to SQL with T5 using Spider Dataset",
38
- description="This model finds the closest match in the Spider dataset for your query and generates the corresponding SQL."
39
  )
40
 
41
  # Launch the app
42
  if __name__ == "__main__":
43
  interface.launch()
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
44
 
 
 
 
 
 
 
 
 
45
 
 
 
 
 
 
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 WikiSQL dataset
7
+ wikisql_dataset = load_dataset("wikisql", 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")
 
17
 
18
  def generate_sql_from_user_input(query):
19
  # Find the closest match in the dataset
20
+ matched_query = find_closest_match(query, wikisql_dataset)
21
  if not matched_query:
22
  return "No close match found in the dataset.", ""
23
 
 
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 WikiSQL Dataset",
37
+ description="This model finds the closest match in the WikiSQL dataset for your query and generates the corresponding SQL."
38
  )
39
 
40
  # Launch the app
41
  if __name__ == "__main__":
42
  interface.launch()
43
+ import gradio as gr
44
+ from transformers import AutoTokenizer, AutoModelForSeq2SeqLM
45
+ from datasets import load_dataset
46
+ from difflib import get_close_matches
47
+
48
+ # Load the WikiSQL dataset
49
+ wikisql_dataset = load_dataset("wikisql", split='train[:100]') # Increase the number of examples for better matching
50
+
51
+ # Load tokenizer and model
52
+ tokenizer = AutoTokenizer.from_pretrained("mrm8488/t5-base-finetuned-wikiSQL")
53
+ model = AutoModelForSeq2SeqLM.from_pretrained("mrm8488/t5-base-finetuned-wikiSQL")
54
+
55
+ def find_closest_match(query, dataset):
56
+ questions = [item['question'] for item in dataset]
57
+ matches = get_close_matches(query, questions, n=1)
58
+ return matches[0] if matches else None
59
+
60
+ def generate_sql_from_user_input(query):
61
+ # Find the closest match in the dataset
62
+ matched_query = find_closest_match(query, wikisql_dataset)
63
+ if not matched_query:
64
+ return "No close match found in the dataset.", ""
65
+
66
+ # Generate SQL for the matched query
67
+ input_text = "translate English to SQL: " + matched_query
68
+ inputs = tokenizer(input_text, return_tensors="pt", padding=True)
69
+ outputs = model.generate(**inputs, max_length=512)
70
+ sql_query = tokenizer.decode(outputs[0], skip_special_tokens=True)
71
+ return matched_query, sql_query
72
 
73
+ # Create a Gradio interface
74
+ interface = gr.Interface(
75
+ fn=generate_sql_from_user_input,
76
+ inputs=gr.Textbox(label="Enter your natural language query"),
77
+ outputs=[gr.Textbox(label="Matched Query from Dataset"), gr.Textbox(label="Generated SQL Query")],
78
+ title="NL to SQL with T5 using WikiSQL Dataset",
79
+ description="This model finds the closest match in the WikiSQL dataset for your query and generates the corresponding SQL."
80
+ )
81
 
82
+ # Launch the app
83
+ if __name__ == "__main__":
84
+ interface.launch()