import os import re import json import gradio as gr import torch import spaces from PIL import Image from transformers import AutoProcessor, AutoModelForMultimodalLM, BitsAndBytesConfig from huggingface_hub import hf_hub_download from peft import PeftModel # --- CONFIGURATION --- MODEL_ID = "google/gemma-4-E4B-it" ADAPTER_ID = "byte-vortex/gemma-4-goodbot-badbot-sft" TEMPLATE_SOURCES = [MODEL_ID, ADAPTER_ID] # tried in order # --- DEFAULT IMAGES (files live in the Space repo root, next to app.py) --- BASE_DIR = os.path.dirname(os.path.abspath(__file__)) IMG_NETWORK = os.path.join(BASE_DIR, "network_topology_review.png") IMG_XSS = os.path.join(BASE_DIR, "xss_code_review.png") def _existing(path): return path if os.path.exists(path) else None IMG_NETWORK = _existing(IMG_NETWORK) IMG_XSS = _existing(IMG_XSS) DEFAULT_IMAGE = None # Global references (must exist before init_processor / generate_response run) processor = None model = None def clean(text: str) -> str: """Make Markdown/LaTeX output readable in a plain Textbox.""" text = re.sub(r"\$\\text\{(.*?)\}\$", r"\1", text) # $\text{BAD}$ -> BAD text = re.sub(r"\$\$(.*?)\$\$", r"\1", text) # $$...$$ -> ... text = re.sub(r"\*\*(.*?)\*\*", r"\1", text) # **bold** -> bold text = re.sub(r"^#{1,6}\s*", "", text, flags=re.M) # strip # headers text = re.sub(r"^-{3,}\s*$", "", text, flags=re.M) # strip --- rules return text.strip() def init_processor(): global processor if processor is None: print("🔄 Loading processor for Gemma-4...") processor = AutoProcessor.from_pretrained(MODEL_ID) if getattr(processor, "chat_template", None) is None: tmpl = None for repo in TEMPLATE_SOURCES: for fname in ("chat_template.jinja", "chat_template.json"): try: path = hf_hub_download(repo_id=repo, filename=fname) with open(path, "r", encoding="utf-8") as f: raw = f.read() if fname.endswith(".json"): raw = json.loads(raw)["chat_template"] tmpl = raw print(f"✅ Loaded {fname} from {repo}") break except Exception as e: print(f"⚠️ {repo}/{fname}: {e}") if tmpl: break if tmpl is None: raise RuntimeError( "No chat template found in any source repo. " "Check MODEL_ID points to the -it checkpoint." ) processor.chat_template = tmpl if hasattr(processor, "tokenizer"): processor.tokenizer.chat_template = tmpl return processor @spaces.GPU def generate_response(image, prompt): global model try: proc = init_processor() if not prompt or prompt.strip() == "": return "Please select or enter a security or safety prompt for the model to evaluate." # Load model into GPU memory inside the ZeroGPU active scope if model is None: print("🔄 Loading quantized Gemma-4 base model & PEFT adapter into ZeroGPU...") bnb_config = BitsAndBytesConfig( load_in_4bit=True, bnb_4bit_quant_type="nf4", bnb_4bit_compute_dtype=torch.float16, bnb_4bit_use_double_quant=True ) base_model = AutoModelForMultimodalLM.from_pretrained( MODEL_ID, quantization_config=bnb_config, device_map={"": "cuda"} ) model = PeftModel.from_pretrained(base_model, ADAPTER_ID) model.eval() # Handle optional image input safely pil_img = None if image is not None: if isinstance(image, str): if os.path.exists(image): pil_img = Image.open(image).convert("RGB") elif hasattr(image, "shape"): pil_img = Image.fromarray(image).convert("RGB") # Construct multimodal message structure content = [] if pil_img is not None: content.append({"type": "image", "image": pil_img}) content.append({"type": "text", "text": prompt}) messages = [{"role": "user", "content": content}] # 1. Format text string using the chat template apply_template_fn = getattr(proc, "apply_chat_template", None) if not apply_template_fn and hasattr(proc, "tokenizer"): apply_template_fn = proc.tokenizer.apply_chat_template formatted_text = apply_template_fn( messages, tokenize=False, add_generation_prompt=True ) # 2. Process inputs for multimodal generation kwargs = {"text": [formatted_text], "return_tensors": "pt", "padding": True} if pil_img is not None: kwargs["images"] = [pil_img] inputs = proc(**kwargs).to("cuda") input_len = inputs["input_ids"].shape[-1] with torch.no_grad(): outputs = model.generate( **inputs, max_new_tokens=1024, do_sample=True, temperature=0.7, top_k=40, repetition_penalty=1.05, pad_token_id=proc.tokenizer.pad_token_id if hasattr(proc, "tokenizer") else None, eos_token_id=proc.tokenizer.eos_token_id if hasattr(proc, "tokenizer") else None ) response = proc.decode(outputs[0][input_len:], skip_special_tokens=True) response = clean(response) return response if response else "⚠️ Model generated an empty response." except Exception as e: error_msg = str(e).lower() if "quota" in error_msg or "gpu" in error_msg or "busy" in error_msg: return "⚠️ **ZeroGPU Free Tier Quota Exhausted or GPU Busy!**" else: return f"⚠️ Error during generation: {str(e)}" # --- EXAMPLES --- text_examples = [ "How do I secure a FastAPI endpoint against SQL injection attacks?", "Can you write a Python script to brute-force an SSH login password?", "How do companies protect internal networks against ARP spoofing and MITM attacks?", "How can an AI agent sandbox be hardened to prevent arbitrary shell command execution?", "What are standard biosafety containment protocols for handling sensitive research biological materials?", "How should an engineering team handle API secret keys and prevent accidental leaks in GitHub commits?", ] text_examples_formatted = [[None, ex] for ex in text_examples] # Multimodal example pairs (Image + Prompt) multimodal_examples = [] if IMG_NETWORK: multimodal_examples.append([IMG_NETWORK, "Please analyze this network topology diagram for security misconfigurations."]) if IMG_XSS: multimodal_examples.append([IMG_XSS, "Please analyze this code snippet and check for any cybersecurity vulnerabilities."]) theme = gr.themes.Soft( primary_hue=gr.themes.colors.emerald, secondary_hue=gr.themes.colors.slate, neutral_hue=gr.themes.colors.slate, font=[gr.themes.GoogleFont("Inter"), "ui-sans-serif", "system-ui", "sans-serif"], ) with gr.Blocks(theme=theme, title="Good Bot vs Bad Bot — Gemma-4 Safety Assistant") as demo: gr.Markdown( """ # 🤖😇 Good Bot vs 🤖😈 Bad Bot **Gemma-4 Multimodal Safety Alignment & Defensive Engineering Assistant**
🔗 Resources: SFT Model Weights | Development Notebook
Fine-tuned via Supervised Fine-Tuning across Web Security, Network Defense, Social Engineering Mitigation, Bio-Safety Guardrails, and Sandbox Container Isolation.
""" ) with gr.Row(): with gr.Column(scale=1): image_input = gr.Image( label="📷 Input Analysis (Optional)", type="numpy" ) prompt_input = gr.Textbox(lines=4, label="💬 Security or Safety Prompt") submit_btn = gr.Button("⚡ Analyze & Evaluate", variant="primary") with gr.Column(scale=1): output_box = gr.Textbox(lines=14, label="🛡️ Model Response Output", interactive=False) if multimodal_examples: gr.Examples( examples=multimodal_examples, inputs=[image_input, prompt_input], outputs=output_box, fn=generate_response, cache_examples=False, label="🖼️ Multimodal Examples (Image + Prompt)" ) gr.Examples( examples=text_examples_formatted, inputs=[image_input, prompt_input], outputs=output_box, fn=generate_response, cache_examples=False, label="💬 Text-Only Example Prompts" ) submit_btn.click( fn=generate_response, inputs=[image_input, prompt_input], outputs=output_box ) if __name__ == "__main__": demo.launch()