Movatterモバイル変換


[0]ホーム

URL:


Skip to content
DEV Community
Log in Create account

DEV Community

Akshay Keerthi
Akshay Keerthi

Posted on

Building Environmental Analyzer using Lyzr SDK

TheEnvironmental Data Navigator is not just another data analysis tool; it’s a gateway to unlocking the insights hidden within vast environmental datasets. With an intuitive user interface powered byStreamlit, users can seamlessly upload their data and embark on a journey of exploration and discovery.

Image description

One of the key features of the app is its ability to simplify the complexities of data analysis. Users can upload CSV files containingenvironmental data with ease, and the app takes care of the rest. Leveraging the power ofLyzr’s DataAnalyzr agent, the app performs insightful analyses and presents users withdescriptive summaries and actionable queries.

Why use Lyzr SDK’s?

WithLyzr SDKs, crafting your ownGenAI application is a breeze, requiring only a few lines of code to get up and running swiftly.

Checkout the Lyzr SDK’s

Lets get Started!
Create a new fileapp.py and use that

import osfrom pathlib import Pathimport streamlit as stimport pandas as pd  # Import pandas modulefrom utils import utilsfrom lyzr import DataConnector, DataAnalyzr
Enter fullscreen modeExit fullscreen mode

This Python script sets up aStreamlit web application for data analysis. It imports necessary libraries such as Streamlit and Pandas, along with custom utility functions from a module called “utils”. The“lyzr” module is also imported, suggesting integration with Lyzr for data analysis capabilities. The script defines functions for data uploading, analysis, and visualization. It also configures the Streamlit app layout and handles user interactions.

Subsequently, it initializes the OpenAI API key using Streamlit’s secrets management. By accessing the specific key stored securely in Streamlit’s secrets, where the OpenAI API key is securely stored, it replaces the placeholder “OPENAI_API_KEY”. This ensures secure access to the OpenAI API within the Streamlit application.

# Set OpenAI API keyos.environ["OPENAI_API_KEY"] = st.secrets["apikey"]
Enter fullscreen modeExit fullscreen mode
# create directory if it doesn't existdata = "data"plot = 'plot'os.makedirs(data, exist_ok=True)os.makedirs(plot, exist_ok=True)
Enter fullscreen modeExit fullscreen mode

This code snippet creates directories named “data” and “plot” if they don’t already exist. It uses theos.makedirs() function to create the directories. Theexist_ok=True argument ensures that the function does not raise an error if the directories already exist; it simply skips creating them.

def data_uploader():    st.subheader("Upload Data file")    # Upload csv file    uploaded_file = st.file_uploader("Choose csv file", type=["csv"])    if uploaded_file is not None:        utils.save_uploaded_file(uploaded_file)    else:        utils.remove_existing_files(data)        utils.remove_existing_files(plot)
Enter fullscreen modeExit fullscreen mode

This function,data_uploader(), is responsible for allowing users to upload a CSV file. It first displays a subheader "Upload Data file" using Streamlit'sst.subheader() function. Then, it provides a file uploader component usingst.file_uploader() with a label "Choose csv file" and restricts the file type to CSV using the type parameter.

If a file isuploaded (uploaded_file is not None), it calls a functionutils.save_uploaded_file() from a module named "utils" to save the uploaded file.

If no file is uploaded, it calls functionsutils.remove_existing_files(data) andutils.remove_existing_files(plot) to remove any existing files in the "data" and "plot" directories, respectively. This ensures that previous files are cleared if no new file is uploaded.

def analyzr():    path = utils.get_files_in_directory(data)    path = path[0]    dataframe = DataConnector().fetch_dataframe_from_csv(file_path=Path(path))    analyzr_instance = DataAnalyzr(df=dataframe, api_key=st.secrets["apikey"])    return analyzr_instance
Enter fullscreen modeExit fullscreen mode

Theanalyzr() function orchestrates the data analysis process for the uploaded CSV file within the application. It begins by retrieving the file path of the uploaded file, likely from the "data" directory. This path is then used to fetch the data, which is loaded into a Pandas DataFrame using the DataConnector class. Subsequently, an instance of theLyzr DataAnalyzr class is created, passing the DataFrame and theAPI key for authentication.

This instance encapsulates the data analysis operations, allowing for insights and queries to be generated. Finally, the function returns theanalyzr_instance, providing the results of the data analysis for further processing or display.

def file_checker():    file = []    for filename in os.listdir(data):        file_path = os.path.join(data, filename)        file.append(file_path)    return file
Enter fullscreen modeExit fullscreen mode

Thefile_checker() function iterates through the files in the "data" directory to verify their existence. It initializes an empty list file to store file paths. Then, for each file in the directory obtained viaos.listdir(data), it constructs the full file path usingos.path.join() and appends it to the file list. Finally, the function returns the list of file paths.

# Function to display the dataset descriptiondef display_description(analyzr):    description = analyzr.dataset_description()    if description is not None:        st.subheader("Dataset Description:")        st.write(description)# Function to display queriesdef display_queries(analyzr):    queries = analyzr.ai_queries_df()    if queries is not None:        st.subheader("These Queries you can run on the data:")        st.write(queries)
Enter fullscreen modeExit fullscreen mode

Thedisplay_description(analyzr) function presents the dataset description generated by the Lyzr DataAnalyzr instance passed as an argument. It calls thedataset_description() method of the analyzr object to obtain the description, and if it's not empty, it displays it with a subheader "Dataset Description" and writes it to the Streamlit app interface using st.write().

Similarly, thedisplay_queries(analyzr) function showcases the queries available for the dataset. It invokes theai_queries_df() method of the analyzr object to fetch the queries, and if there are any, it presents them with a subheader "These Queries you can run on the data" and writes them to the Streamlit app interface using st.write().

# Modify DataConnector class to specify encoding when reading CSV fileclass DataConnector:    def fetch_dataframe_from_csv(self, file_path):        """        Fetches a Pandas DataFrame from a CSV file.        Parameters:            file_path (Path): Path to the CSV file.        Returns:            dataframe (DataFrame): Pandas DataFrame containing the data from the CSV file.        """        try:            # Specify encoding as 'latin1' when reading the CSV file            dataframe = pd.read_csv(file_path, encoding='latin1')            return dataframe        except Exception as e:            raise RuntimeError(f"Error occurred while reading CSV file '{file_path}': {str(e)}")
Enter fullscreen modeExit fullscreen mode

TheDataConnector class has been modified to include a specific encoding parameter when reading CSV files. In thefetch_dataframe_from_csv() method, the encoding parameter is set to 'latin1' to handle characters that may not be encoded correctly with the default encoding. This modification ensures that the method can handle a wider range of characters and prevents potential encoding errors when reading CSV files. If an error occurs during the reading process, a RuntimeError is raised, providing information about the file path and the encountered error for debugging purposes.

In conclusion, we’ve embarked on an exciting journey to build anEnvironmental Data Navigator using Streamlit and Lyzr. Our app serves as a testament to the transformative potential of technology in simplifying complex data analytics tasks. By empowering users with the tools and insights needed to navigate environmental datasets, we’re fostering a culture of data-driven decision-making towards a sustainable future.

As we wrap up our tutorial, let’s not forget that this is just the beginning. There are endless possibilities for further enhancing ourEnvironmental Data Navigator. Consider adding features such as interactive visualizations, advanced analytics algorithms, or integration with real-time environmental data sources. The sky’s the limit!

App link:https://environmental-analyzer-lyzr.streamlit.app/

Source Code:https://github.com/isakshay007/Library-Assistant

Connect with Lyzr
To learn more about Lyzr and its SDK’s, visit our website or get in touch with our team:

Website:Lyzr.ai
Book a Demo:Book a Demo
Discord:Join our Discord community
Slack:Join our Slack channel

Top comments(0)

Subscribe
pic
Create template

Templates let you quickly answer FAQs or store snippets for re-use.

Dismiss

Are you sure you want to hide this comment? It will become hidden in your post, but will still be visible via the comment'spermalink.

For further actions, you may consider blocking this person and/orreporting abuse

  • Joined

More fromAkshay Keerthi

DEV Community

We're a place where coders share, stay up-to-date and grow their careers.

Log in Create account

[8]ページ先頭

©2009-2025 Movatter.jp