Arnav-J commited on
Commit
5c7d5a6
·
verified ·
1 Parent(s): a331161

Created app.py

Browse files
Files changed (1) hide show
  1. app.py +128 -0
app.py ADDED
@@ -0,0 +1,128 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from types import ClassMethodDescriptorType
2
+ from click import prompt
3
+ from dotenv import load_dotenv
4
+ from langchain.llms import OpenAI
5
+ from langchain import PromptTemplate
6
+ from langchain.chains import LLMChain
7
+ import streamlit as st
8
+ from streamlit_extras.let_it_rain import rain
9
+ import os
10
+ import time
11
+ import random
12
+
13
+ #setting up the prerequites
14
+
15
+ # load_dotenv()
16
+ # os.environ["OPENAI_API_KEY"] = os.getenv('openai_key')
17
+ llm = OpenAI(temperature = 0.8)
18
+ #llm2 = OpenAI(model_name = "gpt-4-turbo-preview", temperature = 0.7)
19
+
20
+ st.set_page_config(page_title="Schem-SQL-Query",
21
+ page_icon="🧊",
22
+ layout="wide",
23
+ initial_sidebar_state="collapsed")
24
+
25
+ rain(
26
+ emoji="❄️",
27
+ font_size=20,
28
+ falling_speed=10,
29
+ animation_length="infinite",
30
+ )
31
+
32
+ with open( "style.css" ) as css:
33
+ st.markdown( f'<style>{css.read()}</style>' , unsafe_allow_html= True)
34
+
35
+ st.markdown("<h1 style='text-align: center; color: white; text-decoration: underline; font-family: 'Times New Roman';'>SQL Generator</h1>", unsafe_allow_html=True)
36
+ col1 , col2 = st.columns([0.4,0.6], gap="large")
37
+
38
+ col1.header("Database Schema Input")
39
+ schema_input = col1.text_area("Submit Database Schema", height=450)
40
+ schema_submit_button = col1.button("Submit Schema", key="schema_submit")
41
+
42
+ col2.header("Enter your question")
43
+ user_prompt = col2.text_area("User's Prompt", height=150)
44
+ prompt_submit_button = col2.button("Submit Prompt", key="prompt_submit")
45
+
46
+ col2.markdown("---")
47
+
48
+ #query prompt
49
+ q_template = """You are an expert in writing SQL queries thus, based on the table schema below,
50
+ write a SQL query that would answer the user's question:
51
+ {schema} \n Question: {user_prompt} \nSQL Query:"""
52
+
53
+ ## Prompt Templates
54
+ first_input_prompt = PromptTemplate(
55
+ input_variables = ['schema','user_prompt'],
56
+ template = q_template
57
+ )
58
+
59
+ #query chain
60
+ chain1 = LLMChain(llm = llm, prompt = first_input_prompt, verbose=True, output_key='query')
61
+ ########################
62
+
63
+ #scenario prompt
64
+ s_template = """Based on the following schema, generate 9 sql questions that
65
+ are relevant to the schema. The questions should be in increasing order of difficulty and complexity.
66
+ Include atleast one problem which involves "join" operation.
67
+ Just describe the problem statement in one line. Schema: {schema} """
68
+
69
+ third_input_prompt = PromptTemplate(
70
+ input_variables = ['schema'],
71
+ template = s_template
72
+ )
73
+
74
+ #scenario chain
75
+ chain3 = LLMChain(llm = llm, prompt = third_input_prompt, verbose=True, output_key='scenario')
76
+
77
+ if schema_submit_button:
78
+ with col1:
79
+ with st.spinner('Wait for it...'):
80
+ time.sleep(random.random() * 3)
81
+ col1.success("Schema Submitted Successfully!!")
82
+
83
+
84
+ expander = col1.expander("See Scenarios")
85
+ scenarios = chain3({"schema": schema_input}, return_only_outputs=True)
86
+ expander.write(scenarios["scenario"])
87
+
88
+
89
+ ############################
90
+
91
+ #algorithm prompt
92
+ a_template = """You are an expert in explaining SQL queries thus
93
+ Explain the following SQL Query in an Algorithmic step-by-step fashion. Write "Start" and "End" to denote the begining
94
+ and ending of the Algorithm. Write at least 4 lines and a maximum of 12 lines to explain.
95
+ Explain each step briefly, in a single line. Refer the below example.
96
+ Example : Start
97
+ 1. Start by selecting distinct customer details and product application details.
98
+ 2. Join the customers table with the life events table on customer id.
99
+ 3. Join the resulting set with the products table on customer_id.
100
+ 4. Filter for customers who are household heads.
101
+ 5. Filter for customers located in California.
102
+ 6. Filter for life events that include 'relocation'.
103
+ 7. Filter for life events that occurred in the last 6 months.
104
+ 8. Filter for product applications that include 'insurance'.
105
+ 9. Filter for product applications that occurred in the last 6 months.
106
+ \n
107
+ End
108
+ Query: {query}"""
109
+
110
+ second_input_prompt = PromptTemplate(
111
+ input_variables = ['query'],
112
+ template = a_template
113
+ )
114
+
115
+ #algorithm chain
116
+ chain2 = LLMChain(llm = llm, prompt = second_input_prompt, verbose=True, output_key='algorithm')
117
+
118
+ if prompt_submit_button:
119
+ with st.spinner('Generating response...'):
120
+ qu = chain1({"schema": schema_input, "user_prompt":user_prompt}, return_only_outputs=True)
121
+ #print(type(qu))
122
+ col2.subheader("Query")
123
+ col2.code(qu["query"], language="sql")
124
+ #st.write(qu["query"])
125
+ col2.markdown("---")
126
+ col2.subheader("Algorithm")
127
+ algo = chain2({"query": qu}, return_only_outputs=True)
128
+ col2.write(algo["algorithm"])