Spaces:
Build error
Build error
First Commit
Browse files- Final_layout.py +137 -0
- requirements.txt +19 -0
- style.css +15 -0
Final_layout.py
ADDED
|
@@ -0,0 +1,137 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 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="Test",
|
| 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 |
+
|
requirements.txt
ADDED
|
@@ -0,0 +1,19 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
openai
|
| 2 |
+
langchain
|
| 3 |
+
streamlit
|
| 4 |
+
PyPDF2
|
| 5 |
+
faiss-cpu
|
| 6 |
+
tiktoken
|
| 7 |
+
unstructured
|
| 8 |
+
chromadb
|
| 9 |
+
huggingface_hub
|
| 10 |
+
google-generativeai
|
| 11 |
+
python-dotenv
|
| 12 |
+
langchain_google_genai
|
| 13 |
+
streamlit-extras
|
| 14 |
+
youtube_transcript_api
|
| 15 |
+
pathlib
|
| 16 |
+
langchain-experimental
|
| 17 |
+
sqlalchemy
|
| 18 |
+
numpy
|
| 19 |
+
opencv
|
style.css
ADDED
|
@@ -0,0 +1,15 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
/* customizable snowflake styling */
|
| 2 |
+
.snowflake {
|
| 3 |
+
color: #FFF;
|
| 4 |
+
font-size: 1em;
|
| 5 |
+
font-family: Arial;
|
| 6 |
+
text-shadow: 0 0 1px #000;
|
| 7 |
+
}
|
| 8 |
+
|
| 9 |
+
@-webkit-keyframes snowflakes-fall{0%{top:-10%}100%{top:100%}}@-webkit-keyframes snowflakes-shake{0%{-webkit-transform:translateX(0px);transform:translateX(0px)}50%{-webkit-transform:translateX(80px);transform:translateX(80px)}100%{-webkit-transform:translateX(0px);transform:translateX(0px)}}@keyframes snowflakes-fall{0%{top:-10%}100%{top:100%}}@keyframes snowflakes-shake{0%{transform:translateX(0px)}50%{transform:translateX(80px)}100%{transform:translateX(0px)}}.snowflake{position:fixed;top:-10%;z-index:9999;-webkit-user-select:none;-moz-user-select:none;-ms-user-select:none;user-select:none;cursor:default;-webkit-animation-name:snowflakes-fall,snowflakes-shake;-webkit-animation-duration:10s,3s;-webkit-animation-timing-function:linear,ease-in-out;-webkit-animation-iteration-count:infinite,infinite;-webkit-animation-play-state:running,running;animation-name:snowflakes-fall,snowflakes-shake;animation-duration:10s,3s;animation-timing-function:linear,ease-in-out;animation-iteration-count:infinite,infinite;animation-play-state:running,running}.snowflake:nth-of-type(0){left:1%;-webkit-animation-delay:0s,0s;animation-delay:0s,0s}.snowflake:nth-of-type(1){left:10%;-webkit-animation-delay:1s,1s;animation-delay:1s,1s}.snowflake:nth-of-type(2){left:20%;-webkit-animation-delay:6s,.5s;animation-delay:6s,.5s}.snowflake:nth-of-type(3){left:30%;-webkit-animation-delay:4s,2s;animation-delay:4s,2s}.snowflake:nth-of-type(4){left:40%;-webkit-animation-delay:2s,2s;animation-delay:2s,2s}.snowflake:nth-of-type(5){left:50%;-webkit-animation-delay:8s,3s;animation-delay:8s,3s}.snowflake:nth-of-type(6){left:60%;-webkit-animation-delay:6s,2s;animation-delay:6s,2s}.snowflake:nth-of-type(7){left:70%;-webkit-animation-delay:2.5s,1s;animation-delay:2.5s,1s}.snowflake:nth-of-type(8){left:80%;-webkit-animation-delay:1s,0s;animation-delay:1s,0s}.snowflake:nth-of-type(9){left:90%;-webkit-animation-delay:3s,1.5s;animation-delay:3s,1.5s}
|
| 10 |
+
|
| 11 |
+
|
| 12 |
+
/* [OPTIONAL] hide streamlit branding */
|
| 13 |
+
#MainMenu {visibility: hidden;}
|
| 14 |
+
footer {visibility: hidden;}
|
| 15 |
+
header {visibility: hidden;}
|