├── src ├── .example.env └── main.py ├── .streamlit └── config.toml ├── requirements.txt ├── docs └── application_image.jpg ├── .devcontainer ├── Dockerfile └── devcontainer.json ├── LICENSE ├── README.md └── .gitignore /src/.example.env: -------------------------------------------------------------------------------- 1 | OPENAI_API_KEY=yourapikey -------------------------------------------------------------------------------- /.streamlit/config.toml: -------------------------------------------------------------------------------- 1 | [server] 2 | enableCORS = false 3 | enableXsrfProtection = false -------------------------------------------------------------------------------- /requirements.txt: -------------------------------------------------------------------------------- 1 | altair==5.1 2 | streamlit==1.26 3 | watchdog==3.0 4 | openai==0.27 5 | python-decouple==3.8 6 | -------------------------------------------------------------------------------- /docs/application_image.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/joeychrys/streamlit-chatGPT/HEAD/docs/application_image.jpg -------------------------------------------------------------------------------- /.devcontainer/Dockerfile: -------------------------------------------------------------------------------- 1 | # See here for image contents: https://github.com/microsoft/vscode-dev-containers/tree/v0.209.6/containers/python-3/.devcontainer/base.Dockerfile 2 | 3 | # [Choice] Python version (use -bullseye variants on local arm64/Apple Silicon): 3, 3.10, 3.9, 3.8, 3.7, 3.6, 3-bullseye, 3.10-bullseye, 3.9-bullseye, 3.8-bullseye, 3.7-bullseye, 3.6-bullseye, 3-buster, 3.10-buster, 3.9-buster, 3.8-buster, 3.7-buster, 3.6-buster 4 | ARG VARIANT="3.10-bullseye" 5 | FROM mcr.microsoft.com/vscode/devcontainers/python:0-${VARIANT} 6 | 7 | ENV PATH="/home/vscode/.local/bin:${PATH}" 8 | 9 | # [Optional] Uncomment this section to install additional OS packages. 10 | # RUN apt-get update && export DEBIAN_FRONTEND=noninteractive \ 11 | # && apt-get -y install --no-install-recommends -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2023 Joey Chrysanthopoulos 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. -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Streamlit + ChatGPT 2 | 3 | ![alt text](https://github.com/joeychrys/streamlit-chatGPT/blob/master/docs/application_image.jpg?raw=true) 4 | ## Overview 5 | 6 | This is a sample Streamlit application that demonstrates how to use Streamlit as a UI with OpenAI's Chat API. 7 | 8 | ## Installation 9 | 10 | 1. Clone the repository 11 | 12 | ```bash 13 | git clone https://github.com/joeychrys/streamlit-chatGPT.git 14 | ``` 15 | 16 | 2. Create a `.env` file based on the `.env.example` file inside the `src` directory and add your `OPENAI_API_KEY` 17 | 18 | ```bash 19 | OPENAI_API_KEY=yourapikey 20 | ``` 21 | 22 | 3. Create and activate a new virtual environment 23 | 24 | ```bash 25 | python -m venv env 26 | source env/bin/activate 27 | ``` 28 | 4. Move to project directory 29 | 30 | ```bash 31 | cd streamlit-chatGPT 32 | ``` 33 | 34 | 5. Install the required packages 35 | 36 | ```bash 37 | pip install -r requirements.txt 38 | ``` 39 | 40 | 6. Run the Streamlit application 41 | 42 | ```bash 43 | streamlit run src/main.py 44 | ``` 45 | 46 | ## Usage 47 | 48 | Once the application is running, you can interact with it by following the on-screen instructions at `http://localhost:8501` 49 | 50 | ## Contributing 51 | 52 | Contributions are welcome! Please feel free to submit a Pull Request. 53 | 54 | ## License 55 | 56 | This project is licensed under the MIT License 57 | -------------------------------------------------------------------------------- /.devcontainer/devcontainer.json: -------------------------------------------------------------------------------- 1 | // For format details, see https://aka.ms/devcontainer.json. For config options, see the README at: 2 | // https://github.com/microsoft/vscode-dev-containers/tree/v0.209.6/containers/python-3 3 | { 4 | "name": "Python 3", 5 | "build": { 6 | "dockerfile": "Dockerfile", 7 | "context": "..", 8 | "args": { 9 | // Update 'VARIANT' to pick a Python version: 3, 3.10, 3.9, 3.8, 3.7, 3.6 10 | // Append -bullseye or -buster to pin to an OS version. 11 | // Use -bullseye variants on local on arm64/Apple Silicon. 12 | "VARIANT": "3.9", 13 | } 14 | }, 15 | // Set *default* container specific settings.json values on container create. 16 | "settings": { 17 | "python.defaultInterpreterPath": "/usr/local/bin/python", 18 | }, 19 | // Add the IDs of extensions you want installed when the container is created. 20 | "extensions": [ 21 | "ms-python.python", 22 | "ms-python.vscode-pylance" 23 | ], 24 | // Use 'forwardPorts' to make a list of ports inside the container available locally. 25 | "forwardPorts": [ 26 | 8501 27 | ], 28 | // Use 'postCreateCommand' to run commands after the container is created. 29 | // Install NPM dependencies. 30 | "postCreateCommand": "pip install -U streamlit", 31 | // Comment out connect as root instead. More info: https://aka.ms/vscode-remote/containers/non-root. 32 | "remoteUser": "vscode", 33 | "features": { 34 | "git": "latest", 35 | "github-cli": "latest" 36 | } 37 | } -------------------------------------------------------------------------------- /src/main.py: -------------------------------------------------------------------------------- 1 | import streamlit as st 2 | from decouple import config 3 | import openai 4 | 5 | response = False 6 | prompt_tokens = 0 7 | completion_tokes = 0 8 | total_tokens_used = 0 9 | cost_of_response = 0 10 | 11 | API_KEY = config('OPENAI_API_KEY') 12 | openai.api_key = API_KEY 13 | 14 | 15 | def make_request(question_input: str): 16 | response = openai.ChatCompletion.create( 17 | model="gpt-3.5-turbo", 18 | messages=[ 19 | {"role": "system", "content": f"{question_input}"}, 20 | ] 21 | ) 22 | return response 23 | 24 | 25 | st.header("Streamlit + OpenAI ChatGPT API") 26 | 27 | st.markdown("""---""") 28 | 29 | question_input = st.text_input("Enter question") 30 | rerun_button = st.button("Rerun") 31 | 32 | st.markdown("""---""") 33 | 34 | if question_input: 35 | response = make_request(question_input) 36 | else: 37 | pass 38 | 39 | if rerun_button: 40 | response = make_request(question_input) 41 | else: 42 | pass 43 | 44 | if response: 45 | st.write("Response:") 46 | st.write(response["choices"][0]["message"]["content"]) 47 | 48 | prompt_tokens = response["usage"]["prompt_tokens"] 49 | completion_tokes = response["usage"]["completion_tokens"] 50 | total_tokens_used = response["usage"]["total_tokens"] 51 | 52 | cost_of_response = total_tokens_used * 0.000002 53 | else: 54 | pass 55 | 56 | 57 | with st.sidebar: 58 | st.title("Usage Stats:") 59 | st.markdown("""---""") 60 | st.write("Promt tokens used :", prompt_tokens) 61 | st.write("Completion tokens used :", completion_tokes) 62 | st.write("Total tokens used :", total_tokens_used) 63 | st.write("Total cost of request: ${:.8f}".format(cost_of_response)) 64 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | # Byte-compiled / optimized / DLL files 2 | __pycache__/ 3 | *.py[cod] 4 | *$py.class 5 | 6 | # C extensions 7 | *.so 8 | 9 | # Distribution / packaging 10 | .Python 11 | build/ 12 | develop-eggs/ 13 | dist/ 14 | downloads/ 15 | eggs/ 16 | .eggs/ 17 | lib/ 18 | lib64/ 19 | parts/ 20 | sdist/ 21 | var/ 22 | wheels/ 23 | share/python-wheels/ 24 | *.egg-info/ 25 | .installed.cfg 26 | *.egg 27 | MANIFEST 28 | 29 | # PyInstaller 30 | # Usually these files are written by a python script from a template 31 | # before PyInstaller builds the exe, so as to inject date/other infos into it. 32 | *.manifest 33 | *.spec 34 | 35 | # Installer logs 36 | pip-log.txt 37 | pip-delete-this-directory.txt 38 | 39 | # Unit test / coverage reports 40 | htmlcov/ 41 | .tox/ 42 | .nox/ 43 | .coverage 44 | .coverage.* 45 | .cache 46 | nosetests.xml 47 | coverage.xml 48 | *.cover 49 | *.py,cover 50 | .hypothesis/ 51 | .pytest_cache/ 52 | cover/ 53 | 54 | # Translations 55 | *.mo 56 | *.pot 57 | 58 | # Django stuff: 59 | *.log 60 | local_settings.py 61 | db.sqlite3 62 | db.sqlite3-journal 63 | 64 | # Flask stuff: 65 | instance/ 66 | .webassets-cache 67 | 68 | # Scrapy stuff: 69 | .scrapy 70 | 71 | # Sphinx documentation 72 | docs/_build/ 73 | 74 | # PyBuilder 75 | .pybuilder/ 76 | target/ 77 | 78 | # Jupyter Notebook 79 | .ipynb_checkpoints 80 | 81 | # IPython 82 | profile_default/ 83 | ipython_config.py 84 | 85 | # pyenv 86 | # For a library or package, you might want to ignore these files since the code is 87 | # intended to run in multiple environments; otherwise, check them in: 88 | # .python-version 89 | 90 | # pipenv 91 | # According to pypa/pipenv#598, it is recommended to include Pipfile.lock in version control. 92 | # However, in case of collaboration, if having platform-specific dependencies or dependencies 93 | # having no cross-platform support, pipenv may install dependencies that don't work, or not 94 | # install all needed dependencies. 95 | #Pipfile.lock 96 | 97 | # poetry 98 | # Similar to Pipfile.lock, it is generally recommended to include poetry.lock in version control. 99 | # This is especially recommended for binary packages to ensure reproducibility, and is more 100 | # commonly ignored for libraries. 101 | # https://python-poetry.org/docs/basic-usage/#commit-your-poetrylock-file-to-version-control 102 | #poetry.lock 103 | 104 | # pdm 105 | # Similar to Pipfile.lock, it is generally recommended to include pdm.lock in version control. 106 | #pdm.lock 107 | # pdm stores project-wide configurations in .pdm.toml, but it is recommended to not include it 108 | # in version control. 109 | # https://pdm.fming.dev/#use-with-ide 110 | .pdm.toml 111 | 112 | # PEP 582; used by e.g. github.com/David-OConnor/pyflow and github.com/pdm-project/pdm 113 | __pypackages__/ 114 | 115 | # Celery stuff 116 | celerybeat-schedule 117 | celerybeat.pid 118 | 119 | # SageMath parsed files 120 | *.sage.py 121 | 122 | # Environments 123 | .env 124 | .env.* 125 | !.env.example 126 | .venv 127 | env/ 128 | venv/ 129 | ENV/ 130 | env.bak/ 131 | venv.bak/ 132 | 133 | # Spyder project settings 134 | .spyderproject 135 | .spyproject 136 | 137 | # Rope project settings 138 | .ropeproject 139 | 140 | # mkdocs documentation 141 | /site 142 | 143 | # mypy 144 | .mypy_cache/ 145 | .dmypy.json 146 | dmypy.json 147 | 148 | # Pyre type checker 149 | .pyre/ 150 | 151 | # pytype static type analyzer 152 | .pytype/ 153 | 154 | # Cython debug symbols 155 | cython_debug/ 156 | 157 | # PyCharm 158 | # JetBrains specific template is maintained in a separate JetBrains.gitignore that can 159 | # be found at https://github.com/github/gitignore/blob/main/Global/JetBrains.gitignore 160 | # and can be added to the global gitignore or merged into this file. For a more nuclear 161 | # option (not recommended) you can uncomment the following to ignore the entire idea folder. 162 | #.idea/ --------------------------------------------------------------------------------