Building a Shareable GenAI App without the need to host it.

Amit Bhardwaj
3 min readSep 29, 2024

--

Gen AI app workflow using Streamlit

Creating user-friendly applications that leverage advanced models like OpenAI’s GPT can be exciting and rewarding in the ever-evolving landscape of artificial intelligence.

In this article, we will guide you through building a lightweight Streamlit app that connects to Snowflake, manipulates data using Python, and interacts with OpenAI’s API. We’ll even show you how to package your app as an executable file (.exe) for easy sharing, without the need for hosting it online.

Additionally, we’ll explore options for hosting your app on Streamlit Community Cloud if you choose to share it publicly.

Connecting to Snowflake

First, we establish a connection to Snowflake, a powerful data platform. Using Streamlit’s built-in features, you can easily connect to your Snowflake database. Ensure you have the necessary credentials and install the required libraries, including `snowflake-snowpark-python`. Here’s a snippet to set up your connection:

import streamlit as st
from snowflake.snowpark import Session
# Load connection details from Streamlit secrets
session = Session.builder.configs(st.secrets["snowflake"]).create()

This code snippet allows you to create a session with your Snowflake account, enabling you to query and manipulate data seamlessly.

Data Manipulation with Python

Once connected, you can manipulate your data using Python. For instance, you can execute SQL queries and transform the results into a format suitable for analysis or visualization. Here’s an example of how to fetch and display data:

data = session.sql("SELECT * FROM your_table").to_dataframe()
st.write(data)

This simple command retrieves data from a specified table and displays it in your Streamlit app.

Integrating OpenAI API

Next, we integrate OpenAI’s API to enhance our app’s functionality. By providing user input through a text box, we can send prompts to the API and receive generated responses. Here’s how you can implement this:

import openai
openai.api_key = st.secrets["OPENAI_API_KEY"]
user_input = st.text_input("Enter your prompt:")
if user_input:
response = openai.Completion.create(
engine="davinci",
prompt=user_input,
max_tokens=100
)
st.write(response.choices[0].text)

This code allows users to input prompts, which are then sent to OpenAI’s API, and the generated output is displayed in the app.

Collecting Outputs with Different Prompts

To enhance user interaction, you can allow users to experiment with different prompts. Create a form that collects various inputs and displays the results accordingly. This feature adds depth to your app, making it more engaging.

Downloading Data Locally

After processing the data and collecting outputs, users may want to download the results. Streamlit provides an easy way to enable file downloads. Here’s how you can implement a download button:


# Assuming 'results' is a DataFrame containing the output
results = pd.DataFrame({"Output": [response.choices[0].text]})
st.download_button("Download Results", results.to_csv().encode('utf-8'), "results.csv", "text/csv")
```

This button allows users to download the generated results as a CSV file.

Creating the User Interface

With the core functionalities in place, it’s time to create an intuitive user interface. Streamlit offers various widgets to enhance user experience. You can use sliders, buttons, and forms to make your app interactive and visually appealing.


st.title("GenAI App")
st.sidebar.header("Settings")

This simple setup gives your app a professional look and feel, inviting users to explore its capabilities.

Packaging with PyInstaller

Once your app is ready, you can package it as a standalone executable using PyInstaller. This step allows you to share your app easily without requiring users to install Python or any dependencies. Here’s how to do it:


pip install pyinstaller
pyinstaller - onefile your_app.py

This command generates a `.exe` file in the `dist` folder, which you can share with anyone.

Alternatively: Hosting on Streamlit Community Cloud

If you prefer to host your app publicly, Streamlit Community Cloud is an excellent option. To deploy your app:

1. Push your code to a public GitHub repository.
2. Create a `requirements.txt` file listing all dependencies.
3. Sign in to Streamlit Community Cloud and deploy your app by linking your GitHub repository.

Once deployed, you’ll receive a unique URL to share your app with the world.

Conclusion

Building a shareable GenAI app using Streamlit and packaging it as an executable file opens up exciting possibilities.

You can create a powerful tool that leverages advanced AI capabilities while ensuring accessibility and ease of use.

Please reach out or DM me on Linkedin for any doubts.

Citations

[1] https://www.restack.io/docs/streamlit-knowledge-streamlit-openai-api-integration
[2] https://www.restack.io/docs/streamlit-knowledge-streamlit-sharing-guide
[3] https://docs.streamlit.io/develop/api-reference/connections/st.connections.snowflakeconnection
[4] https://github.com/streamlit/streamlit/issues/400

--

--