Update app.py
Browse files
app.py
CHANGED
@@ -22,19 +22,44 @@ tokenizer = AutoTokenizer.from_pretrained(peft_model_id)
|
|
22 |
pipe = pipeline("text-generation", model=model, tokenizer=tokenizer)
|
23 |
|
24 |
|
25 |
-
|
26 |
-
def
|
27 |
-
|
28 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
29 |
prompt = pipe.tokenizer.apply_chat_template(combined_json_data, tokenize=False, add_generation_prompt=True)
|
30 |
outputs = pipe(prompt, max_new_tokens=256, do_sample=False, temperature=0.1, top_k=50, top_p=0.1, eos_token_id=pipe.tokenizer.eos_token_id, pad_token_id=pipe.tokenizer.pad_token_id)
|
31 |
-
|
32 |
-
|
33 |
-
question_input = gr.inputs.Textbox(lines=7, label="Enter your question")
|
34 |
-
output_text = gr.outputs.Textbox(label="Generated SQL Query")
|
35 |
|
36 |
-
|
37 |
-
gr.Interface(fn=translate_to_sql, inputs=question_input, outputs=output_text, title="Text to SQL Translator", description="Translate English questions to SQL queries.").launch()
|
38 |
|
39 |
-
# Create
|
40 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
22 |
pipe = pipeline("text-generation", model=model, tokenizer=tokenizer)
|
23 |
|
24 |
|
25 |
+
|
26 |
+
def text_to_sql(text):
|
27 |
+
# Load Model with PEFT adapter
|
28 |
+
model = AutoModelForCausalLM.from_pretrained(
|
29 |
+
"jinhybr/code-llama-7b-text-to-sql",
|
30 |
+
device="cuda" if torch.cuda.is_available() else "cpu",
|
31 |
+
torch_dtype=torch.float16
|
32 |
+
)
|
33 |
+
tokenizer = AutoTokenizer.from_pretrained("jinhybr/code-llama-7b-text-to-sql")
|
34 |
+
|
35 |
+
# load into pipeline
|
36 |
+
pipe = pipeline("text-generation", model=model, tokenizer=tokenizer)
|
37 |
+
|
38 |
+
# Define schema and user question
|
39 |
+
#schema = "CREATE TABLE table_17429402_7 (school VARCHAR, last_occ_championship VARCHAR)"
|
40 |
+
schema = 'You are an text to SQL query translator. Users will ask you questions in English and you will generate a SQL query based on the provided SCHEMA.\nSCHEMA:\nCREATE TABLE table_17429402_7 (school VARCHAR, last_occ_championship VARCHAR)'
|
41 |
+
user_question = text
|
42 |
+
#user_question = 'How many schools won their last occ championship in 2006?'
|
43 |
+
|
44 |
+
# Combine schema and user question
|
45 |
+
combined_json_data = [
|
46 |
+
{'content': schema, 'role': 'system'},
|
47 |
+
{'content': user_question, 'role': 'user'}
|
48 |
+
]
|
49 |
+
|
50 |
+
# Generate SQL query
|
51 |
prompt = pipe.tokenizer.apply_chat_template(combined_json_data, tokenize=False, add_generation_prompt=True)
|
52 |
outputs = pipe(prompt, max_new_tokens=256, do_sample=False, temperature=0.1, top_k=50, top_p=0.1, eos_token_id=pipe.tokenizer.eos_token_id, pad_token_id=pipe.tokenizer.pad_token_id)
|
53 |
+
sql_query = outputs[0]['generated_text'][len(prompt):].strip()
|
|
|
|
|
|
|
54 |
|
55 |
+
return sql_query
|
|
|
56 |
|
57 |
+
# Create Gradio Interface
|
58 |
+
iface = gr.Interface(
|
59 |
+
fn=text_to_sql,
|
60 |
+
inputs=gr.inputs.Textbox(lines=7, label="User Question"),
|
61 |
+
outputs=gr.outputs.Textbox(label="SQL Query"),
|
62 |
+
title="Text to SQL Translator",
|
63 |
+
description="Translate text to SQL query based on the provided schema."
|
64 |
+
)
|
65 |
+
iface.launch()
|