├── .chainlit
└── config.toml
├── .gcloudignore
├── .gitignore
├── Dockerfile
├── LICENSE
├── README.md
├── app.yaml
├── chainlit.md
├── cloudbuild.yaml
├── demo_app
├── __init__.py
├── _utils.py
├── main.py
└── prompts.py
├── docker-compose.yml
├── poetry.lock
├── poetry.toml
├── pyproject.toml
├── requirements.txt
└── ui.PNG
/.chainlit/config.toml:
--------------------------------------------------------------------------------
1 | [project]
2 | # Name of the app and chatbot.
3 | name = "Chatbot"
4 | # Description of the app and chatbot. This is used for HTML tags.
5 | # description = ""
6 |
7 | # If true (default), the app will be available to anonymous users (once deployed).
8 | # If false, users will need to authenticate and be part of the project to use the app.
9 | public = true
10 |
11 | # The project ID (found on https://cloud.chainlit.io).
12 | # If provided, all the message data will be stored in the cloud.
13 | # The project ID is required when public is set to false.
14 | #id = ""
15 |
16 | # Whether to enable telemetry (default: true). No personal data is collected.
17 | enable_telemetry = true
18 |
19 | # List of environment variables to be provided by each user to use the app.
20 | user_env = ["OPENAI_API_KEY"]
21 |
22 | # Hide the chain of thought details from the user in the UI.
23 | hide_cot = false
24 |
25 | # Link to your github repo. This will add a github button in the UI's header.
26 | github = "https://github.com/amjadraza/pandasai-chainlit-docker-deployment-template"
27 |
28 | # Limit the number of requests per user.
29 | #request_limit = "10 per day"
30 |
--------------------------------------------------------------------------------
/.gcloudignore:
--------------------------------------------------------------------------------
1 | # Local data
2 | data/local_data/
3 |
4 | # Secrets
5 | .streamlit/secrets.toml
6 |
7 | # VSCode
8 | .vscode/
9 |
10 | # TODO
11 | TODO.md
12 |
13 | # Byte-compiled / optimized / DLL files
14 | __pycache__/
15 | *.py[cod]
16 | *$py.class
17 |
18 | # C extensions
19 | *.so
20 |
21 | # Distribution / packaging
22 | .Python
23 | build/
24 | develop-eggs/
25 | dist/
26 | downloads/
27 | eggs/
28 | .eggs/
29 | lib/
30 | lib64/
31 | parts/
32 | sdist/
33 | var/
34 | wheels/
35 | share/python-wheels/
36 | *.egg-info/
37 | .installed.cfg
38 | *.egg
39 | MANIFEST
40 |
41 | # PyInstaller
42 | # Usually these files are written by a python script from a template
43 | # before PyInstaller builds the exe, so as to inject date/other infos into it.
44 | *.manifest
45 | *.spec
46 |
47 | # Installer logs
48 | pip-log.txt
49 | pip-delete-this-directory.txt
50 |
51 | # Unit test / coverage reports
52 | htmlcov/
53 | .tox/
54 | .nox/
55 | .coverage
56 | .coverage.*
57 | .cache
58 | nosetests.xml
59 | coverage.xml
60 | *.cover
61 | *.py,cover
62 | .hypothesis/
63 | .pytest_cache/
64 | cover/
65 |
66 | # Translations
67 | *.mo
68 | *.pot
69 |
70 | # Django stuff:
71 | *.log
72 | local_settings.py
73 | db.sqlite3
74 | db.sqlite3-journal
75 |
76 | # Flask stuff:
77 | instance/
78 | .webassets-cache
79 |
80 | # Scrapy stuff:
81 | .scrapy
82 |
83 | # Sphinx documentation
84 | docs/_build/
85 |
86 | # PyBuilder
87 | .pybuilder/
88 | target/
89 |
90 | # Jupyter Notebook
91 | .ipynb_checkpoints
92 |
93 | # IPython
94 | profile_default/
95 | ipython_config.py
96 |
97 | # pdm
98 | # Similar to Pipfile.lock, it is generally recommended to include pdm.lock in version control.
99 | #pdm.lock
100 | # pdm stores project-wide configurations in .pdm.toml, but it is recommended to not include it
101 | # in version control.
102 | # https://pdm.fming.dev/#use-with-ide
103 | .pdm.toml
104 |
105 | # PEP 582; used by e.g. github.com/David-OConnor/pyflow and github.com/pdm-project/pdm
106 | __pypackages__/
107 |
108 | # Celery stuff
109 | celerybeat-schedule
110 | celerybeat.pid
111 |
112 | # SageMath parsed files
113 | *.sage.py
114 |
115 | # Environments
116 | .env
117 | .venv
118 | env/
119 | venv/
120 | ENV/
121 | env.bak/
122 | venv.bak/
123 |
124 | # Spyder project settings
125 | .spyderproject
126 | .spyproject
127 |
128 | # Rope project settings
129 | .ropeproject
130 |
131 | # mkdocs documentation
132 | /site
133 |
134 | # mypy
135 | .mypy_cache/
136 | .dmypy.json
137 | dmypy.json
138 |
139 | # Pyre type checker
140 | .pyre/
141 |
142 | # pytype static type analyzer
143 | .pytype/
144 |
145 | # Cython debug symbols
146 | cython_debug/
147 |
148 | # PyCharm
149 | # JetBrains specific template is maintained in a separate JetBrains.gitignore that can
150 | # be found at https://github.com/github/gitignore/blob/main/Global/JetBrains.gitignore
151 | # and can be added to the global gitignore or merged into this file. For a more nuclear
152 | # option (not recommended) you can uncomment the following to ignore the entire idea folder.
153 | .idea/
154 | .idea
155 | .gcloudignore
156 | credentials.json
157 | token.json
158 |
159 |
--------------------------------------------------------------------------------
/.gitignore:
--------------------------------------------------------------------------------
1 | # Local data
2 | data/local_data/
3 |
4 | # Secrets
5 | .streamlit/secrets.toml
6 |
7 | # VSCode
8 | .vscode/
9 |
10 | # TODO
11 | TODO.md
12 |
13 | # Byte-compiled / optimized / DLL files
14 | __pycache__/
15 | *.py[cod]
16 | *$py.class
17 |
18 | # C extensions
19 | *.so
20 |
21 | # Distribution / packaging
22 | .Python
23 | build/
24 | develop-eggs/
25 | dist/
26 | downloads/
27 | eggs/
28 | .eggs/
29 | lib/
30 | lib64/
31 | parts/
32 | sdist/
33 | var/
34 | wheels/
35 | share/python-wheels/
36 | *.egg-info/
37 | .installed.cfg
38 | *.egg
39 | MANIFEST
40 |
41 | # PyInstaller
42 | # Usually these files are written by a python script from a template
43 | # before PyInstaller builds the exe, so as to inject date/other infos into it.
44 | *.manifest
45 | *.spec
46 |
47 | # Installer logs
48 | pip-log.txt
49 | pip-delete-this-directory.txt
50 |
51 | # Unit test / coverage reports
52 | htmlcov/
53 | .tox/
54 | .nox/
55 | .coverage
56 | .coverage.*
57 | .cache
58 | nosetests.xml
59 | coverage.xml
60 | *.cover
61 | *.py,cover
62 | .hypothesis/
63 | .pytest_cache/
64 | cover/
65 |
66 | # Translations
67 | *.mo
68 | *.pot
69 |
70 | # Django stuff:
71 | *.log
72 | local_settings.py
73 | db.sqlite3
74 | db.sqlite3-journal
75 |
76 | # Flask stuff:
77 | instance/
78 | .webassets-cache
79 |
80 | # Scrapy stuff:
81 | .scrapy
82 |
83 | # Sphinx documentation
84 | docs/_build/
85 |
86 | # PyBuilder
87 | .pybuilder/
88 | target/
89 |
90 | # Jupyter Notebook
91 | .ipynb_checkpoints
92 |
93 | # IPython
94 | profile_default/
95 | ipython_config.py
96 |
97 | # pdm
98 | # Similar to Pipfile.lock, it is generally recommended to include pdm.lock in version control.
99 | #pdm.lock
100 | # pdm stores project-wide configurations in .pdm.toml, but it is recommended to not include it
101 | # in version control.
102 | # https://pdm.fming.dev/#use-with-ide
103 | .pdm.toml
104 |
105 | # PEP 582; used by e.g. github.com/David-OConnor/pyflow and github.com/pdm-project/pdm
106 | __pypackages__/
107 |
108 | # Celery stuff
109 | celerybeat-schedule
110 | celerybeat.pid
111 |
112 | # SageMath parsed files
113 | *.sage.py
114 |
115 | # Environments
116 | .env
117 | .venv
118 | env/
119 | venv/
120 | ENV/
121 | env.bak/
122 | venv.bak/
123 |
124 | # Spyder project settings
125 | .spyderproject
126 | .spyproject
127 |
128 | # Rope project settings
129 | .ropeproject
130 |
131 | # mkdocs documentation
132 | /site
133 |
134 | # mypy
135 | .mypy_cache/
136 | .dmypy.json
137 | dmypy.json
138 |
139 | # Pyre type checker
140 | .pyre/
141 |
142 | # pytype static type analyzer
143 | .pytype/
144 |
145 | # Cython debug symbols
146 | cython_debug/
147 |
148 | # PyCharm
149 | # JetBrains specific template is maintained in a separate JetBrains.gitignore that can
150 | # be found at https://github.com/github/gitignore/blob/main/Global/JetBrains.gitignore
151 | # and can be added to the global gitignore or merged into this file. For a more nuclear
152 | # option (not recommended) you can uncomment the following to ignore the entire idea folder.
153 | .idea/
154 | credentials.json
155 | token.json
156 |
157 |
--------------------------------------------------------------------------------
/Dockerfile:
--------------------------------------------------------------------------------
1 | # The builder image, used to build the virtual environment
2 | FROM python:3.11-slim-buster as builder
3 |
4 | RUN apt-get update && apt-get install -y git
5 |
6 | RUN pip install poetry==1.4.2
7 |
8 | ENV POETRY_NO_INTERACTION=1 \
9 | POETRY_VIRTUALENVS_IN_PROJECT=1 \
10 | POETRY_VIRTUALENVS_CREATE=1 \
11 | POETRY_CACHE_DIR=/tmp/poetry_cache
12 |
13 | ENV HOST=0.0.0.0
14 | ENV LISTEN_PORT 8000
15 | EXPOSE 8000
16 |
17 | WORKDIR /app
18 |
19 | COPY pyproject.toml poetry.lock ./
20 |
21 | RUN poetry install --without dev --no-root && rm -rf $POETRY_CACHE_DIR
22 |
23 | # The runtime image, used to just run the code provided its virtual environment
24 | FROM python:3.11-slim-buster as runtime
25 |
26 | ENV VIRTUAL_ENV=/app/.venv \
27 | PATH="/app/.venv/bin:$PATH"
28 |
29 | COPY --from=builder ${VIRTUAL_ENV} ${VIRTUAL_ENV}
30 |
31 | COPY ./demo_app ./demo_app
32 | COPY ./.chainlit ./.chainlit
33 | COPY chainlit.md ./
34 |
35 | CMD ["chainlit", "run", "demo_app/main.py"]
36 |
--------------------------------------------------------------------------------
/LICENSE:
--------------------------------------------------------------------------------
1 | MIT License
2 |
3 | Copyright (c) 2023 DR. AMJAD RAZA
4 |
5 | Permission is hereby granted, free of charge, to any person obtaining a copy
6 | of this software and associated documentation files (the "Software"), to deal
7 | in the Software without restriction, including without limitation the rights
8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9 | copies of the Software, and to permit persons to whom the Software is
10 | furnished to do so, subject to the following conditions:
11 |
12 | The above copyright notice and this permission notice shall be included in all
13 | copies or substantial portions of the Software.
14 |
15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21 | SOFTWARE.
22 |
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 |
2 | 📖 PandasAI-Chainlit-Docker-Deployment App Template
3 |
4 |
5 | 
6 |
7 |
8 | ## 🔧 Features
9 |
10 | - Basic Skeleton App configured with `openai` API
11 | - A ChatBot using PandasAI and Chainlit
12 | - Docker Support with Optimisation Cache etc
13 | - Deployment on Google Cloud App Engine
14 | - Deployment on Google Cloud using `Cloud Run`
15 |
16 | > Reference repository: https://github.com/amjadraza/langchain-chainlit-docker-template
17 |
18 | This repo contains an `main.py` file which has a template for a chatbot talking to CSV implementation.
19 |
20 | ## Adding your chain
21 | To add your chain, you need to change the `load_chain` function in `main.py`.
22 | Depending on the type of your chain, you may also need to change the inputs/outputs that occur later on.
23 |
24 |
25 | ## 💻 Running Locally
26 |
27 | 1. Clone the repository📂
28 |
29 | ```bash
30 | git clone https://github.com/amjadraza/pandasai-chainlit-docker-deployment-template
31 | ```
32 |
33 | 2. Install dependencies with [Poetry](https://python-poetry.org/) and activate virtual environment🔨
34 |
35 | ```bash
36 | poetry install
37 | poetry shell
38 | ```
39 |
40 | 3. Run the Chainlit server🚀
41 |
42 | ```bash
43 | chainlit run demo_app/main.py
44 | ```
45 |
46 | Run App using Docker
47 | --------------------
48 | This project includes `Dockerfile` to run the app in Docker container. In order to optimise the Docker Image
49 | size and building time with cache techniques, I have follow tricks in below Article
50 | https://medium.com/@albertazzir/blazing-fast-python-docker-builds-with-poetry-a78a66f5aed0
51 |
52 | Build the docker container
53 |
54 | ``docker build . -t pandasai-chainlit-chat-app:latest``
55 |
56 | To generate Image with `DOCKER_BUILDKIT`, follow below command
57 |
58 | ```DOCKER_BUILDKIT=1 docker build --target=runtime . -t pandasai-chainlit-chat-app:latest```
59 |
60 | 1. Run the docker container directly
61 |
62 | ``docker run -d --name pandasai-chainlit-chat-app -p 8000:8000 pandasai-chainlit-chat-app ``
63 |
64 | 2. Run the docker container using docker-compose (Recommended)
65 |
66 | ``docker-compose up``
67 |
68 |
69 | Deploy App on Google App Engine
70 | --------------------------------
71 | This app can be deployed on Google App Engine following below steps.
72 |
73 | ## Prerequisites
74 |
75 | Follow below guide on basic Instructions.
76 | [How to deploy Streamlit apps to Google App Engine](https://dev.to/whitphx/how-to-deploy-streamlit-apps-to-google-app-engine-407o)
77 |
78 | We added below tow configurations files
79 |
80 | 1. `app.yaml`: A Configuration file for `gcloud`
81 | 2. `.gcloudignore` : Configure the file to ignore file / folders to be uploaded
82 |
83 | I have adopted `Dockerfile` to deploy the app on GCP APP Engine.
84 |
85 | 1. Initialise & Configure the App
86 |
87 | ``gcloud app create --project=[YOUR_PROJECT_ID]``
88 |
89 | 2. Deploy the App using
90 |
91 | ``gcloud app deploy``
92 |
93 | 3. Access the App using
94 |
95 | https://pandasai-chat-app-dpy4wfgkcq-ts.a.run.app/
96 |
97 |
98 | Deploy App on Google Cloud using Cloud Run (RECOMMENDED)
99 | --------------------------------------------------------
100 | This app can be deployed on Google Cloud using Cloud Run following below steps.
101 |
102 | ## Prerequisites
103 |
104 | Follow below guide on basic Instructions.
105 | [How to deploy Streamlit apps to Google App Engine](https://dev.to/whitphx/how-to-deploy-streamlit-apps-to-google-app-engine-407o)
106 |
107 | We added below tow configurations files
108 |
109 | 1. `cloudbuild.yaml`: A Configuration file for `gcloud`
110 | 2. `.gcloudignore` : Configure the file to ignore file / folders to be uploaded
111 |
112 | we are going to use `Dockerfile` to deploy the app using Google Cloud Run.
113 |
114 | 1. Initialise & Configure the Google Project using Command Prompt
115 |
116 | `gcloud app create --project=[YOUR_PROJECT_ID]`
117 |
118 | or set the project
119 |
120 | `gcloud config set pandasai-app`
121 |
122 | Set the Region if not done before
123 |
124 | `gcloud config set compute/region australia-southeast1`
125 |
126 |
127 | 2. Enable Services for the Project
128 |
129 | ```
130 | gcloud services enable cloudbuild.googleapis.com
131 | gcloud services enable run.googleapis.com
132 | ```
133 |
134 | 3. Create Service Account
135 |
136 | ```
137 | gcloud iam service-accounts create pandasai-app-cr \
138 | --display-name="pandasai-app-cr"
139 |
140 | gcloud projects add-iam-policy-binding pandasai-app \
141 | --member="serviceAccount:pandasai-app-cr@pandasai-app.iam.gserviceaccount.com" \
142 | --role="roles/run.invoker"
143 |
144 | gcloud projects add-iam-policy-binding pandasai-app \
145 | --member="serviceAccount:pandasai-app-cr@pandasai-app.iam.gserviceaccount.com" \
146 | --role="roles/serviceusage.serviceUsageConsumer"
147 |
148 | gcloud projects add-iam-policy-binding pandasai-app \
149 | --member="serviceAccount:pandasai-app-cr@pandasai-app.iam.gserviceaccount.com" \
150 | --role="roles/run.admin"
151 | ```
152 |
153 | 4. Generate the Docker
154 |
155 | `DOCKER_BUILDKIT=1 docker build --target=runtime . -t australia-southeast1-docker.pkg.dev/pandasai-app/pai-app/pandasai-chainlit-chat-app:latest`
156 |
157 | 5. Push Image to Google Artifact's Registry
158 |
159 | Create the repository with name `pai-app`
160 |
161 | ```
162 | gcloud artifacts repositories create pai-app \
163 | --repository-format=docker \
164 | --location=australia-southeast1 \
165 | --description="A PandasAI Chainlit App" \
166 | --async
167 | ```
168 |
169 | Configure-docker
170 |
171 | `gcloud auth configure-docker australia-southeast1-docker.pkg.dev`
172 |
173 | In order to push the `docker-image` to Artifact registry, first create app in the region of choice.
174 |
175 | Check the artifacts locations
176 |
177 | `gcloud artifacts locations list`
178 |
179 |
180 |
181 | Once ready, let us push the image to location
182 |
183 | `docker push australia-southeast1-docker.pkg.dev/pandasai-app/pai-app/pandasai-chainlit-chat-app:latest`
184 |
185 | 6. Deploy using Cloud Run
186 |
187 | Once image is pushed to Google Cloud Artifacts Registry. Let us deploy the image.
188 |
189 | ```
190 | gcloud run deploy pandasai-chat-app --image=australia-southeast1-docker.pkg.dev/pandasai-app/pai-app/pandasai-chainlit-chat-app:latest \
191 | --region=australia-southeast1 \
192 | --service-account=pandasai-app-cr@pandasai-app.iam.gserviceaccount.com \
193 | --port=8000
194 | ```
195 |
196 | 7. Test the App Yourself
197 |
198 | You can try the app using below link
199 |
200 | https://pandasai-chat-app-dpy4wfgkcq-ts.a.run.app/
201 |
202 |
203 | ## Report Feedbacks
204 |
205 | As `pandasai-chainlit-docker-deployment-template` is a template project with minimal example. Report issues if you face any.
206 |
207 | ## DISCLAIMER
208 |
209 | This is a template App, when using with openai_api key, you will be charged a nominal fee depending
210 | on number of prompts etc.
--------------------------------------------------------------------------------
/app.yaml:
--------------------------------------------------------------------------------
1 | # Copyright 2021 Google LLC
2 | #
3 | # Licensed under the Apache License, Version 2.0 (the "License");
4 | # you may not use this file except in compliance with the License.
5 | # You may obtain a copy of the License at
6 | #
7 | # http://www.apache.org/licenses/LICENSE-2.0
8 | #
9 | # Unless required by applicable law or agreed to in writing, software
10 | # distributed under the License is distributed on an "AS IS" BASIS,
11 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 | # See the License for the specific language governing permissions and
13 | # limitations under the License.
14 |
15 | #---------------------------------------------------------------
16 | #runtime: python
17 | #env: flex
18 | #entrypoint: streamlit run -b pandasai_app/main.py
19 | #
20 | #runtime_config:
21 | # operating_system: ubuntu22
22 | #---------------------------------------------------------------
23 |
24 | # With Dockerfile
25 | runtime: custom
26 | env: flex
27 |
28 | # This sample incurs costs to run on the App Engine flexible environment.
29 | # The settings below are to reduce costs during testing and are not appropriate
30 | # for production use. For more information, see:
31 | # https://cloud.google.com/appengine/docs/flexible/python/configuring-your-app-with-app-yaml
32 |
33 | #network:
34 | # forwarded_ports:
35 | # - 8501/tcp
36 |
37 | manual_scaling:
38 | instances: 1
39 |
40 | resources:
41 | cpu: 1
42 | memory_gb: 0.5
43 | disk_size_gb: 10
--------------------------------------------------------------------------------
/chainlit.md:
--------------------------------------------------------------------------------
1 | # Welcome to PandasAI Chainlit: App! 🚀🤖
2 |
3 | Hi there, Developers! 👋 We're excited to have you on board. This template project is a powerful tool designed to help you prototype, debug and share applications built on top of LLMs with Docker Deployment.
4 |
5 | ## Useful Links 🔗
6 |
7 | - **Chain Link Documentation:** Get started with our comprehensive [Chainlit Documentation](https://docs.chainlit.io) 📚
8 | - **PandasAI Documentation:** Get started with PandasAI Documentation [PandasAI Documentation](https://pandas-ai.readthedocs.io/en/latest/) 📚
9 |
10 | ## FAQ
11 | ### How to use App Template?
12 | This is a basic template to set up PandasAI Demo App with Docker and Chainlit
13 |
14 | ### What Libraries are being use?
15 | Basic Setup is using Pandasai, Chainlit and openai.
16 |
17 | ### How to test the APP?
18 | Provide the OpenAI API keys and run the App
19 |
20 | ### Disclaimer?
21 | This is a template App, when using with openai_api key, you will be charged a nominal fee depending
22 | on number of prompts etc.
23 |
24 | We can't wait to see what you create with Chainlit! Happy coding! 💻😊
--------------------------------------------------------------------------------
/cloudbuild.yaml:
--------------------------------------------------------------------------------
1 | steps:
2 | - name: 'gcr.io/cloud-builders/docker'
3 | id: Build Image
4 | entrypoint: bash
5 | args:
6 | - -c
7 | - |
8 | DOCKER_BUILDKIT=1 docker build --target=runtime . -t australia-southeast1-docker.pkg.dev/langchain-chat/app/langchain-chat-app:latest \
9 | && docker push australia-southeast1-docker.pkg.dev/langchain-chat/app/langchain-chat-app:latest
10 |
11 | - name: 'gcr.io/google.com/cloudsdktool/cloud-sdk'
12 | entrypoint: gcloud
13 | id: Deploy API
14 | args: ['run', 'deploy', 'langchain-chat',
15 | '--image=australia-southeast1-docker.pkg.dev/langchain-chat/app/langchain-chat-app:latest',
16 | '--region=australia-southeast1', '--service-account=langchain-app-cr@langchain-chat.iam.gserviceaccount.com',
17 | '--allow-unauthenticated',
18 | '--set-env-vars=STREAMLIT_SERVER_PORT=8080']
19 | waitFor: [ 'Build Image' ]
20 |
21 | images:
22 | - australia-southeast1-docker.pkg.dev/langchain-chat/app/langchain-chat-app:latest
--------------------------------------------------------------------------------
/demo_app/__init__.py:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/amjadraza/pandasai-chainlit-docker-deployment-template/f989b1f54f3fde7d43d259c024b9a9c7b1ca2d64/demo_app/__init__.py
--------------------------------------------------------------------------------
/demo_app/_utils.py:
--------------------------------------------------------------------------------
1 | import re
2 | import os
3 | from io import BytesIO
4 | from typing import Any, Dict, List
5 | from pandasai import PandasAI
6 | from pandasai.llm.openai import OpenAI
7 | from pandasai.llm.open_assistant import OpenAssistant
8 | from pandasai.llm.starcoder import Starcoder
9 | import pandas as pd
10 | import chainlit as cl
11 |
12 | models = {
13 | "OpenAI": OpenAI,
14 | "Starcoder": Starcoder,
15 | "Open-Assistant": OpenAssistant
16 | }
17 |
18 | file_formats = {
19 | "csv": pd.read_csv,
20 | "xls": pd.read_excel,
21 | "xlsx": pd.read_excel,
22 | "xlsm": pd.read_excel,
23 | "xlsb": pd.read_excel,
24 | "json": pd.read_json,
25 | "html": pd.read_html,
26 | "sql": pd.read_sql,
27 | "feather": pd.read_feather,
28 | "parquet": pd.read_parquet,
29 | "dta": pd.read_stata,
30 | "sas7bdat": pd.read_sas,
31 | "h5": pd.read_hdf,
32 | "hdf5": pd.read_hdf,
33 | "pkl": pd.read_pickle,
34 | "pickle": pd.read_pickle,
35 | "gbq": pd.read_gbq,
36 | "orc": pd.read_orc,
37 | "xpt": pd.read_sas,
38 | "sav": pd.read_spss,
39 | "gz": pd.read_csv,
40 | "zip": pd.read_csv,
41 | "bz2": pd.read_csv,
42 | "xz": pd.read_csv,
43 | "txt": pd.read_csv,
44 | "xml": pd.read_xml,
45 | }
46 |
47 |
48 | def generate_pandasai_response(df,
49 | prompt,
50 | model_option="OpenAI",
51 | is_conversational_answer=False,
52 | is_verbose=False):
53 | """
54 | A function to run the Query on given Pandas Dataframe
55 | Args:
56 |
57 | df: A Pandas dataframe
58 | prompt: Query / Prompt related to data
59 | model_option: Select the Model from ["OpenAI", "Starcoder", "Open-Assistant"]
60 | is_conversational_answer: Run model in Conversational mode
61 | verbose: A parameter to show the intermediate python code generation
62 |
63 | Returns: Response / Results
64 |
65 | """
66 |
67 | user_env = cl.user_session.get("env")
68 | # os.environ["OPENAI_API_KEY"] = user_env.get("OPENAI_API_KEY")
69 |
70 | llm = models[model_option](api_token=user_env.get("OPENAI_API_KEY"))
71 | pandas_ai = PandasAI(llm, conversational=False, verbose=is_verbose)
72 | response = pandas_ai.run(df, prompt=prompt,
73 | is_conversational_answer=is_conversational_answer)
74 |
75 | return response
--------------------------------------------------------------------------------
/demo_app/main.py:
--------------------------------------------------------------------------------
1 | """Python file to serve as the frontend"""
2 | import sys
3 | import os
4 | import pandas as pd
5 | import io
6 | sys.path.append(os.path.abspath('.'))
7 |
8 | from demo_app._utils import generate_pandasai_response
9 |
10 | import chainlit as cl
11 |
12 | from chainlit.types import AskFileResponse
13 |
14 |
15 | def process_file(file: AskFileResponse):
16 | import tempfile
17 |
18 | with tempfile.NamedTemporaryFile() as tempfile:
19 | tempfile.write(file.content)
20 | df = pd.read_csv(tempfile.name)
21 |
22 | print(df.head(5))
23 |
24 | return df
25 |
26 | @cl.on_chat_start
27 | async def on_chat_start():
28 | files = await cl.AskFileMessage(
29 | content="Please upload a CSV file", accept=["text/csv"]
30 | ).send()
31 | file = files[0]
32 | csv_file = io.BytesIO(file.content)
33 | df = pd.read_csv(csv_file, encoding="latin1")
34 | cl.user_session.set('data', df)
35 | await cl.Message(
36 | content="The first 5 rows of the file are:\n" + str(df.head())
37 | ).send()
38 |
39 |
40 | @cl.on_message
41 | async def main(message: str):
42 | # Your custom logic goes here...
43 |
44 | # Use PandasAI to Run the Query on Uploaded CSV file
45 | answer = generate_pandasai_response(df=cl.user_session.get('data'),
46 | prompt=message,
47 | model_option="OpenAI",
48 | is_conversational_answer=True,
49 | is_verbose=True)
50 |
51 | # Send a response back to the user
52 | await cl.Message(
53 | content=answer,
54 | ).send()
--------------------------------------------------------------------------------
/demo_app/prompts.py:
--------------------------------------------------------------------------------
1 | # flake8: noqa
2 | # from langchain.prompts import PromptTemplate
3 |
4 | ## Use a shorter template to reduce the number of tokens in the prompt
5 | template = """Create a final answer to the given questions using the provided document excerpts(in no particular order) as references. ALWAYS include a "SOURCES" section in your answer including only the minimal set of sources needed to answer the question. If you are unable to answer the question, simply state that you do not know. Do not attempt to fabricate an answer and leave the SOURCES section empty.
6 |
7 | ---------
8 |
9 | QUESTION: What is the purpose of ARPA-H?
10 | =========
11 | Content: More support for patients and families. \n\nTo get there, I call on Congress to fund ARPA-H, the Advanced Research Projects Agency for Health. \n\nIt’s based on DARPA—the Defense Department project that led to the Internet, GPS, and so much more. \n\nARPA-H will have a singular purpose—to drive breakthroughs in cancer, Alzheimer’s, diabetes, and more.
12 | Source: 1-32
13 | Content: While we’re at it, let’s make sure every American can get the health care they need. \n\nWe’ve already made historic investments in health care. \n\nWe’ve made it easier for Americans to get the care they need, when they need it. \n\nWe’ve made it easier for Americans to get the treatments they need, when they need them. \n\nWe’ve made it easier for Americans to get the medications they need, when they need them.
14 | Source: 1-33
15 | Content: The V.A. is pioneering new ways of linking toxic exposures to disease, already helping veterans get the care they deserve. \n\nWe need to extend that same care to all Americans. \n\nThat’s why I’m calling on Congress to pass legislation that would establish a national registry of toxic exposures, and provide health care and financial assistance to those affected.
16 | Source: 1-30
17 | =========
18 | FINAL ANSWER: The purpose of ARPA-H is to drive breakthroughs in cancer, Alzheimer’s, diabetes, and more.
19 | SOURCES: 1-32
20 |
21 | ---------
22 |
23 | QUESTION: {question}
24 | =========
25 | {summaries}
26 | =========
27 | FINAL ANSWER:"""
28 |
29 | # STUFF_PROMPT = PromptTemplate(
30 | # template=template, input_variables=["summaries", "question"]
31 | # )
32 |
--------------------------------------------------------------------------------
/docker-compose.yml:
--------------------------------------------------------------------------------
1 | version: '3'
2 | services:
3 | langchain-chainlit-chat-app:
4 | image: pandasai-chainlit-chat-app:latest
5 | build: ./app
6 | command: chainlit run demo_app/main.py
7 | volumes:
8 | - ./demo_app/:/app/demo_app
9 | ports:
10 | - 8000:8000
11 |
--------------------------------------------------------------------------------
/poetry.toml:
--------------------------------------------------------------------------------
1 | [virtualenvs]
2 | in-project = true
3 | path = "."
4 |
--------------------------------------------------------------------------------
/pyproject.toml:
--------------------------------------------------------------------------------
1 | [tool.poetry]
2 | name = "pandasai-chainlit-docker-template"
3 | version = "0.1.0"
4 | description = "A template PandasAI Chainlit App with Docker Deployments"
5 | authors = ["Amjad Raza"]
6 | license = "MIT"
7 | readme = "README.md"
8 | packages = [{include = "pandasai_chainlit_docker_deployment_template"}]
9 |
10 |
11 | [tool.poetry.dependencies]
12 | python = "^3.10"
13 | openai = "^0.27.8"
14 | chainlit = "^0.3.0"
15 | pandasai = "^0.5.2"
16 |
17 |
18 | [build-system]
19 | requires = ["poetry-core"]
20 | build-backend = "poetry.core.masonry.api"
21 |
--------------------------------------------------------------------------------
/requirements.txt:
--------------------------------------------------------------------------------
1 | langchain == 0.0.184
2 | openai == 0.27.7
3 | streamlit == 1.22.0
4 | streamlit-chat == 0.0.2.2
5 |
--------------------------------------------------------------------------------
/ui.PNG:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/amjadraza/pandasai-chainlit-docker-deployment-template/f989b1f54f3fde7d43d259c024b9a9c7b1ca2d64/ui.PNG
--------------------------------------------------------------------------------