Tonic commited on
Commit
8347fc4
1 Parent(s): 6eb1106

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +85 -0
app.py ADDED
@@ -0,0 +1,85 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import torch
2
+ from transformers import AutoTokenizer, AutoModelForCausalLM, pipeline
3
+ import gradio as gr
4
+ import spaces
5
+
6
+ title = """# 🙋🏻‍♂️Welcome to 🌟Tonic's Defog 🌬️🌁🌫️SqlCoder-34B-Alpha
7
+ You can use this Space to test out the current model [defog/sqlcoder-34b-alpha](https://huggingface.co/defog/sqlcoder-34b-alpha). [defog/sqlcoder-34b-alpha](https://huggingface.co/defog/sqlcoder-34b-alpha) is a 34B parameter model that outperforms gpt-4 and gpt-4-turbo for natural language to SQL generation tasks on our sql-eval framework, and significantly outperforms all popular open-source models. SQLCoder-34B is fine-tuned on a base CodeLlama model.
8
+ You can also use 👨🏻‍⚕️❤️‍🩹🧑🏻‍⚕️Meditron by cloning this space. 🧬🔬🔍 Simply click here: <a style="display:inline-block" href="https://huggingface.co/spaces/Tonic/Meditron70B-AWQ?duplicate=true"><img src="https://img.shields.io/badge/-Duplicate%20Space-blue?labelColor=white&style=flat&logo=data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABAAAAAQCAYAAAAf8/9hAAAAAXNSR0IArs4c6QAAAP5JREFUOE+lk7FqAkEURY+ltunEgFXS2sZGIbXfEPdLlnxJyDdYB62sbbUKpLbVNhyYFzbrrA74YJlh9r079973psed0cvUD4A+4HoCjsA85X0Dfn/RBLBgBDxnQPfAEJgBY+A9gALA4tcbamSzS4xq4FOQAJgCDwV2CPKV8tZAJcAjMMkUe1vX+U+SMhfAJEHasQIWmXNN3abzDwHUrgcRGmYcgKe0bxrblHEB4E/pndMazNpSZGcsZdBlYJcEL9Afo75molJyM2FxmPgmgPqlWNLGfwZGG6UiyEvLzHYDmoPkDDiNm9JR9uboiONcBXrpY1qmgs21x1QwyZcpvxt9NS09PlsPAAAAAElFTkSuQmCC&logoWidth=14" alt="Duplicate Space"></a></h3>
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
+ @spaces.GPU
13
+ class SQLQueryGenerator:
14
+ def __init__(self, model_name, prompt_file="prompt.md", metadata_file="metadata.sql"):
15
+ self.tokenizer, self.model = self.get_tokenizer_model(model_name)
16
+ self.prompt_file = prompt_file
17
+ self.metadata_file = metadata_file
18
+
19
+ def get_tokenizer_model(self, model_name):
20
+ tokenizer = AutoTokenizer.from_pretrained(model_name)
21
+ model = AutoModelForCausalLM.from_pretrained(
22
+ model_name,
23
+ trust_remote_code=True,
24
+ torch_dtype=torch.float16,
25
+ device_map="auto",
26
+ use_cache=True,
27
+ )
28
+ return tokenizer, model
29
+
30
+ def generate_prompt(self, question):
31
+ with open(self.prompt_file, "r") as f:
32
+ prompt = f.read()
33
+
34
+ with open(self.metadata_file, "r") as f:
35
+ table_metadata_string = f.read()
36
+
37
+ prompt = prompt.format(
38
+ user_question=question, table_metadata_string=table_metadata_string
39
+ )
40
+ return prompt
41
+
42
+ def run_inference(self, question):
43
+ prompt = self.generate_prompt(question)
44
+ eos_token_id = self.tokenizer.eos_token_id
45
+ pipe = pipeline(
46
+ "text-generation",
47
+ model=self.model,
48
+ tokenizer=self.tokenizer,
49
+ max_new_tokens=300,
50
+ do_sample=False,
51
+ num_beams=5,
52
+ )
53
+ generated_query = (
54
+ pipe(
55
+ prompt,
56
+ num_return_sequences=1,
57
+ eos_token_id=eos_token_id,
58
+ pad_token_id=eos_token_id,
59
+ )[0]["generated_text"]
60
+ .split("```sql")[-1]
61
+ .split("```")[0]
62
+ .split(";")[0]
63
+ .strip()
64
+ + ";"
65
+ )
66
+ return generated_query
67
+
68
+ def main():
69
+ model_name = "defog/sqlcoder-34b-alpha"
70
+ sql_query_generator = SQLQueryGenerator(model_name)
71
+
72
+ def generate_sql(question):
73
+ return sql_query_generator.run_inference(question)
74
+
75
+ with gr.Blocks() as demo:
76
+ gr.Markdown(title)
77
+ question = gr.Textbox(label="Enter your question")
78
+ submit = gr.Button("Generate SQL Query")
79
+ output = gr.Textbox(label="🌬️🌁🌫️SqlCoder-34B-alpha")
80
+ submit.click(fn=generate_sql, inputs=question, outputs=output)
81
+
82
+ demo.launch()
83
+
84
+ if __name__ == "__main__":
85
+ main()