Benjamin G commited on
Commit
0f36821
1 Parent(s): c3faf6b

fix gpu usage (tested)

Browse files
Files changed (2) hide show
  1. .gitignore +2 -0
  2. app.py +13 -12
.gitignore ADDED
@@ -0,0 +1,2 @@
 
 
 
1
+ flagged/
2
+ __pycache__/
app.py CHANGED
@@ -27,7 +27,7 @@ model = AutoModelForCausalLM.from_pretrained(
27
  # still includes these tokens in the output but stops generating after them
28
  class Phi2StoppingCriteria(StoppingCriteria):
29
  def __init__(self):
30
- stop_list = ["Exercise", "Exercises", "<|endoftext|>"]
31
  tokenphrases = []
32
  for token in stop_list:
33
  tokenphrases.append(
@@ -46,13 +46,13 @@ class Phi2StoppingCriteria(StoppingCriteria):
46
  def generate(
47
  prompt,
48
  max_new_tokens=75,
49
- avoid_hallucinated_prompts=True,
50
  sampling=False,
51
  temperature=1.0,
52
  top_k=50,
53
  top_p=1.0,
54
  ):
55
- inputs = tokenizer(prompt, return_tensors="pt")
56
  # thanks https://huggingface.co/spaces/joaogante/transformers_streaming/blob/main/app.py
57
  streamer = TextIteratorStreamer(tokenizer)
58
  generation_kwargs = dict(
@@ -61,7 +61,7 @@ def generate(
61
  max_new_tokens=max_new_tokens,
62
  do_sample=sampling,
63
  stopping_criteria=[Phi2StoppingCriteria()]
64
- if avoid_hallucinated_prompts
65
  else None,
66
  temperature=temperature,
67
  top_k=top_k,
@@ -83,10 +83,10 @@ demo = gr.Interface(
83
  label="prompt",
84
  value="Write a detailed analogy between mathematics and a lighthouse.",
85
  ),
86
- gr.Slider(minimum=0, maximum=500, step=1, value=75, label="max new tokens"),
87
  gr.Checkbox(
88
  value=True,
89
- label="avoid hallucinated prompts",
90
  info="stop generation after getting tokens like 'Exercise' or '<|endoftext|>, but will not remove them.",
91
  ),
92
  gr.Checkbox(
@@ -127,22 +127,23 @@ demo = gr.Interface(
127
  ],
128
  [
129
  "Instruct: Write a detailed analogy between mathematics and a lighthouse.\nOutput:",
130
- 75,
131
  ],
132
  [
133
  "Alice: I don't know why, I'm struggling to maintain focus while studying. Any suggestions?\n\nBob: ",
134
  150,
135
  ],
136
  [
137
- '''def print_prime(n):
 
138
  """
139
  Print all primes between 1 and n
140
  """\n''',
141
- 100,
142
  ],
143
- ["User: How does sleep affect mood?\nAI:", 75],
144
- ["Who was Ada Lovelace?", 100],
145
- ["Explain the concept of skip lists.", 125],
146
  ],
147
  title="Microsoft Phi-2",
148
  description="Unofficial demo of Microsoft Phi-2, a high performing model with only 2.7B parameters.",
 
27
  # still includes these tokens in the output but stops generating after them
28
  class Phi2StoppingCriteria(StoppingCriteria):
29
  def __init__(self):
30
+ stop_list = ["Exercise", "Exercises", "exercises:", "<|endoftext|>"]
31
  tokenphrases = []
32
  for token in stop_list:
33
  tokenphrases.append(
 
46
  def generate(
47
  prompt,
48
  max_new_tokens=75,
49
+ terminate_hallucinated_prompts=True,
50
  sampling=False,
51
  temperature=1.0,
52
  top_k=50,
53
  top_p=1.0,
54
  ):
55
+ inputs = tokenizer(prompt, return_tensors="pt").to(device)
56
  # thanks https://huggingface.co/spaces/joaogante/transformers_streaming/blob/main/app.py
57
  streamer = TextIteratorStreamer(tokenizer)
58
  generation_kwargs = dict(
 
61
  max_new_tokens=max_new_tokens,
62
  do_sample=sampling,
63
  stopping_criteria=[Phi2StoppingCriteria()]
64
+ if terminate_hallucinated_prompts
65
  else None,
66
  temperature=temperature,
67
  top_k=top_k,
 
83
  label="prompt",
84
  value="Write a detailed analogy between mathematics and a lighthouse.",
85
  ),
86
+ gr.Slider(minimum=0, maximum=500, step=1, value=50, label="max new tokens"),
87
  gr.Checkbox(
88
  value=True,
89
+ label="terminate hallucinated prompts",
90
  info="stop generation after getting tokens like 'Exercise' or '<|endoftext|>, but will not remove them.",
91
  ),
92
  gr.Checkbox(
 
127
  ],
128
  [
129
  "Instruct: Write a detailed analogy between mathematics and a lighthouse.\nOutput:",
130
+ 100,
131
  ],
132
  [
133
  "Alice: I don't know why, I'm struggling to maintain focus while studying. Any suggestions?\n\nBob: ",
134
  150,
135
  ],
136
  [
137
+ '''```
138
+ def print_prime(n):
139
  """
140
  Print all primes between 1 and n
141
  """\n''',
142
+ 125,
143
  ],
144
+ ["User: How does sleep affect mood?\nAI:", 100],
145
+ ["Who was Ada Lovelace?", 25],
146
+ ["Explain the concept of skip lists.", 400],
147
  ],
148
  title="Microsoft Phi-2",
149
  description="Unofficial demo of Microsoft Phi-2, a high performing model with only 2.7B parameters.",