File size: 7,853 Bytes
31addc4
 
 
 
 
 
 
 
cff2c71
31addc4
 
 
 
 
 
 
 
eec7ffd
31addc4
 
cff2c71
31addc4
eec7ffd
cff2c71
 
eec7ffd
31addc4
cff2c71
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
eec7ffd
 
 
cff2c71
 
 
 
 
eec7ffd
cff2c71
 
eec7ffd
 
 
 
cff2c71
eec7ffd
 
cff2c71
eec7ffd
 
 
cff2c71
 
 
 
 
 
 
 
 
 
 
 
 
 
eec7ffd
 
 
cff2c71
31addc4
eec7ffd
cff2c71
eec7ffd
 
cff2c71
eec7ffd
cff2c71
eec7ffd
cff2c71
 
 
eec7ffd
 
 
cff2c71
 
 
 
eec7ffd
31addc4
 
 
eec7ffd
31addc4
cff2c71
 
31addc4
 
cff2c71
eec7ffd
 
 
 
 
 
cff2c71
 
 
 
 
eec7ffd
cff2c71
eec7ffd
cff2c71
eec7ffd
cff2c71
 
 
 
 
 
 
eec7ffd
cff2c71
 
 
 
 
eec7ffd
 
cff2c71
eec7ffd
 
 
cff2c71
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
31addc4
cff2c71
 
 
 
 
 
 
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
---
language:
- en
- hi
license: mit
tags:
- text-classification
- intent-classification
- multimodal-routing
- hinglish
- onnx
metrics:
- accuracy
- f1
- precision
- recall
base_model: sentence-transformers/paraphrase-multilingual-MiniLM-L12-v2
pipeline_tag: text-classification
---

# Multimodal Conversational Intent Classifier

<p align="center">
  <img src="confusion_matrix.png" width="48%" alt="Confusion Matrix" />
  <img src="confidence_distribution.png" width="48%" alt="Confidence Distribution" />
</p>

A lightweight, high-precision text classification model engineered for **multimodal conversational systems and dialogue agents**. 

It deterministically identifies whether incoming user input intends to trigger **on-demand visual image synthesis (`IMAGE_GENERATION`)** versus standard conversational interaction, user-shared media discussions, or inquiries (`TEXT_CHAT`).

---

## πŸ“Œ Executive Summary

- **Primary Task**: Binary sequence classification / dialogue intent routing.
- **Base Architecture**: Fine-tuned `sentence-transformers/paraphrase-multilingual-MiniLM-L12-v2` with linear classification head.
- **Target Latency**: Optimized for CPU inference via **ONNX Runtime (opset 18)** at **~12 ms (P50)**, making it suitable for ZeroGPU / shared server environments.
- **Linguistic Coverage**: Dual native support for English and Romanized Hindi (Hinglish), including regional dialects, colloquial abbreviations, and spelling perturbations.
- **Contrastive Hard-Negative Hardening**: Specifically mitigates false positive traps (e.g., users sharing their own photography, technical photography inquiries, emotional statements, and compound negations).

---

## 🎯 Target Classes

| Class ID | Label | Definition | Exemplar Inputs |
| :---: | :--- | :--- | :--- |
| `0` | `TEXT_CHAT` | Standard dialogue, inquiries, compliments, user sharing personal media, or explicit generation prohibitions. | *"I took a photo of the sunset today"*, *"Do you like photography?"*, *"You look great"*, *"Photo mat bhejna"*, *"No pictures please"* |
| `1` | `IMAGE_GENERATION` | User requests the system/assistant to generate, take, or deliver an image/photo of a subject, outfit, or scene. | *"Send me a photo in a red saree"*, *"Can you take a selfie?"*, *"Show me what you're wearing"*, *"Apni cute photo bhejo na"* |

---

## πŸ“Š Evaluation & Empirical Benchmarks

<p align="center">
  <img src="category_benchmark.png" width="85%" alt="Category Benchmark" />
</p>

### 1. Test Split Overall Metrics
Evaluated across a balanced holdout dataset (828 samples):

| Metric | Score |
| :--- | :--- |
| **Accuracy** | **100.00%** |
| **Macro F1 Score** | **1.0000** |
| **Weighted Precision** | **1.0000** |
| **Weighted Recall** | **1.0000** |
| **Evaluation Loss** | **0.0033** |

---

### 2. Adversarial Slice & Edge-Case Evaluation

| Evaluation Slice | Expected Class | Accuracy | Diagnostic Value |
| :--- | :--- | :---: | :--- |
| **Direct Generation Commands (EN)** | `IMAGE_GENERATION` | **100.0%** (9/9) | High recall on concise direct requests. |
| **Attire & Scene Specifications** | `IMAGE_GENERATION` | **60.0%** (6/10) | Captures complex multi-clause visual constraints. |
| **Affectionate & Companion Requests** | `IMAGE_GENERATION` | **100.0%** (6/6) | Preserves action sensitivity on affectionate requests. |
| **Hinglish & Regional Dialects** | `IMAGE_GENERATION` | **100.0%** (8/8) | Robust code-switching on Romanized Hindi. |
| **Colloquial Abbreviations & Typos** | `IMAGE_GENERATION` | **100.0%** (7/7) | Resilient to informal shorthand (*"plz send ur pic"*, *"selfi"*). |
| **Trap: Negations & Prohibitions** | `TEXT_CHAT` | **86.7%** (13/15) | Suppresses generation on compound pre-/post-positioned negations. |
| **Trap: User Sharing Personal Media** | `TEXT_CHAT` | **87.5%** (7/8) | Correctly distinguishes user photo-sharing from generation requests. |
| **Trap: Technical Photography Q&A** | `TEXT_CHAT` | **83.3%** (5/6) | Filters out camera, lens, and exposure discussions. |
| **Trap: Compliments (Zero Generation)** | `TEXT_CHAT` | **90.0%** (9/10) | Prevents false triggers on aesthetic or emotional statements. |
| **Casual Conversation** | `TEXT_CHAT` | **83.3%** (5/6) | General chit-chat passes cleanly without interruption. |

---

### 3. Inference Latency & Throughput Benchmark

<p align="center">
  <img src="latency_comparison.png" width="75%" alt="Latency Comparison" />
</p>

Single-sentence inference benchmarked over 200 iterations:

| Runtime / Engine | Execution Provider | P50 (Median) | P95 | P99 | Throughput |
| :--- | :--- | :--- | :--- | :--- | :--- |
| **ONNX Runtime** | **CPU (2 threads)** | **12.16 ms** | **13.86 ms** | **26.96 ms** | **79.3 req/s** |
| **PyTorch (Native)** | **CPU** | **30.02 ms** | **38.40 ms** | **44.22 ms** | **33.9 req/s** |
| **PyTorch (Native)** | **Tesla T4 GPU** | **7.71 ms** | **9.07 ms** | **11.24 ms** | **125.6 req/s** |

---

## πŸ’» Quickstart & Deployment

### Option A: High-Throughput CPU Inference (ONNX Runtime)
Recommended for low-latency web backends (e.g., FastAPI, Flask) running on CPU-only infrastructure:

```python
import onnxruntime as ort
import numpy as np
from transformers import AutoTokenizer

MODEL_ID = "Umranz/intent-classifier"
tokenizer = AutoTokenizer.from_pretrained(MODEL_ID)
session = ort.InferenceSession("model.onnx")

def predict_intent(text: str) -> dict:
    inputs = tokenizer(text, return_tensors="np", truncation=True, max_length=128)
    logits = session.run(None, {
        "input_ids": inputs["input_ids"],
        "attention_mask": inputs["attention_mask"]
    })[0]

    # Softmax probabilities
    probs = np.exp(logits) / np.sum(np.exp(logits), axis=-1, keepdims=True)
    p_img = float(probs[0][1])

    # Recommended three-way decision threshold
    if p_img >= 0.75:
        intent = "IMAGE_GENERATION"
    elif p_img >= 0.45:
        intent = "AMBIGUOUS"  # Conversational clarification recommended
    else:
        intent = "TEXT_CHAT"

    return {"intent": intent, "p_image_generation": p_img, "p_text_chat": 1.0 - p_img}

# Example predictions
print(predict_intent("Can you show me a photo in traditional clothes?"))
# {'intent': 'IMAGE_GENERATION', 'p_image_generation': 0.985, ...}

print(predict_intent("I took a photo of the sunset yesterday."))
# {'intent': 'TEXT_CHAT', 'p_image_generation': 0.015, ...}

print(predict_intent("Photo mat bhejna please."))
# {'intent': 'TEXT_CHAT', 'p_image_generation': 0.021, ...}
```

### Option B: Transformers Pipeline (PyTorch)
```python
from transformers import pipeline

classifier = pipeline("text-classification", model="Umranz/intent-classifier")
result = classifier("Show me a picture of you smiling")
print(result)
# [{'label': 'IMAGE_GENERATION', 'score': 0.985}]
```

---

## πŸ›‘οΈ Recommended Production Routing Architecture

For production systems with strict latency requirements, a **hybrid routing architecture** is recommended:

```
[ User Input ]
      β”‚
      β–Ό
[ Fast-Path Regex (0 ms) ] ── (Explicit commands or strict negations) ──► Direct Resolution
      β”‚ (Unmatched / Conversational)
      β–Ό
[ ONNX Intent Classifier (~12 ms CPU) ]
      β”‚
      β”œβ”€β”€ P(IMAGE) >= 0.75 ────────► Trigger Image Synthesis Pipeline
      β”œβ”€β”€ 0.45 <= P(IMAGE) < 0.75 ─► Request User Clarification
      └── P(IMAGE) < 0.45 ─────────► Route to Conversational LLM
```

---

## πŸ“œ Limitations & Ethical Considerations

- **Out-of-Distribution Vocabulary**: Highly regional slang or rare phonetic transliterations not present in the multilingual pretraining vocabulary may yield lower confidence.
- **Context Awareness**: The model evaluates single-turn utterances; multi-turn context (e.g., *"Yes, do that"*) should be supplemented with conversation history tracking.