Update app.py
Browse files
app.py
CHANGED
|
@@ -6,57 +6,104 @@ import os
|
|
| 6 |
# Load environment variables
|
| 7 |
load_dotenv()
|
| 8 |
|
| 9 |
-
|
| 10 |
-
genai.configure(api_key=os.
|
| 11 |
|
| 12 |
def main():
|
| 13 |
# Function to generate SQL query based on prompt input
|
| 14 |
def gemini_ans(prompt_input):
|
| 15 |
-
model = genai.GenerativeModel('gemini-
|
| 16 |
response = model.generate_content([prompt_input])
|
| 17 |
return response.text
|
| 18 |
|
| 19 |
# Set page configuration
|
| 20 |
st.set_page_config(page_title='SQL Query Generator', page_icon=':robot:')
|
| 21 |
|
| 22 |
-
#
|
| 23 |
st.markdown(
|
| 24 |
"""
|
| 25 |
<style>
|
|
|
|
|
|
|
|
|
|
|
|
|
| 26 |
.header {
|
|
|
|
| 27 |
padding: 20px;
|
| 28 |
-
background-color: #f0f0f0;
|
| 29 |
border-radius: 10px;
|
|
|
|
| 30 |
margin-bottom: 20px;
|
| 31 |
-
color: black;
|
| 32 |
}
|
| 33 |
-
.header h1
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 34 |
margin: 0;
|
| 35 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 36 |
}
|
| 37 |
</style>
|
| 38 |
-
"""
|
| 39 |
-
|
|
|
|
| 40 |
|
|
|
|
| 41 |
st.markdown(
|
| 42 |
"""
|
| 43 |
<div class="header">
|
| 44 |
-
<h1
|
| 45 |
-
<
|
| 46 |
-
<p style="text-align: center;">This tool allows you to generate SQL queries based on your prompt.</p>
|
| 47 |
</div>
|
| 48 |
"""
|
| 49 |
, unsafe_allow_html=True)
|
| 50 |
-
|
| 51 |
-
#
|
| 52 |
-
|
| 53 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 54 |
# Generate SQL Query button
|
| 55 |
submit = st.button('Generate SQL Query', key='generate_button')
|
| 56 |
|
| 57 |
# Prompts for model responses
|
| 58 |
prompt = """
|
| 59 |
-
You are an English to SQL language translator.
|
|
|
|
| 60 |
write a SQL query only without making any mistakes.
|
| 61 |
"""
|
| 62 |
|
|
@@ -78,12 +125,20 @@ def main():
|
|
| 78 |
|
| 79 |
# Handle button click event
|
| 80 |
if submit:
|
|
|
|
|
|
|
|
|
|
|
|
|
| 81 |
with st.spinner('Generating SQL Query...'):
|
| 82 |
# Generate SQL query
|
| 83 |
-
sql_query = gemini_ans(prompt.format(
|
|
|
|
|
|
|
|
|
|
|
|
|
| 84 |
st.header('Model Response')
|
| 85 |
st.success("Generated SQL Query Successfully!")
|
| 86 |
-
st.
|
| 87 |
|
| 88 |
# Generate sample expected output
|
| 89 |
sql_table = gemini_ans(prompt1.format(query=sql_query))
|
|
|
|
| 6 |
# Load environment variables
|
| 7 |
load_dotenv()
|
| 8 |
|
| 9 |
+
os.environ["GOOGLE_API_KEY"] = os.getenv("GOOGLE_API_KEY")
|
| 10 |
+
genai.configure(api_key=os.environ["GOOGLE_API_KEY"])
|
| 11 |
|
| 12 |
def main():
|
| 13 |
# Function to generate SQL query based on prompt input
|
| 14 |
def gemini_ans(prompt_input):
|
| 15 |
+
model = genai.GenerativeModel('gemini-1.5-flash-latest')
|
| 16 |
response = model.generate_content([prompt_input])
|
| 17 |
return response.text
|
| 18 |
|
| 19 |
# Set page configuration
|
| 20 |
st.set_page_config(page_title='SQL Query Generator', page_icon=':robot:')
|
| 21 |
|
| 22 |
+
# Custom CSS for styling
|
| 23 |
st.markdown(
|
| 24 |
"""
|
| 25 |
<style>
|
| 26 |
+
body {
|
| 27 |
+
background-color: #f5f5f5;
|
| 28 |
+
font-family: 'Arial', sans-serif;
|
| 29 |
+
}
|
| 30 |
.header {
|
| 31 |
+
background-color: #ffffff;
|
| 32 |
padding: 20px;
|
|
|
|
| 33 |
border-radius: 10px;
|
| 34 |
+
box-shadow: 0 4px 8px rgba(0, 0, 0, 0.1);
|
| 35 |
margin-bottom: 20px;
|
|
|
|
| 36 |
}
|
| 37 |
+
.header h1 {
|
| 38 |
+
margin: 0;
|
| 39 |
+
color: #333333;
|
| 40 |
+
text-align: center;
|
| 41 |
+
}
|
| 42 |
+
.header p {
|
| 43 |
margin: 0;
|
| 44 |
+
text-align: center;
|
| 45 |
+
color: #666666;
|
| 46 |
+
font-size: 14px;
|
| 47 |
+
}
|
| 48 |
+
.input-container {
|
| 49 |
+
background-color: #ffffff;
|
| 50 |
+
padding: 20px;
|
| 51 |
+
border-radius: 10px;
|
| 52 |
+
box-shadow: 0 4px 8px rgba(0, 0, 0, 0.1);
|
| 53 |
+
margin-bottom: 20px;
|
| 54 |
+
}
|
| 55 |
+
.stTextArea, .stTextInput, .stButton {
|
| 56 |
+
margin-top: 15px;
|
| 57 |
+
}
|
| 58 |
+
.stButton > button {
|
| 59 |
+
background-color: #4CAF50;
|
| 60 |
+
color: white;
|
| 61 |
+
border: none;
|
| 62 |
+
padding: 10px 20px;
|
| 63 |
+
font-size: 16px;
|
| 64 |
+
border-radius: 5px;
|
| 65 |
+
cursor: pointer;
|
| 66 |
+
}
|
| 67 |
+
.stButton > button:hover {
|
| 68 |
+
background-color: #45a049;
|
| 69 |
}
|
| 70 |
</style>
|
| 71 |
+
""",
|
| 72 |
+
unsafe_allow_html=True
|
| 73 |
+
)
|
| 74 |
|
| 75 |
+
# Header section
|
| 76 |
st.markdown(
|
| 77 |
"""
|
| 78 |
<div class="header">
|
| 79 |
+
<h1>SQL Query Generator</h1>
|
| 80 |
+
<p>Effortlessly transform your natural language input into SQL queries!</p>
|
|
|
|
| 81 |
</div>
|
| 82 |
"""
|
| 83 |
, unsafe_allow_html=True)
|
| 84 |
+
|
| 85 |
+
# Input section with a card-like design
|
| 86 |
+
st.markdown(
|
| 87 |
+
"""
|
| 88 |
+
<div class="input-container">
|
| 89 |
+
<h4>Enter your details below:</h4>
|
| 90 |
+
</div>
|
| 91 |
+
""",
|
| 92 |
+
unsafe_allow_html=True
|
| 93 |
+
)
|
| 94 |
+
|
| 95 |
+
# Inputs
|
| 96 |
+
input_text = st.text_area('Enter your natural language query...')
|
| 97 |
+
schema_name = st.text_input('Enter the schema name (optional):', placeholder="e.g., public")
|
| 98 |
+
table_name = st.text_input('Enter the table name:', placeholder="e.g., users")
|
| 99 |
+
|
| 100 |
# Generate SQL Query button
|
| 101 |
submit = st.button('Generate SQL Query', key='generate_button')
|
| 102 |
|
| 103 |
# Prompts for model responses
|
| 104 |
prompt = """
|
| 105 |
+
You are an English to SQL language translator. The schema name is {schema},
|
| 106 |
+
and the table name is {table}. Using the given text here {en},
|
| 107 |
write a SQL query only without making any mistakes.
|
| 108 |
"""
|
| 109 |
|
|
|
|
| 125 |
|
| 126 |
# Handle button click event
|
| 127 |
if submit:
|
| 128 |
+
if not table_name:
|
| 129 |
+
st.error("Please enter the table name!")
|
| 130 |
+
return
|
| 131 |
+
|
| 132 |
with st.spinner('Generating SQL Query...'):
|
| 133 |
# Generate SQL query
|
| 134 |
+
sql_query = gemini_ans(prompt.format(
|
| 135 |
+
schema=schema_name or 'default schema',
|
| 136 |
+
table=table_name,
|
| 137 |
+
en=input_text
|
| 138 |
+
))
|
| 139 |
st.header('Model Response')
|
| 140 |
st.success("Generated SQL Query Successfully!")
|
| 141 |
+
st.code(sql_query, language="sql")
|
| 142 |
|
| 143 |
# Generate sample expected output
|
| 144 |
sql_table = gemini_ans(prompt1.format(query=sql_query))
|