JohanBeytell commited on
Commit
5066051
·
verified ·
1 Parent(s): b7710e7

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +32 -18
app.py CHANGED
@@ -4,6 +4,7 @@ import torch.nn as nn
4
  import re, unicodedata, random
5
  from pathlib import Path
6
  import pandas as pd
 
7
 
8
  # === Constants and Config ===
9
  DEVICE = 'cuda' if torch.cuda.is_available() else 'cpu'
@@ -17,7 +18,7 @@ stoi, itos = ckpt['stoi'], ckpt['itos']
17
  SPECIAL = ['<pad>', '<bos>', '<eos>', '<sep>']
18
  PAD, BOS, EOS, SEP = [stoi[s] for s in SPECIAL]
19
  VOCAB_SIZE = len(itos)
20
- MAX_LEN = 128 # match training
21
 
22
  # === Model ===
23
  class GPTSmall(nn.Module):
@@ -72,33 +73,46 @@ def sample_once(prompt, temperature=1.0, top_k=40, max_new=24):
72
  if idx == EOS:
73
  break
74
  seq.append(idx)
75
-
76
  generated = [itos[i] for i in seq if i not in {BOS, SEP, EOS, PAD}]
77
  name = ''.join(generated).replace(prompt, "").strip()
78
  return clean_name(name)
79
 
80
- # === Gradio UI ===
81
- def generate_ui(prompt, temperature, top_k, count):
82
  results = []
83
  for _ in range(count):
84
- name = sample_once(prompt, temperature=temperature, top_k=top_k)
85
- results.append({"Generated Name": name})
86
- return pd.DataFrame(results)
 
 
 
 
 
 
 
 
 
 
 
87
 
88
- description = """🎭 **Fantasy Name Generator**
89
- Give it a prompt like `a forgotten warrior king` or `mistress of the black swamp` and it'll generate creative fantasy-style names.
90
- This model is trained from scratch and runs entirely on PyTorch."""
91
 
92
  with gr.Blocks() as demo:
93
  gr.Markdown(description)
94
  with gr.Row():
95
- prompt = gr.Textbox(label="Prompt", placeholder="e.g. 'a villain who whispers to shadows'", lines=1)
96
- with gr.Row():
97
- temperature = gr.Slider(0.1, 1.5, step=0.1, value=1.0, label="Temperature")
98
- top_k = gr.Slider(10, 100, step=10, value=40, label="Top-K")
99
- count = gr.Slider(1, 5, step=1, value=3, label="Names to Generate")
100
- generate_btn = gr.Button("Generate Names")
101
- output = gr.Dataframe(headers=["Generated Name"], datatype="str", label="Generated Fantasy Names", interactive=False)
102
- generate_btn.click(fn=generate_ui, inputs=[prompt, temperature, top_k, count], outputs=output)
 
 
 
 
103
 
104
  demo.launch()
 
4
  import re, unicodedata, random
5
  from pathlib import Path
6
  import pandas as pd
7
+ import tempfile
8
 
9
  # === Constants and Config ===
10
  DEVICE = 'cuda' if torch.cuda.is_available() else 'cpu'
 
18
  SPECIAL = ['<pad>', '<bos>', '<eos>', '<sep>']
19
  PAD, BOS, EOS, SEP = [stoi[s] for s in SPECIAL]
20
  VOCAB_SIZE = len(itos)
21
+ MAX_LEN = 128
22
 
23
  # === Model ===
24
  class GPTSmall(nn.Module):
 
73
  if idx == EOS:
74
  break
75
  seq.append(idx)
 
76
  generated = [itos[i] for i in seq if i not in {BOS, SEP, EOS, PAD}]
77
  name = ''.join(generated).replace(prompt, "").strip()
78
  return clean_name(name)
79
 
80
+ # === Generation Function ===
81
+ def generate_names(prompt, temperature, top_k, count, retries):
82
  results = []
83
  for _ in range(count):
84
+ for attempt in range(retries):
85
+ name = sample_once(prompt, temperature=temperature, top_k=top_k)
86
+ if len(name) >= 4:
87
+ results.append({"Generated Name": name})
88
+ break
89
+ df = pd.DataFrame(results)
90
+ file_path = tempfile.NamedTemporaryFile(delete=False, suffix=".txt").name
91
+ df.to_csv(file_path, index=False, header=False)
92
+ return df, file_path
93
+
94
+ # === UI ===
95
+ description = """# 🧠 Kaos: A Fantasy Name Generator
96
+ Kaos is a small GPT-style transformer trained from scratch using character-level tokenization.
97
+ It excels at fantasy and mythic naming conventions.
98
 
99
+ 💡 Give it a prompt like `'a forgotten warrior king'`, `'priestess of the dusk sea'`, or `'demon of frost'`.
100
+ The model will generate fitting fantasy-style names, ready for your characters, worlds, or gods."""
 
101
 
102
  with gr.Blocks() as demo:
103
  gr.Markdown(description)
104
  with gr.Row():
105
+ with gr.Column():
106
+ prompt = gr.Textbox(label="Prompt", placeholder="e.g. 'a villain who whispers to shadows'")
107
+ temperature = gr.Slider(0.1, 1.5, step=0.1, value=1.0, label="Temperature")
108
+ top_k = gr.Slider(10, 100, step=10, value=40, label="Top-K Sampling")
109
+ count = gr.Slider(1, 20, step=1, value=5, label="Names to Generate")
110
+ retries = gr.Slider(1, 5, step=1, value=3, label="Max Retries per Name")
111
+ generate_btn = gr.Button("🎲 Generate Names")
112
+ with gr.Column():
113
+ output = gr.Dataframe(headers=["Generated Name"], datatype="str", label="Generated Names", interactive=False)
114
+ download = gr.File(label="📥 Export Names as .txt")
115
+
116
+ generate_btn.click(fn=generate_names, inputs=[prompt, temperature, top_k, count, retries], outputs=[output, download])
117
 
118
  demo.launch()