Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,63 +1,100 @@
|
|
| 1 |
-
from dotenv import load_dotenv
|
| 2 |
import streamlit as st
|
| 3 |
-
import os
|
| 4 |
-
import sqlite3
|
| 5 |
import google.generativeai as genai
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 6 |
|
| 7 |
-
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
|
|
|
| 26 |
"""
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 34 |
"""
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
st.
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
st.success("Query executed successfully. Results:")
|
| 59 |
-
st.table(results)
|
| 60 |
-
else:
|
| 61 |
-
st.warning("No results found.")
|
| 62 |
-
except Exception as e:
|
| 63 |
-
st.error(f"An error occurred: {e}")
|
|
|
|
|
|
|
| 1 |
import streamlit as st
|
|
|
|
|
|
|
| 2 |
import google.generativeai as genai
|
| 3 |
+
from dotenv import load_dotenv
|
| 4 |
+
import os
|
| 5 |
+
|
| 6 |
+
# Load environment variables
|
| 7 |
+
load_dotenv()
|
| 8 |
+
|
| 9 |
+
# Configure GenerativeAI with API key
|
| 10 |
+
genai.configure(api_key=os.getenv('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-pro')
|
| 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 |
+
# Header section
|
| 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, .header h3 {
|
| 34 |
+
margin: 0;
|
| 35 |
+
color: black;
|
| 36 |
+
}
|
| 37 |
+
</style>
|
| 38 |
+
"""
|
| 39 |
+
, unsafe_allow_html=True)
|
| 40 |
|
| 41 |
+
st.markdown(
|
| 42 |
+
"""
|
| 43 |
+
<div class="header">
|
| 44 |
+
<h1 style="text-align: center;">SQL Query Generator 🤖</h1>
|
| 45 |
+
<h3 style="text-align: center;">Generate SQL queries with ease!</h3>
|
| 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 |
+
# Text area for input
|
| 52 |
+
input_text = st.text_area('Enter your query...')
|
| 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. Using the given text here {en},
|
| 60 |
+
write a SQL query only without making any mistakes.
|
| 61 |
"""
|
| 62 |
+
|
| 63 |
+
prompt1 = """
|
| 64 |
+
What would be the expected response of this query snippet:
|
| 65 |
+
```
|
| 66 |
+
{query}
|
| 67 |
+
```
|
| 68 |
+
Provide a sample tabular response with no explanation.
|
| 69 |
+
"""
|
| 70 |
+
|
| 71 |
+
prompt2 = """
|
| 72 |
+
Explain the SQL query:
|
| 73 |
+
```
|
| 74 |
+
{query}
|
| 75 |
+
```
|
| 76 |
+
Please provide the simplest explanation.
|
| 77 |
"""
|
| 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(en=input_text))
|
| 84 |
+
st.header('Model Response')
|
| 85 |
+
st.success("Generated SQL Query Successfully!")
|
| 86 |
+
st.write(sql_query)
|
| 87 |
+
|
| 88 |
+
# Generate sample expected output
|
| 89 |
+
sql_table = gemini_ans(prompt1.format(query=sql_query))
|
| 90 |
+
st.success('Sample Expected Output')
|
| 91 |
+
st.markdown(sql_table)
|
| 92 |
+
|
| 93 |
+
# Generate explanation of the given query
|
| 94 |
+
sql_explanation = gemini_ans(prompt2.format(query=sql_query))
|
| 95 |
+
st.success('Explanation of the Given Query')
|
| 96 |
+
st.markdown(sql_explanation)
|
| 97 |
+
|
| 98 |
+
# Execute main function
|
| 99 |
+
if __name__ == "__main__":
|
| 100 |
+
main()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|