Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
@@ -1,49 +1,53 @@
|
|
|
|
1 |
import streamlit as st
|
2 |
-
from transformers import GPT2LMHeadModel, GPT2Tokenizer
|
3 |
-
|
4 |
-
# Load the pre-trained model and tokenizer
|
5 |
-
@st.cache_resource
|
6 |
-
def load_model():
|
7 |
-
model = GPT2LMHeadModel.from_pretrained("rakeshkiriyath/gpt2Medium_text_to_sql")
|
8 |
-
tokenizer = GPT2Tokenizer.from_pretrained("rakeshkiriyath/gpt2Medium_text_to_sql")
|
9 |
-
return model, tokenizer
|
10 |
-
|
11 |
-
# Function to generate SQL from English text
|
12 |
-
def generate_text_to_sql(query, model, tokenizer, max_length=256):
|
13 |
-
prompt = f"Translate the following English question to SQL: {query}"
|
14 |
-
|
15 |
-
# Tokenize the input text
|
16 |
-
input_tensor = tokenizer.encode(prompt, return_tensors='pt').to('cpu') # Use CPU if no GPU
|
17 |
|
18 |
-
|
19 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
20 |
|
21 |
-
|
22 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
23 |
|
24 |
# Extract and return the SQL query
|
25 |
-
|
26 |
-
return
|
27 |
|
28 |
-
#
|
29 |
def main():
|
30 |
# Display title and instructions
|
31 |
-
st.title("Text-to-SQL with GPT-
|
32 |
-
st.write("Enter
|
33 |
-
|
34 |
-
# Input
|
35 |
-
|
36 |
-
|
37 |
-
|
38 |
-
|
39 |
-
|
40 |
-
|
41 |
-
|
42 |
-
|
43 |
-
|
44 |
-
# Display the SQL query
|
45 |
st.write("Generated SQL Query:")
|
46 |
st.code(sql_query)
|
47 |
|
48 |
if __name__ == "__main__":
|
49 |
main()
|
|
|
|
1 |
+
import openai
|
2 |
import streamlit as st
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
3 |
|
4 |
+
# Set up OpenAI API key
|
5 |
+
openai.api_key = "YOUR_API_KEY" # Replace with your OpenAI API key
|
6 |
+
|
7 |
+
# Function to generate SQL query using GPT-3
|
8 |
+
def generate_sql_query(query, schema):
|
9 |
+
prompt = f"""
|
10 |
+
Given the following database schema:
|
11 |
+
|
12 |
+
{schema}
|
13 |
+
|
14 |
+
Translate the following question into an SQL query:
|
15 |
+
|
16 |
+
Question: {query}
|
17 |
|
18 |
+
SQL Query:
|
19 |
+
"""
|
20 |
+
|
21 |
+
# Request GPT-3 to generate the SQL query
|
22 |
+
response = openai.Completion.create(
|
23 |
+
engine="text-davinci-003", # Use gpt-3.5-turbo for faster and cheaper results
|
24 |
+
prompt=prompt,
|
25 |
+
max_tokens=150,
|
26 |
+
temperature=0.0, # Lower temperature for deterministic results
|
27 |
+
)
|
28 |
|
29 |
# Extract and return the SQL query
|
30 |
+
sql_query = response.choices[0].text.strip()
|
31 |
+
return sql_query
|
32 |
|
33 |
+
# Streamlit interface
|
34 |
def main():
|
35 |
# Display title and instructions
|
36 |
+
st.title("Text-to-SQL with GPT-3")
|
37 |
+
st.write("Enter a database schema and a natural language query to generate the corresponding SQL query.")
|
38 |
+
|
39 |
+
# Input boxes for schema and query
|
40 |
+
schema_input = st.text_area("Enter the Database Schema:", height=200)
|
41 |
+
query_input = st.text_input("Enter your Question:")
|
42 |
+
|
43 |
+
if schema_input and query_input:
|
44 |
+
# Generate the SQL query
|
45 |
+
sql_query = generate_sql_query(query_input, schema_input)
|
46 |
+
|
47 |
+
# Display the generated SQL query
|
|
|
|
|
48 |
st.write("Generated SQL Query:")
|
49 |
st.code(sql_query)
|
50 |
|
51 |
if __name__ == "__main__":
|
52 |
main()
|
53 |
+
|