This is the
merged (ready-to-use) version of
mmbert32k-modality-router-lora. LoRA weights have been merged into the
mmbert-32k-yarn base model for easy deployment without the PEFT dependency.
A text classifier based on ModernBERT (307M params, 32K context, 1800+ languages) that determines the appropriate response modality for user prompts:
1from transformers import pipeline
2
3classifier = pipeline(
4 "text-classification",
5 model="llm-semantic-router/mmbert32k-modality-router-merged",
6)
7
8results = classifier([
9 "What are the benefits of exercise?",
10 "A serene Japanese garden with cherry blossoms, watercolor style",
11 "Explain how neural networks work and generate a diagram",
12])
13
14for r in results:
15 print(f"{r['label']}: {r['score']:.3f}")
16# AR: 0.995
17# DIFFUSION: 0.717
18# BOTH: 0.978
1from transformers import AutoTokenizer, AutoModelForSequenceClassification
2import torch
3
4model = AutoModelForSequenceClassification.from_pretrained(
5 "llm-semantic-router/mmbert32k-modality-router-merged"
6)
7tokenizer = AutoTokenizer.from_pretrained(
8 "llm-semantic-router/mmbert32k-modality-router-merged"
9)
10
11prompts = [
12 "Summarize the key points of quantum computing",
13 "portrait of a woman in renaissance style, oil painting, dramatic lighting",
14 "Write a blog post about climate change and include relevant charts",
15]
16
17model.eval()
18inputs = tokenizer(prompts, return_tensors="pt", truncation=True, padding=True, max_length=512)
19with torch.no_grad():
20 outputs = model(**inputs)
21
22predictions = torch.argmax(outputs.logits, dim=-1)
23labels = model.config.id2label
24for prompt, pred_id in zip(prompts, predictions):
25 print(f"{labels[pred_id.item()]}: {prompt[:60]}...")
26# AR: Summarize the key points of quantum computing...
27# DIFFUSION: portrait of a woman in renaissance style, oil painting, d...
28# BOTH: Write a blog post about climate change and include releva...
1# Example: Route requests to different model backends
2def route_request(prompt: str, classifier) -> str:
3 """Route a user prompt to the appropriate model backend."""
4 result = classifier(prompt)[0]
5 modality = result["label"]
6 confidence = result["score"]
7
8 if modality == "AR":
9 return call_llm_backend(prompt) # e.g., Llama, Qwen
10 elif modality == "DIFFUSION":
11 return call_diffusion_backend(prompt) # e.g., Flux, SDXL
12 else: # BOTH
13 text = call_llm_backend(prompt)
14 image = call_diffusion_backend(prompt)
15 return combine_response(text, image)
The base model (
mmbert-32k-yarn) supports ONNX export for sub-5ms inference on AMD MI300X GPUs.
1@misc{modality-router-2025,
2 title={Modality Router: Smart Output Modality Selection for Multi-Model Serving},
3 author={vLLM Semantic Router Team},
4 year={2025},
5 url={https://huggingface.co/llm-semantic-router/mmbert32k-modality-router-merged}
6}