Omkar1872 commited on
Commit
16e3c44
·
verified ·
1 Parent(s): 5c81141

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +5 -6
app.py CHANGED
@@ -3,9 +3,9 @@ import pandas as pd
3
  from transformers import AutoTokenizer, AutoModelForSeq2SeqLM
4
  import sqlite3
5
 
6
- # Load model
7
  model_name = "mrm8488/t5-base-finetuned-wikiSQL"
8
- tokenizer = AutoTokenizer.from_pretrained(model_name, use_fast=False) # <-- use slow tokenizer
9
  model = AutoModelForSeq2SeqLM.from_pretrained(model_name)
10
 
11
  def nl_to_sql(question, file):
@@ -18,15 +18,14 @@ def nl_to_sql(question, file):
18
  conn = sqlite3.connect(":memory:")
19
  df.to_sql("data_table", conn, index=False, if_exists="replace")
20
 
21
- # Schema description
22
  schema = ", ".join(df.columns)
23
  text = f"translate English to SQL: {question} | table columns: {schema}"
24
 
 
25
  inputs = tokenizer(text, return_tensors="pt")
26
  outputs = model.generate(**inputs, max_length=256)
27
  sql_query = tokenizer.decode(outputs[0], skip_special_tokens=True)
28
 
29
- # Execute SQL query
30
  try:
31
  result = pd.read_sql_query(sql_query, conn)
32
  except Exception as e:
@@ -45,8 +44,8 @@ iface = gr.Interface(
45
  gr.Textbox(label="Generated SQL Query"),
46
  gr.Dataframe(label="Result Preview")
47
  ],
48
- title="🧠 Natural Language to SQL Generator",
49
- description="Upload a CSV and ask questions in plain English. Generates SQL and shows results instantly."
50
  )
51
 
52
  if __name__ == "__main__":
 
3
  from transformers import AutoTokenizer, AutoModelForSeq2SeqLM
4
  import sqlite3
5
 
6
+ # Load model with slow tokenizer explicitly
7
  model_name = "mrm8488/t5-base-finetuned-wikiSQL"
8
+ tokenizer = AutoTokenizer.from_pretrained(model_name, use_fast=False, local_files_only=False) # Force slow tokenizer
9
  model = AutoModelForSeq2SeqLM.from_pretrained(model_name)
10
 
11
  def nl_to_sql(question, file):
 
18
  conn = sqlite3.connect(":memory:")
19
  df.to_sql("data_table", conn, index=False, if_exists="replace")
20
 
 
21
  schema = ", ".join(df.columns)
22
  text = f"translate English to SQL: {question} | table columns: {schema}"
23
 
24
+ # Tokenize with slow tokenizer
25
  inputs = tokenizer(text, return_tensors="pt")
26
  outputs = model.generate(**inputs, max_length=256)
27
  sql_query = tokenizer.decode(outputs[0], skip_special_tokens=True)
28
 
 
29
  try:
30
  result = pd.read_sql_query(sql_query, conn)
31
  except Exception as e:
 
44
  gr.Textbox(label="Generated SQL Query"),
45
  gr.Dataframe(label="Result Preview")
46
  ],
47
+ title="🧠 NL to SQL Generator",
48
+ description="Upload a CSV and ask questions in plain English. Generates SQL and shows results."
49
  )
50
 
51
  if __name__ == "__main__":