shwethd commited on
Commit
9b300f4
·
verified ·
1 Parent(s): 6696ccb

Upload app.py

Browse files
Files changed (1) hide show
  1. app.py +46 -24
app.py CHANGED
@@ -220,8 +220,22 @@ print(f"Model ready on {device}")
220
  enc = tiktoken.get_encoding('gpt2')
221
 
222
 
223
- def generate_text(prompt, max_new_tokens=100, temperature=0.7, top_k=50, top_p=0.9, repetition_penalty=1.1):
224
- """Generate text from prompt with improved sampling"""
 
 
 
 
 
 
 
 
 
 
 
 
 
 
225
  try:
226
  if not model_loaded:
227
  return "❌ Error: Model not loaded correctly. Please check that model_checkpoint_final.pt is uploaded to HuggingFace Model Hub (shwethd/gpt2-shakespeare-124m)."
@@ -996,6 +1010,13 @@ with gr.Blocks(title="GPT-2 124M Shakespeare Model") as demo:
996
  - Model Parameters: 124.44M
997
  - Training Steps: 1,637
998
 
 
 
 
 
 
 
 
999
  Enter a prompt below to generate Shakespeare-style text!
1000
 
1001
  {"⚠️ **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 ""}
@@ -1020,33 +1041,33 @@ with gr.Blocks(title="GPT-2 124M Shakespeare Model") as demo:
1020
  label="Temperature",
1021
  minimum=0.1,
1022
  maximum=2.0,
1023
- value=0.7,
1024
  step=0.1,
1025
- info="Lower = more focused, Higher = more creative (0.7 recommended for better coherence)"
1026
  )
1027
  top_k = gr.Slider(
1028
  label="Top-K",
1029
  minimum=10,
1030
  maximum=100,
1031
- value=50,
1032
  step=10,
1033
- info="Number of top tokens to consider"
1034
  )
1035
  top_p = gr.Slider(
1036
  label="Top-P (Nucleus)",
1037
  minimum=0.1,
1038
  maximum=1.0,
1039
- value=0.85,
1040
  step=0.05,
1041
- info="Nucleus sampling - 0.85-0.9 recommended. Lower (0.3) = too restrictive, Higher (0.95+) = too random"
1042
  )
1043
  repetition_penalty = gr.Slider(
1044
  label="Repetition Penalty",
1045
  minimum=1.0,
1046
  maximum=1.5,
1047
- value=1.1,
1048
  step=0.05,
1049
- info="Penalize repeated tokens - higher = less repetition (1.1 recommended)"
1050
  )
1051
  generate_btn = gr.Button("Generate", variant="primary")
1052
 
@@ -1063,20 +1084,21 @@ with gr.Blocks(title="GPT-2 124M Shakespeare Model") as demo:
1063
  examples = gr.Examples(
1064
  examples=[
1065
  # Format: [prompt, max_tokens, temperature, top_k, top_p, repetition_penalty]
1066
- ["First Citizen:", 100, 0.7, 50, 0.85, 1.1],
1067
- ["ROMEO:", 100, 0.65, 45, 0.88, 1.15], # Romantic - slightly lower temp
1068
- ["To be or not", 80, 0.6, 40, 0.85, 1.2], # Quote - more focused
1069
- ["HAMLET:", 100, 0.7, 50, 0.85, 1.1],
1070
- ["MACBETH:", 100, 0.7, 50, 0.85, 1.1],
1071
- ["JULIET:", 100, 0.65, 45, 0.88, 1.15], # Romantic
1072
- ["KING:", 100, 0.7, 50, 0.85, 1.1],
1073
- ["LADY MACBETH:", 100, 0.7, 50, 0.85, 1.1],
1074
- ["OTHELLO:", 100, 0.7, 50, 0.85, 1.1],
1075
- ["What light through yonder", 100, 0.65, 45, 0.88, 1.15], # Romantic quote
1076
- ["All the world's a stage", 100, 0.7, 50, 0.85, 1.1], # Metaphorical
1077
- ["Double, double toil and trouble", 80, 0.7, 50, 0.85, 1.15], # Witches chant
1078
- ["Friends, Romans, countrymen", 100, 0.7, 50, 0.85, 1.1], # Speech
1079
- ["A rose by any other name", 100, 0.65, 45, 0.88, 1.15], # Romantic quote
 
1080
  ],
1081
  inputs=[prompt_input, max_tokens, temperature, top_k, top_p, repetition_penalty]
1082
  )
 
220
  enc = tiktoken.get_encoding('gpt2')
221
 
222
 
223
+ def generate_text(prompt, max_new_tokens=100, temperature=0.6, top_k=40, top_p=0.8, repetition_penalty=1.15):
224
+ """
225
+ Generate text from prompt with improved sampling.
226
+
227
+ Note on Grammar Quality:
228
+ Despite training, grammar may not be perfect due to:
229
+ 1. Model size (124M) - relatively small for perfect grammar
230
+ 2. Training loss measures token prediction, not grammatical correctness
231
+ 3. Shakespeare text has archaic grammar patterns the model learns
232
+ 4. BPE tokenization can split words, making grammar harder to learn
233
+
234
+ For better grammar, try:
235
+ - Lower temperature (0.5-0.6) for more focused, coherent text
236
+ - Higher repetition penalty (1.15-1.2) to reduce repetitive patterns
237
+ - Lower top_p (0.8-0.85) for more conservative sampling
238
+ """
239
  try:
240
  if not model_loaded:
241
  return "❌ Error: Model not loaded correctly. Please check that model_checkpoint_final.pt is uploaded to HuggingFace Model Hub (shwethd/gpt2-shakespeare-124m)."
 
1010
  - Model Parameters: 124.44M
1011
  - Training Steps: 1,637
1012
 
1013
+ **📝 Note on Grammar Quality:**
1014
+ The model learns token patterns from Shakespeare text, but grammar may not be perfect due to:
1015
+ - Model size (124M is relatively small for perfect grammar)
1016
+ - Training loss measures token prediction, not grammatical correctness
1017
+ - Shakespeare text has archaic grammar patterns
1018
+ - For better grammar, use lower temperature (0.5-0.6) and higher repetition penalty (1.15-1.2)
1019
+
1020
  Enter a prompt below to generate Shakespeare-style text!
1021
 
1022
  {"⚠️ **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 ""}
 
1041
  label="Temperature",
1042
  minimum=0.1,
1043
  maximum=2.0,
1044
+ value=0.6,
1045
  step=0.1,
1046
+ info="Lower = more focused/grammatical (0.5-0.6 recommended). Higher = more creative but less coherent"
1047
  )
1048
  top_k = gr.Slider(
1049
  label="Top-K",
1050
  minimum=10,
1051
  maximum=100,
1052
+ value=40,
1053
  step=10,
1054
+ info="Number of top tokens to consider. Lower (30-40) = more focused, Higher = more diverse"
1055
  )
1056
  top_p = gr.Slider(
1057
  label="Top-P (Nucleus)",
1058
  minimum=0.1,
1059
  maximum=1.0,
1060
+ value=0.8,
1061
  step=0.05,
1062
+ info="Nucleus sampling. Lower (0.75-0.8) = more grammatical/coherent. Higher (0.9+) = more random"
1063
  )
1064
  repetition_penalty = gr.Slider(
1065
  label="Repetition Penalty",
1066
  minimum=1.0,
1067
  maximum=1.5,
1068
+ value=1.15,
1069
  step=0.05,
1070
+ info="Penalize repeated tokens. Higher (1.15-1.2) = better grammar/less repetition. Lower = more repetitive"
1071
  )
1072
  generate_btn = gr.Button("Generate", variant="primary")
1073
 
 
1084
  examples = gr.Examples(
1085
  examples=[
1086
  # Format: [prompt, max_tokens, temperature, top_k, top_p, repetition_penalty]
1087
+ # Optimized for better grammar: lower temp (0.5-0.6), lower top_p (0.75-0.8), higher rep_penalty (1.15-1.2)
1088
+ ["First Citizen:", 100, 0.6, 40, 0.8, 1.15],
1089
+ ["ROMEO:", 100, 0.55, 35, 0.78, 1.2], # Romantic - lower temp for coherence
1090
+ ["To be or not", 80, 0.5, 30, 0.75, 1.2], # Quote - very focused for grammar
1091
+ ["HAMLET:", 100, 0.6, 40, 0.8, 1.15],
1092
+ ["MACBETH:", 100, 0.6, 40, 0.8, 1.15],
1093
+ ["JULIET:", 100, 0.55, 35, 0.78, 1.2], # Romantic - lower temp
1094
+ ["KING:", 100, 0.6, 40, 0.8, 1.15],
1095
+ ["LADY MACBETH:", 100, 0.6, 40, 0.8, 1.15],
1096
+ ["OTHELLO:", 100, 0.6, 40, 0.8, 1.15],
1097
+ ["What light through yonder", 100, 0.55, 35, 0.78, 1.2], # Romantic quote
1098
+ ["All the world's a stage", 100, 0.6, 40, 0.8, 1.15], # Metaphorical
1099
+ ["Double, double toil and trouble", 80, 0.6, 40, 0.8, 1.15], # Witches chant
1100
+ ["Friends, Romans, countrymen", 100, 0.6, 40, 0.8, 1.15], # Speech
1101
+ ["A rose by any other name", 100, 0.55, 35, 0.78, 1.2], # Romantic quote
1102
  ],
1103
  inputs=[prompt_input, max_tokens, temperature, top_k, top_p, repetition_penalty]
1104
  )