Spaces:
Runtime error
Runtime error
v3: swap to Llama 4 Scout via Together-hosted endpoint (82% task, 72% Governance)
Browse files1234567895 unmodified lines1516171819202122130 unmodified lines1531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102113 unmodified lines2152162172182192202212222232242252262272282292302314 unmodified lines2362372382392402412422432442452462472482497 unmodified lines257258259260261262263264265266267268269"""HF Space demo for construction-code-cite (v1.1 LoRA).Uses transformers + peft (not mlx-lm) so it runs on Space Linux CPU.Fetches the OSHA 1926 corpus at cold boot from the public HF dataset,loads the LoRA adapter on top of Qwen 2.5 1.5B-Instruct, and servesstrict-JSON hazard + citation predictions through Gradio."""from __future__ import annotations5 unmodified linesimport gradio as grBASE_MODEL = os.environ.get("BASE_MODEL", "meta-llama/Llama-3.2-3B-Instruct")ADAPTER_REPO = os.environ.get("ADAPTER_REPO", "rigidhat/llama-3.2-3b-construction-codecite-v2")DATASET_REPO = os.environ.get("DATASET_REPO", "rigidhat/construction-code-corpus-v1")MAX_NEW_TOKENS = 384RAG_K = 5130 unmodified linesreturn {}_PIPELINE = Nonedef get_pipeline():global _PIPELINEif _PIPELINE is not None:return _PIPELINEcorpus = ensure_corpus()verify = build_verifier(corpus)search = build_bm25(corpus)print(f"Loading base model: {BASE_MODEL}")import torchfrom peft import PeftModelfrom transformers import AutoModelForCausalLM, AutoTokenizertokenizer = AutoTokenizer.from_pretrained(BASE_MODEL, trust_remote_code=True)base = AutoModelForCausalLM.from_pretrained(BASE_MODEL, torch_dtype=torch.float32, trust_remote_code=True)print(f"Loading LoRA adapter: {ADAPTER_REPO}")model = PeftModel.from_pretrained(base, ADAPTER_REPO)_PIPELINE = {"tokenizer": tokenizer,"model": model,"search": search,"verify": verify,"torch": torch,}return _PIPELINEdef format_candidates(hits) -> str:if not hits:return "(no high-confidence candidates)"return "\n".join(f"- {h['section']}: {h['heading'][:80]}" for h in hits)def predict(narrative: str):if not (narrative or "").strip():return "{}", "(paste an incident narrative first)", "—"t0 = time.time()pipe = get_pipeline()hits = pipe["search"](narrative, k=RAG_K)prompt = PROMPT.format(candidates=format_candidates(hits), narrative=narrative[:1800])messages = [{"role": "user", "content": prompt}]chat = pipe["tokenizer"].apply_chat_template(messages, add_generation_prompt=True, tokenize=False)inputs = pipe["tokenizer"](chat, return_tensors="pt")with pipe["torch"].no_grad():out = pipe["model"].generate(**inputs,3 unmodified lines)generated = out[0][inputs["input_ids"].shape[1]:]raw = pipe["tokenizer"].decode(generated, skip_special_tokens=True)parsed = parse_json(raw)if parsed and "citations" in parsed:for c in parsed["citations"]:is_valid, heading = pipe["verify"](c.get("standard", ""))c["verified"] = is_validif heading and not c.get("section_heading"):c["section_heading"] = headingrag_view = "\n".join(f"- {h['section']}: {h['heading'][:80]}" for h in hits)return json.dumps(parsed, indent=2), rag_view, f"{time.time() - t0:.2f}s"EXAMPLES = [4 unmodified lineswith gr.Blocks(title="Construction Code-Citation") as demo:gr.Markdown("# Construction Code-Citation Model (v2 · AutoScientist)")gr.Markdown("Llama 3.2 3B fine-tuned by **AutoScientist** on OSHA Severe Injury Reports ""for the [Adaption Labs AutoScientist Challenge](https://adaptionlabs.ai/auto-scientist) ""\"All Other Domains\" category. **77% win rate** vs base Llama 3.2 3B on our test set. ""Given a construction-site incident narrative, returns strict JSON with OIICS hazard ""codes plus OSHA 29 CFR 1926 citations, verifier-grounded against the corpus. ""First request downloads the base model (~6 GB, one-time).")with gr.Row():with gr.Column():7 unmodified lineswith gr.Column():output_json = gr.Code(label="Hazards + Citations (JSON)", language="json")rag_view = gr.Textbox(label="OSHA 1926 RAG candidates (BM25)", lines=6)elapsed = gr.Textbox(label="Latency", lines=1)submit.click(predict, inputs=[narrative], outputs=[output_json, rag_view, elapsed])gr.Markdown("**Artifacts:** ""[Dataset](https://huggingface.co/datasets/rigidhat/construction-code-corpus-v1) · ""[v2 Model (Llama 3.2 3B · AutoScientist)](https://huggingface.co/rigidhat/llama-3.2-3b-construction-codecite-v2) · ""[v1 Baseline (Qwen 2.5 1.5B)](https://huggingface.co/rigidhat/qwen-2.5-construction-codecite-v1) · ""[Source](https://github.com/snakezilla/construction-code-llm)"1234567891011121314155 unmodified lines2122232425262728293031323334130 unmodified lines1651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272283 unmodified lines2322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712724 unmodified lines2772782792802812822832842852862872882892907 unmodified lines298299300301302303304305306307308309310311312313"""HF Space demo for construction-code-cite (v3 · Llama 4 Scout 17B-16E).The v3 model is 109B total params (17B active MoE) and does not fit in-Space.Inference goes to Together AI's hosted endpoint; the Space runs the RAGpipeline + verifier + Gradio UI only. If TOGETHER_API_KEY is missing or theendpoint returns an error, we fall back to the v2 (Llama 3.2 3B) in-Spaceadapter so the demo never goes dark.Set the following secrets in the Space:- TOGETHER_API_KEY (required for v3)- V3_MODEL_ID (default: rigidhat/llama-4-scout-17b-construction-codecite-v3)- FALLBACK_ADAPTER (default: rigidhat/llama-3.2-3b-construction-codecite-v2)"""from __future__ import annotations5 unmodified linesimport gradio as grV3_MODEL_ID = os.environ.get("V3_MODEL_ID", "rigidhat/llama-4-scout-17b-construction-codecite-v3")TOGETHER_API_KEY = os.environ.get("TOGETHER_API_KEY", "")FALLBACK_BASE = os.environ.get("FALLBACK_BASE", "meta-llama/Llama-3.2-3B-Instruct")FALLBACK_ADAPTER = os.environ.get("FALLBACK_ADAPTER", "rigidhat/llama-3.2-3b-construction-codecite-v2")DATASET_REPO = os.environ.get("DATASET_REPO", "rigidhat/construction-code-corpus-v1")MAX_NEW_TOKENS = 384RAG_K = 5130 unmodified linesreturn {}_STATE = {"search": None, "verify": None, "fallback": None}def get_search_verify():if _STATE["search"] is not None:return _STATE["search"], _STATE["verify"]corpus = ensure_corpus()_STATE["search"] = build_bm25(corpus)_STATE["verify"] = build_verifier(corpus)return _STATE["search"], _STATE["verify"]def get_fallback():"""Lazy-load v2 in-Space adapter as the fallback path."""if _STATE["fallback"] is not None:return _STATE["fallback"]import torchfrom peft import PeftModelfrom transformers import AutoModelForCausalLM, AutoTokenizerprint(f"Loading fallback base: {FALLBACK_BASE}")tokenizer = AutoTokenizer.from_pretrained(FALLBACK_BASE, trust_remote_code=True)base = AutoModelForCausalLM.from_pretrained(FALLBACK_BASE, torch_dtype=torch.float32, trust_remote_code=True)print(f"Loading fallback adapter: {FALLBACK_ADAPTER}")model = PeftModel.from_pretrained(base, FALLBACK_ADAPTER)_STATE["fallback"] = {"tokenizer": tokenizer, "model": model, "torch": torch}return _STATE["fallback"]def generate_together(prompt: str) -> tuple[str, str]:"""Call Together AI hosted endpoint. Returns (text, path_label)."""if not TOGETHER_API_KEY:raise RuntimeError("TOGETHER_API_KEY not set")try:from together import Togetherexcept ImportError as e:raise RuntimeError(f"together package not installed: {e}")client = Together(api_key=TOGETHER_API_KEY)response = client.chat.completions.create(model=V3_MODEL_ID,messages=[{"role": "user", "content": prompt}],max_tokens=MAX_NEW_TOKENS,temperature=0.0,)return response.choices[0].message.content, "v3 · Llama 4 Scout 17B-16E (Together)"def generate_fallback(prompt: str) -> tuple[str, str]:"""Fall back to v2 in-Space."""pipe = get_fallback()messages = [{"role": "user", "content": prompt}]chat = pipe["tokenizer"].apply_chat_template(messages, add_generation_prompt=True, tokenize=False)inputs = pipe["tokenizer"](chat, return_tensors="pt")with pipe["torch"].no_grad():out = pipe["model"].generate(**inputs,3 unmodified lines)generated = out[0][inputs["input_ids"].shape[1]:]raw = pipe["tokenizer"].decode(generated, skip_special_tokens=True)return raw, "v2 · Llama 3.2 3B (in-Space fallback)"def format_candidates(hits) -> str:if not hits:return "(no high-confidence candidates)"return "\n".join(f"- {h['section']}: {h['heading'][:80]}" for h in hits)def predict(narrative: str):if not (narrative or "").strip():return "{}", "(paste an incident narrative first)", "—", "—"t0 = time.time()search, verify = get_search_verify()hits = search(narrative, k=RAG_K)prompt = PROMPT.format(candidates=format_candidates(hits), narrative=narrative[:1800])path_label = ""raw = ""try:raw, path_label = generate_together(prompt)except Exception as e:print(f"Together path failed: {e}. Falling back to v2 in-Space.")raw, path_label = generate_fallback(prompt)parsed = parse_json(raw)if parsed and "citations" in parsed:for c in parsed["citations"]:is_valid, heading = verify(c.get("standard", ""))c["verified"] = is_validif heading and not c.get("section_heading"):c["section_heading"] = headingrag_view = "\n".join(f"- {h['section']}: {h['heading'][:80]}" for h in hits)return json.dumps(parsed, indent=2), rag_view, f"{time.time() - t0:.2f}s", path_labelEXAMPLES = [4 unmodified lineswith gr.Blocks(title="Construction Code-Citation") as demo:gr.Markdown("# Construction Code-Citation Model (v3 · Llama 4 Scout 17B-16E · AutoScientist)")gr.Markdown("Llama 4 Scout 17B-16E (MoE) fine-tuned by **AutoScientist** on OSHA Severe ""Injury Reports for the [Adaption Labs AutoScientist Challenge](https://adaptionlabs.ai/auto-scientist) ""\"All Other Domains\" category. Given a construction-site incident narrative, ""returns strict JSON with OIICS hazard codes plus OSHA 29 CFR 1926 citations, ""verifier-grounded against the corpus. **Inference via Together AI hosted ""endpoint** — v2 (Llama 3.2 3B) auto-falls-back if the endpoint is unavailable.")with gr.Row():with gr.Column():7 unmodified lineswith gr.Column():output_json = gr.Code(label="Hazards + Citations (JSON)", language="json")rag_view = gr.Textbox(label="OSHA 1926 RAG candidates (BM25)", lines=6)with gr.Row():elapsed = gr.Textbox(label="Latency", lines=1)path = gr.Textbox(label="Model path", lines=1)submit.click(predict, inputs=[narrative], outputs=[output_json, rag_view, elapsed, path])gr.Markdown("**Artifacts:** ""[Dataset](https://huggingface.co/datasets/rigidhat/construction-code-corpus-v1) · ""[v3 Model (Llama 4 Scout 17B-16E · AutoScientist)](https://huggingface.co/rigidhat/llama-4-scout-17b-construction-codecite-v3) · ""[v2 Model (Llama 3.2 3B · AutoScientist)](https://huggingface.co/rigidhat/llama-3.2-3b-construction-codecite-v2) · ""[v1 Baseline (Qwen 2.5 1.5B)](https://huggingface.co/rigidhat/qwen-2.5-construction-codecite-v1) · ""[Source](https://github.com/snakezilla/construction-code-llm)"
app.py+98-54