Spaces:
Running on Zero
Running on Zero
Upload app.py with huggingface_hub
Browse files
app.py
CHANGED
|
@@ -20,34 +20,36 @@ class CanvasStreamer(TextDiffusionStreamer):
|
|
| 20 |
|
| 21 |
`put_draft` fires every denoising step with the full canvas of the block being
|
| 22 |
denoised (the text morphs as it denoises). `put` fires when a canvas/block is
|
| 23 |
-
committed.
|
| 24 |
"""
|
| 25 |
|
| 26 |
def __init__(self, tokenizer, **kwargs):
|
| 27 |
-
super().__init__(tokenizer,
|
| 28 |
self.queue = Queue()
|
| 29 |
self.committed = ""
|
|
|
|
|
|
|
| 30 |
self._takes_logits = False
|
| 31 |
|
| 32 |
def put_draft(self, value, **kwargs):
|
| 33 |
if len(value.shape) > 1:
|
| 34 |
value = value[0]
|
| 35 |
-
|
| 36 |
-
self.
|
|
|
|
| 37 |
|
| 38 |
def put(self, value):
|
| 39 |
if len(value.shape) > 1 and value.shape[0] > 1:
|
| 40 |
raise ValueError("batch size 1 only")
|
| 41 |
elif len(value.shape) > 1:
|
| 42 |
value = value[0]
|
| 43 |
-
if
|
| 44 |
-
self.next_tokens_are_prompt = False
|
| 45 |
return
|
| 46 |
self.committed += self.tokenizer.decode(value, skip_special_tokens=True)
|
|
|
|
| 47 |
self.queue.put((self.committed, ""))
|
| 48 |
|
| 49 |
def end(self):
|
| 50 |
-
self.next_tokens_are_prompt = True
|
| 51 |
self.queue.put(_SENTINEL)
|
| 52 |
|
| 53 |
|
|
@@ -72,7 +74,6 @@ def generate_streaming(messages, max_new_tokens, max_denoising_steps, enable_thi
|
|
| 72 |
return_tensors="pt",
|
| 73 |
enable_thinking=enable_thinking,
|
| 74 |
).to("cuda")
|
| 75 |
-
prompt_len = inputs["input_ids"].shape[1]
|
| 76 |
|
| 77 |
streamer = CanvasStreamer(processor.tokenizer)
|
| 78 |
result = {}
|
|
@@ -104,8 +105,7 @@ def generate_streaming(messages, max_new_tokens, max_denoising_steps, enable_thi
|
|
| 104 |
if "error" in result:
|
| 105 |
raise result["error"]
|
| 106 |
|
| 107 |
-
|
| 108 |
-
final_text = processor.decode(generated, skip_special_tokens=True).strip()
|
| 109 |
yield build_highlight(final_text, ""), final_text, final_text
|
| 110 |
|
| 111 |
|
|
|
|
| 20 |
|
| 21 |
`put_draft` fires every denoising step with the full canvas of the block being
|
| 22 |
denoised (the text morphs as it denoises). `put` fires when a canvas/block is
|
| 23 |
+
committed. Any `put` before denoising starts is the prompt context and is ignored.
|
| 24 |
"""
|
| 25 |
|
| 26 |
def __init__(self, tokenizer, **kwargs):
|
| 27 |
+
super().__init__(tokenizer, skip_special_tokens=True, **kwargs)
|
| 28 |
self.queue = Queue()
|
| 29 |
self.committed = ""
|
| 30 |
+
self.last_draft = ""
|
| 31 |
+
self.started = False
|
| 32 |
self._takes_logits = False
|
| 33 |
|
| 34 |
def put_draft(self, value, **kwargs):
|
| 35 |
if len(value.shape) > 1:
|
| 36 |
value = value[0]
|
| 37 |
+
self.started = True
|
| 38 |
+
self.last_draft = self.tokenizer.decode(value, skip_special_tokens=True)
|
| 39 |
+
self.queue.put((self.committed, self.last_draft))
|
| 40 |
|
| 41 |
def put(self, value):
|
| 42 |
if len(value.shape) > 1 and value.shape[0] > 1:
|
| 43 |
raise ValueError("batch size 1 only")
|
| 44 |
elif len(value.shape) > 1:
|
| 45 |
value = value[0]
|
| 46 |
+
if not self.started: # prompt context, before any denoising step
|
|
|
|
| 47 |
return
|
| 48 |
self.committed += self.tokenizer.decode(value, skip_special_tokens=True)
|
| 49 |
+
self.last_draft = ""
|
| 50 |
self.queue.put((self.committed, ""))
|
| 51 |
|
| 52 |
def end(self):
|
|
|
|
| 53 |
self.queue.put(_SENTINEL)
|
| 54 |
|
| 55 |
|
|
|
|
| 74 |
return_tensors="pt",
|
| 75 |
enable_thinking=enable_thinking,
|
| 76 |
).to("cuda")
|
|
|
|
| 77 |
|
| 78 |
streamer = CanvasStreamer(processor.tokenizer)
|
| 79 |
result = {}
|
|
|
|
| 105 |
if "error" in result:
|
| 106 |
raise result["error"]
|
| 107 |
|
| 108 |
+
final_text = (streamer.committed + streamer.last_draft).strip()
|
|
|
|
| 109 |
yield build_highlight(final_text, ""), final_text, final_text
|
| 110 |
|
| 111 |
|