Spaces:
Sleeping
Sleeping
Commit
Β·
bbffad2
1
Parent(s):
fc8f2d9
Add preset configurations for better quality
Browse files- app.py +121 -175
- generation_config.py +40 -0
app.py
CHANGED
|
@@ -1,153 +1,127 @@
|
|
| 1 |
"""
|
| 2 |
-
Scholar Sage - Language Model Web Interface
|
| 3 |
-
|
| 4 |
"""
|
| 5 |
|
| 6 |
import torch
|
| 7 |
import gradio as gr
|
| 8 |
from transformers import AutoTokenizer
|
| 9 |
from model.transformer_explained import TinyTransformerLM
|
|
|
|
| 10 |
|
| 11 |
|
| 12 |
class TextGenerator:
|
| 13 |
def __init__(self, model_path="models/best_model_FIXED.pt"):
|
| 14 |
-
"""Initialize the text generator with the trained model."""
|
| 15 |
print("π Loading model...")
|
| 16 |
self.device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
|
| 17 |
-
|
| 18 |
-
# Load tokenizer
|
| 19 |
self.tokenizer = AutoTokenizer.from_pretrained("gpt2")
|
| 20 |
-
vocab_size = self.tokenizer.vocab_size
|
| 21 |
|
| 22 |
-
# Create model with same architecture as training
|
| 23 |
self.model = TinyTransformerLM(
|
| 24 |
-
vocab_size=vocab_size,
|
| 25 |
-
d_model=512,
|
| 26 |
-
n_layers=6,
|
| 27 |
-
num_heads=8,
|
| 28 |
-
d_ff=2048,
|
| 29 |
-
max_len=512
|
| 30 |
)
|
| 31 |
-
|
| 32 |
-
# Load trained weights
|
| 33 |
self.model.load_state_dict(torch.load(model_path, map_location=self.device))
|
| 34 |
self.model.to(self.device)
|
| 35 |
self.model.eval()
|
| 36 |
|
| 37 |
-
|
| 38 |
-
print(f"β
Model loaded! ({total_params:,} parameters)")
|
| 39 |
-
print(f"π₯οΈ Device: {self.device}")
|
| 40 |
|
| 41 |
-
def generate(
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
max_length=50,
|
| 45 |
-
temperature=0.8,
|
| 46 |
-
top_k=40,
|
| 47 |
-
top_p=0.92,
|
| 48 |
-
repetition_penalty=1.2,
|
| 49 |
-
num_return_sequences=1
|
| 50 |
-
):
|
| 51 |
-
"""
|
| 52 |
-
Generate text based on the prompt with advanced sampling.
|
| 53 |
|
| 54 |
-
|
| 55 |
-
prompt: Input text to start generation
|
| 56 |
-
max_length: Maximum number of tokens to generate
|
| 57 |
-
temperature: Sampling temperature (higher = more random)
|
| 58 |
-
top_k: Top-k sampling parameter
|
| 59 |
-
top_p: Top-p (nucleus) sampling parameter
|
| 60 |
-
repetition_penalty: Penalty for repeating tokens (>1.0 discourages repetition)
|
| 61 |
-
num_return_sequences: Number of different outputs to generate
|
| 62 |
-
"""
|
| 63 |
if not prompt.strip():
|
| 64 |
return "β οΈ Please enter a prompt!"
|
| 65 |
|
| 66 |
-
|
|
|
|
| 67 |
|
|
|
|
| 68 |
for _ in range(num_return_sequences):
|
| 69 |
-
|
| 70 |
-
input_ids = self.tokenizer(prompt, return_tensors="pt")["input_ids"].to(self.device)
|
| 71 |
-
original_length = input_ids.size(1)
|
| 72 |
|
| 73 |
with torch.no_grad():
|
| 74 |
for step in range(max_length):
|
| 75 |
-
# Get logits
|
| 76 |
logits, _ = self.model(input_ids)
|
| 77 |
next_token_logits = logits[:, -1, :].clone()
|
| 78 |
|
| 79 |
-
#
|
| 80 |
if repetition_penalty != 1.0:
|
| 81 |
for token_id in set(input_ids[0].tolist()):
|
| 82 |
-
# If score < 0, multiply by penalty (make it more negative)
|
| 83 |
-
# If score > 0, divide by penalty (make it smaller)
|
| 84 |
if next_token_logits[0, token_id] < 0:
|
| 85 |
next_token_logits[0, token_id] *= repetition_penalty
|
| 86 |
else:
|
| 87 |
next_token_logits[0, token_id] /= repetition_penalty
|
| 88 |
|
| 89 |
-
# Apply temperature
|
| 90 |
next_token_logits = next_token_logits / temperature
|
| 91 |
|
| 92 |
-
#
|
| 93 |
if top_k > 0:
|
| 94 |
-
indices_to_remove = next_token_logits < torch.topk(
|
|
|
|
|
|
|
| 95 |
next_token_logits[indices_to_remove] = float('-inf')
|
| 96 |
|
| 97 |
-
#
|
| 98 |
if top_p < 1.0:
|
| 99 |
sorted_logits, sorted_indices = torch.sort(next_token_logits, descending=True)
|
| 100 |
cumulative_probs = torch.cumsum(torch.softmax(sorted_logits, dim=-1), dim=-1)
|
| 101 |
-
|
| 102 |
-
# Remove tokens with cumulative probability above the threshold
|
| 103 |
sorted_indices_to_remove = cumulative_probs > top_p
|
| 104 |
sorted_indices_to_remove[..., 1:] = sorted_indices_to_remove[..., :-1].clone()
|
| 105 |
sorted_indices_to_remove[..., 0] = 0
|
| 106 |
-
|
| 107 |
indices_to_remove = sorted_indices_to_remove.scatter(1, sorted_indices, sorted_indices_to_remove)
|
| 108 |
next_token_logits[indices_to_remove] = float('-inf')
|
| 109 |
|
| 110 |
-
# Sample from the filtered distribution
|
| 111 |
probs = torch.softmax(next_token_logits, dim=-1)
|
| 112 |
next_token = torch.multinomial(probs, num_samples=1)
|
| 113 |
-
|
| 114 |
-
# Append to sequence
|
| 115 |
input_ids = torch.cat([input_ids, next_token], dim=1)
|
| 116 |
|
| 117 |
-
#
|
| 118 |
-
# Stop if we hit the model's max length
|
| 119 |
if input_ids.size(1) >= 512:
|
| 120 |
break
|
| 121 |
-
|
| 122 |
-
# Stop if we generate end-of-sequence token
|
| 123 |
if next_token.item() == self.tokenizer.eos_token_id:
|
| 124 |
break
|
|
|
|
|
|
|
|
|
|
| 125 |
|
| 126 |
-
# Decode the generated sequence
|
| 127 |
generated_text = self.tokenizer.decode(input_ids[0], skip_special_tokens=True)
|
| 128 |
outputs.append(generated_text)
|
| 129 |
|
| 130 |
-
|
| 131 |
-
if num_return_sequences == 1:
|
| 132 |
-
return outputs[0]
|
| 133 |
-
else:
|
| 134 |
-
return "\n\n" + "="*70 + "\n\n".join(outputs)
|
| 135 |
|
| 136 |
|
| 137 |
-
# Initialize generator
|
| 138 |
generator = TextGenerator()
|
| 139 |
|
| 140 |
|
| 141 |
-
def
|
| 142 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 143 |
try:
|
| 144 |
result = generator.generate(
|
| 145 |
prompt=prompt,
|
| 146 |
max_length=int(max_length),
|
| 147 |
-
temperature=float(
|
| 148 |
top_k=int(top_k),
|
| 149 |
top_p=float(top_p),
|
| 150 |
-
repetition_penalty=float(
|
| 151 |
num_return_sequences=int(num_outputs)
|
| 152 |
)
|
| 153 |
return result
|
|
@@ -155,133 +129,105 @@ def generate_text(prompt, max_length, temperature, top_k, top_p, repetition_pena
|
|
| 155 |
return f"β Error: {str(e)}"
|
| 156 |
|
| 157 |
|
| 158 |
-
#
|
| 159 |
-
with gr.Blocks(title="Scholar Sage -
|
| 160 |
-
gr.Markdown(
|
| 161 |
-
|
| 162 |
-
|
| 163 |
-
|
| 164 |
-
|
| 165 |
-
|
| 166 |
-
|
| 167 |
-
|
| 168 |
-
|
| 169 |
-
|
| 170 |
-
|
| 171 |
-
β οΈ **Note**: This is a small research model (~45M params vs GPT-3's 175B). For best results:
|
| 172 |
-
- Use **Repetition Penalty = 1.2-1.5** to prevent repetitive text
|
| 173 |
-
- Keep prompts clear and specific
|
| 174 |
-
- Expect limited context understanding compared to large commercial models
|
| 175 |
-
"""
|
| 176 |
-
)
|
| 177 |
|
| 178 |
with gr.Row():
|
| 179 |
with gr.Column(scale=1):
|
| 180 |
prompt_input = gr.Textbox(
|
| 181 |
-
label="π Enter
|
| 182 |
-
placeholder="
|
| 183 |
-
lines=
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 184 |
)
|
| 185 |
|
| 186 |
-
with gr.Accordion("βοΈ
|
| 187 |
-
|
| 188 |
-
|
| 189 |
-
|
| 190 |
-
|
| 191 |
-
|
| 192 |
-
label="Max Length (tokens to generate)"
|
| 193 |
-
)
|
| 194 |
-
|
| 195 |
-
temperature = gr.Slider(
|
| 196 |
-
minimum=0.1,
|
| 197 |
-
maximum=2.0,
|
| 198 |
-
value=0.8,
|
| 199 |
-
step=0.1,
|
| 200 |
-
label="Temperature (higher = more random)"
|
| 201 |
-
)
|
| 202 |
-
|
| 203 |
-
top_k = gr.Slider(
|
| 204 |
-
minimum=0,
|
| 205 |
-
maximum=100,
|
| 206 |
-
value=40,
|
| 207 |
-
step=5,
|
| 208 |
-
label="Top-k (0 = disabled)"
|
| 209 |
-
)
|
| 210 |
-
|
| 211 |
-
top_p = gr.Slider(
|
| 212 |
-
minimum=0.0,
|
| 213 |
-
maximum=1.0,
|
| 214 |
-
value=0.92,
|
| 215 |
-
step=0.02,
|
| 216 |
-
label="Top-p / Nucleus Sampling"
|
| 217 |
-
)
|
| 218 |
-
|
| 219 |
-
repetition_penalty = gr.Slider(
|
| 220 |
-
minimum=1.0,
|
| 221 |
-
maximum=2.0,
|
| 222 |
-
value=1.2,
|
| 223 |
-
step=0.1,
|
| 224 |
-
label="Repetition Penalty (higher = less repetition)"
|
| 225 |
-
)
|
| 226 |
-
|
| 227 |
-
num_outputs = gr.Slider(
|
| 228 |
-
minimum=1,
|
| 229 |
-
maximum=3,
|
| 230 |
-
value=1,
|
| 231 |
-
step=1,
|
| 232 |
-
label="Number of outputs"
|
| 233 |
-
)
|
| 234 |
|
| 235 |
generate_btn = gr.Button("π Generate", variant="primary", size="lg")
|
| 236 |
|
| 237 |
with gr.Column(scale=1):
|
| 238 |
output_text = gr.Textbox(
|
| 239 |
label="β¨ Generated Text",
|
| 240 |
-
lines=
|
| 241 |
show_copy_button=True
|
| 242 |
)
|
| 243 |
|
| 244 |
-
#
|
| 245 |
-
gr.Markdown("### π‘ Example Prompts")
|
| 246 |
gr.Examples(
|
| 247 |
examples=[
|
| 248 |
-
["
|
| 249 |
-
["
|
| 250 |
-
["
|
| 251 |
-
["
|
| 252 |
-
["
|
| 253 |
],
|
| 254 |
-
inputs=[prompt_input,
|
| 255 |
-
|
| 256 |
-
fn=generate_text,
|
| 257 |
-
cache_examples=False
|
| 258 |
)
|
| 259 |
|
| 260 |
-
# Connect the button
|
| 261 |
generate_btn.click(
|
| 262 |
-
fn=
|
| 263 |
-
inputs=[prompt_input,
|
|
|
|
| 264 |
outputs=output_text
|
| 265 |
)
|
| 266 |
|
| 267 |
-
gr.Markdown(
|
| 268 |
-
|
| 269 |
-
|
| 270 |
-
|
| 271 |
-
|
| 272 |
-
|
| 273 |
-
|
| 274 |
-
|
| 275 |
-
|
| 276 |
-
|
| 277 |
-
|
| 278 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 279 |
|
| 280 |
|
| 281 |
if __name__ == "__main__":
|
| 282 |
-
demo.launch(
|
| 283 |
-
server_name="0.0.0.0",
|
| 284 |
-
server_port=7860,
|
| 285 |
-
share=False,
|
| 286 |
-
show_error=True
|
| 287 |
-
)
|
|
|
|
| 1 |
"""
|
| 2 |
+
Scholar Sage - Improved Language Model Web Interface
|
| 3 |
+
Optimized for better text generation quality
|
| 4 |
"""
|
| 5 |
|
| 6 |
import torch
|
| 7 |
import gradio as gr
|
| 8 |
from transformers import AutoTokenizer
|
| 9 |
from model.transformer_explained import TinyTransformerLM
|
| 10 |
+
from generation_config import CONFIGS
|
| 11 |
|
| 12 |
|
| 13 |
class TextGenerator:
|
| 14 |
def __init__(self, model_path="models/best_model_FIXED.pt"):
|
|
|
|
| 15 |
print("π Loading model...")
|
| 16 |
self.device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
|
|
|
|
|
|
|
| 17 |
self.tokenizer = AutoTokenizer.from_pretrained("gpt2")
|
|
|
|
| 18 |
|
|
|
|
| 19 |
self.model = TinyTransformerLM(
|
| 20 |
+
vocab_size=self.tokenizer.vocab_size,
|
| 21 |
+
d_model=512, n_layers=6, num_heads=8, d_ff=2048, max_len=512
|
|
|
|
|
|
|
|
|
|
|
|
|
| 22 |
)
|
|
|
|
|
|
|
| 23 |
self.model.load_state_dict(torch.load(model_path, map_location=self.device))
|
| 24 |
self.model.to(self.device)
|
| 25 |
self.model.eval()
|
| 26 |
|
| 27 |
+
print(f"β
Model loaded on {self.device}")
|
|
|
|
|
|
|
| 28 |
|
| 29 |
+
def generate(self, prompt, max_length=50, temperature=0.7, top_k=40,
|
| 30 |
+
top_p=0.9, repetition_penalty=1.3, num_return_sequences=1):
|
| 31 |
+
"""Generate text with optimized sampling."""
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 32 |
|
| 33 |
+
# Improved prompt preprocessing
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 34 |
if not prompt.strip():
|
| 35 |
return "β οΈ Please enter a prompt!"
|
| 36 |
|
| 37 |
+
# Add context hints for better generation
|
| 38 |
+
enhanced_prompt = prompt.strip()
|
| 39 |
|
| 40 |
+
outputs = []
|
| 41 |
for _ in range(num_return_sequences):
|
| 42 |
+
input_ids = self.tokenizer(enhanced_prompt, return_tensors="pt")["input_ids"].to(self.device)
|
|
|
|
|
|
|
| 43 |
|
| 44 |
with torch.no_grad():
|
| 45 |
for step in range(max_length):
|
|
|
|
| 46 |
logits, _ = self.model(input_ids)
|
| 47 |
next_token_logits = logits[:, -1, :].clone()
|
| 48 |
|
| 49 |
+
# Enhanced repetition penalty
|
| 50 |
if repetition_penalty != 1.0:
|
| 51 |
for token_id in set(input_ids[0].tolist()):
|
|
|
|
|
|
|
| 52 |
if next_token_logits[0, token_id] < 0:
|
| 53 |
next_token_logits[0, token_id] *= repetition_penalty
|
| 54 |
else:
|
| 55 |
next_token_logits[0, token_id] /= repetition_penalty
|
| 56 |
|
|
|
|
| 57 |
next_token_logits = next_token_logits / temperature
|
| 58 |
|
| 59 |
+
# Top-k filtering
|
| 60 |
if top_k > 0:
|
| 61 |
+
indices_to_remove = next_token_logits < torch.topk(
|
| 62 |
+
next_token_logits, min(top_k, next_token_logits.size(-1))
|
| 63 |
+
)[0][..., -1, None]
|
| 64 |
next_token_logits[indices_to_remove] = float('-inf')
|
| 65 |
|
| 66 |
+
# Top-p filtering
|
| 67 |
if top_p < 1.0:
|
| 68 |
sorted_logits, sorted_indices = torch.sort(next_token_logits, descending=True)
|
| 69 |
cumulative_probs = torch.cumsum(torch.softmax(sorted_logits, dim=-1), dim=-1)
|
|
|
|
|
|
|
| 70 |
sorted_indices_to_remove = cumulative_probs > top_p
|
| 71 |
sorted_indices_to_remove[..., 1:] = sorted_indices_to_remove[..., :-1].clone()
|
| 72 |
sorted_indices_to_remove[..., 0] = 0
|
|
|
|
| 73 |
indices_to_remove = sorted_indices_to_remove.scatter(1, sorted_indices, sorted_indices_to_remove)
|
| 74 |
next_token_logits[indices_to_remove] = float('-inf')
|
| 75 |
|
|
|
|
| 76 |
probs = torch.softmax(next_token_logits, dim=-1)
|
| 77 |
next_token = torch.multinomial(probs, num_samples=1)
|
|
|
|
|
|
|
| 78 |
input_ids = torch.cat([input_ids, next_token], dim=1)
|
| 79 |
|
| 80 |
+
# Better stopping conditions
|
|
|
|
| 81 |
if input_ids.size(1) >= 512:
|
| 82 |
break
|
|
|
|
|
|
|
| 83 |
if next_token.item() == self.tokenizer.eos_token_id:
|
| 84 |
break
|
| 85 |
+
# Stop on double newline for cleaner outputs
|
| 86 |
+
if step > 10 and self.tokenizer.decode(input_ids[0, -2:]) == "\n\n":
|
| 87 |
+
break
|
| 88 |
|
|
|
|
| 89 |
generated_text = self.tokenizer.decode(input_ids[0], skip_special_tokens=True)
|
| 90 |
outputs.append(generated_text)
|
| 91 |
|
| 92 |
+
return outputs[0] if num_return_sequences == 1 else "\n\n---\n\n".join(outputs)
|
|
|
|
|
|
|
|
|
|
|
|
|
| 93 |
|
| 94 |
|
|
|
|
| 95 |
generator = TextGenerator()
|
| 96 |
|
| 97 |
|
| 98 |
+
def generate_with_preset(prompt, preset, max_length, custom_temp, custom_top_k,
|
| 99 |
+
custom_top_p, custom_rep_pen, num_outputs):
|
| 100 |
+
"""Generate using preset or custom parameters."""
|
| 101 |
+
if not prompt.strip():
|
| 102 |
+
return "β οΈ Please enter a prompt!"
|
| 103 |
+
|
| 104 |
+
# Use preset if selected, otherwise use custom values
|
| 105 |
+
if preset != "custom":
|
| 106 |
+
config = CONFIGS[preset]
|
| 107 |
+
temp = config["temperature"]
|
| 108 |
+
top_k = config["top_k"]
|
| 109 |
+
top_p = config["top_p"]
|
| 110 |
+
rep_pen = config["repetition_penalty"]
|
| 111 |
+
else:
|
| 112 |
+
temp = custom_temp
|
| 113 |
+
top_k = custom_top_k
|
| 114 |
+
top_p = custom_top_p
|
| 115 |
+
rep_pen = custom_rep_pen
|
| 116 |
+
|
| 117 |
try:
|
| 118 |
result = generator.generate(
|
| 119 |
prompt=prompt,
|
| 120 |
max_length=int(max_length),
|
| 121 |
+
temperature=float(temp),
|
| 122 |
top_k=int(top_k),
|
| 123 |
top_p=float(top_p),
|
| 124 |
+
repetition_penalty=float(rep_pen),
|
| 125 |
num_return_sequences=int(num_outputs)
|
| 126 |
)
|
| 127 |
return result
|
|
|
|
| 129 |
return f"β Error: {str(e)}"
|
| 130 |
|
| 131 |
|
| 132 |
+
# Build Gradio Interface
|
| 133 |
+
with gr.Blocks(title="Scholar Sage - Improved", theme=gr.themes.Soft()) as demo:
|
| 134 |
+
gr.Markdown("""
|
| 135 |
+
# π Scholar Sage - Language Model (Optimized)
|
| 136 |
+
|
| 137 |
+
A 45M parameter transformer trained on WikiText-2. **Use presets** for best results!
|
| 138 |
+
|
| 139 |
+
π‘ **Tips for Quality Output:**
|
| 140 |
+
- Use **"Balanced" preset** for general use
|
| 141 |
+
- Start with encyclopedia-style prompts (model trained on WikiText)
|
| 142 |
+
- Try longer prompts (10-20 words) for better context
|
| 143 |
+
- Experiment with different presets for different styles
|
| 144 |
+
""")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 145 |
|
| 146 |
with gr.Row():
|
| 147 |
with gr.Column(scale=1):
|
| 148 |
prompt_input = gr.Textbox(
|
| 149 |
+
label="π Enter Your Prompt",
|
| 150 |
+
placeholder="Example: The theory of relativity is a scientific theory that",
|
| 151 |
+
lines=4
|
| 152 |
+
)
|
| 153 |
+
|
| 154 |
+
preset_selector = gr.Radio(
|
| 155 |
+
choices=["balanced", "creative", "focused", "factual", "custom"],
|
| 156 |
+
value="balanced",
|
| 157 |
+
label="ποΈ Preset Configuration",
|
| 158 |
+
info="Balanced is recommended for most uses"
|
| 159 |
+
)
|
| 160 |
+
|
| 161 |
+
max_length = gr.Slider(
|
| 162 |
+
minimum=20, maximum=150, value=60, step=10,
|
| 163 |
+
label="π Max Length (tokens)"
|
| 164 |
+
)
|
| 165 |
+
|
| 166 |
+
num_outputs = gr.Slider(
|
| 167 |
+
minimum=1, maximum=3, value=1, step=1,
|
| 168 |
+
label="π’ Number of Outputs"
|
| 169 |
)
|
| 170 |
|
| 171 |
+
with gr.Accordion("βοΈ Custom Settings", open=False):
|
| 172 |
+
gr.Markdown("*Only used when 'custom' preset is selected*")
|
| 173 |
+
custom_temp = gr.Slider(0.1, 2.0, 0.7, step=0.1, label="Temperature")
|
| 174 |
+
custom_top_k = gr.Slider(0, 100, 40, step=5, label="Top-k")
|
| 175 |
+
custom_top_p = gr.Slider(0.0, 1.0, 0.9, step=0.05, label="Top-p")
|
| 176 |
+
custom_rep_pen = gr.Slider(1.0, 2.0, 1.3, step=0.1, label="Repetition Penalty")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 177 |
|
| 178 |
generate_btn = gr.Button("π Generate", variant="primary", size="lg")
|
| 179 |
|
| 180 |
with gr.Column(scale=1):
|
| 181 |
output_text = gr.Textbox(
|
| 182 |
label="β¨ Generated Text",
|
| 183 |
+
lines=18,
|
| 184 |
show_copy_button=True
|
| 185 |
)
|
| 186 |
|
| 187 |
+
# Example prompts optimized for WikiText-2 training
|
| 188 |
+
gr.Markdown("### π‘ Example Prompts (Optimized for this Model)")
|
| 189 |
gr.Examples(
|
| 190 |
examples=[
|
| 191 |
+
["The history of artificial intelligence began in", "balanced", 60, 0.7, 40, 0.9, 1.3, 1],
|
| 192 |
+
["Python programming language is a high-level", "factual", 60, 0.3, 20, 0.8, 1.4, 1],
|
| 193 |
+
["In the field of quantum mechanics,", "balanced", 60, 0.7, 40, 0.9, 1.3, 1],
|
| 194 |
+
["The United States is a country located in", "factual", 60, 0.3, 20, 0.8, 1.4, 1],
|
| 195 |
+
["Machine learning algorithms can be used to", "balanced", 60, 0.7, 40, 0.9, 1.3, 1],
|
| 196 |
],
|
| 197 |
+
inputs=[prompt_input, preset_selector, max_length, custom_temp, custom_top_k,
|
| 198 |
+
custom_top_p, custom_rep_pen, num_outputs],
|
|
|
|
|
|
|
| 199 |
)
|
| 200 |
|
|
|
|
| 201 |
generate_btn.click(
|
| 202 |
+
fn=generate_with_preset,
|
| 203 |
+
inputs=[prompt_input, preset_selector, max_length, custom_temp, custom_top_k,
|
| 204 |
+
custom_top_p, custom_rep_pen, num_outputs],
|
| 205 |
outputs=output_text
|
| 206 |
)
|
| 207 |
|
| 208 |
+
gr.Markdown("""
|
| 209 |
+
---
|
| 210 |
+
### π Understanding the Presets
|
| 211 |
+
|
| 212 |
+
- **Balanced** (default): Best for general encyclopedia-style text
|
| 213 |
+
- **Creative**: More diverse outputs, good for storytelling
|
| 214 |
+
- **Focused**: Deterministic, good for factual content
|
| 215 |
+
- **Factual**: Highest coherence, lowest creativity
|
| 216 |
+
- **Custom**: Manual control over all parameters
|
| 217 |
+
|
| 218 |
+
### β οΈ Model Limitations
|
| 219 |
+
|
| 220 |
+
This is a 45M parameter research model (vs GPT-3's 175B). It works best with:
|
| 221 |
+
- β
Encyclopedia-style content (trained on WikiText-2)
|
| 222 |
+
- β
Factual, informative text
|
| 223 |
+
- β
Short to medium generations (20-100 tokens)
|
| 224 |
+
|
| 225 |
+
It struggles with:
|
| 226 |
+
- β Creative fiction or dialogue
|
| 227 |
+
- β Very long context understanding
|
| 228 |
+
- β Highly specialized technical content
|
| 229 |
+
""")
|
| 230 |
|
| 231 |
|
| 232 |
if __name__ == "__main__":
|
| 233 |
+
demo.launch()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
generation_config.py
ADDED
|
@@ -0,0 +1,40 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# Optimized Generation Configurations for Different Use Cases
|
| 2 |
+
|
| 3 |
+
CONFIGS = {
|
| 4 |
+
"creative": {
|
| 5 |
+
"temperature": 0.9,
|
| 6 |
+
"top_k": 50,
|
| 7 |
+
"top_p": 0.95,
|
| 8 |
+
"repetition_penalty": 1.1,
|
| 9 |
+
"description": "More creative and diverse outputs"
|
| 10 |
+
},
|
| 11 |
+
"balanced": {
|
| 12 |
+
"temperature": 0.7,
|
| 13 |
+
"top_k": 40,
|
| 14 |
+
"top_p": 0.9,
|
| 15 |
+
"repetition_penalty": 1.3,
|
| 16 |
+
"description": "Balanced creativity and coherence (recommended)"
|
| 17 |
+
},
|
| 18 |
+
"focused": {
|
| 19 |
+
"temperature": 0.5,
|
| 20 |
+
"top_k": 30,
|
| 21 |
+
"top_p": 0.85,
|
| 22 |
+
"repetition_penalty": 1.5,
|
| 23 |
+
"description": "More focused and deterministic"
|
| 24 |
+
},
|
| 25 |
+
"factual": {
|
| 26 |
+
"temperature": 0.3,
|
| 27 |
+
"top_k": 20,
|
| 28 |
+
"top_p": 0.8,
|
| 29 |
+
"repetition_penalty": 1.4,
|
| 30 |
+
"description": "Best for encyclopedia-style content"
|
| 31 |
+
}
|
| 32 |
+
}
|
| 33 |
+
|
| 34 |
+
# Better prompts for small models
|
| 35 |
+
PROMPT_TEMPLATES = {
|
| 36 |
+
"article": "Wikipedia article about {topic}:\n\n",
|
| 37 |
+
"definition": "{term} is defined as",
|
| 38 |
+
"explanation": "Here is an explanation of {topic}:\n\n",
|
| 39 |
+
"continuation": "{text}"
|
| 40 |
+
}
|