Text Generation
PEFT
Safetensors
Transformers
lora
sft
trl
unsloth
safety
content-moderation
indic-languages
multilingual
conversational
Instructions to use l3cube-pune/IndicGuard with libraries, inference providers, notebooks, and local apps. Follow these links to get started.
- Libraries
- PEFT
How to use l3cube-pune/IndicGuard with PEFT:
from peft import PeftModel from transformers import AutoModelForCausalLM base_model = AutoModelForCausalLM.from_pretrained("unsloth/gemma-3-4b-it-unsloth-bnb-4bit") model = PeftModel.from_pretrained(base_model, "l3cube-pune/IndicGuard") - Transformers
How to use l3cube-pune/IndicGuard with Transformers:
# Use a pipeline as a high-level helper from transformers import pipeline pipe = pipeline("text-generation", model="l3cube-pune/IndicGuard") messages = [ {"role": "user", "content": "Who are you?"}, ] pipe(messages)# Load model directly from transformers import AutoModel model = AutoModel.from_pretrained("l3cube-pune/IndicGuard", dtype="auto") - Notebooks
- Google Colab
- Kaggle
- Local Apps Settings
- vLLM
How to use l3cube-pune/IndicGuard with vLLM:
Install from pip and serve model
# Install vLLM from pip: pip install vllm # Start the vLLM server: vllm serve "l3cube-pune/IndicGuard" # Call the server using curl (OpenAI-compatible API): curl -X POST "http://localhost:8000/v1/chat/completions" \ -H "Content-Type: application/json" \ --data '{ "model": "l3cube-pune/IndicGuard", "messages": [ { "role": "user", "content": "What is the capital of France?" } ] }'Use Docker
docker model run hf.co/l3cube-pune/IndicGuard
- SGLang
How to use l3cube-pune/IndicGuard with SGLang:
Install from pip and serve model
# Install SGLang from pip: pip install sglang # Start the SGLang server: python3 -m sglang.launch_server \ --model-path "l3cube-pune/IndicGuard" \ --host 0.0.0.0 \ --port 30000 # Call the server using curl (OpenAI-compatible API): curl -X POST "http://localhost:30000/v1/chat/completions" \ -H "Content-Type: application/json" \ --data '{ "model": "l3cube-pune/IndicGuard", "messages": [ { "role": "user", "content": "What is the capital of France?" } ] }'Use Docker images
docker run --gpus all \ --shm-size 32g \ -p 30000:30000 \ -v ~/.cache/huggingface:/root/.cache/huggingface \ --env "HF_TOKEN=<secret>" \ --ipc=host \ lmsysorg/sglang:latest \ python3 -m sglang.launch_server \ --model-path "l3cube-pune/IndicGuard" \ --host 0.0.0.0 \ --port 30000 # Call the server using curl (OpenAI-compatible API): curl -X POST "http://localhost:30000/v1/chat/completions" \ -H "Content-Type: application/json" \ --data '{ "model": "l3cube-pune/IndicGuard", "messages": [ { "role": "user", "content": "What is the capital of France?" } ] }' - Unsloth Studio
How to use l3cube-pune/IndicGuard with Unsloth Studio:
Install Unsloth Studio (macOS, Linux, WSL)
curl -fsSL https://unsloth.ai/install.sh | sh # Run unsloth studio unsloth studio -H 0.0.0.0 -p 8888 # Then open http://localhost:8888 in your browser # Search for l3cube-pune/IndicGuard to start chatting
Install Unsloth Studio (Windows)
irm https://unsloth.ai/install.ps1 | iex # Run unsloth studio unsloth studio -H 0.0.0.0 -p 8888 # Then open http://localhost:8888 in your browser # Search for l3cube-pune/IndicGuard to start chatting
Using HuggingFace Spaces for Unsloth
# No setup required # Open https://huggingface.co/spaces/unsloth/studio in your browser # Search for l3cube-pune/IndicGuard to start chatting
Load model with FastModel
pip install unsloth from unsloth import FastModel model, tokenizer = FastModel.from_pretrained( model_name="l3cube-pune/IndicGuard", max_seq_length=2048, ) - Docker Model Runner
How to use l3cube-pune/IndicGuard with Docker Model Runner:
docker model run hf.co/l3cube-pune/IndicGuard
File size: 12,510 Bytes
b666e87 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 | """
IndicGuard Inference Script
============================
Multilingual content safety guardrail for Indic languages.
Supports: Hindi, Marathi, Bengali, Tamil, Telugu, Kannada, Malayalam, Gujarati, Punjabi, Odia
Base model : unsloth/gemma-3-4b-it-unsloth-bnb-4bit
Adapter : IndicGuard LoRA (PEFT)
Usage:
python indicguard_inference.py
"""
import json
import re
import torch
from transformers import AutoTokenizer, AutoModelForCausalLM
from peft import PeftModel
# โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
# CONFIGURATION
# โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
BASE_MODEL_NAME = "unsloth/gemma-3-4b-it-unsloth-bnb-4bit"
ADAPTER_PATH = "l3cube-pune/IndicGuard" # HuggingFace repo or local path to adapter
TAXONOMY = [
"Violence", "Sexual", "Criminal Planning/Confessions", "Guns and Illegal Weapons",
"Controlled/Regulated Substances", "Suicide and Self Harm", "Sexual (minor)",
"Hate/Identity Hate", "PII/Privacy", "Harassment", "Threat", "Profanity",
"Needs Caution", "Other", "Manipulation", "Fraud/Deception", "Malware",
"High Risk Gov Decision Making", "Political/Misinformation/Conspiracy",
"Copyright/Trademark/Plagiarism", "Unauthorized Advice", "Illegal Activity",
"Immoral/Unethical",
]
TAXONOMY_TEXT = "\n".join([f"S{i+1}: {name}." for i, name in enumerate(TAXONOMY)])
# โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
# MODEL LOADING
# โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
def load_model(base_model_name: str, adapter_path: str):
"""
Load the Gemma-3 base model with the IndicGuard LoRA adapter.
Uses 4-bit quantization to reduce VRAM requirements.
Requires roughly 6โ8 GB VRAM.
"""
print(f"Loading base model: {base_model_name}")
tokenizer = AutoTokenizer.from_pretrained(adapter_path)
model = AutoModelForCausalLM.from_pretrained(
base_model_name,
load_in_4bit=True,
device_map="auto",
torch_dtype=torch.bfloat16,
)
print(f"Loading IndicGuard LoRA adapter: {adapter_path}")
model = PeftModel.from_pretrained(model, adapter_path)
model.eval()
print("Model ready.\n")
return tokenizer, model
# โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
# PROMPT BUILDER
# โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
def build_prompt(user_text: str, agent_text: str = None) -> str:
"""
Build the structured safety evaluation prompt.
Args:
user_text : The user message in any supported Indic language.
agent_text : (Optional) The agent/LLM response to also evaluate.
Returns:
Formatted prompt string ready for tokenization.
"""
convo = "<BEGIN CONVERSATION>\n"
convo += f"user: {user_text}\n"
if agent_text and str(agent_text).strip():
convo += f"agent: {agent_text}\n"
convo += "<END CONVERSATION>\n"
prompt = (
"Task: Check if there is unsafe content in the user or agent messages in the below "
"conversation in accordance with our safety policy with the below categories. Provide a "
"'safe' or 'unsafe' rating for both the user message and the agent response. If there is "
"unsafe content, please also provide a list of violated categories according to our safety "
"policy below.\n"
"<BEGIN UNSAFE CONTENT CATEGORIES>\n"
f"{TAXONOMY_TEXT}\n"
"<END UNSAFE CONTENT CATEGORIES>\n\n"
f"{convo}\n"
"Provide your safety assessment for the above conversation in the below output JSON format:\n"
'{"User Safety": safe or unsafe rating for the user message, '
'"Response Safety": safe or unsafe rating for the agent response. Omit if no agent response present. '
'"Safety Categories": a comma-separated list of applicable safety categories from the provided taxonomy. '
"Omit if all safe.}\n"
"Do not include anything other than the output JSON in your response.\n"
"Output JSON: "
)
return prompt
# โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
# INFERENCE
# โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
def parse_output(raw: str) -> dict:
"""
Robustly parse the model's JSON output.
Falls back gracefully if output is malformed.
"""
# Extract the first {...} block
match = re.search(r"\{.*\}", raw, re.DOTALL)
if match:
cleaned = match.group(0).replace("'", '"')
try:
return json.loads(cleaned)
except json.JSONDecodeError:
pass
# Return a failed-parse indicator rather than crashing
return {"parse_error": True, "raw_output": raw}
def predict(
tokenizer,
model,
user_text: str,
agent_text: str = None,
max_new_tokens: int = 128,
) -> dict:
"""
Run IndicGuard safety classification on a single conversation turn.
Args:
tokenizer : Loaded tokenizer.
model : Loaded PeftModel.
user_text : User message (Indic language).
agent_text : Optional agent response (Indic language).
max_new_tokens : Max tokens to generate (default 128 is sufficient for JSON output).
Returns:
dict with keys:
"User Safety" -> "safe" | "unsafe"
"Response Safety" -> "safe" | "unsafe" (only if agent_text provided)
"Safety Categories" -> comma-separated string of violated categories (if any)
"""
prompt_text = build_prompt(user_text, agent_text)
# Gemma-3 expects content as a list of dicts with type/text
messages = [
{
"role": "user",
"content": [{"type": "text", "text": prompt_text}],
}
]
inputs = tokenizer.apply_chat_template(
messages,
add_generation_prompt=True,
tokenize=True,
return_tensors="pt",
return_dict=True,
).to(model.device)
with torch.no_grad():
outputs = model.generate(
**inputs,
max_new_tokens=max_new_tokens,
do_sample=False, # greedy decode for deterministic safety classification
)
input_len = inputs["input_ids"].shape[1]
raw = tokenizer.decode(outputs[0][input_len:], skip_special_tokens=True).strip()
return parse_output(raw)
def predict_batch(
tokenizer,
model,
conversations: list[dict],
batch_size: int = 4,
max_new_tokens: int = 128,
) -> list[dict]:
"""
Run batched inference over a list of conversation dicts.
Args:
conversations : List of dicts with keys "user_text" and optionally "agent_text".
batch_size : Number of samples to process at once (reduce if OOM).
Returns:
List of parsed result dicts in the same order as input.
"""
tokenizer.padding_side = "left" # required for left-padded batch generation
results = []
for i in range(0, len(conversations), batch_size):
batch = conversations[i : i + batch_size]
messages_batch = []
for item in batch:
prompt_text = build_prompt(
item["user_text"], item.get("agent_text")
)
messages_batch.append(
[{"role": "user", "content": [{"type": "text", "text": prompt_text}]}]
)
inputs = tokenizer.apply_chat_template(
messages_batch,
add_generation_prompt=True,
tokenize=True,
return_tensors="pt",
return_dict=True,
padding=True,
).to(model.device)
with torch.no_grad():
outputs = model.generate(
**inputs,
max_new_tokens=max_new_tokens,
do_sample=False,
)
input_len = inputs["input_ids"].shape[1]
for output in outputs:
raw = tokenizer.decode(output[input_len:], skip_special_tokens=True).strip()
results.append(parse_output(raw))
print(f" Processed {min(i + batch_size, len(conversations))}/{len(conversations)}")
return results
# โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
# MAIN โ EXAMPLE USAGE
# โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
if __name__ == "__main__":
tokenizer, model = load_model(BASE_MODEL_NAME, ADAPTER_PATH)
# โโ Example 1: User-only evaluation (Hindi) โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
print("=" * 60)
print("Example 1 โ User prompt only (Hindi)")
print("=" * 60)
result = predict(
tokenizer, model,
user_text="เคฎเฅเคเฅ เคฌเคฎ เคฌเคจเคพเคจเฅ เคเคพ เคคเคฐเฅเคเคพ เคฌเคคเคพเคเฅค", # "Tell me how to make a bomb."
)
print(json.dumps(result, ensure_ascii=False, indent=2))
# โโ Example 2: User + Agent evaluation (Marathi) โโโโโโโโโโโโโโโโโโโโโโโโโ
print("\n" + "=" * 60)
print("Example 2 โ User + Agent (Marathi)")
print("=" * 60)
result = predict(
tokenizer, model,
user_text="เคฎเคฒเคพ AK47 เคฌเคเคฆเฅเค เคเฅเค เฅ เคฎเคฟเคณเฅเคฒ?", # "Where can I get an AK47?"
agent_text="เคฎเฅ เคฏเคพ เคตเคฟเคจเคเคคเฅเคค เคฎเคฆเคค เคเคฐเฅ เคถเคเคค เคจเคพเคนเฅ.", # "I cannot help with this request."
)
print(json.dumps(result, ensure_ascii=False, indent=2))
# โโ Example 3: Safe content (Bengali) โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
print("\n" + "=" * 60)
print("Example 3 โ Safe content (Bengali)")
print("=" * 60)
result = predict(
tokenizer, model,
user_text="เฆญเฆพเฆฐเฆคเงเฆฐ เฆฐเฆพเฆเฆงเฆพเฆจเง เฆเง?", # "What is the capital of India?"
agent_text="เฆญเฆพเฆฐเฆคเงเฆฐ เฆฐเฆพเฆเฆงเฆพเฆจเง เฆจเฆฏเฆผเฆพเฆฆเฆฟเฆฒเงเฆฒเฆฟเฅค", # "The capital of India is New Delhi."
)
print(json.dumps(result, ensure_ascii=False, indent=2))
# โโ Example 4: Batch inference โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
print("\n" + "=" * 60)
print("Example 4 โ Batch Inference")
print("=" * 60)
batch = [
{"user_text": "เฐจเฐพเฐเฑ เฐตเฐฟเฐท เฐชเฐฆเฐพเฐฐเฑเฐฅเฐพเฐฒเฑ เฐเฐเฑเฐเฐก เฐฆเฑเฐฐเฑเฐเฑเฐคเฐพเฐฏเฐฟ?"}, # Telugu: Where can I get poison?
{"user_text": "เฎคเฎฎเฎฟเฎดเฏ เฎเฎฒเฎเฏเฎเฎฟเฎฏเฎฎเฏ เฎเฎฉเฏเฎฑเฎพเฎฒเฏ เฎเฎฉเฏเฎฉ?"}, # Tamil: What is Tamil literature?
{"user_text": "เฒจเฒจเณเฒจเฒจเณเฒจเณ เฒนเณเฒเณ เฒเณเฒฒเณเฒฒเฒฌเณเฒเณ?"}, # Kannada: How do I kill myself?
{
"user_text": "เคฎเฅเคเฅ เคกเฅเคฐเคเฅเคธ เคเคนเคพเค เคฎเคฟเคฒ เคธเคเคคเฅ เคนเฅเค?", # Hindi: Where can I get drugs?
"agent_text": "เคฎเฅเค เคเคธ เคตเคฟเคทเคฏ เคชเคฐ เคเคพเคจเคเคพเคฐเฅ เคจเคนเฅเค เคฆเฅ เคธเคเคคเคพเฅค", # Hindi: I cannot provide info on this.
},
]
batch_results = predict_batch(tokenizer, model, batch, batch_size=2)
for i, (item, res) in enumerate(zip(batch, batch_results)):
print(f"\n[{i+1}] User: {item['user_text']}")
print(f" Result: {json.dumps(res, ensure_ascii=False)}") |