Spaces:
Runtime error
Runtime error
Johannes commited on
Commit ·
7b065dc
1
Parent(s): 2be3343
update
Browse filesFiles changed (2)
- app.py+23-16
- example_strings.py+12-24
2 unmodified lines3456789101112131415162 unmodified lines19202122232425264 unmodified lines313233343536373 unmodified lines414243444546474849502 unmodified linesfrom example_strings import example1, example2, example3# tokenizer6B = AutoTokenizer.from_pretrained(f"NumbersStation/nsql-6B")# model6B = AutoModelForCausalLM.from_pretrained(f"NumbersStation/nsql-6B")# tokenizer2B = AutoTokenizer.from_pretrained(f"NumbersStation/nsql-2B")# model2B = AutoModelForCausalLM.from_pretrained(f"NumbersStation/nsql-2B")# tokenizer350M = AutoTokenizer.from_pretrained(f"NumbersStation/nsql-2B")# model350M = AutoModelForCausalLM.from_pretrained(f"NumbersStation/nsql-2B")def load_model(model_name: str):2 unmodified linesreturn tokenizer, modeldef infer(input_text, model_choice):tokenizer, model = load_model(model_choice)input_ids = tokenizer(input_text, return_tensors="pt").input_idsgenerated_ids = model.generate(input_ids, max_length=500)return (tokenizer.decode(generated_ids[0], skip_special_tokens=True))4 unmodified lines- [nsql-2B](https://huggingface.co/NumbersStation/nsql-2B)- [nsql-350M]((https://huggingface.co/NumbersStation/nsql-350M))This demo let's you choose which one you want to use and provides the three examples you can also find in their model cards.In general you should first provide the table schemas of the tables you have questions about and then prompt it with a natural language question.The model will then generate a SQL query that you can run against your database.3 unmodified linestitle="Text to SQL with NSQL",description=description,fn=infer,inputs=["text",gr.Dropdown(["nsql-6B", "nsql-2B", "nsql-350M"], value="nsql-6B")],outputs="text",examples=[[example1, "nsql-350M"],[example2, "nsql-2B"],[example3, "nsql-350M"]])iface.launch()2 unmodified lines34567891011121314152 unmodified lines1819202122232425262728293031324 unmodified lines373839404142433 unmodified lines47484950515253545556572 unmodified linesfrom example_strings import example1, example2, example3template_str = """{table_schemas}\n \n{task_spec}\n \n{prompt}\n \nSELECT"""def load_model(model_name: str):2 unmodified linesreturn tokenizer, modeldef build_complete_prompt(table_schemas: str, task_spec: str, prompt: str) -> str:return template_str.format(table_schemas=table_schemas, task_spec=task_spec, prompt=prompt)def infer(table_schemas: str, task_spec: str, prompt: str, model_choice: str = "nsql-350M"):tokenizer, model = load_model(model_choice)input_text = build_complete_prompt(table_schemas, task_spec, prompt)input_ids = tokenizer(input_text, return_tensors="pt").input_idsgenerated_ids = model.generate(input_ids, max_length=500)return (tokenizer.decode(generated_ids[0], skip_special_tokens=True))4 unmodified lines- [nsql-2B](https://huggingface.co/NumbersStation/nsql-2B)- [nsql-350M]((https://huggingface.co/NumbersStation/nsql-350M))For now you can only use the 350M version of the model here, as the file size of the other models exceeds the max memory available in spaces.In general you should first provide the table schemas of the tables you have questions about and then prompt it with a natural language question.The model will then generate a SQL query that you can run against your database.3 unmodified linestitle="Text to SQL with NSQL",description=description,fn=infer,inputs=[gr.Text(label="Table schemas", placeholder="Insert your table schemas here"),gr.Text(label="Specify Task", value="Using valid SQLite, answer the following questions for the tables provided above."),gr.Text(label="Prompt", placeholder="Put your natural language prompt here"),# gr.Dropdown(["nsql-6B", "nsql-2B", "nsql-350M"], value="nsql-6B")],outputs="text",examples=[example1, example2, example3])iface.launch()
123424 unmodified lines293031323334353637383940414243444546474849505152535455562 unmodified lines59606162636465666768example1 = """CREATE TABLE stadium (stadium_id number,location text,name text,24 unmodified linesCREATE TABLE singer_in_concert (concert_id number,singer_id text)-- Using valid SQLite, answer the following questions for the tables provided above.-- What is the maximum, the average, and the minimum capacity of stadiums ?SELECT"""example2 = """CREATE TABLE stadium (stadium_id number,location text,name text,capacity number,)-- Using valid SQLite, answer the following questions for the tables provided above.-- how many stadiums in total?SELECT"""example3 = """CREATE TABLE work_orders (ID NUMBER,CREATED_AT TEXT,COST FLOAT,2 unmodified linesIS_OPEN BOOLEAN,IS_OVERDUE BOOLEAN,COUNTRY_NAME TEXT,)-- Using valid SQLite, answer the following questions for the tables provided above.-- how many work orders are open?SELECT"""123424 unmodified lines29303132333435363738394041424344454647482 unmodified lines515253545556example1 = ["""CREATE TABLE stadium (stadium_id number,location text,name text,24 unmodified linesCREATE TABLE singer_in_concert (concert_id number,singer_id text)""","Using valid SQLite, answer the following questions for the tables provided above.","What is the maximum, the average, and the minimum capacity of stadiums ?"]example2 = ["""CREATE TABLE stadium (stadium_id number,location text,name text,capacity number,)""","Using valid SQLite, answer the following questions for the tables provided above.","how many stadiums in total?"]example3 = ["""CREATE TABLE work_orders (ID NUMBER,CREATED_AT TEXT,COST FLOAT,2 unmodified linesIS_OPEN BOOLEAN,IS_OVERDUE BOOLEAN,COUNTRY_NAME TEXT,)""","Using valid SQLite, answer the following questions for the tables provided above.","how many work orders are open?"]
app.py+23-16
example_strings.py+12-24