File size: 8,709 Bytes
1419b82
 
 
 
 
 
 
 
 
 
8fce99a
 
 
 
 
 
 
1419b82
 
 
 
 
 
 
 
 
 
 
8fce99a
 
 
 
765dead
 
1419b82
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
8fce99a
 
 
 
 
 
 
8f651ac
1419b82
 
f1b8cae
 
 
 
 
 
 
 
 
 
 
 
 
8fce99a
f1b8cae
 
 
 
 
 
8fce99a
f1b8cae
 
 
 
 
 
 
 
 
 
 
 
 
 
 
8f651ac
 
 
1419b82
 
8f651ac
8fce99a
 
1419b82
 
 
 
 
 
 
 
 
f1b8cae
 
 
 
7dbf1b6
8f651ac
8fce99a
 
8f651ac
7dbf1b6
 
 
 
 
 
8fce99a
7dbf1b6
 
8fce99a
7dbf1b6
 
 
f1b8cae
7dbf1b6
 
f1b8cae
 
 
 
 
7dbf1b6
 
f1b8cae
 
 
7dbf1b6
 
 
1419b82
 
 
f1b8cae
7dbf1b6
8f651ac
 
8fce99a
f1b8cae
7dbf1b6
 
 
1419b82
 
 
 
 
 
 
 
 
f1b8cae
 
1419b82
 
 
 
 
f1b8cae
1419b82
f1b8cae
1419b82
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
# Tiny Army β€” BLS Mini-Code 1.0 ZeroGPU coding sidecar.
#
# Exposes the SAME Gradio contract as the Mellum2 / Tiny Aya sidecars so the main app's
# gradio_client can talk to it unchanged (see app.py:_space_text_stream / _space_text_generate):
#   /generate_stream(system, user, max_tokens:int, temperature:float) -> str   # CUMULATIVE text, streamed
#   /generate(system, user, max_tokens:int, temperature:float)        -> str   # final text, one shot
#
# Model: CohereLabs/BLS-Mini-Code-1.0 β€” 30B MoE (cohere2_moe), BF16 only upstream (no FP8
# weight published as of 2026-06), so we quantize AT LOAD via bitsandbytes to fit the ZeroGPU
# H200 slice. TINY_BLS_QUANT selects 4bit (default, ~18GB) / 8bit (~32GB) / bf16 (~60GB, tight).
#
# REASONING: BLS-Mini-Code is a Cohere reasoning model. Its chat template, with
# add_generation_prompt=True, force-opens <|START_RESPONSE|> (non-reasoning mode) β€” which makes
# the model dump its reasoning as prose into the answer. Instead we open a <|START_THINKING|>
# block so it reasons in a dedicated section we DISCARD, and we stream only the clean code from
# <|START_RESPONSE|>…<|END_RESPONSE|>. TINY_BLS_THINK_BUDGET extra tokens are reserved for the
# (discarded) thinking so the requested max_tokens still applies to the visible code.
import os
import threading

import gradio as gr
import spaces
import torch
from transformers import AutoModelForCausalLM, AutoTokenizer, TextIteratorStreamer

MODEL_ID = os.environ.get("TINY_BLS_MODEL", "CohereLabs/BLS-Mini-Code-1.0")
QUANT = os.environ.get("TINY_BLS_QUANT", "4bit").strip().lower()
GPU_DURATION = int(os.environ.get("TINY_BLS_GPU_DURATION", "120"))
THINK_BUDGET = int(os.environ.get("TINY_BLS_THINK_BUDGET", "1024"))

START_THINK, END_THINK = "<|START_THINKING|>", "<|END_THINKING|>"
START_RESP, END_RESP = "<|START_RESPONSE|>", "<|END_RESPONSE|>"
_STRIP = (START_THINK, END_THINK, START_RESP, END_RESP,
          "<|START_TEXT|>", "<|END_TEXT|>", "<|END_OF_TURN_TOKEN|>")

print(f"[bls-code] loading {MODEL_ID} quant={QUANT}", flush=True)

_tok = AutoTokenizer.from_pretrained(MODEL_ID)


def _load_kwargs():
    kw = {"torch_dtype": torch.bfloat16, "device_map": "cuda"}
    if QUANT == "bf16":
        return kw
    from transformers import BitsAndBytesConfig

    if QUANT == "8bit":
        kw["quantization_config"] = BitsAndBytesConfig(load_in_8bit=True)
    else:  # 4bit (default)
        kw["quantization_config"] = BitsAndBytesConfig(
            load_in_4bit=True,
            bnb_4bit_quant_type="nf4",
            bnb_4bit_compute_dtype=torch.bfloat16,
            bnb_4bit_use_double_quant=True,
        )
    return kw


_model = AutoModelForCausalLM.from_pretrained(MODEL_ID, **_load_kwargs())
_model.eval()
print("[bls-code] model ready", flush=True)


def _build_inputs(system, user):
    messages = []
    if system and system.strip():
        messages.append({"role": "system", "content": system.strip()})
    messages.append({"role": "user", "content": (user or "").strip()})
    text = _tok.apply_chat_template(messages, add_generation_prompt=True, tokenize=False)
    # The template force-opens <|START_RESPONSE|> (non-reasoning). Swap it for a thinking block
    # so the model reasons where we can discard it, leaving clean code in the response section.
    t = text.rstrip()
    if t.endswith(START_RESP):
        text = t[: -len(START_RESP)] + START_THINK
    enc = _tok(text, return_tensors="pt", add_special_tokens=False)
    return {k: v.to(_model.device) for k, v in enc.items()}


def _clean(s):
    for mark in _STRIP:
        s = s.replace(mark, "")
    return s


def _split(raw):
    """Split a (possibly partial) raw decode into (thinking, response, response_started):
    everything before <|START_RESPONSE|> (or <|END_THINKING|>) is reasoning; the rest, up to
    <|END_RESPONSE|>, is the answer."""
    resp_i = raw.find(START_RESP)
    if resp_i != -1:
        think_part, resp, started = raw[:resp_i], raw[resp_i + len(START_RESP):], True
    else:
        end_t = raw.find(END_THINK)
        if end_t != -1:
            think_part, resp, started = raw[:end_t], raw[end_t + len(END_THINK):], True
        else:
            think_part, resp, started = raw, "", False
    k = resp.find(END_RESP)
    if k != -1:
        resp = resp[:k]
    return _clean(think_part).strip(), _clean(resp).strip(), started


def _render(raw, think):
    """Cumulative output string. think=False β†’ clean answer only (reasoning discarded).
    think=True β†’ reasoning wrapped in <think>…</think> ahead of the answer; the main app
    strips it for the clean view but shows it in a debug panel (same convention the persona
    models use), so the user can watch the model reason."""
    thinking, resp, started = _split(raw)
    if not think:
        return resp
    if started:
        return f"<think>\n{thinking}\n</think>\n{resp}".strip()
    return f"<think>\n{thinking}".strip()


def _gen_kwargs(inputs, max_tokens, temperature):
    temp = float(temperature if temperature is not None else 0.6)
    kw = dict(
        **inputs,
        # Reserve THINK_BUDGET on top so the discarded reasoning doesn't eat the code budget.
        max_new_tokens=int(max_tokens or 512) + THINK_BUDGET,
        do_sample=temp > 0,
        pad_token_id=_tok.pad_token_id or _tok.eos_token_id,
    )
    if temp > 0:
        kw.update(temperature=temp, top_p=0.95)
    return kw


@spaces.GPU(duration=GPU_DURATION)
def generate_stream(system, user, max_tokens, temperature, think=False):
    """Stream CUMULATIVE output. think=False suppresses reasoning (clean code only); think=True
    streams the reasoning live wrapped in <think>…</think>. The main app diffs successive yields
    into deltas. On failure, yield the traceback so it isn't a silent empty stream."""
    try:
        inputs = _build_inputs(system, user)
        # skip_special_tokens=False so we can SEE the thinking/response markers and split on them.
        streamer = TextIteratorStreamer(_tok, skip_prompt=True, skip_special_tokens=False)
        kw = _gen_kwargs(inputs, max_tokens, temperature)
        kw["streamer"] = streamer
        err = {}

        def _run():
            try:
                _model.generate(**kw)
            except Exception:  # noqa: BLE001
                import traceback
                err["tb"] = traceback.format_exc()
                streamer.end()

        thread = threading.Thread(target=_run)
        thread.start()
        acc, emitted = "", False
        for piece in streamer:
            acc += piece
            # When hiding thinking, emit nothing until the response block opens.
            if not think and not _split(acc)[2]:
                continue
            emitted = True
            yield _render(acc, think)
        thread.join()
        if err:
            yield (_render(acc, think) + "\n[GENERATE ERROR]\n" + err["tb"])
        elif not emitted:
            yield _render(acc, think) or "[EMPTY OUTPUT β€” no response block produced]"
    except Exception:  # noqa: BLE001
        import traceback
        yield "[SETUP ERROR]\n" + traceback.format_exc()


@spaces.GPU(duration=GPU_DURATION)
def generate(system, user, max_tokens, temperature, think=False):
    try:
        inputs = _build_inputs(system, user)
        out = _model.generate(**_gen_kwargs(inputs, max_tokens, temperature))
        raw = _tok.decode(out[0][inputs["input_ids"].shape[-1]:], skip_special_tokens=False)
        return _render(raw, think) or "[EMPTY OUTPUT]"
    except Exception:  # noqa: BLE001
        import traceback
        return "[ERROR]\n" + traceback.format_exc()


# Minimal UI; the named API endpoints are what the main app consumes.
with gr.Blocks(title="BLS Mini-Code 1.0 β€” Tiny Army sidecar") as demo:
    gr.Markdown("## BLS Mini-Code 1.0 β€” ZeroGPU coding sidecar")
    sys_in = gr.Textbox(label="system", lines=2)
    usr_in = gr.Textbox(label="user", lines=6)
    mt_in = gr.Slider(16, 2048, value=512, step=16, label="max_tokens")
    temp_in = gr.Slider(0.0, 1.5, value=0.6, step=0.05, label="temperature")
    # 5th input β€” defaults False so existing 4-arg API callers keep getting clean code.
    think_in = gr.Checkbox(value=False, label="show thinking (wrap reasoning in <think>…</think>)")
    out = gr.Textbox(label="output", lines=12)
    with gr.Row():
        stream_btn = gr.Button("Stream", variant="primary")
        once_btn = gr.Button("Generate")
    stream_btn.click(
        generate_stream, [sys_in, usr_in, mt_in, temp_in, think_in], out, api_name="generate_stream"
    )
    once_btn.click(generate, [sys_in, usr_in, mt_in, temp_in, think_in], out, api_name="generate")

if __name__ == "__main__":
    demo.queue().launch()