Spaces:
Sleeping
Sleeping
Upload app.py
Browse files219 unmodified lines220221222223224225226227768 unmodified lines9969979989991000100118 unmodified lines10201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210 unmodified lines10631064106510661067106810691070107110721073107410751076107710781079108010811082219 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"""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,637Enter 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 lineslabel="Temperature",minimum=0.1,maximum=2.0,value=0.7,step=0.1,info="Lower = more focused, Higher = more creative (0.7 recommended for better coherence)")top_k = gr.Slider(label="Top-K",minimum=10,maximum=100,value=50,step=10,info="Number of top tokens to consider")top_p = gr.Slider(label="Top-P (Nucleus)",minimum=0.1,maximum=1.0,value=0.85,step=0.05,info="Nucleus sampling - 0.85-0.9 recommended. Lower (0.3) = too restrictive, Higher (0.95+) = too random")repetition_penalty = gr.Slider(label="Repetition Penalty",minimum=1.0,maximum=1.5,value=1.1,step=0.05,info="Penalize repeated tokens - higher = less repetition (1.1 recommended)")generate_btn = gr.Button("Generate", variant="primary")10 unmodified linesexamples = gr.Examples(examples=[# Format: [prompt, max_tokens, temperature, top_k, top_p, repetition_penalty]["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],inputs=[prompt_input, max_tokens, temperature, top_k, top_p, repetition_penalty])219 unmodified lines220221222223224225226227228229230231232233234235236237238239240241768 unmodified lines101010111012101310141015101610171018101910201021102218 unmodified lines10411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310 unmodified lines108410851086108710881089109010911092109310941095109610971098109911001101110211031104219 unmodified linesenc = tiktoken.get_encoding('gpt2')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 grammar2. Training loss measures token prediction, not grammatical correctness3. Shakespeare text has archaic grammar patterns the model learns4. BPE tokenization can split words, making grammar harder to learnFor 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 lineslabel="Temperature",minimum=0.1,maximum=2.0,value=0.6,step=0.1,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=40,step=10,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.8,step=0.05,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.15,step=0.05,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 linesexamples = 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.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