GlastonR commited on
Commit
8fc9239
1 Parent(s): 3bb06e0

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +40 -36
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
- # Generate SQL using the model
19
- output = model.generate(input_tensor, max_length=max_length, num_return_sequences=1, pad_token_id=tokenizer.eos_token_id)
 
 
 
 
 
 
 
 
 
 
 
20
 
21
- # Decode the output
22
- decoded_output = tokenizer.decode(output[0], skip_special_tokens=True)
 
 
 
 
 
 
 
 
23
 
24
  # Extract and return the SQL query
25
- sql_output = decoded_output[len(prompt):].strip()
26
- return sql_output
27
 
28
- # Main Streamlit app
29
  def main():
30
  # Display title and instructions
31
- st.title("Text-to-SQL with GPT-2")
32
- st.write("Enter an English query, and get the corresponding SQL query.")
33
-
34
- # Input box for English query
35
- query = st.text_input("Enter your question:")
36
-
37
- if query:
38
- # Load the model and tokenizer
39
- model, tokenizer = load_model()
40
-
41
- # Generate SQL query
42
- sql_query = generate_text_to_sql(query, model, tokenizer)
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
+