Spaces:
Sleeping
Sleeping
Commit ·
c683388
1
Parent(s): 0e999fe
Try streaming again
Browse files
main.py
CHANGED
|
@@ -98,7 +98,7 @@ def clean_output(raw_prediction: str) -> str:
|
|
| 98 |
|
| 99 |
return predicted_code + "\n" + "\n".join(udt.declaration("") for udt in predicted_udts)
|
| 100 |
|
| 101 |
-
from transformers import AutoModelForCausalLM, AutoTokenizer, pipeline #, BitsAndBytesConfig
|
| 102 |
from peft import PeftModel
|
| 103 |
|
| 104 |
# Define your model and adapter identifiers
|
|
@@ -169,29 +169,53 @@ def _predict_sync(inp):
|
|
| 169 |
|
| 170 |
|
| 171 |
def predict(inp):
|
| 172 |
-
# Stream
|
| 173 |
import threading, time
|
| 174 |
-
|
| 175 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 176 |
try:
|
| 177 |
-
|
|
|
|
|
|
|
|
|
|
| 178 |
except Exception as e:
|
| 179 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 180 |
|
| 181 |
-
t = threading.Thread(target=
|
| 182 |
t.start()
|
| 183 |
|
| 184 |
-
|
| 185 |
-
#
|
| 186 |
-
|
| 187 |
-
|
| 188 |
-
yield
|
| 189 |
-
time.sleep(1)
|
| 190 |
|
| 191 |
t.join()
|
| 192 |
-
|
| 193 |
-
|
| 194 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 195 |
|
| 196 |
def run():
|
| 197 |
|
|
|
|
| 98 |
|
| 99 |
return predicted_code + "\n" + "\n".join(udt.declaration("") for udt in predicted_udts)
|
| 100 |
|
| 101 |
+
from transformers import AutoModelForCausalLM, AutoTokenizer, pipeline, TextIteratorStreamer #, BitsAndBytesConfig
|
| 102 |
from peft import PeftModel
|
| 103 |
|
| 104 |
# Define your model and adapter identifiers
|
|
|
|
| 169 |
|
| 170 |
|
| 171 |
def predict(inp):
|
| 172 |
+
# Stream tokens without moving model weights; only move input tensors to model device
|
| 173 |
import threading, time
|
| 174 |
+
initial_tokens = len(tokenizer.tokenize(inp))
|
| 175 |
+
new_tokens = math.floor(initial_tokens * 1.5)
|
| 176 |
+
|
| 177 |
+
inp2 = canonicalize_output(inp)
|
| 178 |
+
# Prepare inputs on CPU then move to model device when generating
|
| 179 |
+
inputs = tokenizer(inp2, return_tensors="pt")
|
| 180 |
+
|
| 181 |
+
# Use TextIteratorStreamer to stream tokens as they're generated
|
| 182 |
+
streamer = TextIteratorStreamer(tokenizer, skip_prompt=True, timeout=10.0)
|
| 183 |
+
|
| 184 |
+
def _generate():
|
| 185 |
try:
|
| 186 |
+
device = next(model.parameters()).device
|
| 187 |
+
inputs_device = {k: v.to(device) for k, v in inputs.items()}
|
| 188 |
+
# Call model.generate which will use streamer to yield tokens; model weights stay on their device
|
| 189 |
+
model.generate(**inputs_device, max_new_tokens=new_tokens, streamer=streamer)
|
| 190 |
except Exception as e:
|
| 191 |
+
try:
|
| 192 |
+
streamer.end()
|
| 193 |
+
except:
|
| 194 |
+
pass
|
| 195 |
+
# Attach exception to streamer by yielding nothing and storing in container
|
| 196 |
+
streamer.exception = e
|
| 197 |
|
| 198 |
+
t = threading.Thread(target=_generate, daemon=True)
|
| 199 |
t.start()
|
| 200 |
|
| 201 |
+
aggregated = ""
|
| 202 |
+
# Yield incremental aggregated text as tokenizer emits chunks
|
| 203 |
+
for chunk in streamer:
|
| 204 |
+
aggregated += chunk
|
| 205 |
+
yield aggregated
|
|
|
|
| 206 |
|
| 207 |
t.join()
|
| 208 |
+
|
| 209 |
+
# Propagate any generation exception
|
| 210 |
+
if hasattr(streamer, 'exception'):
|
| 211 |
+
raise streamer.exception
|
| 212 |
+
|
| 213 |
+
# Final cleaning of the aggregated output
|
| 214 |
+
try:
|
| 215 |
+
cleaned = clean_output(aggregated)
|
| 216 |
+
yield cleaned
|
| 217 |
+
except Exception:
|
| 218 |
+
yield aggregated
|
| 219 |
|
| 220 |
def run():
|
| 221 |
|