Upload README.md with huggingface_hub
Browse files
README.md
CHANGED
|
@@ -6,9 +6,8 @@ license: mit
|
|
| 6 |
tags:
|
| 7 |
- text-classification
|
| 8 |
- intent-classification
|
|
|
|
| 9 |
- hinglish
|
| 10 |
-
- ai-companion
|
| 11 |
-
- shruti
|
| 12 |
- onnx
|
| 13 |
metrics:
|
| 14 |
- accuracy
|
|
@@ -19,108 +18,169 @@ base_model: sentence-transformers/paraphrase-multilingual-MiniLM-L12-v2
|
|
| 19 |
pipeline_tag: text-classification
|
| 20 |
---
|
| 21 |
|
| 22 |
-
#
|
| 23 |
|
| 24 |
<p align="center">
|
| 25 |
-
<img src="confusion_matrix.png" width="48%" />
|
| 26 |
-
<img src="confidence_distribution.png" width="48%" />
|
| 27 |
</p>
|
| 28 |
|
| 29 |
-
A
|
| 30 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 31 |
|
| 32 |
---
|
| 33 |
|
| 34 |
-
## π
|
|
|
|
|
|
|
|
|
|
|
|
|
| 35 |
|
| 36 |
-
### 1. Test Split
|
| 37 |
-
|
| 38 |
|
| 39 |
| Metric | Score |
|
| 40 |
| :--- | :--- |
|
| 41 |
| **Accuracy** | **100.00%** |
|
| 42 |
-
| **Macro F1** | **1.0000** |
|
| 43 |
| **Weighted Precision** | **1.0000** |
|
| 44 |
| **Weighted Recall** | **1.0000** |
|
|
|
|
| 45 |
|
| 46 |
---
|
| 47 |
|
| 48 |
-
### 2.
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
|
|
| 55 |
-
|
|
| 56 |
-
| **
|
| 57 |
-
| **
|
| 58 |
-
| **
|
| 59 |
-
| **
|
| 60 |
-
| **
|
| 61 |
-
| **
|
| 62 |
-
| **Trap: User Sharing Own Photos** | `TEXT_CHAT` | **87.5%** | Passed |
|
| 63 |
-
| **Trap: Photography Q&A** | `TEXT_CHAT` | **83.3%** | Passed |
|
| 64 |
-
| **Trap: Romantic Compliments** | `TEXT_CHAT` | **90.0%** | Hardened |
|
| 65 |
-
| **Casual Conversation** | `TEXT_CHAT` | **83.3%** | Passed |
|
| 66 |
|
| 67 |
---
|
| 68 |
|
| 69 |
-
### 3. Latency & Throughput Benchmark
|
| 70 |
|
| 71 |
<p align="center">
|
| 72 |
-
<img src="latency_comparison.png" width="75%" />
|
| 73 |
</p>
|
| 74 |
|
| 75 |
-
|
| 76 |
|
| 77 |
-
| Runtime / Engine |
|
| 78 |
| :--- | :--- | :--- | :--- | :--- | :--- |
|
| 79 |
-
| **ONNX Runtime** | **CPU (2
|
| 80 |
-
| **PyTorch (Native)** | **CPU** | **
|
| 81 |
-
| **PyTorch (Native)** | **T4 GPU** | **
|
| 82 |
|
| 83 |
---
|
| 84 |
|
| 85 |
-
##
|
|
|
|
|
|
|
|
|
|
| 86 |
|
| 87 |
-
### Option A: Lightning-Fast CPU Inference (<10ms via ONNX Runtime)
|
| 88 |
```python
|
| 89 |
import onnxruntime as ort
|
| 90 |
import numpy as np
|
| 91 |
from transformers import AutoTokenizer
|
| 92 |
|
| 93 |
-
|
|
|
|
| 94 |
session = ort.InferenceSession("model.onnx")
|
| 95 |
|
| 96 |
-
def predict_intent(text: str):
|
| 97 |
inputs = tokenizer(text, return_tensors="np", truncation=True, max_length=128)
|
| 98 |
logits = session.run(None, {
|
| 99 |
"input_ids": inputs["input_ids"],
|
| 100 |
"attention_mask": inputs["attention_mask"]
|
| 101 |
})[0]
|
| 102 |
-
|
| 103 |
-
probs = np.exp(logits) / np.sum(np.exp(logits), axis=-1)
|
| 104 |
-
p_img = probs[0][1]
|
| 105 |
|
| 106 |
-
#
|
|
|
|
|
|
|
|
|
|
|
|
|
| 107 |
if p_img >= 0.75:
|
| 108 |
-
|
| 109 |
elif p_img >= 0.45:
|
| 110 |
-
|
| 111 |
else:
|
| 112 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 113 |
|
| 114 |
-
print(predict_intent("
|
| 115 |
-
|
| 116 |
-
|
|
|
|
|
|
|
| 117 |
```
|
| 118 |
|
| 119 |
-
### Option B: Transformers Pipeline
|
| 120 |
```python
|
| 121 |
from transformers import pipeline
|
| 122 |
|
| 123 |
-
classifier = pipeline("text-classification", model="Umranz/
|
| 124 |
-
|
| 125 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 126 |
```
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 6 |
tags:
|
| 7 |
- text-classification
|
| 8 |
- intent-classification
|
| 9 |
+
- multimodal-routing
|
| 10 |
- hinglish
|
|
|
|
|
|
|
| 11 |
- onnx
|
| 12 |
metrics:
|
| 13 |
- accuracy
|
|
|
|
| 18 |
pipeline_tag: text-classification
|
| 19 |
---
|
| 20 |
|
| 21 |
+
# Multimodal Conversational Intent Classifier
|
| 22 |
|
| 23 |
<p align="center">
|
| 24 |
+
<img src="confusion_matrix.png" width="48%" alt="Confusion Matrix" />
|
| 25 |
+
<img src="confidence_distribution.png" width="48%" alt="Confidence Distribution" />
|
| 26 |
</p>
|
| 27 |
|
| 28 |
+
A lightweight, high-precision text classification model engineered for **multimodal conversational systems and dialogue agents**.
|
| 29 |
+
|
| 30 |
+
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`).
|
| 31 |
+
|
| 32 |
+
---
|
| 33 |
+
|
| 34 |
+
## π Executive Summary
|
| 35 |
+
|
| 36 |
+
- **Primary Task**: Binary sequence classification / dialogue intent routing.
|
| 37 |
+
- **Base Architecture**: Fine-tuned `sentence-transformers/paraphrase-multilingual-MiniLM-L12-v2` with linear classification head.
|
| 38 |
+
- **Target Latency**: Optimized for CPU inference via **ONNX Runtime (opset 18)** at **~12 ms (P50)**, making it suitable for ZeroGPU / shared server environments.
|
| 39 |
+
- **Linguistic Coverage**: Dual native support for English and Romanized Hindi (Hinglish), including regional dialects, colloquial abbreviations, and spelling perturbations.
|
| 40 |
+
- **Contrastive Hard-Negative Hardening**: Specifically mitigates false positive traps (e.g., users sharing their own photography, technical photography inquiries, emotional statements, and compound negations).
|
| 41 |
+
|
| 42 |
+
---
|
| 43 |
+
|
| 44 |
+
## π― Target Classes
|
| 45 |
+
|
| 46 |
+
| Class ID | Label | Definition | Exemplar Inputs |
|
| 47 |
+
| :---: | :--- | :--- | :--- |
|
| 48 |
+
| `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"* |
|
| 49 |
+
| `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"* |
|
| 50 |
|
| 51 |
---
|
| 52 |
|
| 53 |
+
## π Evaluation & Empirical Benchmarks
|
| 54 |
+
|
| 55 |
+
<p align="center">
|
| 56 |
+
<img src="category_benchmark.png" width="85%" alt="Category Benchmark" />
|
| 57 |
+
</p>
|
| 58 |
|
| 59 |
+
### 1. Test Split Overall Metrics
|
| 60 |
+
Evaluated across a balanced holdout dataset (828 samples):
|
| 61 |
|
| 62 |
| Metric | Score |
|
| 63 |
| :--- | :--- |
|
| 64 |
| **Accuracy** | **100.00%** |
|
| 65 |
+
| **Macro F1 Score** | **1.0000** |
|
| 66 |
| **Weighted Precision** | **1.0000** |
|
| 67 |
| **Weighted Recall** | **1.0000** |
|
| 68 |
+
| **Evaluation Loss** | **0.0033** |
|
| 69 |
|
| 70 |
---
|
| 71 |
|
| 72 |
+
### 2. Adversarial Slice & Edge-Case Evaluation
|
| 73 |
+
|
| 74 |
+
| Evaluation Slice | Expected Class | Accuracy | Diagnostic Value |
|
| 75 |
+
| :--- | :--- | :---: | :--- |
|
| 76 |
+
| **Direct Generation Commands (EN)** | `IMAGE_GENERATION` | **100.0%** (9/9) | High recall on concise direct requests. |
|
| 77 |
+
| **Attire & Scene Specifications** | `IMAGE_GENERATION` | **60.0%** (6/10) | Captures complex multi-clause visual constraints. |
|
| 78 |
+
| **Affectionate & Companion Requests** | `IMAGE_GENERATION` | **100.0%** (6/6) | Preserves action sensitivity on affectionate requests. |
|
| 79 |
+
| **Hinglish & Regional Dialects** | `IMAGE_GENERATION` | **100.0%** (8/8) | Robust code-switching on Romanized Hindi. |
|
| 80 |
+
| **Colloquial Abbreviations & Typos** | `IMAGE_GENERATION` | **100.0%** (7/7) | Resilient to informal shorthand (*"plz send ur pic"*, *"selfi"*). |
|
| 81 |
+
| **Trap: Negations & Prohibitions** | `TEXT_CHAT` | **86.7%** (13/15) | Suppresses generation on compound pre-/post-positioned negations. |
|
| 82 |
+
| **Trap: User Sharing Personal Media** | `TEXT_CHAT` | **87.5%** (7/8) | Correctly distinguishes user photo-sharing from generation requests. |
|
| 83 |
+
| **Trap: Technical Photography Q&A** | `TEXT_CHAT` | **83.3%** (5/6) | Filters out camera, lens, and exposure discussions. |
|
| 84 |
+
| **Trap: Compliments (Zero Generation)** | `TEXT_CHAT` | **90.0%** (9/10) | Prevents false triggers on aesthetic or emotional statements. |
|
| 85 |
+
| **Casual Conversation** | `TEXT_CHAT` | **83.3%** (5/6) | General chit-chat passes cleanly without interruption. |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 86 |
|
| 87 |
---
|
| 88 |
|
| 89 |
+
### 3. Inference Latency & Throughput Benchmark
|
| 90 |
|
| 91 |
<p align="center">
|
| 92 |
+
<img src="latency_comparison.png" width="75%" alt="Latency Comparison" />
|
| 93 |
</p>
|
| 94 |
|
| 95 |
+
Single-sentence inference benchmarked over 200 iterations:
|
| 96 |
|
| 97 |
+
| Runtime / Engine | Execution Provider | P50 (Median) | P95 | P99 | Throughput |
|
| 98 |
| :--- | :--- | :--- | :--- | :--- | :--- |
|
| 99 |
+
| **ONNX Runtime** | **CPU (2 threads)** | **12.16 ms** | **13.86 ms** | **26.96 ms** | **79.3 req/s** |
|
| 100 |
+
| **PyTorch (Native)** | **CPU** | **30.02 ms** | **38.40 ms** | **44.22 ms** | **33.9 req/s** |
|
| 101 |
+
| **PyTorch (Native)** | **Tesla T4 GPU** | **7.71 ms** | **9.07 ms** | **11.24 ms** | **125.6 req/s** |
|
| 102 |
|
| 103 |
---
|
| 104 |
|
| 105 |
+
## π» Quickstart & Deployment
|
| 106 |
+
|
| 107 |
+
### Option A: High-Throughput CPU Inference (ONNX Runtime)
|
| 108 |
+
Recommended for low-latency web backends (e.g., FastAPI, Flask) running on CPU-only infrastructure:
|
| 109 |
|
|
|
|
| 110 |
```python
|
| 111 |
import onnxruntime as ort
|
| 112 |
import numpy as np
|
| 113 |
from transformers import AutoTokenizer
|
| 114 |
|
| 115 |
+
MODEL_ID = "Umranz/intent-classifier"
|
| 116 |
+
tokenizer = AutoTokenizer.from_pretrained(MODEL_ID)
|
| 117 |
session = ort.InferenceSession("model.onnx")
|
| 118 |
|
| 119 |
+
def predict_intent(text: str) -> dict:
|
| 120 |
inputs = tokenizer(text, return_tensors="np", truncation=True, max_length=128)
|
| 121 |
logits = session.run(None, {
|
| 122 |
"input_ids": inputs["input_ids"],
|
| 123 |
"attention_mask": inputs["attention_mask"]
|
| 124 |
})[0]
|
|
|
|
|
|
|
|
|
|
| 125 |
|
| 126 |
+
# Softmax probabilities
|
| 127 |
+
probs = np.exp(logits) / np.sum(np.exp(logits), axis=-1, keepdims=True)
|
| 128 |
+
p_img = float(probs[0][1])
|
| 129 |
+
|
| 130 |
+
# Recommended three-way decision threshold
|
| 131 |
if p_img >= 0.75:
|
| 132 |
+
intent = "IMAGE_GENERATION"
|
| 133 |
elif p_img >= 0.45:
|
| 134 |
+
intent = "AMBIGUOUS" # Conversational clarification recommended
|
| 135 |
else:
|
| 136 |
+
intent = "TEXT_CHAT"
|
| 137 |
+
|
| 138 |
+
return {"intent": intent, "p_image_generation": p_img, "p_text_chat": 1.0 - p_img}
|
| 139 |
+
|
| 140 |
+
# Example predictions
|
| 141 |
+
print(predict_intent("Can you show me a photo in traditional clothes?"))
|
| 142 |
+
# {'intent': 'IMAGE_GENERATION', 'p_image_generation': 0.985, ...}
|
| 143 |
|
| 144 |
+
print(predict_intent("I took a photo of the sunset yesterday."))
|
| 145 |
+
# {'intent': 'TEXT_CHAT', 'p_image_generation': 0.015, ...}
|
| 146 |
+
|
| 147 |
+
print(predict_intent("Photo mat bhejna please."))
|
| 148 |
+
# {'intent': 'TEXT_CHAT', 'p_image_generation': 0.021, ...}
|
| 149 |
```
|
| 150 |
|
| 151 |
+
### Option B: Transformers Pipeline (PyTorch)
|
| 152 |
```python
|
| 153 |
from transformers import pipeline
|
| 154 |
|
| 155 |
+
classifier = pipeline("text-classification", model="Umranz/intent-classifier")
|
| 156 |
+
result = classifier("Show me a picture of you smiling")
|
| 157 |
+
print(result)
|
| 158 |
+
# [{'label': 'IMAGE_GENERATION', 'score': 0.985}]
|
| 159 |
+
```
|
| 160 |
+
|
| 161 |
+
---
|
| 162 |
+
|
| 163 |
+
## π‘οΈ Recommended Production Routing Architecture
|
| 164 |
+
|
| 165 |
+
For production systems with strict latency requirements, a **hybrid routing architecture** is recommended:
|
| 166 |
+
|
| 167 |
+
```
|
| 168 |
+
[ User Input ]
|
| 169 |
+
β
|
| 170 |
+
βΌ
|
| 171 |
+
[ Fast-Path Regex (0 ms) ] ββ (Explicit commands or strict negations) βββΊ Direct Resolution
|
| 172 |
+
β (Unmatched / Conversational)
|
| 173 |
+
βΌ
|
| 174 |
+
[ ONNX Intent Classifier (~12 ms CPU) ]
|
| 175 |
+
β
|
| 176 |
+
βββ P(IMAGE) >= 0.75 βββββββββΊ Trigger Image Synthesis Pipeline
|
| 177 |
+
βββ 0.45 <= P(IMAGE) < 0.75 ββΊ Request User Clarification
|
| 178 |
+
βββ P(IMAGE) < 0.45 ββββββββββΊ Route to Conversational LLM
|
| 179 |
```
|
| 180 |
+
|
| 181 |
+
---
|
| 182 |
+
|
| 183 |
+
## π Limitations & Ethical Considerations
|
| 184 |
+
|
| 185 |
+
- **Out-of-Distribution Vocabulary**: Highly regional slang or rare phonetic transliterations not present in the multilingual pretraining vocabulary may yield lower confidence.
|
| 186 |
+
- **Context Awareness**: The model evaluates single-turn utterances; multi-turn context (e.g., *"Yes, do that"*) should be supplemented with conversation history tracking.
|