marksverdhei commited on
Commit
bd88c29
β€’
1 Parent(s): ab14957
Files changed (4) hide show
  1. .pre-commit-config.yaml +27 -0
  2. app.py +76 -0
  3. pyproject.toml +9 -0
  4. requirements.txt +3 -0
.pre-commit-config.yaml ADDED
@@ -0,0 +1,27 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ repos:
2
+ - repo: https://github.com/pre-commit/pre-commit-hooks
3
+ rev: v4.3.0
4
+ hooks:
5
+ - id: trailing-whitespace
6
+ - id: requirements-txt-fixer
7
+
8
+ - repo: https://github.com/PyCQA/flake8
9
+ rev: 6.0.0
10
+ hooks:
11
+ - id: flake8
12
+ args: [
13
+ # E501: line too long
14
+ "--ignore", "E501",
15
+ # "--max-line-length", "120",
16
+ ]
17
+
18
+ - repo: https://github.com/psf/black
19
+ rev: 22.12.0
20
+ hooks:
21
+ - id: black
22
+
23
+ - repo: https://github.com/pycqa/isort
24
+ rev: 5.11.4
25
+ hooks:
26
+ - id: isort
27
+ name: isort (python)
app.py ADDED
@@ -0,0 +1,76 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
+ target_text = """\
8
+ Obama was born in Honolulu, Hawaii.
9
+ After graduating from Columbia University in 1983, he worked as a community organizer in Chicago.
10
+ In 1988, he enrolled in Harvard Law School, where he was the first black president of the Harvard Law Review.
11
+ 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.
12
+ Turning to elective politics, he represented the 13th district in the Illinois Senate from 1997 until 2004, when he ran for the U.S.
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(target_text)
45
+ with gr.Row():
46
+ with gr.Column():
47
+ guess = gr.Textbox(label="Guess!")
48
+ guess_btn = gr.Button(value="Guess!")
49
+ with gr.Column():
50
+ lm_guess = gr.Textbox(label="LM guess")
51
+
52
+ guess_btn.click(get_next_word, inputs=guess, outputs=[prompt_text, lm_guess], api_name="get_next_word")
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
+
60
+ def wip_sign():
61
+ with gr.Blocks() as demo:
62
+ gr.Markdown("<h1><center>Can you beat a language model?</center></h1>")
63
+ with gr.Row():
64
+ gr.Markdown("<h1><center>β›”πŸ‘·β€β™‚οΈ Work in progress, come back later </center></h1>")
65
+
66
+ return demo
67
+
68
+
69
+ def main():
70
+ # demo = build_demo()
71
+ demo = wip_sign()
72
+ demo.launch(debug=True)
73
+
74
+
75
+ if __name__ == "__main__":
76
+ main()
pyproject.toml ADDED
@@ -0,0 +1,9 @@
 
 
 
 
 
 
 
 
 
 
1
+ [tool.black]
2
+ line-length = 120
3
+
4
+ [tool.isort]
5
+ atomic = true
6
+ profile = "black"
7
+ line_length = 120
8
+ force_single_line = true
9
+ known_first_party = ["black"]
requirements.txt ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ gradio==3.16.2
2
+ torch==1.13.1
3
+ transformers==4.25.1