Create README.md
Browse files
README.md
ADDED
|
@@ -0,0 +1,49 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
---
|
| 2 |
+
language: en
|
| 3 |
+
widget:
|
| 4 |
+
- text: >-
|
| 5 |
+
convert question and table into SQL query. tables: people_name(id,name),
|
| 6 |
+
people_age(people_id,age). question: how many people with name jui and age
|
| 7 |
+
less than 25
|
| 8 |
+
license: cc-by-sa-4.0
|
| 9 |
+
---
|
| 10 |
+
|
| 11 |
+
This is an upgraded version of [https://huggingface.co/juierror/flan-t5-text2sql-with-schema](https://huggingface.co/juierror/flan-t5-text2sql-with-schema).
|
| 12 |
+
|
| 13 |
+
It supports the '<' sign and can handle multiple tables.
|
| 14 |
+
|
| 15 |
+
# How to use
|
| 16 |
+
```python
|
| 17 |
+
from typing import List
|
| 18 |
+
from transformers import AutoTokenizer, AutoModelForSeq2SeqLM
|
| 19 |
+
|
| 20 |
+
tokenizer = AutoTokenizer.from_pretrained("juierror/flan-t5-text2sql-with-schema-v2")
|
| 21 |
+
model = AutoModelForSeq2SeqLM.from_pretrained("juierror/flan-t5-text2sql-with-schema-v2")
|
| 22 |
+
|
| 23 |
+
def get_prompt(tables, question):
|
| 24 |
+
prompt = f"""convert question and table into SQL query. tables: {tables}. question: {question}"""
|
| 25 |
+
return prompt
|
| 26 |
+
|
| 27 |
+
def prepare_input(question: str, tables: Dict[str, List[str]]):
|
| 28 |
+
tables = [f"""{table_name}({",".join(tables[table_name])})""" for table_name in tables]
|
| 29 |
+
tables = ", ".join(tables)
|
| 30 |
+
prompt = get_prompt(tables, question)
|
| 31 |
+
input_ids = tokenizer(prompt, max_length=512, return_tensors="pt").input_ids
|
| 32 |
+
return input_ids
|
| 33 |
+
|
| 34 |
+
def inference(question: str, tables: Dict[str, List[str]]) -> str:
|
| 35 |
+
input_data = prepare_input(question=question, tables=tables)
|
| 36 |
+
input_data = input_data.to(model.device)
|
| 37 |
+
outputs = model.generate(inputs=input_data, num_beams=10, top_k=10, max_length=512)
|
| 38 |
+
result = tokenizer.decode(token_ids=outputs[0], skip_special_tokens=True)
|
| 39 |
+
return result
|
| 40 |
+
|
| 41 |
+
print(inference("how many people with name jui and age less than 25", {
|
| 42 |
+
"people_name": ["id", "name"],
|
| 43 |
+
"people_age": ["people_id", "age"]
|
| 44 |
+
}))
|
| 45 |
+
|
| 46 |
+
print(inference("what is id with name jui and age less than 25", {
|
| 47 |
+
"people_name": ["id", "name", "age"]
|
| 48 |
+
})))
|
| 49 |
+
```
|