Spaces:
Running
Running
Commit
·
7780b98
1
Parent(s):
55a2b5a
Add app
Browse files- Dockerfile +28 -0
- LICENSE +21 -0
- README.md +1 -0
- app.py +56 -0
- requirements.txt +3 -0
Dockerfile
ADDED
@@ -0,0 +1,28 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
FROM python:3.11
|
2 |
+
|
3 |
+
# Set up a new user named "user" with user ID 1000
|
4 |
+
RUN useradd -m -u 1000 user
|
5 |
+
|
6 |
+
# Switch to the "user" user
|
7 |
+
USER user
|
8 |
+
|
9 |
+
# Set home to the user's home directory
|
10 |
+
ENV HOME=/home/user \
|
11 |
+
PATH=/home/user/.local/bin:$PATH
|
12 |
+
|
13 |
+
# Set the working directory to the user's home directory
|
14 |
+
WORKDIR $HOME/app
|
15 |
+
|
16 |
+
# Try and run pip command after setting the user with `USER user` to avoid permission issues with Python
|
17 |
+
RUN pip install --no-cache-dir --upgrade pip
|
18 |
+
|
19 |
+
# Copy the current directory contents into the container at $HOME/app setting the owner to the user
|
20 |
+
COPY --chown=user . $HOME/app
|
21 |
+
|
22 |
+
COPY --chown=user requirements.txt .
|
23 |
+
|
24 |
+
RUN pip install --no-cache-dir --upgrade -r requirements.txt
|
25 |
+
|
26 |
+
COPY --chown=user app.py .
|
27 |
+
|
28 |
+
ENTRYPOINT ["solara", "run", "app.py", "--host=0.0.0.0", "--port", "7860"]
|
LICENSE
ADDED
@@ -0,0 +1,21 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
MIT License
|
2 |
+
|
3 |
+
Copyright (c) 2023 Alonso Silva Allende
|
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
CHANGED
@@ -6,6 +6,7 @@ colorTo: blue
|
|
6 |
sdk: docker
|
7 |
pinned: false
|
8 |
license: mit
|
|
|
9 |
---
|
10 |
|
11 |
Check out the configuration reference at https://huggingface.co/docs/hub/spaces-config-reference
|
|
|
6 |
sdk: docker
|
7 |
pinned: false
|
8 |
license: mit
|
9 |
+
app_port: 7860
|
10 |
---
|
11 |
|
12 |
Check out the configuration reference at https://huggingface.co/docs/hub/spaces-config-reference
|
app.py
ADDED
@@ -0,0 +1,56 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import pandas as pd
|
2 |
+
import random
|
3 |
+
import solara
|
4 |
+
from transformers import AutoTokenizer, AutoModelForCausalLM
|
5 |
+
|
6 |
+
tokenizer = AutoTokenizer.from_pretrained('gpt2')
|
7 |
+
model = AutoModelForCausalLM.from_pretrained('gpt2')
|
8 |
+
text = solara.reactive("Example text is here")
|
9 |
+
text2 = solara.reactive("")
|
10 |
+
text3 = solara.reactive("")
|
11 |
+
|
12 |
+
# Create dataframe mapping token IDs and tokens
|
13 |
+
df = pd.DataFrame()
|
14 |
+
df["token ID"] = range(50257)
|
15 |
+
df["token"] = [tokenizer.decode([i]) for i in range(50257)]
|
16 |
+
|
17 |
+
@solara.component
|
18 |
+
def Page():
|
19 |
+
with solara.Column(margin=10):
|
20 |
+
solara.Markdown("#GPT token encoder and decoder")
|
21 |
+
solara.InputText("Enter text to tokenize it:", value=text, continuous_update=True)
|
22 |
+
tokens = tokenizer.encode(text.value, return_tensors="pt")
|
23 |
+
spans = ""
|
24 |
+
spans1 = ""
|
25 |
+
for i, token in enumerate(tokens[0]):
|
26 |
+
random.seed(i)
|
27 |
+
random_color = ''.join([random.choice('0123456789ABCDEF') for k in range(6)])
|
28 |
+
spans += " " + f"<span style='font-family: cursive;color: #{random_color}'>{token.numpy()}</span>"
|
29 |
+
spans1 += " " + f"""<span style="
|
30 |
+
padding: 5px;
|
31 |
+
border-right: 3px solid white;
|
32 |
+
line-height: 3em;
|
33 |
+
font-family: courier;
|
34 |
+
background-color: #{random_color};
|
35 |
+
color: white;
|
36 |
+
position: relative;
|
37 |
+
"><span style="position: absolute; top: 5.5ch; line-height: 1em; left: -0.5px; font-size: 0.45em">{token.numpy()}</span>{tokenizer.decode(token)}</span>"""
|
38 |
+
solara.Markdown(f"{spans}")
|
39 |
+
if len(tokens[0]) == 1:
|
40 |
+
solara.Markdown(f"{len(tokens[0])} token")
|
41 |
+
else:
|
42 |
+
solara.Markdown(f"{len(tokens[0])} tokens")
|
43 |
+
solara.Markdown(f'{spans1}')
|
44 |
+
solara.InputText("Or convert space separated tokens to text:", value=text2, continuous_update=True)
|
45 |
+
spans2 = text2.value.split(' ')
|
46 |
+
spans2 = [int(span) for span in spans2 if span != ""]
|
47 |
+
spans2 = tokenizer.decode(spans2)
|
48 |
+
solara.Markdown(f'{spans2}')
|
49 |
+
solara.Markdown("##Search tokens")
|
50 |
+
solara.InputText("Search for a token:", value=text3, continuous_update=True)
|
51 |
+
df_subset = df[df["token"].str.startswith(text3.value)]
|
52 |
+
solara.Markdown(f"{df_subset.shape[0]:,} results")
|
53 |
+
solara.DataFrame(df_subset, items_per_page=10)
|
54 |
+
|
55 |
+
Page()
|
56 |
+
|
requirements.txt
ADDED
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
1 |
+
solara
|
2 |
+
pandas
|
3 |
+
transformers[torch]
|