Spaces:
Sleeping
Sleeping
Johannes
commited on
Commit
·
dd9e5f1
1
Parent(s):
02e6cce
init
Browse files- app.py +47 -0
- example_strings.py +68 -0
- requirements.txt +1 -0
app.py
ADDED
|
@@ -0,0 +1,47 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
from transformers import AutoTokenizer, AutoModelForCausalLM
|
| 3 |
+
from example_strings import example1, example2, example3
|
| 4 |
+
|
| 5 |
+
|
| 6 |
+
# tokenizer6B = AutoTokenizer.from_pretrained(f"NumbersStation/nsql-6B")
|
| 7 |
+
# model6B = AutoModelForCausalLM.from_pretrained(f"NumbersStation/nsql-6B")
|
| 8 |
+
|
| 9 |
+
# tokenizer2B = AutoTokenizer.from_pretrained(f"NumbersStation/nsql-2B")
|
| 10 |
+
# model2B = AutoModelForCausalLM.from_pretrained(f"NumbersStation/nsql-2B")
|
| 11 |
+
|
| 12 |
+
# tokenizer350M = AutoTokenizer.from_pretrained(f"NumbersStation/nsql-2B")
|
| 13 |
+
# model350M = AutoModelForCausalLM.from_pretrained(f"NumbersStation/nsql-2B")
|
| 14 |
+
|
| 15 |
+
|
| 16 |
+
def load_model(model_name: str):
|
| 17 |
+
tokenizer = AutoTokenizer.from_pretrained(f"NumbersStation/{model_name}")
|
| 18 |
+
model = AutoModelForCausalLM.from_pretrained(f"NumbersStation/{model_name}")
|
| 19 |
+
return tokenizer, model
|
| 20 |
+
|
| 21 |
+
|
| 22 |
+
def infer(input_text, model_choice):
|
| 23 |
+
tokenizer, model = load_model(model_choice)
|
| 24 |
+
input_ids = tokenizer(input_text, return_tensors="pt").input_ids
|
| 25 |
+
generated_ids = model.generate(input_ids, max_length=500)
|
| 26 |
+
return (tokenizer.decode(generated_ids[0], skip_special_tokens=True))
|
| 27 |
+
|
| 28 |
+
|
| 29 |
+
iface = gr.Interface(
|
| 30 |
+
title="Text to SQL with NSQL",
|
| 31 |
+
description="""The NSQL model family was published by [Numbers Station](https://www.numbersstation.ai/) and is available in three flavors:
|
| 32 |
+
- [nsql-6B](https://huggingface.co/NumbersStation/nsql-6B)
|
| 33 |
+
- [nsql-2B](https://huggingface.co/NumbersStation/nsql-2B)
|
| 34 |
+
- [nsql-350M]((https://huggingface.co/NumbersStation/nsql-350M))
|
| 35 |
+
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.
|
| 36 |
+
|
| 37 |
+
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.
|
| 38 |
+
The model will then generate a SQL query that you can run against your database.
|
| 39 |
+
""",
|
| 40 |
+
fn=infer,
|
| 41 |
+
inputs=["text",
|
| 42 |
+
gr.Dropdown(["nsql-6B", "nsql-2B", "nsql-350M"], value="nsql-6B")],
|
| 43 |
+
outputs="text",
|
| 44 |
+
examples=[[example1, "nsql-6B"],
|
| 45 |
+
[example2, "nsql-2B"],
|
| 46 |
+
[example3, "nsql-350M"]])
|
| 47 |
+
iface.launch()
|
example_strings.py
ADDED
|
@@ -0,0 +1,68 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
example1 = """CREATE TABLE stadium (
|
| 2 |
+
stadium_id number,
|
| 3 |
+
location text,
|
| 4 |
+
name text,
|
| 5 |
+
capacity number,
|
| 6 |
+
highest number,
|
| 7 |
+
lowest number,
|
| 8 |
+
average number
|
| 9 |
+
)
|
| 10 |
+
|
| 11 |
+
CREATE TABLE singer (
|
| 12 |
+
singer_id number,
|
| 13 |
+
name text,
|
| 14 |
+
country text,
|
| 15 |
+
song_name text,
|
| 16 |
+
song_release_year text,
|
| 17 |
+
age number,
|
| 18 |
+
is_male others
|
| 19 |
+
)
|
| 20 |
+
|
| 21 |
+
CREATE TABLE concert (
|
| 22 |
+
concert_id number,
|
| 23 |
+
concert_name text,
|
| 24 |
+
theme text,
|
| 25 |
+
stadium_id text,
|
| 26 |
+
year text
|
| 27 |
+
)
|
| 28 |
+
|
| 29 |
+
CREATE TABLE singer_in_concert (
|
| 30 |
+
concert_id number,
|
| 31 |
+
singer_id text
|
| 32 |
+
)
|
| 33 |
+
|
| 34 |
+
-- Using valid SQLite, answer the following questions for the tables provided above.
|
| 35 |
+
|
| 36 |
+
-- What is the maximum, the average, and the minimum capacity of stadiums ?
|
| 37 |
+
|
| 38 |
+
SELECT"""
|
| 39 |
+
|
| 40 |
+
example2 = """CREATE TABLE stadium (
|
| 41 |
+
stadium_id number,
|
| 42 |
+
location text,
|
| 43 |
+
name text,
|
| 44 |
+
capacity number,
|
| 45 |
+
)
|
| 46 |
+
|
| 47 |
+
-- Using valid SQLite, answer the following questions for the tables provided above.
|
| 48 |
+
|
| 49 |
+
-- how many stadiums in total?
|
| 50 |
+
|
| 51 |
+
SELECT"""
|
| 52 |
+
|
| 53 |
+
example3 = """CREATE TABLE work_orders (
|
| 54 |
+
ID NUMBER,
|
| 55 |
+
CREATED_AT TEXT,
|
| 56 |
+
COST FLOAT,
|
| 57 |
+
INVOICE_AMOUNT FLOAT,
|
| 58 |
+
IS_DUE BOOLEAN,
|
| 59 |
+
IS_OPEN BOOLEAN,
|
| 60 |
+
IS_OVERDUE BOOLEAN,
|
| 61 |
+
COUNTRY_NAME TEXT,
|
| 62 |
+
)
|
| 63 |
+
|
| 64 |
+
-- Using valid SQLite, answer the following questions for the tables provided above.
|
| 65 |
+
|
| 66 |
+
-- how many work orders are open?
|
| 67 |
+
|
| 68 |
+
SELECT"""
|
requirements.txt
ADDED
|
@@ -0,0 +1 @@
|
|
|
|
|
|
|
| 1 |
+
transformers
|