Spaces:
Runtime error
Runtime error
Markus Sverdvik Heiervang
commited on
Commit
β’
2f35e98
1
Parent(s):
717888f
Get next token on guess
Browse files
app.py
CHANGED
@@ -1,47 +1,52 @@
|
|
1 |
-
# from transformers import AutoTokenizer, AutoModelForCausalLM
|
2 |
import gradio as gr
|
3 |
import torch
|
4 |
from transformers import AutoModelForCausalLM
|
5 |
from transformers import AutoTokenizer
|
6 |
|
7 |
-
|
8 |
-
|
9 |
-
|
10 |
-
|
11 |
-
|
12 |
-
|
13 |
-
Senate.
|
14 |
-
Obama received national attention in 2004 with his March Senate primary win, his well-received July Democratic National Convention keynote address, and his landslide November election to the Senate.
|
15 |
-
In 2008, after a close primary campaign against Hillary Clinton, he was nominated by the Democratic Party for president and chose Joe Biden as his running mate.
|
16 |
-
Obama was elected over Republican nominee John McCain in the presidential election and was inaugurated on January 20, 2009.
|
17 |
-
Nine months later, he was named the 2009 Nobel Peace Prize laureate, a decision that drew a mixture of praise and criticism.
|
18 |
-
"""
|
19 |
|
20 |
|
21 |
tokenizer = AutoTokenizer.from_pretrained("gpt2")
|
22 |
model = AutoModelForCausalLM.from_pretrained("gpt2")
|
23 |
model.eval()
|
24 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
25 |
|
26 |
-
def get_next_word(text: str) -> str:
|
27 |
-
inputs = tokenizer(text, return_tensors="pt")
|
28 |
with torch.no_grad():
|
29 |
logits = model(**inputs).logits
|
30 |
|
31 |
last_token = logits[0, -1]
|
32 |
top_3 = torch.topk(last_token, 3)
|
33 |
-
input_ids = list(inputs.input_ids.squeeze())
|
34 |
-
argmax = top_3.indices[0]
|
35 |
-
input_ids.append(argmax)
|
36 |
-
return "!!!", tokenizer.decode(input_ids)
|
37 |
|
|
|
38 |
|
39 |
def build_demo():
|
40 |
with gr.Blocks() as demo:
|
41 |
gr.Markdown("<h1><center>Can you beat a language model?</center></h1>")
|
42 |
|
43 |
with gr.Row():
|
44 |
-
prompt_text = gr.Markdown(
|
45 |
with gr.Row():
|
46 |
with gr.Column():
|
47 |
guess = gr.Textbox(label="Guess!")
|
@@ -49,11 +54,7 @@ def build_demo():
|
|
49 |
with gr.Column():
|
50 |
lm_guess = gr.Textbox(label="LM guess")
|
51 |
|
52 |
-
guess_btn.click(
|
53 |
-
# examples = gr.Examples(
|
54 |
-
# examples=["I went to the supermarket yesterday.", "Helen is a good swimmer."], inputs=[guess]
|
55 |
-
# )
|
56 |
-
|
57 |
return demo
|
58 |
|
59 |
|
@@ -67,8 +68,8 @@ def wip_sign():
|
|
67 |
|
68 |
|
69 |
def main():
|
70 |
-
# demo = build_demo()
|
71 |
demo = wip_sign()
|
|
|
72 |
demo.launch(debug=True)
|
73 |
|
74 |
|
|
|
|
|
1 |
import gradio as gr
|
2 |
import torch
|
3 |
from transformers import AutoModelForCausalLM
|
4 |
from transformers import AutoTokenizer
|
5 |
|
6 |
+
from state import ProgramState
|
7 |
+
from text import get_text
|
8 |
+
|
9 |
+
STATE = ProgramState(
|
10 |
+
current_token=20,
|
11 |
+
)
|
|
|
|
|
|
|
|
|
|
|
|
|
12 |
|
13 |
|
14 |
tokenizer = AutoTokenizer.from_pretrained("gpt2")
|
15 |
model = AutoModelForCausalLM.from_pretrained("gpt2")
|
16 |
model.eval()
|
17 |
|
18 |
+
all_tokens = tokenizer.encode(get_text())
|
19 |
+
|
20 |
+
|
21 |
+
def handle_guess(text: str) -> str:
|
22 |
+
"""
|
23 |
+
Retreives
|
24 |
+
"""
|
25 |
+
STATE.current_token += 1
|
26 |
+
decoded_str = tokenizer.decode(all_tokens[:STATE.current_token])
|
27 |
+
return decoded_str, ""
|
28 |
+
|
29 |
+
|
30 |
+
def get_model_predictions(input_text: str) -> torch.Tensor:
|
31 |
+
"""
|
32 |
+
Returns the indices as a torch tensor of the top 3 predicted tokens.
|
33 |
+
"""
|
34 |
+
inputs = tokenizer(input_text, return_tensors="pt")
|
35 |
|
|
|
|
|
36 |
with torch.no_grad():
|
37 |
logits = model(**inputs).logits
|
38 |
|
39 |
last_token = logits[0, -1]
|
40 |
top_3 = torch.topk(last_token, 3)
|
|
|
|
|
|
|
|
|
41 |
|
42 |
+
return top_3
|
43 |
|
44 |
def build_demo():
|
45 |
with gr.Blocks() as demo:
|
46 |
gr.Markdown("<h1><center>Can you beat a language model?</center></h1>")
|
47 |
|
48 |
with gr.Row():
|
49 |
+
prompt_text = gr.Markdown()
|
50 |
with gr.Row():
|
51 |
with gr.Column():
|
52 |
guess = gr.Textbox(label="Guess!")
|
|
|
54 |
with gr.Column():
|
55 |
lm_guess = gr.Textbox(label="LM guess")
|
56 |
|
57 |
+
guess_btn.click(handle_guess, inputs=guess, outputs=[prompt_text, lm_guess], api_name="get_next_word")
|
|
|
|
|
|
|
|
|
58 |
return demo
|
59 |
|
60 |
|
|
|
68 |
|
69 |
|
70 |
def main():
|
|
|
71 |
demo = wip_sign()
|
72 |
+
# demo = build_demo()
|
73 |
demo.launch(debug=True)
|
74 |
|
75 |
|
state.py
ADDED
@@ -0,0 +1,10 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from dataclasses import dataclass
|
2 |
+
|
3 |
+
|
4 |
+
@dataclass
|
5 |
+
class ProgramState:
|
6 |
+
current_token: int
|
7 |
+
# full_text: str
|
8 |
+
|
9 |
+
def get_text(self):
|
10 |
+
pass
|
text.py
ADDED
@@ -0,0 +1,16 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
target_text = """\
|
2 |
+
Obama was born in Honolulu, Hawaii.
|
3 |
+
After graduating from Columbia University in 1983, he worked as a community organizer in Chicago.
|
4 |
+
In 1988, he enrolled in Harvard Law School, where he was the first black president of the Harvard Law Review.
|
5 |
+
After graduating, he became a civil rights attorney and an academic, teaching constitutional law at the University of Chicago Law School from 1992 to 2004.
|
6 |
+
Turning to elective politics, he represented the 13th district in the Illinois Senate from 1997 until 2004, when he ran for the U.S.
|
7 |
+
Senate.
|
8 |
+
Obama received national attention in 2004 with his March Senate primary win, his well-received July Democratic National Convention keynote address, and his landslide November election to the Senate.
|
9 |
+
In 2008, after a close primary campaign against Hillary Clinton, he was nominated by the Democratic Party for president and chose Joe Biden as his running mate.
|
10 |
+
Obama was elected over Republican nominee John McCain in the presidential election and was inaugurated on January 20, 2009.
|
11 |
+
Nine months later, he was named the 2009 Nobel Peace Prize laureate, a decision that drew a mixture of praise and criticism.
|
12 |
+
"""
|
13 |
+
|
14 |
+
|
15 |
+
def get_text():
|
16 |
+
return target_text
|