import spaces import torch from transformers import AutoTokenizer, AutoModelForCausalLM, pipeline import gradio as gr title = """# 🙋🏻‍♂️Welcome to 🌟Tonic's Defog 🌬️🌁🌫️SqlCoder-2 You can use this Space to test out the current model [defog/sqlcoder2](https://huggingface.co/defog/sqlcoder2). [defog/sqlcoder2](https://huggingface.co/defog/sqlcoder2) is a 15B parameter model that doesn't outperform 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. You can also use efog 🌬️🌁🌫️SqlCoder by cloning this space. 🧬🔬🔍 Simply click here: Duplicate Space 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 🤗 """ global_tokenizer, global_model = None, None def load_tokenizer_model(model_name): global global_tokenizer, global_model global_tokenizer = AutoTokenizer.from_pretrained(model_name) global_model = AutoModelForCausalLM.from_pretrained( model_name, trust_remote_code=True, torch_dtype=torch.float16, device_map="auto", use_cache=True, ) def generate_prompt(question, prompt_file="prompt.md", metadata_file="metadata.sql"): with open(prompt_file, "r") as f: prompt = f.read() with open(metadata_file, "r") as f: table_metadata_string = f.read() prompt = prompt.format( user_question=question, table_metadata_string=table_metadata_string ) return prompt @spaces.GPU def run_inference(question): global global_tokenizer, global_model global_model.to('cuda') prompt = generate_prompt(question) eos_token_id = global_tokenizer.eos_token_id pipe = pipeline( "text-generation", model=global_model, tokenizer=global_tokenizer, max_new_tokens=300, do_sample=False, num_beams=5, ) generated_query = ( pipe( prompt, num_return_sequences=1, eos_token_id=eos_token_id, pad_token_id=eos_token_id, )[0]["generated_text"] .split("```sql")[-1] .split("```")[0] .split(";")[0] .strip() + ";" ) return generated_query def main(): model_name = "defog/sqlcoder2" load_tokenizer_model(model_name) with gr.Blocks() as demo: gr.Markdown(title) question = gr.Textbox(label="Enter your question") submit = gr.Button("Generate SQL Query") output = gr.Textbox(label="🌬️🌁🌫️SqlCoder-2") submit.click(fn=run_inference, inputs=question, outputs=output) demo.launch() if __name__ == "__main__": main()