jejunepixels commited on
Commit
7c13c11
·
verified ·
1 Parent(s): 18621b5

Automatically continue generation until stopped

Browse files
Files changed (3) hide show
  1. README.md +8 -1
  2. app.py +12 -5
  3. engine.py +2 -2
README.md CHANGED
@@ -47,6 +47,13 @@ Extract the archive and upload its six files to the root of your Gradio Space.
47
 
48
  ## Controls
49
 
 
 
 
 
 
 
 
50
  - **Begin:** replace the stream and initialize a new random sequence.
51
  - **Stop:** request cancellation; the current inference step completes first.
52
  - **Continue:** preserve generated token IDs and RNG state, including across a stop.
@@ -96,7 +103,7 @@ cap is 2 GB (`FLOW_MAX_DOWNLOAD_GB` can change it); this is not a RAM guarantee.
96
  Use compatible small models. Cached files persist until manually removed; ephemeral
97
  Space storage may be reset by a restart or rebuild.
98
 
99
- A pass generates at most 1,024 tokens. Continue extends it up to 16,384 tokens per
100
  session. The context defaults to the most recent 128 tokens. The Memory span slider
101
  can select 16?2,048 tokens, bounded by the model's smaller advertised context limit.
102
  Memory changes apply while generating, including shrinking or expanding the active
 
47
 
48
  ## Controls
49
 
50
+ **Automatically continue** is on by default. Generation keeps flowing past the
51
+ pass limit without restarting the model cache or random sequence. Stop, an emitted
52
+ end token, and the 16,384-token session limit still end the stream. Toggle it off
53
+ live to use Tokens per pass; if that limit has already been passed, generation
54
+ stops after the current token. This removes manual continuation pauses; CPU and
55
+ context-rebuilding delays can still occur.
56
+
57
  - **Begin:** replace the stream and initialize a new random sequence.
58
  - **Stop:** request cancellation; the current inference step completes first.
59
  - **Continue:** preserve generated token IDs and RNG state, including across a stop.
 
103
  Use compatible small models. Cached files persist until manually removed; ephemeral
104
  Space storage may be reset by a restart or rebuild.
105
 
106
+ With automatic continuation off, a pass generates at most 1,024 tokens. Continue extends it up to 16,384 tokens per
107
  session. The context defaults to the most recent 128 tokens. The Memory span slider
108
  can select 16?2,048 tokens, bounded by the model's smaller advertised context limit.
109
  Memory changes apply while generating, including shrinking or expanding the active
app.py CHANGED
@@ -67,13 +67,15 @@ def control_handler(name):
67
  return update
68
 
69
 
70
- def run(flow, temperature, top_p, penalty, seed, count, watch, delay, memory=128, fresh=False):
 
71
  if STORE.model is None:
72
  raise gr.Error("Load a model first.")
73
  if fresh:
74
  clear(flow)
75
  flow.update_controls(temperature=temperature, top_p=top_p, penalty=penalty,
76
- count=count, watch=watch, delay=delay, memory=memory)
 
77
  flow.stop.clear()
78
  panel = EMPTY
79
  started = time.monotonic()
@@ -97,6 +99,8 @@ def run(flow, temperature, top_p, penalty, seed, count, watch, delay, memory=128
97
  f" · memory {step['context_limit']} tokens"
98
  f" · temperature {step['temperature']:g} · top-p {step['top_p']:g}"
99
  f" · repetition {step['penalty']:g}")
 
 
100
  if trimmed:
101
  status += f" · Only the latest {step['context_limit']} tokens inform the next choice."
102
  yield last_text, panel if watching else "", status
@@ -151,6 +155,9 @@ def build_app():
151
  "2 GB download limit by default. One model is shared by visitors; loading another replaces it.")
152
  with gr.Group():
153
  gr.Markdown("### Live controls\nAdjust these sliders while the stream runs.")
 
 
 
154
  gr.Markdown("Defaults favor wandering associations. Shorter memory loosens the connection "
155
  "to earlier text; higher temperature adds surprise and can also break language.")
156
  with gr.Row():
@@ -167,7 +174,7 @@ def build_app():
167
  seed = gr.Number(value=None, precision=0, label="Random seed (optional)",
168
  info="Blank chooses a seed. Applied on Begin; Continue keeps the RNG sequence.")
169
  count = gr.Slider(1, 1024, value=256, step=1, label="Tokens per pass",
170
- info="Changes the current pass limit live. Increase it before the pass ends to extend it.")
171
  delay = gr.Slider(0, .5, value=.06, step=.02, label="Reading delay per token (seconds)",
172
  info="Slows the display, without changing the sampling distribution.")
173
  with gr.Row():
@@ -193,11 +200,11 @@ def build_app():
193
  "Model output may take any form. Sliders apply live from the next token after the update arrives; "
194
  "the current inference step completes first. Seed applies on Begin, which replaces the current stream.")
195
  outputs = [page, panel, status]
196
- inputs = [flow, temperature, top_p, penalty, seed, count, watch, delay, memory]
197
  common = dict(concurrency_id="model", concurrency_limit=1)
198
  for component, name in [(temperature, "temperature"), (top_p, "top_p"),
199
  (penalty, "penalty"), (memory, "memory"), (count, "count"),
200
- (delay, "delay"), (watch, "watch")]:
201
  component.input(control_handler(name), [component, flow], [], queue=False,
202
  trigger_mode="always_last", api_name=f"update_{name}", show_progress="hidden")
203
  load.click(load_model, [repo, flow], [model_status, *outputs], **common)
 
67
  return update
68
 
69
 
70
+ def run(flow, temperature, top_p, penalty, seed, count, watch, delay, memory=128,
71
+ auto_continue=True, fresh=False):
72
  if STORE.model is None:
73
  raise gr.Error("Load a model first.")
74
  if fresh:
75
  clear(flow)
76
  flow.update_controls(temperature=temperature, top_p=top_p, penalty=penalty,
77
+ count=count, watch=watch, delay=delay, memory=memory,
78
+ auto_continue=auto_continue)
79
  flow.stop.clear()
80
  panel = EMPTY
81
  started = time.monotonic()
 
99
  f" · memory {step['context_limit']} tokens"
100
  f" · temperature {step['temperature']:g} · top-p {step['top_p']:g}"
101
  f" · repetition {step['penalty']:g}")
102
+ if controls["auto_continue"]:
103
+ status += " · automatic continuation on"
104
  if trimmed:
105
  status += f" · Only the latest {step['context_limit']} tokens inform the next choice."
106
  yield last_text, panel if watching else "", status
 
155
  "2 GB download limit by default. One model is shared by visitors; loading another replaces it.")
156
  with gr.Group():
157
  gr.Markdown("### Live controls\nAdjust these sliders while the stream runs.")
158
+ auto_continue = gr.Checkbox(label="Automatically continue", value=True,
159
+ info="Keep flowing until Stop, an end token, or 16,384 tokens. "
160
+ "Turn off to use the pass limit; if already past it, the stream stops after the current token.")
161
  gr.Markdown("Defaults favor wandering associations. Shorter memory loosens the connection "
162
  "to earlier text; higher temperature adds surprise and can also break language.")
163
  with gr.Row():
 
174
  seed = gr.Number(value=None, precision=0, label="Random seed (optional)",
175
  info="Blank chooses a seed. Applied on Begin; Continue keeps the RNG sequence.")
176
  count = gr.Slider(1, 1024, value=256, step=1, label="Tokens per pass",
177
+ info="Used when Automatically continue is off. Changes apply live.")
178
  delay = gr.Slider(0, .5, value=.06, step=.02, label="Reading delay per token (seconds)",
179
  info="Slows the display, without changing the sampling distribution.")
180
  with gr.Row():
 
200
  "Model output may take any form. Sliders apply live from the next token after the update arrives; "
201
  "the current inference step completes first. Seed applies on Begin, which replaces the current stream.")
202
  outputs = [page, panel, status]
203
+ inputs = [flow, temperature, top_p, penalty, seed, count, watch, delay, memory, auto_continue]
204
  common = dict(concurrency_id="model", concurrency_limit=1)
205
  for component, name in [(temperature, "temperature"), (top_p, "top_p"),
206
  (penalty, "penalty"), (memory, "memory"), (count, "count"),
207
+ (delay, "delay"), (watch, "watch"), (auto_continue, "auto_continue")]:
208
  component.input(control_handler(name), [component, flow], [], queue=False,
209
  trigger_mode="always_last", api_name=f"update_{name}", show_progress="hidden")
210
  load.click(load_model, [repo, flow], [model_status, *outputs], **common)
engine.py CHANGED
@@ -21,7 +21,7 @@ class Flow:
21
  bounds = {"temperature": (.05, 2), "top_p": (.05, 1), "penalty": (1, 2),
22
  "memory": (16, 2048), "count": (1, 1024), "delay": (0, .5)}
23
  for name, value in changes.items():
24
- if name == "watch":
25
  if not isinstance(value, bool):
26
  raise ValueError("Watch must be true or false.")
27
  elif name not in bounds or not bounds[name][0] <= value <= bounds[name][1]:
@@ -100,7 +100,7 @@ def generate(model, tokenizer, flow, key, count, temperature, top_p, penalty, se
100
  with torch.inference_mode():
101
  while True:
102
  settings = live_settings() if live_settings is not None else {}
103
- if emitted >= int(settings.get("count", count)):
104
  break
105
  next_limit = min(int(settings.get("memory", context_limit)),
106
  int(getattr(model.config, "max_position_embeddings", context_limit)))
 
21
  bounds = {"temperature": (.05, 2), "top_p": (.05, 1), "penalty": (1, 2),
22
  "memory": (16, 2048), "count": (1, 1024), "delay": (0, .5)}
23
  for name, value in changes.items():
24
+ if name in ("watch", "auto_continue"):
25
  if not isinstance(value, bool):
26
  raise ValueError("Watch must be true or false.")
27
  elif name not in bounds or not bounds[name][0] <= value <= bounds[name][1]:
 
100
  with torch.inference_mode():
101
  while True:
102
  settings = live_settings() if live_settings is not None else {}
103
+ if not settings.get("auto_continue", False) and emitted >= int(settings.get("count", count)):
104
  break
105
  next_limit = min(int(settings.get("memory", context_limit)),
106
  int(getattr(model.config, "max_position_embeddings", context_limit)))