Spaces:
Runtime error
Runtime error
kmacdermid
commited on
Commit
β’
dd51c9f
1
Parent(s):
9b526d3
Second version of the app and small cleanup
Browse files
app.py
CHANGED
@@ -1,46 +1,71 @@
|
|
1 |
"""
|
2 |
-
|
3 |
-
|
4 |
|
5 |
https://huggingface.co/blog/how-to-generate
|
6 |
"""
|
7 |
import gradio as gr
|
|
|
8 |
import transformers as tr
|
9 |
|
|
|
|
|
|
|
|
|
10 |
SPECIAL_TOKENS = {
|
11 |
'eos_token': '<|EOS|>',
|
12 |
'bos_token': '<|endoftext|>',
|
13 |
'pad_token': '<pad>',
|
14 |
'sep_token': '<|body|>'
|
15 |
}
|
16 |
-
MPATH = "./mdl_roomgen2"
|
17 |
-
MODEL = tr.GPT2LMHeadModel.from_pretrained(MPATH)
|
18 |
-
|
19 |
-
# ToDo: Will save tokenizer next time so can replace this with a load
|
20 |
TOK = tr.GPT2Tokenizer.from_pretrained("gpt2")
|
21 |
TOK.add_special_tokens(SPECIAL_TOKENS)
|
22 |
|
23 |
|
24 |
-
|
25 |
-
""
|
26 |
-
|
27 |
-
|
28 |
-
|
29 |
-
|
30 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
31 |
|
32 |
-
Returns:
|
33 |
|
|
|
|
|
|
|
|
|
34 |
"""
|
35 |
prompt = " ".join(
|
36 |
[
|
37 |
SPECIAL_TOKENS["bos_token"],
|
38 |
room_name,
|
39 |
-
SPECIAL_TOKENS["sep_token"]
|
|
|
40 |
]
|
41 |
)
|
|
|
|
|
|
|
42 |
ids = TOK.encode(prompt, return_tensors="pt")
|
|
|
43 |
# Sample
|
|
|
|
|
|
|
44 |
output = MODEL.generate(
|
45 |
ids,
|
46 |
max_length=max_length,
|
@@ -49,7 +74,7 @@ def generate_room(room_name, max_length, top_k, temperature, top_p):
|
|
49 |
temperature=temperature,
|
50 |
top_p=top_p
|
51 |
)
|
52 |
-
output = TOK.decode(output[0][
|
53 |
return output
|
54 |
|
55 |
|
@@ -58,15 +83,14 @@ if __name__ == "__main__":
|
|
58 |
title="RPG Room Generator",
|
59 |
fn=generate_room,
|
60 |
inputs=[
|
61 |
-
|
62 |
-
|
63 |
-
|
64 |
-
|
65 |
-
gr.inputs.Slider(minimum=0, maximum=1.0, default=0.5, step=0.1, label="Top P")
|
66 |
],
|
67 |
outputs="text",
|
68 |
layout="horizontal",
|
69 |
-
allow_flagging=
|
70 |
-
theme="dark"
|
71 |
)
|
72 |
-
app, local_url, share_url = iface.launch(
|
|
|
1 |
"""
|
2 |
+
RPG Room Generator App
|
3 |
+
Sets the sampling parameters and provides minimal interface to the user
|
4 |
|
5 |
https://huggingface.co/blog/how-to-generate
|
6 |
"""
|
7 |
import gradio as gr
|
8 |
+
from gradio import inputs # allows easier doc lookup in Pycharm
|
9 |
import transformers as tr
|
10 |
|
11 |
+
MPATH = "./models/mdl_roomgen2"
|
12 |
+
MODEL = tr.GPT2LMHeadModel.from_pretrained(MPATH)
|
13 |
+
|
14 |
+
# ToDo: Will save tokenizer next time so can replace this with a load
|
15 |
SPECIAL_TOKENS = {
|
16 |
'eos_token': '<|EOS|>',
|
17 |
'bos_token': '<|endoftext|>',
|
18 |
'pad_token': '<pad>',
|
19 |
'sep_token': '<|body|>'
|
20 |
}
|
|
|
|
|
|
|
|
|
21 |
TOK = tr.GPT2Tokenizer.from_pretrained("gpt2")
|
22 |
TOK.add_special_tokens(SPECIAL_TOKENS)
|
23 |
|
24 |
|
25 |
+
SAMPLING_OPTIONS = {
|
26 |
+
"Reasonable":
|
27 |
+
{
|
28 |
+
"top_k": 25,
|
29 |
+
"temperature": 50,
|
30 |
+
"top_p": 60
|
31 |
+
},
|
32 |
+
"Odd":
|
33 |
+
{
|
34 |
+
"top_k": 50,
|
35 |
+
"temperature": 75,
|
36 |
+
"top_p": 90
|
37 |
+
},
|
38 |
+
"Insane":
|
39 |
+
{
|
40 |
+
"top_k": 300,
|
41 |
+
"temperature": 100,
|
42 |
+
"top_p": 85
|
43 |
+
},
|
44 |
+
}
|
45 |
|
|
|
46 |
|
47 |
+
def generate_room(room_name, room_desc, max_length, sampling_method):
|
48 |
+
"""
|
49 |
+
Uses pretrained model to generate text for a dungeon room
|
50 |
+
Returns: Room description text
|
51 |
"""
|
52 |
prompt = " ".join(
|
53 |
[
|
54 |
SPECIAL_TOKENS["bos_token"],
|
55 |
room_name,
|
56 |
+
SPECIAL_TOKENS["sep_token"],
|
57 |
+
room_desc
|
58 |
]
|
59 |
)
|
60 |
+
# Only want to skip the room name part
|
61 |
+
to_skip = TOK.encode(" ".join([SPECIAL_TOKENS["bos_token"], room_name, SPECIAL_TOKENS["sep_token"]]),
|
62 |
+
return_tensors="pt")
|
63 |
ids = TOK.encode(prompt, return_tensors="pt")
|
64 |
+
|
65 |
# Sample
|
66 |
+
top_k = SAMPLING_OPTIONS[sampling_method]["top_k"]
|
67 |
+
temperature = SAMPLING_OPTIONS[sampling_method]["temperature"] / 100.
|
68 |
+
top_p = SAMPLING_OPTIONS[sampling_method]["top_p"] / 100.
|
69 |
output = MODEL.generate(
|
70 |
ids,
|
71 |
max_length=max_length,
|
|
|
74 |
temperature=temperature,
|
75 |
top_p=top_p
|
76 |
)
|
77 |
+
output = TOK.decode(output[0][to_skip.shape[1]:], clean_up_tokenization_spaces=True).replace(" ", " ")
|
78 |
return output
|
79 |
|
80 |
|
|
|
83 |
title="RPG Room Generator",
|
84 |
fn=generate_room,
|
85 |
inputs=[
|
86 |
+
inputs.Textbox(lines=1, label="Room Name"),
|
87 |
+
inputs.Textbox(lines=3, label="Start of Room Description (Optional)", default=""),
|
88 |
+
inputs.Slider(minimum=50, maximum=250, default=200, label="Length"),
|
89 |
+
inputs.Radio(choices=list(SAMPLING_OPTIONS.keys()), default="Odd", label="Craziness"),
|
|
|
90 |
],
|
91 |
outputs="text",
|
92 |
layout="horizontal",
|
93 |
+
allow_flagging="never",
|
94 |
+
theme="dark",
|
95 |
)
|
96 |
+
app, local_url, share_url = iface.launch()
|
{mdl_roomgen2 β models/mdl_roomgen2}/config.json
RENAMED
File without changes
|
{mdl_roomgen2 β models/mdl_roomgen2}/pytorch_model.bin
RENAMED
File without changes
|