Topview Logo
  • Create viral videos with
    GPT-4o + Ads library
    Use GPT-4o to edit video empowered by Youtube & Tiktok & Facebook ads library. Turns your links or media assets into viral videos in one click.
    Try it free
    gpt video

    Google's AI Gemini Pro End to End LLM Project | AI SQL Query Generator App

    blog thumbnail

    Introduction

    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!

    Project Setup

    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.

    Creating Project Directory and Installing Dependencies

    1. Create a new folder and call it sql_query_generator.
    2. Open this folder inside CMD and type code . to open it in VS Code.
    3. Open a new terminal and make sure that you're inside the sql_query_generator folder.
    4. Install the dependencies by typing:
      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.

    Implementing the Frontend

    First, let’s implement the frontend using Streamlit.

    1. Import Streamlit:

      import streamlit as st
      
    2. Define the main function and set up the page config:

      def main():
          st.set_page_config(page_title="SQL Query Generator")
      
    3. 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)
      
    4. 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")
      
    5. We also need to import the Google generative AI library:

      import google.generativeai as genai
      
    6. Configure API key:

      GOOGLE_API_KEY = 'YOUR_API_KEY'
      genai.configure(api_key=GOOGLE_API_KEY)
      
    7. Set up the model:

      model = genai.generative_model(model='gemini-pro')
      

    Generating SQL Queries

    Implementing the backend logic to generate the SQL queries.

    1. Define the structured prompt:

      template = "Create a SQL query snippet using the below text:\n\n()\n\nProvide only the SQL query."
      
    2. Format the template with the user text input:

      formatted_template = template.format(text_input)
      
    3. 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.')
      
    4. Display the SQL query to the user:

      st.success("SQL query generated successfully.")
      st.code(sql_query, language='sql')
      

    Generating Expected Output and Explanation

    Add the expected output and explanation functionalities.

    1. 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."
      
    2. 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)
      
    3. 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)
      

    Final Testing

    1. Run the application:

      streamlit run app.py
      
    2. Interact with the UI to check that the SQL query, expected output, and explanation look good.

    Keywords

    • SQL Queries
    • Google Gemini AI
    • Streamlit Interface
    • AI SQL Query Generator
    • Expected Output
    • Query Explanation

    FAQ

    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.

    One more thing

    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.

    You may also like