Upload app.py with huggingface_hub
Browse files205 unmodified lines206207208209210211212213214215216217218219220221222223224225226227228229205 unmodified linesreturn _tokenizer.apply_chat_template(messages, tokenize=False, add_generation_prompt=True)def llm_explain(structured_message: str) -> str:# Graceful fallbackif (not _USE_LLM) or (_tokenizer is None) or (_pipe is None):try:d = json.loads(structured_message)so = d["verdicts"]["strength_ok"]sv = d["verdicts"]["service_ok"]s_msg = "OK" if so else "NOT OK"v_msg = "OK" if sv else "NOT OK"return f"Like a sturdy spatula holding a pancake: strength is {s_msg} and deflection (L/180) is {v_msg}."except Exception:return "Quick take: strength and deflection (L/180) checks computed; see the table and math."system_prompt = ("You explain engineering to a smart 5-year-old using a quick food analogy. ""You ALWAYS respond in exactly ONE friendly sentence.")user_prompt = ("Here are the inputs, formulas, and results for a cantilever beam calculation.\n""Summarize whether the beam is OK in strength and deflection.\n\n"+ structured_message)formatted = _format_chat(system_prompt, user_prompt)205 unmodified lines206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241205 unmodified linesreturn _tokenizer.apply_chat_template(messages, tokenize=False, add_generation_prompt=True)def llm_explain(structured_message: str) -> str:# Deterministic, number-rich fallbackif (not _USE_LLM) or (_tokenizer is None) or (_pipe is None):try:d = json.loads(structured_message)r = d["results"]v = d["verdicts"]inp = d["inputs_SI"]L = float(inp["L_m"])Sy = float(inp["Sy_MPa"])sigma = float(r["sigma_max_MPa"])delta = float(r["delta_mm"])delta_allow = L/180.0*1e3 # mms_msg = "OK" if v["strength_ok"] else "NOT OK"d_msg = "OK" if v["service_ok"] else "NOT OK"return (f"Strength {s_msg} (σ_max={sigma:.2f} MPa vs Sy={Sy:.0f} MPa); "f"deflection {d_msg} (δ={delta:.2f} mm vs L/180={delta_allow:.2f} mm).")except Exception:return "Quick take: strength and deflection (L/180) checks computed; see the table and math."# LLM path (only if model actually loads)system_prompt = ("You explain engineering to a smart 5-year-old using a quick food analogy. ""You ALWAYS respond in exactly ONE friendly sentence.")user_prompt = ("Here are the inputs, formulas, and results for a cantilever beam calculation.\n""Summarize whether the beam is OK in strength and deflection, with one short food analogy.\n\n"+ structured_message)formatted = _format_chat(system_prompt, user_prompt)
app.py+19-7