Skip to main content
Streamlit is a great tool for building micro front-ends. You can use it to build user interfaces, internal dashboards or tools for internal use. The streamlit-app template provides codebase with Streamlit & PostgreSQL, a stack we love for its simplicity. Its simple but not to be underestimated.

Setup

1

Create a virtual environment

python3 -m venv ~/.venvs/aienv
source ~/.venvs/aienv/bin/activate
2

Install phidata

pip install -U "phidata[aws]"
3

Install docker

Install docker desktop to run your app locally
4

Export your OpenAI key

export OPENAI_API_KEY=sk-***
You can get an API key from here.

Create your codebase

Create your codebase using the streamlit-app template
phi ws create -t streamlit-app -n streamlit-app
This will create a folder streamlit-app with the following structure:
streamlit-app                 # root directory for your streamlit-app
├── ai                      # directory for AI components
    ├── agents          # AI agents
    ├── knowledge_base.py   # agent knowledge base
    └── storage.py          # agent storage
├── app                     # directory for Streamlit apps
├── db                      # directory for database components
├── Dockerfile              # Dockerfile for the application
├── pyproject.toml          # python project definition
├── requirements.txt        # python dependencies generated by pyproject.toml
├── scripts                 # directory for helper scripts
├── tests                   # directory for unit tests
├── utils                   # directory for shared utilities
└── workspace               # phidata workspace directory
    ├── dev_resources.py    # dev resources running locally
    ├── prd_resources.py    # production resources running on AWS
    ├── secrets             # directory for storing secrets
    └── settings.py         # phidata workspace settings

Set OpenAI Key

Set your OPENAI_API_KEY as an environment variable. You can get one from OpenAI.
export OPENAI_API_KEY=sk-***

Local Streamlit App

Streamlit allows us to build micro front-ends and is an extremely useful tool for building basic applications in pure python. Start your workspace using:
phi ws up
Press Enter to confirm and give a few minutes for the image to download (only the first time). Verify container status and view logs on the docker dashboard.
Follow these steps to only run Streamlit without Postgres.
A common use case is to just use Streamlit without the database. In that case, deactivate the database from the workspace/settings.py file and update the agents to not use storage or knowledge base.
1

Disable database

Update the workspace/settings.py file and set dev_db_enabled=False and prd_db_enabled=False
workspace/settings.py
...
ws_settings = WorkspaceSettings(
    ...
    dev_db_enabled=False,
    prd_db_enabled=False,
    ...
2

Remove knowledge base and storage from the Agents

Update each file in the ai/agents folder to remove the storage and knowledge base from the Agents.
return Agent(
    ...
    # storage=pdf_agent_storage,
    # knowledge_base=pdf_knowledge_base,
    ...
)

Add your data

The PDF Agent uses the pdf_knowledge_base defined in the ai/knowledge_base.py file. To add your own PDFs:
1

Create a folder with your data

Create a folder data/pdfs in the root directory of your app
2

Add your data

Add your files to the data/pdfs folder
3

Update Knowledge Base

Click on the Update Knowledge Base button to load the knowledge base.
Checkout the ai/knowledge_base.py file for more information.

How this App works

The streamlit apps are defined in the app folder and the Agents powering these apps are defined in the ai/agents folder. Checkout the files in the ai/agents folder for more information.

Delete local resources

Play around and stop the workspace using:
phi ws down

Next

Congratulations on running your Streamlit App locally. Next Steps: