Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
@@ -1,96 +1,43 @@
|
|
1 |
-
Hugging Face's logo
|
2 |
-
Hugging Face
|
3 |
-
Search models, datasets, users...
|
4 |
-
Models
|
5 |
-
Datasets
|
6 |
-
Spaces
|
7 |
-
Posts
|
8 |
-
Docs
|
9 |
-
Pricing
|
10 |
-
|
11 |
-
|
12 |
-
|
13 |
-
Spaces:
|
14 |
-
|
15 |
-
alonsosilva
|
16 |
-
/
|
17 |
-
NextTokenPrediction
|
18 |
-
|
19 |
-
|
20 |
-
like
|
21 |
-
3
|
22 |
-
App
|
23 |
-
Files
|
24 |
-
Community
|
25 |
-
NextTokenPrediction
|
26 |
-
/
|
27 |
-
app.py
|
28 |
-
|
29 |
-
alonsosilva's picture
|
30 |
-
alonsosilva
|
31 |
-
Change reactive text
|
32 |
-
a4869ab
|
33 |
-
7 months ago
|
34 |
-
raw
|
35 |
-
history
|
36 |
-
blame
|
37 |
-
contribute
|
38 |
-
delete
|
39 |
-
No virus
|
40 |
-
2.63 kB
|
41 |
import solara
|
42 |
-
import random
|
43 |
import torch
|
44 |
import torch.nn.functional as F
|
45 |
import pandas as pd
|
46 |
from transformers import AutoTokenizer, AutoModelForCausalLM
|
47 |
|
48 |
-
|
49 |
-
|
50 |
-
|
|
|
|
|
|
|
|
|
51 |
@solara.component
|
52 |
def Page():
|
53 |
-
|
54 |
-
|
55 |
-
|
56 |
-
|
57 |
-
|
58 |
-
|
59 |
-
|
60 |
-
|
61 |
-
|
62 |
-
|
63 |
-
|
64 |
-
|
65 |
-
|
66 |
-
|
67 |
-
|
68 |
-
|
69 |
-
|
70 |
-
|
71 |
-
|
72 |
-
|
73 |
-
|
74 |
-
|
75 |
-
|
76 |
-
|
77 |
-
|
78 |
-
|
79 |
-
|
80 |
-
|
81 |
-
font-size: 0.45em"> {token}</span>{tokenizer.decode([token])}</span>"""
|
82 |
-
solara.Markdown(f'{spans2}')
|
83 |
-
solara.Markdown(f'{spans1}')
|
84 |
-
outputs = model.generate(tokens, max_new_tokens=1, output_scores=True, return_dict_in_generate=True, pad_token_id=tokenizer.eos_token_id)
|
85 |
-
scores = F.softmax(outputs.scores[0], dim=-1)
|
86 |
-
top_10 = torch.topk(scores, 10)
|
87 |
-
df = pd.DataFrame()
|
88 |
-
df["probs"] = top_10.values[0]
|
89 |
-
df["probs"] = [f"{value:.2%}" for value in df["probs"].values]
|
90 |
-
df["next token ID"] = [top_10.indices[0][i].numpy() for i in range(10)]
|
91 |
-
df["predicted next token"] = [tokenizer.decode(top_10.indices[0][i]) for i in range(10)]
|
92 |
-
solara.Markdown("###Prediction")
|
93 |
-
solara.DataFrame(df, items_per_page=10, cell_actions=cell_actions)
|
94 |
-
Page()
|
95 |
-
|
96 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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 |
+
# Cargar el modelo y el tokenizer
|
8 |
+
model_name = "datificate/gpt2-small-spanish"
|
9 |
+
model = AutoModelForCausalLM.from_pretrained(model_name)
|
10 |
+
tokenizer = AutoTokenizer.from_pretrained(model_name)
|
11 |
+
|
12 |
+
text = solara.reactive("Escribe algo en español")
|
13 |
+
|
14 |
@solara.component
|
15 |
def Page():
|
16 |
+
with solara.Column(margin=10):
|
17 |
+
solara.Markdown("# Predicción del Próximo Token")
|
18 |
+
solara.Markdown("Ingrese un texto en español y vea las predicciones para el próximo token.")
|
19 |
+
|
20 |
+
def on_action_cell(column, row_index):
|
21 |
+
text.value += tokenizer.decode(top_10.indices[0][row_index])
|
22 |
+
|
23 |
+
cell_actions = [solara.CellAction(icon="mdi-thumb-up", name="Seleccionar", on_click=on_action_cell)]
|
24 |
+
|
25 |
+
solara.InputText("Ingrese texto:", value=text, continuous_update=True, on_change=lambda v: text.set(v))
|
26 |
+
|
27 |
+
if text.value != "":
|
28 |
+
tokens = tokenizer.encode(text.value, return_tensors="pt")
|
29 |
+
outputs = model.generate(tokens, max_new_tokens=1, output_scores=True, return_dict_in_generate=True, pad_token_id=tokenizer.eos_token_id)
|
30 |
+
scores = F.softmax(outputs.scores[0], dim=-1)
|
31 |
+
top_10 = torch.topk(scores, 10)
|
32 |
+
|
33 |
+
df = pd.DataFrame({
|
34 |
+
"probs": [f"{value:.2%}" for value in top_10.values[0]],
|
35 |
+
"next token ID": top_10.indices[0].numpy(),
|
36 |
+
"predicted next token": [tokenizer.decode([idx]) for idx in top_10.indices[0]]
|
37 |
+
})
|
38 |
+
|
39 |
+
solara.Markdown("### Predicción")
|
40 |
+
solara.DataFrame(df, items_per_page=10, cell_actions=cell_actions)
|
41 |
+
|
42 |
+
# Ejecutar la aplicación
|
43 |
+
Page()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|