Hello everyone and welcome back! Tired of wrestling with SQL? Imagine a tool that writes your queries for you just by asking in plain English. Yes, this is possible now.
Would you wish to bend SQL to your will with just a wave of your keyboard? We have harnessed the power of Google's cutting-edge Gemini AI to understand your plain English and craft the perfect SQL query, and with Streamlit’s user-friendly interface, your data will come alive with stunning visuals and interactive tables.
Ready to unleash the force of cutting-edge technologies? Subscribe to join the quest for SQL mastery. In this particular video, we are joining Google's cutting-edge Gemini Pro model with Streamlit's powerful user interface to make an application that can generate SQL queries for you along with the expected output and explanation.
So without further ado, let's get started!
First, let’s see how our application looks. Here's a simple UI you can see right over here. I just gave it a prompt like "bring unique age from the employee table." It returned the SQL query, showed the expected response, and provided an explanation. That's actually looking really nice.
Now let's see how to set up our project.
sql_query_generator
.code .
to open it in VS Code.sql_query_generator
folder.pip install -q -U google-generative-ai
This will install Google's Gemini Pro model. Next, create a new file named app.py
where all our logic and code will go.
First, let’s implement the frontend using Streamlit.
Import Streamlit:
import streamlit as st
Define the main function and set up the page config:
def main():
st.set_page_config(page_title="SQL Query Generator")
Set up the markdown for the description:
st.markdown('''<div>
<h1>SQL Query Generator</h1>
<h3>I can generate SQL queries for you</h3>
<p>This tool allows you to generate SQL queries based on your prompts.</p>
<h4>With Explanation as well!</h4>
</div>''', unsafe_allow_html=True)
Add a text area for user input and a submit button:
text_input = st.text_area("Enter your query here in plain English")
submit = st.button("Generate SQL Query")
We also need to import the Google generative AI library:
import google.generativeai as genai
Configure API key:
GOOGLE_API_KEY = 'YOUR_API_KEY'
genai.configure(api_key=GOOGLE_API_KEY)
Set up the model:
model = genai.generative_model(model='gemini-pro')
Implementing the backend logic to generate the SQL queries.
Define the structured prompt:
template = "Create a SQL query snippet using the below text:\n\n()\n\nProvide only the SQL query."
Format the template with the user text input:
formatted_template = template.format(text_input)
Generate the SQL query using the model:
if submit:
with st.spinner('Generating SQL query...'):
response = model.generate_content(formatted_template)
sql_query = response.text.strip('\n.')
Display the SQL query to the user:
st.success("SQL query generated successfully.")
st.code(sql_query, language='sql')
Add the expected output and explanation functionalities.
Define expected output template:
expected_output_template = "What would be the expected response of this SQL query snippet?\n\n()\n\nProvide simple tabular response with no explanation."
Generate and display the expected output:
expected_output_formatted = expected_output_template.format(sql_query)
e_output = model.generate_content(expected_output_formatted).text
st.success("Expected output of this SQL query will be:")
st.markdown(e_output, unsafe_allow_html=True)
Define explanation template and generate the explanation:
explanation_template = "Explain this SQL query:\n\n()\n\nProvide the simplest explanation."
explanation_formatted = explanation_template.format(sql_query)
explanation = model.generate_content(explanation_formatted).text
st.success("Explanation of this SQL query:")
st.markdown(explanation)
Run the application:
streamlit run app.py
Interact with the UI to check that the SQL query, expected output, and explanation look good.
Q: What does this application do? A: This application generates SQL queries based on plain English input using Google's Gemini Pro AI model and Streamlit for UI.
Q: Which AI model is being used? A: The application uses Google's Gemini Pro AI model.
Q: How do I install the needed dependencies?
A: You can install dependencies by running pip install -q -U google-generative-ai
in your terminal.
Q: How do I run the application?
A: After setting up the dependencies and the app.py
file, you can run the application using the command streamlit run app.py
.
Q: What kind of responses can I expect from the application? A: The application can generate SQL queries, provide the expected output in a tabular format, and explain the generated SQL queries.
Q: Is this tool free to use? A: Yes, Google's Gemini Pro model is currently free to use for up to 60 queries per minute, which is sufficient for most tasks.
In addition to the incredible tools mentioned above, for those looking to elevate their video creation process even further, Topview.ai stands out as a revolutionary online AI video editor.
TopView.ai provides two powerful tools to help you make ads video in one click.
Materials to Video: you can upload your raw footage or pictures, TopView.ai will edit video based on media you uploaded for you.
Link to Video: you can paste an E-Commerce product link, TopView.ai will generate a video for you.