-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtext2sql.py
60 lines (47 loc) · 1.6 KB
/
text2sql.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
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
import os
import torch
from datasets import load_dataset
from transformers import (
AutoModelForCausalLM,
AutoTokenizer,
BitsAndBytesConfig,
HfArgumentParser,
TrainingArguments,
pipeline,
logging,
)
import gradio as gr
# Load the entire model on the GPU 0
device_map = {"": 0} # use -1 for cpu
# model_name = "NousResearch/Llama-2-7b-chat-hf" # base model
model_name = "Rakshitgarg99/Llama-2-7b-text2sql-finetune"
new_model = AutoModelForCausalLM.from_pretrained(
model_name,
low_cpu_mem_usage=True,
return_dict=True,
torch_dtype=torch.float16,
device_map=device_map,
)
# Reload tokenizer to save it
tokenizer = AutoTokenizer.from_pretrained(model_name, trust_remote_code=True)
tokenizer.pad_token = tokenizer.eos_token
tokenizer.padding_side = "right"
# Define the function that generates the blog response
def generate_blog(input_text):
pipe = pipeline(task="text-generation", model=new_model, tokenizer=tokenizer, max_length=500)
result = pipe(f"<s>[INST] {input_text} [/INST]")
# print(result[0]['generated_text'])
return result[0]['generated_text'].split("[/INST]")[1].strip()
# Create inputs using Gradio components
input_text = gr.Textbox(label="Enter the SQL question info")
# Create the Gradio interface
iface = gr.Interface(
fn=generate_blog,
inputs=[input_text],
outputs="text",
title="Generate Text TO SQL 🤖",
description="Generate SQL queries based on the Textual Information.",
theme="compact",
)
# Launch the Gradio interface in the notebook
iface.launch()