Spaces:
Runtime error
Runtime error
Commit
·
296e17f
1
Parent(s):
522d3ea
First version
Browse files- app.py +79 -0
- mdl_roomgen2/config.json +41 -0
- mdl_roomgen2/pytorch_model.bin +3 -0
- requirements.txt +10 -0
app.py
ADDED
|
@@ -0,0 +1,79 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
import transformers as tr
|
| 3 |
+
from src.models.fine_tune_gpt2 import SPECIAL_TOKENS
|
| 4 |
+
|
| 5 |
+
MPATH = "./mdl_roomgen2"
|
| 6 |
+
MODEL = tr.GPT2LMHeadModel.from_pretrained(MPATH)
|
| 7 |
+
|
| 8 |
+
# ToDo: Will save tokenizer next time so can replace this with a load
|
| 9 |
+
TOK = tr.GPT2Tokenizer.from_pretrained("gpt2")
|
| 10 |
+
TOK.add_special_tokens(SPECIAL_TOKENS)
|
| 11 |
+
|
| 12 |
+
|
| 13 |
+
def generate_room(room_name, method, max_length):
|
| 14 |
+
"""
|
| 15 |
+
Uses pretrained model to generate text for a dungeon room
|
| 16 |
+
|
| 17 |
+
Args:
|
| 18 |
+
room_name:
|
| 19 |
+
method:
|
| 20 |
+
|
| 21 |
+
Returns:
|
| 22 |
+
|
| 23 |
+
"""
|
| 24 |
+
prompt = " ".join(
|
| 25 |
+
[
|
| 26 |
+
SPECIAL_TOKENS["bos_token"],
|
| 27 |
+
room_name,
|
| 28 |
+
SPECIAL_TOKENS["sep_token"]
|
| 29 |
+
]
|
| 30 |
+
)
|
| 31 |
+
ids = TOK.encode(prompt, return_tensors="pt")
|
| 32 |
+
if method == "Greedy":
|
| 33 |
+
output = MODEL.generate(ids, max_length=max_length)
|
| 34 |
+
elif method == "Beam":
|
| 35 |
+
output = MODEL.generate(
|
| 36 |
+
ids,
|
| 37 |
+
max_length=max_length,
|
| 38 |
+
num_beams=5,
|
| 39 |
+
no_repeat_ngram_size=4,
|
| 40 |
+
early_stopping=True
|
| 41 |
+
)
|
| 42 |
+
elif method == "Sample":
|
| 43 |
+
# Sample
|
| 44 |
+
output = MODEL.generate(
|
| 45 |
+
ids,
|
| 46 |
+
max_length=max_length,
|
| 47 |
+
do_sample=True,
|
| 48 |
+
top_k=0
|
| 49 |
+
)
|
| 50 |
+
elif method == "Top K":
|
| 51 |
+
# Top K - redistribute probability over top words, see Fan et al. 2018
|
| 52 |
+
output = MODEL.generate(
|
| 53 |
+
ids,
|
| 54 |
+
max_length=max_length,
|
| 55 |
+
do_sample=True,
|
| 56 |
+
top_k=50
|
| 57 |
+
)
|
| 58 |
+
else:
|
| 59 |
+
raise ValueError(f"Unexpected generation method, received {method}")
|
| 60 |
+
output = TOK.decode(output[0][ids.shape[1]:], clean_up_tokenization_spaces=True).replace(" ", " ")
|
| 61 |
+
return output
|
| 62 |
+
|
| 63 |
+
|
| 64 |
+
if __name__ == "__main__":
|
| 65 |
+
iface = gr.Interface(
|
| 66 |
+
title="RPG Room Generator",
|
| 67 |
+
fn=generate_room,
|
| 68 |
+
inputs=[
|
| 69 |
+
gr.inputs.Textbox(lines=1),
|
| 70 |
+
gr.inputs.Radio(["Greedy", "Beam", "Sample", "Top K"], default="Top K"),
|
| 71 |
+
gr.inputs.Slider(minimum=50, maximum=250, default=100)
|
| 72 |
+
],
|
| 73 |
+
outputs="text",
|
| 74 |
+
examples=[["Dark Catacombs", "Top K", 150]],
|
| 75 |
+
layout="horizontal",
|
| 76 |
+
allow_flagging=None,
|
| 77 |
+
theme="dark"
|
| 78 |
+
)
|
| 79 |
+
app, local_url, share_url = iface.launch()
|
mdl_roomgen2/config.json
ADDED
|
@@ -0,0 +1,41 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
{
|
| 2 |
+
"_name_or_path": "gpt2",
|
| 3 |
+
"activation_function": "gelu_new",
|
| 4 |
+
"architectures": [
|
| 5 |
+
"GPT2LMHeadModel"
|
| 6 |
+
],
|
| 7 |
+
"attn_pdrop": 0.1,
|
| 8 |
+
"bos_token_id": 50256,
|
| 9 |
+
"embd_pdrop": 0.1,
|
| 10 |
+
"eos_token_id": 50257,
|
| 11 |
+
"initializer_range": 0.02,
|
| 12 |
+
"layer_norm_epsilon": 1e-05,
|
| 13 |
+
"model_type": "gpt2",
|
| 14 |
+
"n_ctx": 1024,
|
| 15 |
+
"n_embd": 768,
|
| 16 |
+
"n_head": 12,
|
| 17 |
+
"n_inner": null,
|
| 18 |
+
"n_layer": 12,
|
| 19 |
+
"n_positions": 1024,
|
| 20 |
+
"pad_token_id": 50258,
|
| 21 |
+
"reorder_and_upcast_attn": false,
|
| 22 |
+
"resid_pdrop": 0.1,
|
| 23 |
+
"scale_attn_by_inverse_layer_idx": false,
|
| 24 |
+
"scale_attn_weights": true,
|
| 25 |
+
"sep_token_id": 50259,
|
| 26 |
+
"summary_activation": null,
|
| 27 |
+
"summary_first_dropout": 0.1,
|
| 28 |
+
"summary_proj_to_labels": true,
|
| 29 |
+
"summary_type": "cls_index",
|
| 30 |
+
"summary_use_proj": true,
|
| 31 |
+
"task_specific_params": {
|
| 32 |
+
"text-generation": {
|
| 33 |
+
"do_sample": true,
|
| 34 |
+
"max_length": 50
|
| 35 |
+
}
|
| 36 |
+
},
|
| 37 |
+
"torch_dtype": "float32",
|
| 38 |
+
"transformers_version": "4.17.0",
|
| 39 |
+
"use_cache": true,
|
| 40 |
+
"vocab_size": 50260
|
| 41 |
+
}
|
mdl_roomgen2/pytorch_model.bin
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:82f9411bb82b07718b5c1dcf60326fde7e2ba639d880f96b05e0a6db58e06b3d
|
| 3 |
+
size 510413609
|
requirements.txt
ADDED
|
@@ -0,0 +1,10 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
datasets >= 1.8.0
|
| 2 |
+
transformers >= 4.17.0
|
| 3 |
+
bs4~=4.10.0
|
| 4 |
+
pandas~=1.4.1
|
| 5 |
+
requests~=2.27.1
|
| 6 |
+
beautifulsoup4~=4.8.2
|
| 7 |
+
tqdm~=4.63.0
|
| 8 |
+
wandb~=0.12.13
|
| 9 |
+
scikit-learn~=1.0.2
|
| 10 |
+
gradio~=2.9.1
|