""" app.py A simple Gradio front-end to demo the multi-model safety analyzer. Push to a Hugging Face Space (runtime: python) and ensure required models are accessible. """ import gradio as gr import json from classifier import analyze_text, MODEL_HANDLES, load_models # Try to ensure models are loaded (load_models is called at import but re-call gracefully) try: load_models() except Exception: pass def pretty_result(text: str): res = analyze_text(text or "") # build human-readable output out_lines = [] out_lines.append("Normalized:\n" + (res.get("normalized") or "")) out_lines.append(f"\nEntropy: {res.get('entropy', 0.0):.2f}\n") if res.get("heuristic_flags"): out_lines.append("Heuristic flags:") for h in res["heuristic_flags"]: out_lines.append(f"- {h.get('type')}: {h.get('explain')}") if "matches" in h: out_lines.append(f" matches: {h['matches']}") if res.get("model_flags"): out_lines.append("\nModel flags:") for m in res["model_flags"]: model = m.get("model", "unknown") label = m.get("label", "") score = m.get("score", None) out_lines.append(f"- {m.get('type')} | {model} | {label} | score={score}") out_lines.append(f" explain: {m.get('explain')}") if not res.get("heuristic_flags") and not res.get("model_flags"): out_lines.append("\nNo flags detected (no guarantees).") out_lines.append("\nNotes: " + res.get("notes", "")) return "\n".join(out_lines), json.dumps(res, indent=2, ensure_ascii=False) demo_description = """ # Text Safety Analyzer (multi-model) Paste text or a prompt below. The system runs heuristics + multiple models (toxicity/harm + URL detection) and returns explainable flags. This is a detection tool—human review recommended for any enforcement action. """ with gr.Blocks() as demo: gr.Markdown(demo_description) txt = gr.Textbox(lines=8, placeholder="Paste text, prompt, or suspicious content here...") analyze_btn = gr.Button("Analyze") out_text = gr.Textbox(lines=14, label="Result (human readable)") out_json = gr.Textbox(lines=20, label="Raw JSON result") analyze_btn.click(fn=pretty_result, inputs=txt, outputs=[out_text, out_json]) if __name__ == "__main__": demo.launch()