DEV Community

Akshay Keerthi
Akshay Keerthi

Posted on

Empowering Efficiency: Transforming Text into SQL with Lyzr SDK

Experience the future of streamlined data analysis today with FormulaGen, a groundbreaking module within Lyzr SDK. Powered by OpenAI’s Large Language Model (LLM), FormulaGen simplifies complex interactions with patterns, regular expressions, SQL queries, and spreadsheet formulas.

Image description

With FormulaGen’s text_to_sql feature, effortlessly translate natural language text queries into SQL queries with detailed explanations, empowering informed decision-making and enhancing efficiency in data analysis.

Why use Lyzr SDK’s?

With Lyzr SDKs, crafting your own GenAI application is a breeze, requiring only a few lines of code to get up and running swiftly.

Checkout Lyzr SDK's

Experience the intuitive process of creating cutting-edge GenAI applications with Lyzr. Give it a try now and discover the possibilities for yourself!

Let’s Build Together — Discord

Lets get Started!

Create a new file Hello.py and use that

Install and import the dependencies

!pip install lyzr
from lyzr import FormulaGen
from google.colab import userdata
import os
Enter fullscreen mode Exit fullscreen mode

In this snippet, the os and PIL libraries are imported for file and image handling respectively. Additionally, streamlit is imported for creating interactive web applications. The lyzr library is imported to utilize its functionalities, particularly the FormulaGen module, which likely assists in simplifying complex data analysis tasks, given its name.

To proceed, integrate the OpenAI API key into your Streamlit application using Streamlit’s secrets management functionality. In your script, ensure you replace the placeholder “OPENAI_API_KEY” with the designated key name as defined within Streamlit secrets, where your actual OpenAI API key is stored. This step secures the API key within your application, ensuring its confidentiality and proper usage.

# Set OpenAI API key
os.environ["OPENAI_API_KEY"] = st.secrets["apikey"]
Enter fullscreen mode Exit fullscreen mode

This step secures the API key within your application, ensuring its confidentiality and proper usage.

# Define the Streamlit app layout
def app():

    # Text input for user to input the text
    text_input = st.text_area("Enter your texts:")

    # Button to trigger the conversion
    if st.button("Convert to SQL"):
        # Text to SQL conversion
        result = generate.text_to_sql(text_input)

        # Display the result
        st.subheader("Generated SQL Query:")
        st.code(result, language="sql")
Enter fullscreen mode Exit fullscreen mode

This Streamlit application defines a simple layout for user interaction. It includes a text area where users can input text data, and a button labeled “Convert to SQL” to trigger the conversion process. When the button is clicked, the generate.text_to_sql function is called to convert the input text into SQL query format.

After conversion, the resulting SQL query is displayed below a subheader labeled “Generated SQL Query” using the st.subheader function. The SQL query is presented in a code block format using the st.code function, with the language parameter set to "sql" to ensure proper syntax highlighting for SQL code. Overall, this code snippet demonstrates the basic structure of a Streamlit app for converting text to SQL queries and displaying the generated SQL output to the user.

# Run the app
if __name__ == "__main__":
    app()
Enter fullscreen mode Exit fullscreen mode

This conditional statement ensures that the app() function, responsible for running the Streamlit application, is executed only when the Python script is directly run as the main program. This prevents the app from running if the script is imported as a module into another program, maintaining modularity and preventing unintended execution.

These code snippets exemplify the power of Streamlit and the Lyzr library in simplifying complex tasks such as converting natural language text into SQL queries. By combining intuitive user interfaces with advanced backend functionality, developers can create powerful tools that enhance productivity and streamline data analysis workflows.

With continued innovation and integration of cutting-edge technologies, the possibilities for creating impactful applications are limitless. Embrace the future of streamlined data analysis and empower your projects with the tools showcased here.

Try out the Application: https://querygenius-lyzr.streamlit.app/

For Source Code : https://github.com/isakshay007/Text2SQL

References

Lyzr Website: Lyzr

Book a Demo: Demo

Lyzr Community Channels: Discord

Slack : Slack

Top comments (0)