Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,3 +1,4 @@
|
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
import torch
|
| 3 |
import torch.nn as nn
|
|
@@ -10,10 +11,10 @@ import os
|
|
| 10 |
|
| 11 |
DEVICE = 'cuda' if torch.cuda.is_available() else 'cpu'
|
| 12 |
SEED = 1337
|
|
|
|
|
|
|
| 13 |
torch.manual_seed(SEED)
|
| 14 |
random.seed(SEED)
|
| 15 |
-
|
| 16 |
-
# Log model load details
|
| 17 |
print(f"π¦ Model loading on: {DEVICE}")
|
| 18 |
|
| 19 |
ckpt = torch.load("kaos.pt", map_location=DEVICE)
|
|
@@ -46,7 +47,6 @@ model = GPTSmall(VOCAB_SIZE).to(DEVICE)
|
|
| 46 |
model.load_state_dict(ckpt['model'])
|
| 47 |
model.eval()
|
| 48 |
|
| 49 |
-
# === Utility Functions ===
|
| 50 |
def proper_case(text):
|
| 51 |
return re.sub(r"\b(of|the|and|in|on|a)\b", lambda m: m.group(0).lower(), text.title())
|
| 52 |
|
|
@@ -62,6 +62,7 @@ def clean_name(text, title_case=True, max_repeats=2):
|
|
| 62 |
return re.sub(r"([a-zA-Z])'S\b", lambda m: m.group(1) + "'s", text)
|
| 63 |
|
| 64 |
def sample_once(prompt, temperature=1.0, top_k=40, max_new=40):
|
|
|
|
| 65 |
seq = [BOS] + [stoi.get(c, PAD) for c in prompt] + [SEP]
|
| 66 |
for _ in range(max_new):
|
| 67 |
x = torch.tensor(seq[-MAX_LEN:], dtype=torch.long, device=DEVICE)[None]
|
|
@@ -77,9 +78,14 @@ def sample_once(prompt, temperature=1.0, top_k=40, max_new=40):
|
|
| 77 |
seq.append(idx)
|
| 78 |
generated = [itos[i] for i in seq if i not in {BOS, SEP, EOS, PAD}]
|
| 79 |
name = ''.join(generated).replace(prompt, "").strip()
|
| 80 |
-
return clean_name(name)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 81 |
|
| 82 |
-
def generate_names(prompt, temperature, top_k, count, retries):
|
| 83 |
prompt = prompt.strip()
|
| 84 |
if not prompt:
|
| 85 |
raise gr.Error("Prompt cannot be empty.")
|
|
@@ -89,25 +95,26 @@ def generate_names(prompt, temperature, top_k, count, retries):
|
|
| 89 |
results = []
|
| 90 |
rejected = []
|
| 91 |
retry_count = 0
|
|
|
|
| 92 |
|
| 93 |
for _ in range(count):
|
| 94 |
for attempt in range(retries):
|
| 95 |
-
name = sample_once(prompt, temperature=temperature, top_k=top_k)
|
| 96 |
retry_count += 1
|
| 97 |
if len(name) >= 3:
|
| 98 |
-
results.append({"Generated Name": name})
|
|
|
|
| 99 |
break
|
| 100 |
else:
|
| 101 |
rejected.append(name)
|
| 102 |
|
| 103 |
df = pd.DataFrame(results)
|
| 104 |
file_path = tempfile.NamedTemporaryFile(delete=False, suffix=".txt").name
|
| 105 |
-
df.to_csv(file_path, index=False, header=False)
|
| 106 |
-
|
| 107 |
-
retry_report = f"Total Retries
|
| 108 |
-
return df, file_path, retry_report
|
| 109 |
|
| 110 |
-
# === Gradio UI ===
|
| 111 |
description = """# KaosGen: A Fantasy Name Generator
|
| 112 |
`Kaos` is a small GPT-style transformer (~890k parameters) trained from scratch using character-level tokenization.
|
| 113 |
It excels at fantasy and mythic naming conventions.
|
|
@@ -124,13 +131,15 @@ with gr.Blocks() as demo:
|
|
| 124 |
top_k = gr.Slider(10, 100, step=10, value=40, label="Top-K Sampling")
|
| 125 |
count = gr.Slider(1, 20, step=1, value=5, label="Names to Generate")
|
| 126 |
retries = gr.Slider(1, 5, step=1, value=3, label="Max Retries per Name")
|
|
|
|
|
|
|
| 127 |
generate_btn = gr.Button("π² Generate Names")
|
| 128 |
with gr.Column():
|
| 129 |
-
output = gr.Dataframe(headers=["Generated Name"], datatype="str", label="Generated Names", interactive=False)
|
| 130 |
download = gr.File(label="π₯ Export Names as .txt")
|
| 131 |
-
retry_report = gr.
|
| 132 |
|
| 133 |
-
generate_btn.click(fn=generate_names, inputs=[prompt, temperature, top_k, count, retries], outputs=[
|
| 134 |
gr.Examples(examples=examples, inputs=prompt)
|
| 135 |
|
| 136 |
demo.launch()
|
|
|
|
| 1 |
+
# === ADDITIONAL UI FEEDBACK + SEED + TIMING ===
|
| 2 |
import gradio as gr
|
| 3 |
import torch
|
| 4 |
import torch.nn as nn
|
|
|
|
| 11 |
|
| 12 |
DEVICE = 'cuda' if torch.cuda.is_available() else 'cpu'
|
| 13 |
SEED = 1337
|
| 14 |
+
|
| 15 |
+
# === Model Loading Diagnostics ===
|
| 16 |
torch.manual_seed(SEED)
|
| 17 |
random.seed(SEED)
|
|
|
|
|
|
|
| 18 |
print(f"π¦ Model loading on: {DEVICE}")
|
| 19 |
|
| 20 |
ckpt = torch.load("kaos.pt", map_location=DEVICE)
|
|
|
|
| 47 |
model.load_state_dict(ckpt['model'])
|
| 48 |
model.eval()
|
| 49 |
|
|
|
|
| 50 |
def proper_case(text):
|
| 51 |
return re.sub(r"\b(of|the|and|in|on|a)\b", lambda m: m.group(0).lower(), text.title())
|
| 52 |
|
|
|
|
| 62 |
return re.sub(r"([a-zA-Z])'S\b", lambda m: m.group(1) + "'s", text)
|
| 63 |
|
| 64 |
def sample_once(prompt, temperature=1.0, top_k=40, max_new=40):
|
| 65 |
+
start_time = time.time()
|
| 66 |
seq = [BOS] + [stoi.get(c, PAD) for c in prompt] + [SEP]
|
| 67 |
for _ in range(max_new):
|
| 68 |
x = torch.tensor(seq[-MAX_LEN:], dtype=torch.long, device=DEVICE)[None]
|
|
|
|
| 78 |
seq.append(idx)
|
| 79 |
generated = [itos[i] for i in seq if i not in {BOS, SEP, EOS, PAD}]
|
| 80 |
name = ''.join(generated).replace(prompt, "").strip()
|
| 81 |
+
return clean_name(name), time.time() - start_time
|
| 82 |
+
|
| 83 |
+
def generate_names(prompt, temperature, top_k, count, retries, seed, randomize_seed):
|
| 84 |
+
if randomize_seed:
|
| 85 |
+
seed = random.randint(1, 999999)
|
| 86 |
+
torch.manual_seed(seed)
|
| 87 |
+
random.seed(seed)
|
| 88 |
|
|
|
|
| 89 |
prompt = prompt.strip()
|
| 90 |
if not prompt:
|
| 91 |
raise gr.Error("Prompt cannot be empty.")
|
|
|
|
| 95 |
results = []
|
| 96 |
rejected = []
|
| 97 |
retry_count = 0
|
| 98 |
+
timings = []
|
| 99 |
|
| 100 |
for _ in range(count):
|
| 101 |
for attempt in range(retries):
|
| 102 |
+
name, t = sample_once(prompt, temperature=temperature, top_k=top_k)
|
| 103 |
retry_count += 1
|
| 104 |
if len(name) >= 3:
|
| 105 |
+
results.append({"Generated Name": name, "Time (s)": f"{t:.2f}"})
|
| 106 |
+
timings.append(t)
|
| 107 |
break
|
| 108 |
else:
|
| 109 |
rejected.append(name)
|
| 110 |
|
| 111 |
df = pd.DataFrame(results)
|
| 112 |
file_path = tempfile.NamedTemporaryFile(delete=False, suffix=".txt").name
|
| 113 |
+
df[["Generated Name"]].to_csv(file_path, index=False, header=False)
|
| 114 |
+
|
| 115 |
+
retry_report = f"## Debug Report\n\n- **Total Retries:** {retry_count - len(results)}\n- **Seed Used:** {seed}\n- **Average Sample Time:** {sum(timings)/len(timings):.2f}s\n\n### Rejected Candidates:\n" + '\n'.join(rejected or ["None"])
|
| 116 |
+
return df, file_path, df, retry_report
|
| 117 |
|
|
|
|
| 118 |
description = """# KaosGen: A Fantasy Name Generator
|
| 119 |
`Kaos` is a small GPT-style transformer (~890k parameters) trained from scratch using character-level tokenization.
|
| 120 |
It excels at fantasy and mythic naming conventions.
|
|
|
|
| 131 |
top_k = gr.Slider(10, 100, step=10, value=40, label="Top-K Sampling")
|
| 132 |
count = gr.Slider(1, 20, step=1, value=5, label="Names to Generate")
|
| 133 |
retries = gr.Slider(1, 5, step=1, value=3, label="Max Retries per Name")
|
| 134 |
+
seed = gr.Number(label="Seed", value=1337, precision=0)
|
| 135 |
+
randomize_seed = gr.Checkbox(label="Use Random Seed", value=False)
|
| 136 |
generate_btn = gr.Button("π² Generate Names")
|
| 137 |
with gr.Column():
|
| 138 |
+
output = gr.Dataframe(headers=["Generated Name", "Time (s)"], datatype=["str", "str"], label="Generated Names", interactive=False)
|
| 139 |
download = gr.File(label="π₯ Export Names as .txt")
|
| 140 |
+
retry_report = gr.Markdown("", label="Debug Info")
|
| 141 |
|
| 142 |
+
generate_btn.click(fn=generate_names, inputs=[prompt, temperature, top_k, count, retries, seed, randomize_seed], outputs=[download, download, output, retry_report])
|
| 143 |
gr.Examples(examples=examples, inputs=prompt)
|
| 144 |
|
| 145 |
demo.launch()
|