Dagriffpatchfan commited on
Commit
bd257ff
·
verified ·
1 Parent(s): 0208660

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +12 -13
app.py CHANGED
@@ -2,34 +2,33 @@ import gradio as gr
2
  from transformers import pipeline
3
  import re
4
 
5
- # Load the model. gpt2-large is highly capable but small enough to run incredibly fast on CPU.
6
- # We use device=-1 to force CPU, which is the default for free HF Spaces.
7
- generator = pipeline("text-generation", model="gpt2-large", device=-1)
 
8
 
9
  def predict_next_word(context):
10
  if not context.strip():
11
  return ""
12
 
13
- # We generate up to 5 tokens to ensure we get a full word,
14
- # since models sometimes split long words into multiple tokens.
15
  result = generator(
16
  context,
17
  max_new_tokens=5,
18
- return_full_text=False, # Returns ONLY the new text, not the prompt
19
- do_sample=False, # Greedy decoding (forces the absolute most likely next path)
20
  pad_token_id=generator.tokenizer.eos_token_id
21
  )
22
 
23
  generated_text = result[0]["generated_text"]
24
 
25
- # Extract just the very first word from the newly generated text
26
- # This ignores trailing spaces and grabs the first sequence of word characters
27
  match = re.search(r'^\s*([a-zA-Z0-9\'-]+)', generated_text)
28
 
29
  if match:
30
  next_word = match.group(1)
31
  else:
32
- # Fallback in case it generates punctuation first
33
  next_word = generated_text.strip().split()[0] if generated_text.strip() else ""
34
 
35
  return next_word
@@ -37,11 +36,11 @@ def predict_next_word(context):
37
  # Create the Gradio Interface
38
  demo = gr.Interface(
39
  fn=predict_next_word,
40
- inputs=gr.Textbox(lines=2, placeholder="e.g., 'Dogs are'", label="Context"),
41
  outputs=gr.Textbox(label="Most Likely Next Word"),
42
- title="Next Word Predictor",
43
  description="Enter a string of context and the model will instantly return the most likely next word.",
44
- flagging_mode="never" # <--- THIS IS THE FIX FOR GRADIO 4.0+
45
  )
46
 
47
  if __name__ == "__main__":
 
2
  from transformers import pipeline
3
  import re
4
 
5
+ # Load the model. Qwen 2.5 (1.5B parameters) is vastly superior to GPT-2.
6
+ # It is a modern, un-gated base model that excels at text continuation.
7
+ # device=-1 forces CPU execution for free HF Spaces.
8
+ generator = pipeline("text-generation", model="Qwen/Qwen2.5-1.5B", device=-1)
9
 
10
  def predict_next_word(context):
11
  if not context.strip():
12
  return ""
13
 
14
+ # Generate a few tokens to ensure we get a complete next word.
 
15
  result = generator(
16
  context,
17
  max_new_tokens=5,
18
+ return_full_text=False, # Returns ONLY the new text
19
+ do_sample=False, # Greedy decoding (forces absolute highest probability path)
20
  pad_token_id=generator.tokenizer.eos_token_id
21
  )
22
 
23
  generated_text = result[0]["generated_text"]
24
 
25
+ # Cleanly extract just the very first word (ignoring leading spaces)
 
26
  match = re.search(r'^\s*([a-zA-Z0-9\'-]+)', generated_text)
27
 
28
  if match:
29
  next_word = match.group(1)
30
  else:
31
+ # Fallback in case the model generates punctuation first
32
  next_word = generated_text.strip().split()[0] if generated_text.strip() else ""
33
 
34
  return next_word
 
36
  # Create the Gradio Interface
37
  demo = gr.Interface(
38
  fn=predict_next_word,
39
+ inputs=gr.Textbox(lines=2, placeholder="e.g., 'How is it'", label="Context"),
40
  outputs=gr.Textbox(label="Most Likely Next Word"),
41
+ title="Next Word Predictor (Powered by Qwen 2.5)",
42
  description="Enter a string of context and the model will instantly return the most likely next word.",
43
+ flagging_mode="never"
44
  )
45
 
46
  if __name__ == "__main__":