Update app.py
Browse files
app.py
CHANGED
@@ -9,78 +9,67 @@ You can also use efog 🌬️🌁🌫️SqlCoder by cloning this space. 🧬🔬
|
|
9 |
Join us : 🌟TeamTonic🌟 is always making cool demos! Join our active builder's🛠️community 👻[![Let's build the future of AI together! 🚀🤖](https://discordapp.com/api/guilds/1109943800132010065/widget.png)](https://discord.gg/GWpVpekp) On 🤗Huggingface: [TeamTonic](https://huggingface.co/TeamTonic) & [MultiTransformer](https://huggingface.co/MultiTransformer) On 🌐Github: [Polytonic](https://github.com/tonic-ai) & contribute to 🌟 [Poly](https://github.com/tonic-ai/poly) 🤗Big thanks to Yuvi Sharma and all the folks at huggingface for the community grant 🤗
|
10 |
"""
|
11 |
|
12 |
-
|
13 |
-
|
14 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
15 |
|
16 |
-
|
17 |
-
|
18 |
-
|
19 |
-
model_name,
|
20 |
-
trust_remote_code=True,
|
21 |
-
torch_dtype=torch.float16,
|
22 |
-
device_map="auto",
|
23 |
-
use_cache=True,
|
24 |
-
)
|
25 |
-
return tokenizer, model
|
26 |
|
27 |
-
|
28 |
-
|
29 |
-
self.tokenizer_model = tokenizer_model
|
30 |
-
self.prompt_file = prompt_file
|
31 |
-
self.metadata_file = metadata_file
|
32 |
|
33 |
-
|
34 |
-
|
35 |
-
|
|
|
36 |
|
37 |
-
|
38 |
-
|
39 |
-
|
40 |
-
|
41 |
-
|
42 |
-
|
43 |
-
|
44 |
-
|
45 |
-
|
46 |
-
|
47 |
-
|
48 |
-
|
49 |
-
|
50 |
-
|
51 |
-
|
52 |
-
|
53 |
-
|
54 |
-
|
55 |
-
|
56 |
-
|
57 |
-
)
|
58 |
-
|
59 |
-
|
60 |
-
|
61 |
-
|
62 |
-
|
63 |
-
|
64 |
-
)[0]["generated_text"]
|
65 |
-
.split("```sql")[-1]
|
66 |
-
.split("```")[0]
|
67 |
-
.split(";")[0]
|
68 |
-
.strip()
|
69 |
-
+ ";"
|
70 |
-
)
|
71 |
-
return generated_query
|
72 |
|
73 |
def main():
|
74 |
model_name = "defog/sqlcoder2"
|
75 |
-
|
76 |
-
sql_query_generator = SQLQueryGenerator(tokenizer_model)
|
77 |
|
78 |
with gr.Blocks() as demo:
|
79 |
gr.Markdown(title)
|
80 |
question = gr.Textbox(label="Enter your question")
|
81 |
submit = gr.Button("Generate SQL Query")
|
82 |
output = gr.Textbox(label="🌬️🌁🌫️SqlCoder-2")
|
83 |
-
submit.click(fn=
|
84 |
|
85 |
demo.launch()
|
86 |
|
|
|
9 |
Join us : 🌟TeamTonic🌟 is always making cool demos! Join our active builder's🛠️community 👻[![Let's build the future of AI together! 🚀🤖](https://discordapp.com/api/guilds/1109943800132010065/widget.png)](https://discord.gg/GWpVpekp) On 🤗Huggingface: [TeamTonic](https://huggingface.co/TeamTonic) & [MultiTransformer](https://huggingface.co/MultiTransformer) On 🌐Github: [Polytonic](https://github.com/tonic-ai) & contribute to 🌟 [Poly](https://github.com/tonic-ai/poly) 🤗Big thanks to Yuvi Sharma and all the folks at huggingface for the community grant 🤗
|
10 |
"""
|
11 |
|
12 |
+
def load_tokenizer_model(model_name):
|
13 |
+
tokenizer = AutoTokenizer.from_pretrained(model_name)
|
14 |
+
model = AutoModelForCausalLM.from_pretrained(
|
15 |
+
model_name,
|
16 |
+
trust_remote_code=True,
|
17 |
+
torch_dtype=torch.float16,
|
18 |
+
device_map="auto",
|
19 |
+
use_cache=True,
|
20 |
+
)
|
21 |
+
return tokenizer, model
|
22 |
|
23 |
+
def generate_prompt(question, prompt_file="prompt.md", metadata_file="metadata.sql"):
|
24 |
+
with open(prompt_file, "r") as f:
|
25 |
+
prompt = f.read()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
26 |
|
27 |
+
with open(metadata_file, "r") as f:
|
28 |
+
table_metadata_string = f.read()
|
|
|
|
|
|
|
29 |
|
30 |
+
prompt = prompt.format(
|
31 |
+
user_question=question, table_metadata_string=table_metadata_string
|
32 |
+
)
|
33 |
+
return prompt
|
34 |
|
35 |
+
@spaces.GPU
|
36 |
+
def run_inference(question, model, tokenizer):
|
37 |
+
model.to('cuda')
|
38 |
+
prompt = generate_prompt(question)
|
39 |
+
eos_token_id = tokenizer.eos_token_id
|
40 |
+
pipe = pipeline(
|
41 |
+
"text-generation",
|
42 |
+
model=model,
|
43 |
+
tokenizer=tokenizer,
|
44 |
+
max_new_tokens=300,
|
45 |
+
do_sample=False,
|
46 |
+
num_beams=5,
|
47 |
+
)
|
48 |
+
generated_query = (
|
49 |
+
pipe(
|
50 |
+
prompt,
|
51 |
+
num_return_sequences=1,
|
52 |
+
eos_token_id=eos_token_id,
|
53 |
+
pad_token_id=eos_token_id,
|
54 |
+
)[0]["generated_text"]
|
55 |
+
.split("```sql")[-1]
|
56 |
+
.split("```")[0]
|
57 |
+
.split(";")[0]
|
58 |
+
.strip()
|
59 |
+
+ ";"
|
60 |
+
)
|
61 |
+
return generated_query
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
62 |
|
63 |
def main():
|
64 |
model_name = "defog/sqlcoder2"
|
65 |
+
tokenizer, model = load_tokenizer_model(model_name)
|
|
|
66 |
|
67 |
with gr.Blocks() as demo:
|
68 |
gr.Markdown(title)
|
69 |
question = gr.Textbox(label="Enter your question")
|
70 |
submit = gr.Button("Generate SQL Query")
|
71 |
output = gr.Textbox(label="🌬️🌁🌫️SqlCoder-2")
|
72 |
+
submit.click(fn=lambda x: run_inference(x, model, tokenizer), inputs=question, outputs=output)
|
73 |
|
74 |
demo.launch()
|
75 |
|