Spaces:
Running
Running
alonsosilva
commited on
Commit
•
691f4df
1
Parent(s):
fd20ecb
Add app
Browse files- Dockerfile +11 -0
- LICENSE +21 -0
- app.py +28 -0
- requirements.txt +4 -0
Dockerfile
ADDED
@@ -0,0 +1,11 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
FROM python:3.11
|
2 |
+
|
3 |
+
WORKDIR /code
|
4 |
+
|
5 |
+
COPY ./requirements.txt /code/requirements.txt
|
6 |
+
|
7 |
+
RUN pip install --no-cache-dir --upgrade -r /code/requirements.txt
|
8 |
+
|
9 |
+
COPY . .
|
10 |
+
|
11 |
+
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.
|
app.py
ADDED
@@ -0,0 +1,28 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import solara
|
2 |
+
import torch
|
3 |
+
import torch.nn.functional as F
|
4 |
+
import pandas as pd
|
5 |
+
from transformers import AutoTokenizer, AutoModelForCausalLM
|
6 |
+
|
7 |
+
tokenizer = AutoTokenizer.from_pretrained('gpt2')
|
8 |
+
model = AutoModelForCausalLM.from_pretrained('gpt2')
|
9 |
+
text1 = solara.reactive("Alan Turing theorized that computers would one day become")
|
10 |
+
@solara.component
|
11 |
+
def Page():
|
12 |
+
with solara.Card(margin=0):
|
13 |
+
solara.Markdown("#Next token prediction visualization")
|
14 |
+
def on_action_cell(column, row_index):
|
15 |
+
text1.value += tokenizer.decode(top_10.indices[0][row_index])
|
16 |
+
cell_actions = [solara.CellAction(icon="mdi-thumb-up", name="Select", on_click=on_action_cell)]
|
17 |
+
solara.InputText("Enter text:", value=text1, continuous_update=True)
|
18 |
+
if text1.value != "":
|
19 |
+
tokens = tokenizer.encode(text1.value, return_tensors="pt")
|
20 |
+
outputs = model.generate(tokens, max_new_tokens=1, output_scores=True, return_dict_in_generate=True, pad_token_id=tokenizer.eos_token_id)
|
21 |
+
scores = F.softmax(outputs.scores[0], dim=-1)
|
22 |
+
top_10 = torch.topk(scores, 10)
|
23 |
+
df = pd.DataFrame()
|
24 |
+
df["probs"] = top_10.values[0]
|
25 |
+
df["probs"] = [f"{value:.2%}" for value in df["probs"].values]
|
26 |
+
df["predicted next token"] = [tokenizer.decode(top_10.indices[0][i]) for i in range(10)]
|
27 |
+
solara.DataFrame(df, items_per_page=10, cell_actions=cell_actions)
|
28 |
+
Page()
|
requirements.txt
ADDED
@@ -0,0 +1,4 @@
|
|
|
|
|
|
|
|
|
|
|
1 |
+
solara
|
2 |
+
pandas
|
3 |
+
torch
|
4 |
+
transformers
|