Spaces:
Runtime error
Runtime error
Commit
·
bd9d551
1
Parent(s):
ea4e004
Doing sample only version instead
Browse files
app.py
CHANGED
|
@@ -1,3 +1,9 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
import transformers as tr
|
| 3 |
|
|
@@ -15,7 +21,7 @@ TOK = tr.GPT2Tokenizer.from_pretrained("gpt2")
|
|
| 15 |
TOK.add_special_tokens(SPECIAL_TOKENS)
|
| 16 |
|
| 17 |
|
| 18 |
-
def generate_room(room_name,
|
| 19 |
"""
|
| 20 |
Uses pretrained model to generate text for a dungeon room
|
| 21 |
|
|
@@ -34,34 +40,15 @@ def generate_room(room_name, method, max_length):
|
|
| 34 |
]
|
| 35 |
)
|
| 36 |
ids = TOK.encode(prompt, return_tensors="pt")
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
)
|
| 47 |
-
elif method == "Sample":
|
| 48 |
-
# Sample
|
| 49 |
-
output = MODEL.generate(
|
| 50 |
-
ids,
|
| 51 |
-
max_length=max_length,
|
| 52 |
-
do_sample=True,
|
| 53 |
-
top_k=0
|
| 54 |
-
)
|
| 55 |
-
elif method == "Top K":
|
| 56 |
-
# Top K - redistribute probability over top words, see Fan et al. 2018
|
| 57 |
-
output = MODEL.generate(
|
| 58 |
-
ids,
|
| 59 |
-
max_length=max_length,
|
| 60 |
-
do_sample=True,
|
| 61 |
-
top_k=50
|
| 62 |
-
)
|
| 63 |
-
else:
|
| 64 |
-
raise ValueError(f"Unexpected generation method, received {method}")
|
| 65 |
output = TOK.decode(output[0][ids.shape[1]:], clean_up_tokenization_spaces=True).replace(" ", " ")
|
| 66 |
return output
|
| 67 |
|
|
@@ -71,14 +58,15 @@ if __name__ == "__main__":
|
|
| 71 |
title="RPG Room Generator",
|
| 72 |
fn=generate_room,
|
| 73 |
inputs=[
|
| 74 |
-
gr.inputs.Textbox(lines=1),
|
| 75 |
-
gr.inputs.
|
| 76 |
-
gr.inputs.Slider(minimum=
|
|
|
|
|
|
|
| 77 |
],
|
| 78 |
outputs="text",
|
| 79 |
-
examples=[["Dark Catacombs", "Top K", 150]],
|
| 80 |
layout="horizontal",
|
| 81 |
allow_flagging=None,
|
| 82 |
theme="dark"
|
| 83 |
)
|
| 84 |
-
app, local_url, share_url = iface.launch()
|
|
|
|
| 1 |
+
"""
|
| 2 |
+
DnD Room Generator App
|
| 3 |
+
Only uses sampling, not beams or greedy
|
| 4 |
+
|
| 5 |
+
https://huggingface.co/blog/how-to-generate
|
| 6 |
+
"""
|
| 7 |
import gradio as gr
|
| 8 |
import transformers as tr
|
| 9 |
|
|
|
|
| 21 |
TOK.add_special_tokens(SPECIAL_TOKENS)
|
| 22 |
|
| 23 |
|
| 24 |
+
def generate_room(room_name, max_length, top_k, temperature, top_p):
|
| 25 |
"""
|
| 26 |
Uses pretrained model to generate text for a dungeon room
|
| 27 |
|
|
|
|
| 40 |
]
|
| 41 |
)
|
| 42 |
ids = TOK.encode(prompt, return_tensors="pt")
|
| 43 |
+
# Sample
|
| 44 |
+
output = MODEL.generate(
|
| 45 |
+
ids,
|
| 46 |
+
max_length=max_length,
|
| 47 |
+
do_sample=True,
|
| 48 |
+
top_k=top_k,
|
| 49 |
+
temperature=temperature,
|
| 50 |
+
top_p=top_p
|
| 51 |
+
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 52 |
output = TOK.decode(output[0][ids.shape[1]:], clean_up_tokenization_spaces=True).replace(" ", " ")
|
| 53 |
return output
|
| 54 |
|
|
|
|
| 58 |
title="RPG Room Generator",
|
| 59 |
fn=generate_room,
|
| 60 |
inputs=[
|
| 61 |
+
gr.inputs.Textbox(lines=1, label="Room Name"),
|
| 62 |
+
gr.inputs.Slider(minimum=50, maximum=250, default=175, label="Length"),
|
| 63 |
+
gr.inputs.Slider(minimum=0, maximum=100, default=50, label="Top K"),
|
| 64 |
+
gr.inputs.Slider(minimum=0, maximum=1.0, default=0.7, step=0.1, label="Temperature"),
|
| 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=None,
|
| 70 |
theme="dark"
|
| 71 |
)
|
| 72 |
+
app, local_url, share_url = iface.launch(share=True)
|