Spaces:
Running on Zero
Running on Zero
fix: minimal gr.Interface tabs (TabbedInterface, not Blocks)
Browse files
app.py
CHANGED
|
@@ -1,173 +1,110 @@
|
|
| 1 |
-
"""surrogate1 ZeroGPU —
|
| 2 |
|
| 3 |
-
|
| 4 |
-
|
| 5 |
"""
|
| 6 |
-
import os, json, re
|
| 7 |
-
import gradio as gr
|
| 8 |
-
import spaces
|
| 9 |
-
import torch
|
| 10 |
|
| 11 |
-
BASE_MODEL = os.environ.get("BASE_MODEL", "Qwen/Qwen2.5-Coder-
|
| 12 |
HF_TOKEN = os.environ.get("HF_TOKEN", "")
|
| 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 |
-
_model = None
|
| 41 |
-
_tok = None
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
def _load_lazy():
|
| 45 |
-
global _model, _tok
|
| 46 |
-
if _model is not None:
|
| 47 |
-
return _model, _tok
|
| 48 |
-
from transformers import AutoModelForCausalLM, AutoTokenizer
|
| 49 |
-
_tok = AutoTokenizer.from_pretrained(
|
| 50 |
-
BASE_MODEL, token=HF_TOKEN or None, trust_remote_code=True)
|
| 51 |
-
if _tok.pad_token_id is None:
|
| 52 |
-
_tok.pad_token_id = _tok.eos_token_id
|
| 53 |
-
from transformers import BitsAndBytesConfig
|
| 54 |
-
bnb = BitsAndBytesConfig(
|
| 55 |
-
load_in_4bit=True,
|
| 56 |
-
bnb_4bit_compute_dtype=torch.bfloat16,
|
| 57 |
-
bnb_4bit_quant_type="nf4",
|
| 58 |
-
bnb_4bit_use_double_quant=True,
|
| 59 |
-
)
|
| 60 |
-
_model = AutoModelForCausalLM.from_pretrained(
|
| 61 |
BASE_MODEL, token=HF_TOKEN or None, trust_remote_code=True,
|
| 62 |
device_map="cuda", quantization_config=bnb)
|
| 63 |
-
return
|
| 64 |
|
| 65 |
|
| 66 |
-
def
|
| 67 |
-
|
| 68 |
-
inputs =
|
| 69 |
-
|
| 70 |
-
|
| 71 |
-
|
| 72 |
-
|
| 73 |
-
pad_token_id=tok.pad_token_id, eos_token_id=tok.eos_token_id)
|
| 74 |
-
return tok.decode(out[0][inputs["input_ids"].shape[1]:],
|
| 75 |
-
skip_special_tokens=True).strip()
|
| 76 |
|
| 77 |
|
| 78 |
@spaces.GPU(duration=300)
|
| 79 |
-
def synth_batch(domain
|
| 80 |
-
|
| 81 |
-
|
| 82 |
out_lines = []
|
| 83 |
for i in range(int(count)):
|
| 84 |
instr_msgs = [
|
| 85 |
{"role": "system",
|
| 86 |
-
"content": f"Generate ONE realistic, diverse, high-quality user request about {
|
| 87 |
"Output ONLY the request as a single paragraph. No preamble."},
|
| 88 |
{"role": "user", "content": ""},
|
| 89 |
]
|
| 90 |
-
ip =
|
| 91 |
-
instruction =
|
| 92 |
if len(instruction) < 30: continue
|
| 93 |
-
|
| 94 |
resp_msgs = [
|
| 95 |
-
{"role": "system",
|
| 96 |
-
"
|
| 97 |
{"role": "user", "content": instruction},
|
| 98 |
]
|
| 99 |
-
rp =
|
| 100 |
-
response =
|
| 101 |
if len(response) < 50: continue
|
| 102 |
-
|
| 103 |
out_lines.append(json.dumps({
|
| 104 |
"prompt": instruction, "response": response,
|
| 105 |
-
"source": "magpie-zerogpu
|
| 106 |
"meta": {"domain": domain, "ix": i},
|
| 107 |
}, ensure_ascii=False))
|
| 108 |
return "\n".join(out_lines)
|
| 109 |
|
| 110 |
|
| 111 |
@spaces.GPU(duration=120)
|
| 112 |
-
def judge_pair(prompt, response
|
| 113 |
if not prompt or not response:
|
| 114 |
return json.dumps({"score": 0.0, "why": "empty"})
|
| 115 |
-
|
| 116 |
-
j_msgs = [
|
| 117 |
-
|
| 118 |
-
|
| 119 |
-
|
| 120 |
-
|
| 121 |
-
|
| 122 |
-
|
| 123 |
-
if m:
|
| 124 |
-
try: return json.dumps(json.loads(m.group(0)), ensure_ascii=False)
|
| 125 |
except: pass
|
| 126 |
-
return json.dumps({"score": 5.0, "why": raw[:
|
| 127 |
|
| 128 |
|
| 129 |
-
|
| 130 |
-
|
| 131 |
-
|
| 132 |
-
|
| 133 |
-
|
| 134 |
-
|
| 135 |
-
|
| 136 |
-
|
| 137 |
-
|
| 138 |
-
|
| 139 |
-
|
| 140 |
-
|
| 141 |
-
|
| 142 |
-
|
| 143 |
-
if "import " in c or "def " in c: s += 0.1
|
| 144 |
-
scored.append((s, c))
|
| 145 |
-
scored.sort(key=lambda x: -x[0])
|
| 146 |
-
return json.dumps({"best": scored[0][1], "best_score": scored[0][0],
|
| 147 |
-
"all": [c for _, c in scored]}, ensure_ascii=False)
|
| 148 |
-
|
| 149 |
-
|
| 150 |
-
with gr.Blocks(title="Surrogate-1 32B synth+judge") as demo:
|
| 151 |
-
gr.Markdown(
|
| 152 |
-
f"# Surrogate-1 — synth + judge (Qwen2.5-Coder-32B AWQ)\n"
|
| 153 |
-
f"3 endpoints: `/run/synth_batch`, `/run/judge_pair`, `/run/best_of_n`"
|
| 154 |
-
)
|
| 155 |
-
with gr.Tab("synth"):
|
| 156 |
-
d = gr.Dropdown(list(DOMAIN_SEEDS.keys()), value="code-python", label="domain")
|
| 157 |
-
n = gr.Slider(1, 20, value=10, step=1, label="count")
|
| 158 |
-
gr.Button("synth_batch").click(synth_batch, [d, n], gr.Textbox(label="JSONL", lines=20),
|
| 159 |
-
api_name="synth_batch")
|
| 160 |
-
with gr.Tab("judge"):
|
| 161 |
-
p = gr.Textbox(label="prompt", lines=3); r = gr.Textbox(label="response", lines=8)
|
| 162 |
-
c = gr.Textbox(label="criteria", value="default")
|
| 163 |
-
gr.Button("judge_pair").click(judge_pair, [p, r, c], gr.Textbox(label="JSON", lines=8),
|
| 164 |
-
api_name="judge_pair")
|
| 165 |
-
with gr.Tab("best-of-n"):
|
| 166 |
-
bp = gr.Textbox(label="prompt", lines=3)
|
| 167 |
-
bn = gr.Slider(2, 6, value=4, step=1, label="n")
|
| 168 |
-
bm = gr.Slider(128, 1024, value=512, step=64, label="max_new")
|
| 169 |
-
gr.Button("best_of_n").click(best_of_n, [bp, bn, bm], gr.Textbox(label="JSON", lines=15),
|
| 170 |
-
api_name="best_of_n")
|
| 171 |
-
|
| 172 |
-
if __name__ == "__main__":
|
| 173 |
-
demo.queue(max_size=6).launch()
|
|
|
|
| 1 |
+
"""surrogate1 ZeroGPU synth+judge — minimal gr.Interface tabs via Blocks.
|
| 2 |
|
| 3 |
+
Same gradio-compat pattern as ashirato (gr.Interface only, no ChatInterface).
|
| 4 |
+
3 endpoints exposed via separate Interfaces, mounted on TabbedInterface.
|
| 5 |
"""
|
| 6 |
+
import os, json, re, gradio as gr, spaces, torch
|
|
|
|
|
|
|
|
|
|
| 7 |
|
| 8 |
+
BASE_MODEL = os.environ.get("BASE_MODEL", "Qwen/Qwen2.5-Coder-7B-Instruct")
|
| 9 |
HF_TOKEN = os.environ.get("HF_TOKEN", "")
|
| 10 |
|
| 11 |
+
DOMAINS = ["code-python", "code-typescript", "code-rust", "code-go",
|
| 12 |
+
"devops-tf", "devops-k8s", "devops-cdk", "ci-github",
|
| 13 |
+
"sec-iam", "sec-cve", "sre-runbook", "sre-slo",
|
| 14 |
+
"data-sql", "ai-eng", "api-rest", "test-pytest"]
|
| 15 |
+
SEED_TPL = "a {} task"
|
| 16 |
+
|
| 17 |
+
JUDGE_RULES = ("Score 0-10: correctness, security, idiomatic, completeness, "
|
| 18 |
+
"real-API citation. Return ONLY JSON: "
|
| 19 |
+
'{"score":float,"why":str}')
|
| 20 |
+
|
| 21 |
+
_m = _t = None
|
| 22 |
+
|
| 23 |
+
|
| 24 |
+
def _load():
|
| 25 |
+
global _m, _t
|
| 26 |
+
if _m is not None: return _m, _t
|
| 27 |
+
from transformers import AutoModelForCausalLM, AutoTokenizer, BitsAndBytesConfig
|
| 28 |
+
_t = AutoTokenizer.from_pretrained(BASE_MODEL, token=HF_TOKEN or None,
|
| 29 |
+
trust_remote_code=True)
|
| 30 |
+
if _t.pad_token_id is None: _t.pad_token_id = _t.eos_token_id
|
| 31 |
+
bnb = BitsAndBytesConfig(load_in_4bit=True,
|
| 32 |
+
bnb_4bit_compute_dtype=torch.bfloat16,
|
| 33 |
+
bnb_4bit_quant_type="nf4",
|
| 34 |
+
bnb_4bit_use_double_quant=True)
|
| 35 |
+
_m = AutoModelForCausalLM.from_pretrained(
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 36 |
BASE_MODEL, token=HF_TOKEN or None, trust_remote_code=True,
|
| 37 |
device_map="cuda", quantization_config=bnb)
|
| 38 |
+
return _m, _t
|
| 39 |
|
| 40 |
|
| 41 |
+
def _gen(prompt, max_new=512, temp=0.7):
|
| 42 |
+
m, t = _load()
|
| 43 |
+
inputs = t(prompt, return_tensors="pt", truncation=True, max_length=8000).to("cuda")
|
| 44 |
+
out = m.generate(**inputs, max_new_tokens=int(max_new),
|
| 45 |
+
temperature=float(temp), top_p=0.9, do_sample=temp > 0,
|
| 46 |
+
pad_token_id=t.pad_token_id, eos_token_id=t.eos_token_id)
|
| 47 |
+
return t.decode(out[0][inputs["input_ids"].shape[1]:], skip_special_tokens=True).strip()
|
|
|
|
|
|
|
|
|
|
| 48 |
|
| 49 |
|
| 50 |
@spaces.GPU(duration=300)
|
| 51 |
+
def synth_batch(domain: str, count: int) -> str:
|
| 52 |
+
m, t = _load()
|
| 53 |
+
seed = SEED_TPL.format(domain.replace("-", " "))
|
| 54 |
out_lines = []
|
| 55 |
for i in range(int(count)):
|
| 56 |
instr_msgs = [
|
| 57 |
{"role": "system",
|
| 58 |
+
"content": f"Generate ONE realistic, diverse, high-quality user request about {seed}. "
|
| 59 |
"Output ONLY the request as a single paragraph. No preamble."},
|
| 60 |
{"role": "user", "content": ""},
|
| 61 |
]
|
| 62 |
+
ip = t.apply_chat_template(instr_msgs, tokenize=False, add_generation_prompt=True)
|
| 63 |
+
instruction = _gen(ip, max_new=180, temp=0.95).strip().split("\n")[0][:600]
|
| 64 |
if len(instruction) < 30: continue
|
|
|
|
| 65 |
resp_msgs = [
|
| 66 |
+
{"role": "system",
|
| 67 |
+
"content": "You are Surrogate-1, expert DevSecOps + coding agent. Real APIs only."},
|
| 68 |
{"role": "user", "content": instruction},
|
| 69 |
]
|
| 70 |
+
rp = t.apply_chat_template(resp_msgs, tokenize=False, add_generation_prompt=True)
|
| 71 |
+
response = _gen(rp, max_new=512, temp=0.4)
|
| 72 |
if len(response) < 50: continue
|
|
|
|
| 73 |
out_lines.append(json.dumps({
|
| 74 |
"prompt": instruction, "response": response,
|
| 75 |
+
"source": "magpie-zerogpu",
|
| 76 |
"meta": {"domain": domain, "ix": i},
|
| 77 |
}, ensure_ascii=False))
|
| 78 |
return "\n".join(out_lines)
|
| 79 |
|
| 80 |
|
| 81 |
@spaces.GPU(duration=120)
|
| 82 |
+
def judge_pair(prompt: str, response: str) -> str:
|
| 83 |
if not prompt or not response:
|
| 84 |
return json.dumps({"score": 0.0, "why": "empty"})
|
| 85 |
+
m, t = _load()
|
| 86 |
+
j_msgs = [{"role": "system", "content": "You are a strict reviewer. " + JUDGE_RULES},
|
| 87 |
+
{"role": "user", "content": f"PROMPT:\n{prompt[:2000]}\n\nRESPONSE:\n{response[:4000]}\n\nScore. JSON only."}]
|
| 88 |
+
raw = _gen(t.apply_chat_template(j_msgs, tokenize=False, add_generation_prompt=True),
|
| 89 |
+
max_new=300, temp=0.1)
|
| 90 |
+
mm = re.search(r"\{[^{}]*\"score\"[^{}]*\}", raw, re.DOTALL)
|
| 91 |
+
if mm:
|
| 92 |
+
try: return json.dumps(json.loads(mm.group(0)), ensure_ascii=False)
|
|
|
|
|
|
|
| 93 |
except: pass
|
| 94 |
+
return json.dumps({"score": 5.0, "why": raw[:200], "raw": True})
|
| 95 |
|
| 96 |
|
| 97 |
+
synth_iface = gr.Interface(synth_batch,
|
| 98 |
+
[gr.Dropdown(DOMAINS, value="code-python", label="domain"),
|
| 99 |
+
gr.Slider(1, 20, value=10, step=1, label="count")],
|
| 100 |
+
gr.Textbox(label="JSONL", lines=20),
|
| 101 |
+
title="Magpie synth_batch")
|
| 102 |
+
|
| 103 |
+
judge_iface = gr.Interface(judge_pair,
|
| 104 |
+
[gr.Textbox(label="prompt", lines=3),
|
| 105 |
+
gr.Textbox(label="response", lines=8)],
|
| 106 |
+
gr.Textbox(label="JSON", lines=6),
|
| 107 |
+
title="LLM-as-judge")
|
| 108 |
+
|
| 109 |
+
demo = gr.TabbedInterface([synth_iface, judge_iface], ["synth", "judge"])
|
| 110 |
+
demo.queue(max_size=6).launch()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|