Spaces:
Paused
Paused
Deploy evaluated source-constrained Qwen V2 serving
Browse filesFiles changed (2)
- app.py+10-1
- security_knowledge.py+35-1
65 unmodified lines6667686969707172821 unmodified lines8948958968978988999009019029039043 unmodified lines90890991091191291391491591691791891992092119 unmodified lines94194294394494594694794865 unmodified lines# It replaces Gemini and needs no API key or credit. Any GGUF chat model can be# set here; on the free cpu-basic tier (2 vCPU) stay around 2B parameters or the# answers will not fit inside the callers' timeouts.from security_knowledge import ground_prompt, finish_grounded_answerfrom security_knowledge import ground_prompt, finish_grounded_answer, reference_grammar
LOCAL_LLM_REPO = os.environ.get("LOCAL_LLM_REPO", "unsloth/Qwen3.5-2B-GGUF")LOCAL_LLM_REVISION = os.environ.get("LOCAL_LLM_REVISION") or None821 unmodified lines half = LOCAL_LLM_MAX_INPUT_CHARS // 2 user = f"{user[:half]}\n[...]\n{user[-half:]}" system, references = ground_prompt(system, user) grammar_text = reference_grammar(references, analyst=not interactive) decoding = {} if grammar_text: from llama_cpp import LlamaGrammar decoding["grammar"] = LlamaGrammar.from_string(grammar_text, verbose=False) if self._chatml: prompt = ( f"<|im_start|>system\n{system}<|im_end|>\n"3 unmodified lines stream = self.llm.create_completion( prompt, max_tokens=max_tokens, temperature=LOCAL_LLM_TEMPERATURE, top_p=0.8, top_k=20, presence_penalty=1.5, stop=["<|im_end|>", "<|endoftext|>"], stream=True, **decoding, ) read = lambda c: c["choices"][0].get("text") or "" else: stream = self.llm.create_chat_completion( messages=[{"role": "system", "content": system}, {"role": "user", "content": user}], max_tokens=max_tokens, temperature=LOCAL_LLM_TEMPERATURE, top_p=0.8, stream=True, **decoding, ) read = lambda c: c["choices"][0].get("delta", {}).get("content") or "" pieces: List[str] = []19 unmodified lines if not text: return {**base, "response": None, "error": "local LLM returned no text"} text, grounding = finish_grounded_answer(text, references, analyst=not interactive) if grammar_text: grounding["generation_constraint"] = "reviewed_passages" return { **base, "response": text,3 unmodified lines456789810111213131 unmodified lines1451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811823 unmodified linesThis bounded library is not a live vulnerability feed or a reputation lookup."""import reimport jsonimport itertools
REVISION = "2026-10-23.2"REVISION = "2026-10-23.3"POLICY = ("Detector policy: the supervised phishing, malware-URL and web-attack classifiers " "flag rescaled scores >= 0.50. The anomaly model measures unusualness, not proof " "of maliciousness. Name it separately; never say all detectors are below threshold "131 unmodified lines return False
def reference_choices(refs, analyst=False): """Bound decoding to reviewed passages, never to user-supplied strings.
The model selects a full or concise passage for each retrieved reference. This is extractive generation, not a test of unaided factual recall. Every option retains the definition and required core facts; the separate sentence guard still handles interruption or token-budget truncation. """ if not refs: return [] choices = [] for ref in refs: sentences = re.split(r"(?<=[.!?])\s+", ref["facts"]) variants = [ref["facts"], " ".join(sentences[:2])] choices.append(list(dict.fromkeys(v for v in variants if not reference_fallback_needed(v, [ref])))) answers = [] for parts in itertools.product(*choices): text = ("\n".join("- " + p for p in parts) if analyst else "\n\n".join(parts)) if (not analyst or len(text.split()) <= 80) and not reference_fallback_needed(text, refs): answers.append(text) if not answers: raise ValueError("Reviewed references have no supported response within the answer budget") return answers
def reference_grammar(refs, analyst=False): """GBNF literals escape only immutable editorial text, not the question.""" choices = reference_choices(refs, analyst) return "root ::= " + " | ".join(json.dumps(c, ensure_ascii=False) for c in choices) if choices else None
def finish_grounded_answer(text, refs, analyst=False): """Expose an explicit reference answer when the model misses core facts.
app.py+10-1
security_knowledge.py+35-1