Spaces:
Build error
Build error
Delete Final_layout.py
Browse files- Final_layout.py +0 -137
Final_layout.py
DELETED
|
@@ -1,137 +0,0 @@
|
|
| 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 |
-
|
| 97 |
-
Example : Start
|
| 98 |
-
1. Start by selecting distinct customer details and product application details.
|
| 99 |
-
2. Join the customers table with the life events table on customer id.
|
| 100 |
-
3. Join the resulting set with the products table on customer_id.
|
| 101 |
-
4. Filter for customers who are household heads.
|
| 102 |
-
5. Filter for customers located in California.
|
| 103 |
-
6. Filter for life events that include 'relocation'.
|
| 104 |
-
7. Filter for life events that occurred in the last 6 months.
|
| 105 |
-
8. Filter for product applications that include 'insurance'.
|
| 106 |
-
9. Filter for product applications that occurred in the last 6 months.
|
| 107 |
-
\n
|
| 108 |
-
End
|
| 109 |
-
|
| 110 |
-
Query: {query}"""
|
| 111 |
-
|
| 112 |
-
second_input_prompt = PromptTemplate(
|
| 113 |
-
input_variables = ['query'],
|
| 114 |
-
template = a_template
|
| 115 |
-
)
|
| 116 |
-
|
| 117 |
-
#algorithm chain
|
| 118 |
-
chain2 = LLMChain(llm = llm, prompt = second_input_prompt, verbose=True, output_key='algorithm')
|
| 119 |
-
|
| 120 |
-
if prompt_submit_button:
|
| 121 |
-
with st.spinner('Generating response...'):
|
| 122 |
-
qu = chain1({"schema": schema_input, "user_prompt":user_prompt}, return_only_outputs=True)
|
| 123 |
-
#print(type(qu))
|
| 124 |
-
col2.subheader("Query")
|
| 125 |
-
col2.code(qu["query"], language="sql")
|
| 126 |
-
#st.write(qu["query"])
|
| 127 |
-
col2.markdown("---")
|
| 128 |
-
col2.subheader("Algorithm")
|
| 129 |
-
algo = chain2({"query": qu}, return_only_outputs=True)
|
| 130 |
-
col2.write(algo["algorithm"])
|
| 131 |
-
|
| 132 |
-
|
| 133 |
-
|
| 134 |
-
|
| 135 |
-
|
| 136 |
-
|
| 137 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|