Quickstart updated
Browse files
README.md
CHANGED
|
@@ -173,10 +173,12 @@ class TurnDetector:
|
|
| 173 |
self.max_length = 8192
|
| 174 |
print("✅ Model and tokenizer loaded successfully.")
|
| 175 |
|
| 176 |
-
def predict(self, text: str) ->
|
| 177 |
"""
|
| 178 |
Predicts if a given text utterance is the end of a turn.
|
| 179 |
-
Returns
|
|
|
|
|
|
|
| 180 |
"""
|
| 181 |
# Tokenize the input text
|
| 182 |
inputs = self.tokenizer(
|
|
@@ -194,12 +196,26 @@ class TurnDetector:
|
|
| 194 |
|
| 195 |
# Run inference
|
| 196 |
outputs = self.session.run(None, feed_dict)
|
| 197 |
-
logits = outputs
|
| 198 |
-
|
| 199 |
-
|
| 200 |
-
|
| 201 |
-
|
| 202 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 203 |
|
| 204 |
# --- Example Usage ---
|
| 205 |
if __name__ == "__main__":
|
|
@@ -213,9 +229,10 @@ if __name__ == "__main__":
|
|
| 213 |
]
|
| 214 |
|
| 215 |
for sentence in sentences:
|
| 216 |
-
|
| 217 |
-
|
| 218 |
-
|
|
|
|
| 219 |
```
|
| 220 |
|
| 221 |
|
|
|
|
| 173 |
self.max_length = 8192
|
| 174 |
print("✅ Model and tokenizer loaded successfully.")
|
| 175 |
|
| 176 |
+
def predict(self, text: str) -> tuple:
|
| 177 |
"""
|
| 178 |
Predicts if a given text utterance is the end of a turn.
|
| 179 |
+
Returns (predicted_label, confidence) where:
|
| 180 |
+
- predicted_label: 0 for "Not End of Turn", 1 for "End of Turn"
|
| 181 |
+
- confidence: confidence score between 0 and 1
|
| 182 |
"""
|
| 183 |
# Tokenize the input text
|
| 184 |
inputs = self.tokenizer(
|
|
|
|
| 196 |
|
| 197 |
# Run inference
|
| 198 |
outputs = self.session.run(None, feed_dict)
|
| 199 |
+
logits = outputs[0]
|
| 200 |
+
|
| 201 |
+
probabilities = self._softmax(logits[0])
|
| 202 |
+
predicted_label = np.argmax(probabilities)
|
| 203 |
+
print(f"Logits: {logits[0]}")
|
| 204 |
+
print(f"Probabilities:")
|
| 205 |
+
print(f" Not End of Turn: {probabilities[0]:.1%}")
|
| 206 |
+
print(f" End of Turn: {probabilities[1]:.1%}")
|
| 207 |
+
print(f"Predicted Label: {predicted_label}")
|
| 208 |
+
print(f"Confidence: {probabilities[predicted_label]:.1%}")
|
| 209 |
+
print(f"Confidence (class=1): {np.max(probabilities):.1%}")
|
| 210 |
+
confidence = float(np.max(probabilities))
|
| 211 |
+
|
| 212 |
+
return predicted_label, confidence
|
| 213 |
+
|
| 214 |
+
def _softmax(self, x, axis=None):
|
| 215 |
+
if axis is None:
|
| 216 |
+
axis = -1
|
| 217 |
+
exp_x = np.exp(x - np.max(x, axis=axis, keepdims=True))
|
| 218 |
+
return exp_x / np.sum(exp_x, axis=axis, keepdims=True)
|
| 219 |
|
| 220 |
# --- Example Usage ---
|
| 221 |
if __name__ == "__main__":
|
|
|
|
| 229 |
]
|
| 230 |
|
| 231 |
for sentence in sentences:
|
| 232 |
+
predicted_label, confidence = detector.predict(sentence)
|
| 233 |
+
result = "End of Turn" if predicted_label == 1 else "Not End of Turn"
|
| 234 |
+
print(f"'{sentence}' -> {result} (confidence: {confidence:.3f})")
|
| 235 |
+
print("-" * 50)
|
| 236 |
```
|
| 237 |
|
| 238 |
|