Spaces:
Sleeping
Sleeping
Upload app.py
Browse files219 unmodified lines220221222223224223224225226227228229230231232233234235236237238239240241768 unmodified lines101010111012101310141015101610171018101910201021102218 unmodified lines1041104210431023104410451025104610471048104910501051103110521053103310541055105610571058105910391060106110411062106310641065106610671047106810691049107010711072107310 unmodified lines10841085108610871066106710681069107010711072107310741075107610771078107910881089109010911092109310941095109610971098109911001101110211031104219 unmodified linesenc = tiktoken.get_encoding('gpt2')
def generate_text(prompt, max_new_tokens=100, temperature=0.7, top_k=50, top_p=0.9, repetition_penalty=1.1): """Generate text from prompt with improved sampling"""def generate_text(prompt, max_new_tokens=100, temperature=0.6, top_k=40, top_p=0.8, repetition_penalty=1.15): """ Generate text from prompt with improved sampling. Note on Grammar Quality: Despite training, grammar may not be perfect due to: 1. Model size (124M) - relatively small for perfect grammar 2. Training loss measures token prediction, not grammatical correctness 3. Shakespeare text has archaic grammar patterns the model learns 4. BPE tokenization can split words, making grammar harder to learn For better grammar, try: - Lower temperature (0.5-0.6) for more focused, coherent text - Higher repetition penalty (1.15-1.2) to reduce repetitive patterns - Lower top_p (0.8-0.85) for more conservative sampling """ try: if not model_loaded: return "❌ Error: Model not loaded correctly. Please check that model_checkpoint_final.pt is uploaded to HuggingFace Model Hub (shwethd/gpt2-shakespeare-124m)."768 unmodified lines - Model Parameters: 124.44M - Training Steps: 1,637 **📝 Note on Grammar Quality:** The model learns token patterns from Shakespeare text, but grammar may not be perfect due to: - Model size (124M is relatively small for perfect grammar) - Training loss measures token prediction, not grammatical correctness - Shakespeare text has archaic grammar patterns - For better grammar, use lower temperature (0.5-0.6) and higher repetition penalty (1.15-1.2) Enter a prompt below to generate Shakespeare-style text! {"⚠️ **Note:** If you see garbled/random text, the model may not have loaded correctly. Check the logs and ensure the model is uploaded to HuggingFace Model Hub: `shwethd/gpt2-shakespeare-124m`" if not model_loaded else ""}18 unmodified lines label="Temperature", minimum=0.1, maximum=2.0, value=0.7, value=0.6, step=0.1, info="Lower = more focused, Higher = more creative (0.7 recommended for better coherence)" info="Lower = more focused/grammatical (0.5-0.6 recommended). Higher = more creative but less coherent" ) top_k = gr.Slider( label="Top-K", minimum=10, maximum=100, value=50, value=40, step=10, info="Number of top tokens to consider" info="Number of top tokens to consider. Lower (30-40) = more focused, Higher = more diverse" ) top_p = gr.Slider( label="Top-P (Nucleus)", minimum=0.1, maximum=1.0, value=0.85, value=0.8, step=0.05, info="Nucleus sampling - 0.85-0.9 recommended. Lower (0.3) = too restrictive, Higher (0.95+) = too random" info="Nucleus sampling. Lower (0.75-0.8) = more grammatical/coherent. Higher (0.9+) = more random" ) repetition_penalty = gr.Slider( label="Repetition Penalty", minimum=1.0, maximum=1.5, value=1.1, value=1.15, step=0.05, info="Penalize repeated tokens - higher = less repetition (1.1 recommended)" info="Penalize repeated tokens. Higher (1.15-1.2) = better grammar/less repetition. Lower = more repetitive" ) generate_btn = gr.Button("Generate", variant="primary") 10 unmodified lines examples = gr.Examples( examples=[ # Format: [prompt, max_tokens, temperature, top_k, top_p, repetition_penalty] # Optimized for better grammar: lower temp (0.5-0.6), lower top_p (0.75-0.8), higher rep_penalty (1.15-1.2) ["First Citizen:", 100, 0.7, 50, 0.85, 1.1], ["ROMEO:", 100, 0.65, 45, 0.88, 1.15], # Romantic - slightly lower temp ["To be or not", 80, 0.6, 40, 0.85, 1.2], # Quote - more focused ["HAMLET:", 100, 0.7, 50, 0.85, 1.1], ["MACBETH:", 100, 0.7, 50, 0.85, 1.1], ["JULIET:", 100, 0.65, 45, 0.88, 1.15], # Romantic ["KING:", 100, 0.7, 50, 0.85, 1.1], ["LADY MACBETH:", 100, 0.7, 50, 0.85, 1.1], ["OTHELLO:", 100, 0.7, 50, 0.85, 1.1], ["What light through yonder", 100, 0.65, 45, 0.88, 1.15], # Romantic quote ["All the world's a stage", 100, 0.7, 50, 0.85, 1.1], # Metaphorical ["Double, double toil and trouble", 80, 0.7, 50, 0.85, 1.15], # Witches chant ["Friends, Romans, countrymen", 100, 0.7, 50, 0.85, 1.1], # Speech ["A rose by any other name", 100, 0.65, 45, 0.88, 1.15], # Romantic quote ["First Citizen:", 100, 0.6, 40, 0.8, 1.15], ["ROMEO:", 100, 0.55, 35, 0.78, 1.2], # Romantic - lower temp for coherence ["To be or not", 80, 0.5, 30, 0.75, 1.2], # Quote - very focused for grammar ["HAMLET:", 100, 0.6, 40, 0.8, 1.15], ["MACBETH:", 100, 0.6, 40, 0.8, 1.15], ["JULIET:", 100, 0.55, 35, 0.78, 1.2], # Romantic - lower temp ["KING:", 100, 0.6, 40, 0.8, 1.15], ["LADY MACBETH:", 100, 0.6, 40, 0.8, 1.15], ["OTHELLO:", 100, 0.6, 40, 0.8, 1.15], ["What light through yonder", 100, 0.55, 35, 0.78, 1.2], # Romantic quote ["All the world's a stage", 100, 0.6, 40, 0.8, 1.15], # Metaphorical ["Double, double toil and trouble", 80, 0.6, 40, 0.8, 1.15], # Witches chant ["Friends, Romans, countrymen", 100, 0.6, 40, 0.8, 1.15], # Speech ["A rose by any other name", 100, 0.55, 35, 0.78, 1.2], # Romantic quote ], inputs=[prompt_input, max_tokens, temperature, top_k, top_p, repetition_penalty] )app.py+46-24