Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,109 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import torch
|
2 |
+
from transformers import AutoTokenizer, AutoModelForCausalLM, pipeline
|
3 |
+
import gradio as gr
|
4 |
+
|
5 |
+
def generate_prompt(question, prompt_file="prompt.md", metadata_file="metadata.sql"):
|
6 |
+
"""
|
7 |
+
Generates the prompt by reading the prompt template and table metadata,
|
8 |
+
then formatting them with the user's question.
|
9 |
+
"""
|
10 |
+
try:
|
11 |
+
with open(prompt_file, "r") as f:
|
12 |
+
prompt = f.read()
|
13 |
+
except FileNotFoundError:
|
14 |
+
return "Error: prompt.md file not found."
|
15 |
+
|
16 |
+
try:
|
17 |
+
with open(metadata_file, "r") as f:
|
18 |
+
table_metadata_string = f.read()
|
19 |
+
except FileNotFoundError:
|
20 |
+
return "Error: metadata.sql file not found."
|
21 |
+
|
22 |
+
prompt = prompt.format(
|
23 |
+
user_question=question, table_metadata_string=table_metadata_string
|
24 |
+
)
|
25 |
+
return prompt
|
26 |
+
|
27 |
+
def get_tokenizer_model(model_name):
|
28 |
+
"""
|
29 |
+
Loads the tokenizer and model from the specified model repository.
|
30 |
+
"""
|
31 |
+
tokenizer = AutoTokenizer.from_pretrained(model_name)
|
32 |
+
model = AutoModelForCausalLM.from_pretrained(
|
33 |
+
model_name,
|
34 |
+
trust_remote_code=True, # Set to True if the model uses custom code
|
35 |
+
torch_dtype=torch.float16,
|
36 |
+
device_map="auto", # Automatically maps the model to available devices
|
37 |
+
use_cache=True,
|
38 |
+
)
|
39 |
+
return tokenizer, model
|
40 |
+
|
41 |
+
# Load the tokenizer and model once when the script starts
|
42 |
+
model_name = "defog/sqlcoder-7b-2" # Replace with your model name
|
43 |
+
print("Loading model and tokenizer...")
|
44 |
+
tokenizer, model = get_tokenizer_model(model_name)
|
45 |
+
print("Model and tokenizer loaded successfully.")
|
46 |
+
|
47 |
+
# Initialize the text generation pipeline
|
48 |
+
text_gen_pipeline = pipeline(
|
49 |
+
"text-generation",
|
50 |
+
model=model,
|
51 |
+
tokenizer=tokenizer,
|
52 |
+
max_new_tokens=300,
|
53 |
+
do_sample=False, # Disable sampling for deterministic output
|
54 |
+
return_full_text=False,
|
55 |
+
num_beams=5, # Use beam search for better quality
|
56 |
+
)
|
57 |
+
|
58 |
+
def run_inference_gradio(question, prompt_file="prompt.md", metadata_file="metadata.sql"):
|
59 |
+
"""
|
60 |
+
Generates an SQL query based on the user's natural language question.
|
61 |
+
"""
|
62 |
+
if not question.strip():
|
63 |
+
return "Please enter a valid question."
|
64 |
+
|
65 |
+
prompt = generate_prompt(question, prompt_file, metadata_file)
|
66 |
+
|
67 |
+
if prompt.startswith("Error:"):
|
68 |
+
return prompt # Return the error message if files are missing
|
69 |
+
|
70 |
+
eos_token_id = tokenizer.eos_token_id
|
71 |
+
try:
|
72 |
+
generated = text_gen_pipeline(
|
73 |
+
prompt,
|
74 |
+
num_return_sequences=1,
|
75 |
+
eos_token_id=eos_token_id,
|
76 |
+
pad_token_id=eos_token_id,
|
77 |
+
)
|
78 |
+
except Exception as e:
|
79 |
+
return f"Error during model inference: {str(e)}"
|
80 |
+
|
81 |
+
generated_text = generated[0]["generated_text"]
|
82 |
+
|
83 |
+
# Extract the SQL query from the generated text
|
84 |
+
sql_query = generated_text.split(";")[0].split("```")[0].strip() + ";"
|
85 |
+
return sql_query
|
86 |
+
|
87 |
+
# Define the Gradio interface
|
88 |
+
iface = gr.Interface(
|
89 |
+
fn=run_inference_gradio,
|
90 |
+
inputs=gr.Textbox(
|
91 |
+
lines=4,
|
92 |
+
placeholder="Enter your natural language question here...",
|
93 |
+
label="Question"
|
94 |
+
),
|
95 |
+
outputs=gr.Textbox(label="Generated SQL Query"),
|
96 |
+
title="Text-to-SQL Generator",
|
97 |
+
description=(
|
98 |
+
"Enter a natural language question related to your database, and this tool "
|
99 |
+
"will generate the corresponding SQL query. Ensure that 'prompt.md' and "
|
100 |
+
"'metadata.sql' are correctly set up in the application directory."
|
101 |
+
),
|
102 |
+
examples=[
|
103 |
+
["Do we get more sales from customers in New York compared to customers in San Francisco? Give me the total sales for each city, and the difference between the two."]
|
104 |
+
],
|
105 |
+
allow_flagging="never"
|
106 |
+
)
|
107 |
+
|
108 |
+
if __name__ == "__main__":
|
109 |
+
iface.launch()
|