github-actions[bot] commited on
Commit
4f34e0f
0 Parent(s):

Sync to HuggingFace Spaces

Browse files
Files changed (12) hide show
  1. .dockerignore +5 -0
  2. .gitattributes +2 -0
  3. .github/workflows/hf-spaces.yml +31 -0
  4. .gitignore +152 -0
  5. Dockerfile +9 -0
  6. LICENSE +21 -0
  7. README.md +53 -0
  8. app.py +27 -0
  9. docker-compose.yml +10 -0
  10. poetry.lock +0 -0
  11. pyproject.toml +19 -0
  12. requirements.txt +65 -0
.dockerignore ADDED
@@ -0,0 +1,5 @@
 
 
 
 
 
 
1
+ .git/
2
+ .venv/
3
+ .github/
4
+
5
+ poetry.lock
.gitattributes ADDED
@@ -0,0 +1,2 @@
 
 
 
1
+ # Auto detect text files and perform LF normalization
2
+ * text=auto
.github/workflows/hf-spaces.yml ADDED
@@ -0,0 +1,31 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ name: Sync to Hugging Face Spaces
2
+
3
+ on:
4
+ push:
5
+ branches:
6
+ - main
7
+ workflow_dispatch:
8
+
9
+ jobs:
10
+ sync-to-spaces:
11
+ runs-on: ubuntu-latest
12
+ steps:
13
+ - uses: actions/checkout@v3
14
+ with:
15
+ fetch-depth: 0
16
+ lfs: true
17
+
18
+ - name: Sync to Hugging Face Spaces
19
+ uses: JacobLinCool/huggingface-sync@v1
20
+ with:
21
+ github: ${{ secrets.GITHUB_TOKEN }}
22
+ user: jacoblincool
23
+ space: tiktoken-calculator
24
+ token: ${{ secrets.HF_TOKEN }}
25
+ emoji: 🤗
26
+ colorFrom: yellow
27
+ colorTo: orange
28
+ sdk: gradio
29
+ app_file: app.py
30
+ python_version: 3.11
31
+ pinned: false
.gitignore ADDED
@@ -0,0 +1,152 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
+ # PEP 582; used by e.g. github.com/David-OConnor/pyflow
105
+ __pypackages__/
106
+
107
+ # Celery stuff
108
+ celerybeat-schedule
109
+ celerybeat.pid
110
+
111
+ # SageMath parsed files
112
+ *.sage.py
113
+
114
+ # Environments
115
+ .env
116
+ .venv
117
+ env/
118
+ venv/
119
+ ENV/
120
+ env.bak/
121
+ venv.bak/
122
+
123
+ # Spyder project settings
124
+ .spyderproject
125
+ .spyproject
126
+
127
+ # Rope project settings
128
+ .ropeproject
129
+
130
+ # mkdocs documentation
131
+ /site
132
+
133
+ # mypy
134
+ .mypy_cache/
135
+ .dmypy.json
136
+ dmypy.json
137
+
138
+ # Pyre type checker
139
+ .pyre/
140
+
141
+ # pytype static type analyzer
142
+ .pytype/
143
+
144
+ # Cython debug symbols
145
+ cython_debug/
146
+
147
+ # PyCharm
148
+ # JetBrains specific template is maintainted in a separate JetBrains.gitignore that can
149
+ # be found at https://github.com/github/gitignore/blob/main/Global/JetBrains.gitignore
150
+ # and can be added to the global gitignore or merged into this file. For a more nuclear
151
+ # option (not recommended) you can uncomment the following to ignore the entire idea folder.
152
+ #.idea/
Dockerfile ADDED
@@ -0,0 +1,9 @@
 
 
 
 
 
 
 
 
 
 
1
+ FROM python:3.11-slim
2
+
3
+ WORKDIR /app
4
+ COPY requirements.txt /app
5
+ RUN pip install -r requirements.txt
6
+
7
+ COPY . /app
8
+
9
+ CMD ["python", "app.py"]
LICENSE ADDED
@@ -0,0 +1,21 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ MIT License
2
+
3
+ Copyright (c) 2023 JacobLinCool
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 ADDED
@@ -0,0 +1,53 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ ---
2
+ title: Tiktoken Calculator
3
+ emoji: 🤗
4
+ colorFrom: yellow
5
+ colorTo: orange
6
+ sdk: gradio
7
+ python_version: 3.11
8
+ app_file: app.py
9
+ pinned: false
10
+ tags: ["gpt", "tiktoken", "tokenizer"]
11
+ ---
12
+
13
+ # Tiktoken Calculator
14
+
15
+ Calculate the token count for GPT-4, GPT-3.5, GPT-3, and GPT-2.
16
+
17
+ > It uses [openai/tiktoken](https://github.com/openai/tiktoken) to calculate the token count.
18
+
19
+ ## How to use
20
+
21
+ ### HuggingFace Spaces
22
+
23
+ Go to <https://huggingface.co/spaces/JacobLinCool/tiktoken-calculator> and try it out!
24
+
25
+ ### Docker
26
+
27
+ There is a pre-built Docker image available on Docker Hub: <https://hub.docker.com/r/jacoblincool/tiktoken-calculator>
28
+
29
+ ```bash
30
+ docker run -p 7860:7860 jacoblincool/tiktoken-calculator
31
+ ```
32
+
33
+ If you prefer to use Docker Compose, you can clone this repository and run:
34
+
35
+ ```bash
36
+ docker compose up -d
37
+ ```
38
+
39
+ ### Local
40
+
41
+ I use Poetry to manage dependencies.
42
+
43
+ Setup the virtual environment after cloning this repository:
44
+
45
+ ```bash
46
+ poetry install
47
+ ```
48
+
49
+ Then run the app:
50
+
51
+ ```bash
52
+ poetry run python app.py
53
+ ```
app.py ADDED
@@ -0,0 +1,27 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ import tiktoken
3
+
4
+ encodings = tiktoken.list_encoding_names()
5
+ encodings.reverse()
6
+
7
+
8
+ def calc(input: str, encoding: str) -> int:
9
+ tokens = tiktoken.get_encoding(encoding).encode(input)
10
+ return len(tokens)
11
+
12
+
13
+ gr.Interface(
14
+ calc,
15
+ inputs=[
16
+ gr.Text(label="Input", lines=6, placeholder="Enter text here..."),
17
+ gr.Dropdown(
18
+ label="Encoding",
19
+ choices=encodings,
20
+ value="cl100k_base",
21
+ info="The encoding to use. (GPT-3.5 and GPT-4 use cl100k_base as their encoding.)",
22
+ ),
23
+ ],
24
+ outputs=gr.Number(label="Output"),
25
+ title="Tiktoken Calculator",
26
+ allow_flagging="never",
27
+ ).launch()
docker-compose.yml ADDED
@@ -0,0 +1,10 @@
 
 
 
 
 
 
 
 
 
 
 
1
+ version: "3.9"
2
+
3
+ services:
4
+ app:
5
+ build: .
6
+ image: jacoblincool/tiktoken-calculator
7
+ ports:
8
+ - "7860:7860"
9
+ environment:
10
+ GRADIO_SERVER_NAME: "0.0.0.0"
poetry.lock ADDED
The diff for this file is too large to render. See raw diff
 
pyproject.toml ADDED
@@ -0,0 +1,19 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ [tool.poetry]
2
+ name = "tiktoken-calculator"
3
+ version = "0.0.0"
4
+ description = "Calculate the token count."
5
+ authors = ["JacobLinCool <[email protected]>"]
6
+ license = "MIT"
7
+ readme = "README.md"
8
+
9
+ [tool.poetry.dependencies]
10
+ python = "^3.11"
11
+ tiktoken = "^0.3.1"
12
+ gradio = "^3.21.0"
13
+
14
+ [tool.poetry.group.dev.dependencies]
15
+ black = "^23.1.0"
16
+
17
+ [build-system]
18
+ requires = ["poetry-core"]
19
+ build-backend = "poetry.core.masonry.api"
requirements.txt ADDED
@@ -0,0 +1,65 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ aiofiles==23.1.0 ; python_version >= "3.11" and python_version < "4.0"
2
+ aiohttp==3.8.4 ; python_version >= "3.11" and python_version < "4.0"
3
+ aiosignal==1.3.1 ; python_version >= "3.11" and python_version < "4.0"
4
+ altair==4.2.2 ; python_version >= "3.11" and python_version < "4.0"
5
+ anyio==3.6.2 ; python_version >= "3.11" and python_version < "4.0"
6
+ async-timeout==4.0.2 ; python_version >= "3.11" and python_version < "4.0"
7
+ attrs==22.2.0 ; python_version >= "3.11" and python_version < "4.0"
8
+ certifi==2022.12.7 ; python_version >= "3.11" and python_version < "4"
9
+ charset-normalizer==3.1.0 ; python_version >= "3.11" and python_version < "4"
10
+ click==8.1.3 ; python_version >= "3.11" and python_version < "4.0"
11
+ colorama==0.4.6 ; python_version >= "3.11" and python_version < "4.0" and platform_system == "Windows"
12
+ contourpy==1.0.7 ; python_version >= "3.11" and python_version < "4.0"
13
+ cycler==0.11.0 ; python_version >= "3.11" and python_version < "4.0"
14
+ entrypoints==0.4 ; python_version >= "3.11" and python_version < "4.0"
15
+ fastapi==0.94.1 ; python_version >= "3.11" and python_version < "4.0"
16
+ ffmpy==0.3.0 ; python_version >= "3.11" and python_version < "4.0"
17
+ filelock==3.9.1 ; python_version >= "3.11" and python_version < "4.0"
18
+ fonttools==4.39.0 ; python_version >= "3.11" and python_version < "4.0"
19
+ frozenlist==1.3.3 ; python_version >= "3.11" and python_version < "4.0"
20
+ fsspec==2023.3.0 ; python_version >= "3.11" and python_version < "4.0"
21
+ gradio==3.21.0 ; python_version >= "3.11" and python_version < "4.0"
22
+ h11==0.14.0 ; python_version >= "3.11" and python_version < "4.0"
23
+ httpcore==0.16.3 ; python_version >= "3.11" and python_version < "4.0"
24
+ httpx==0.23.3 ; python_version >= "3.11" and python_version < "4.0"
25
+ huggingface-hub==0.13.2 ; python_version >= "3.11" and python_version < "4.0"
26
+ idna==3.4 ; python_version >= "3.11" and python_version < "4"
27
+ jinja2==3.1.2 ; python_version >= "3.11" and python_version < "4.0"
28
+ jsonschema==4.17.3 ; python_version >= "3.11" and python_version < "4.0"
29
+ kiwisolver==1.4.4 ; python_version >= "3.11" and python_version < "4.0"
30
+ linkify-it-py==2.0.0 ; python_version >= "3.11" and python_version < "4.0"
31
+ markdown-it-py==2.2.0 ; python_version >= "3.11" and python_version < "4.0"
32
+ markdown-it-py[linkify]==2.2.0 ; python_version >= "3.11" and python_version < "4.0"
33
+ markupsafe==2.1.2 ; python_version >= "3.11" and python_version < "4.0"
34
+ matplotlib==3.7.1 ; python_version >= "3.11" and python_version < "4.0"
35
+ mdit-py-plugins==0.3.3 ; python_version >= "3.11" and python_version < "4.0"
36
+ mdurl==0.1.2 ; python_version >= "3.11" and python_version < "4.0"
37
+ multidict==6.0.4 ; python_version >= "3.11" and python_version < "4.0"
38
+ numpy==1.24.2 ; python_version >= "3.11" and python_version < "4.0"
39
+ orjson==3.8.7 ; python_version >= "3.11" and python_version < "4.0"
40
+ packaging==23.0 ; python_version >= "3.11" and python_version < "4.0"
41
+ pandas==1.5.3 ; python_version >= "3.11" and python_version < "4.0"
42
+ pillow==9.4.0 ; python_version >= "3.11" and python_version < "4.0"
43
+ pydantic==1.10.6 ; python_version >= "3.11" and python_version < "4.0"
44
+ pydub==0.25.1 ; python_version >= "3.11" and python_version < "4.0"
45
+ pyparsing==3.0.9 ; python_version >= "3.11" and python_version < "4.0"
46
+ pyrsistent==0.19.3 ; python_version >= "3.11" and python_version < "4.0"
47
+ python-dateutil==2.8.2 ; python_version >= "3.11" and python_version < "4.0"
48
+ python-multipart==0.0.6 ; python_version >= "3.11" and python_version < "4.0"
49
+ pytz==2022.7.1 ; python_version >= "3.11" and python_version < "4.0"
50
+ pyyaml==6.0 ; python_version >= "3.11" and python_version < "4.0"
51
+ regex==2022.10.31 ; python_version >= "3.11" and python_version < "4.0"
52
+ requests==2.28.2 ; python_version >= "3.11" and python_version < "4"
53
+ rfc3986[idna2008]==1.5.0 ; python_version >= "3.11" and python_version < "4.0"
54
+ six==1.16.0 ; python_version >= "3.11" and python_version < "4.0"
55
+ sniffio==1.3.0 ; python_version >= "3.11" and python_version < "4.0"
56
+ starlette==0.26.1 ; python_version >= "3.11" and python_version < "4.0"
57
+ tiktoken==0.3.1 ; python_version >= "3.11" and python_version < "4.0"
58
+ toolz==0.12.0 ; python_version >= "3.11" and python_version < "4.0"
59
+ tqdm==4.65.0 ; python_version >= "3.11" and python_version < "4.0"
60
+ typing-extensions==4.5.0 ; python_version >= "3.11" and python_version < "4.0"
61
+ uc-micro-py==1.0.1 ; python_version >= "3.11" and python_version < "4.0"
62
+ urllib3==1.26.15 ; python_version >= "3.11" and python_version < "4"
63
+ uvicorn==0.21.0 ; python_version >= "3.11" and python_version < "4.0"
64
+ websockets==10.4 ; python_version >= "3.11" and python_version < "4.0"
65
+ yarl==1.8.2 ; python_version >= "3.11" and python_version < "4.0"