Update README.md
Browse files
README.md
CHANGED
|
@@ -17,4 +17,51 @@ tags:
|
|
| 17 |
- voice
|
| 18 |
- nlp
|
| 19 |
- real-time
|
| 20 |
-
---
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 17 |
- voice
|
| 18 |
- nlp
|
| 19 |
- real-time
|
| 20 |
+
---
|
| 21 |
+
|
| 22 |
+
## Usage
|
| 23 |
+
|
| 24 |
+
```python
|
| 25 |
+
import numpy as np
|
| 26 |
+
import onnxruntime as ort
|
| 27 |
+
from transformers import AutoTokenizer
|
| 28 |
+
from huggingface_hub import hf_hub_download
|
| 29 |
+
import time
|
| 30 |
+
|
| 31 |
+
class SaudiEOU:
|
| 32 |
+
def __init__(self, repo_id="mohamedsamyy/Saudi-EOU"):
|
| 33 |
+
print(f"Loading model from repo: {repo_id}")
|
| 34 |
+
model_path = hf_hub_download(repo_id=repo_id, filename="Saudi_EOU.onnx")
|
| 35 |
+
self.tokenizer = AutoTokenizer.from_pretrained(repo_id)
|
| 36 |
+
self.session = ort.InferenceSession(model_path, providers=["CUDAExecutionProvider"])
|
| 37 |
+
self.max_length = 128
|
| 38 |
+
print("✅ Model and tokenizer loaded successfully.")
|
| 39 |
+
|
| 40 |
+
def predict(self, text: str) -> tuple:
|
| 41 |
+
inputs = self.tokenizer(text, truncation=True, max_length=self.max_length, return_tensors="np")
|
| 42 |
+
feed_dict = {"input_ids": inputs["input_ids"], "attention_mask": inputs["attention_mask"]}
|
| 43 |
+
start = time.perf_counter()
|
| 44 |
+
outputs = self.session.run(None, feed_dict)
|
| 45 |
+
logits = outputs[0][0]
|
| 46 |
+
confidence = self._sigmoid(logits[0])
|
| 47 |
+
end = time.perf_counter()
|
| 48 |
+
print(f"'{text}' -> latency: {end - start:.4f}s")
|
| 49 |
+
predicted_label = 1 if confidence >= 0.5 else 0
|
| 50 |
+
return predicted_label, confidence
|
| 51 |
+
|
| 52 |
+
def _sigmoid(self, x):
|
| 53 |
+
return 1 / (1 + np.exp(-x))
|
| 54 |
+
|
| 55 |
+
# Example usage
|
| 56 |
+
detector = SaudiEOU()
|
| 57 |
+
sentences = ["حياك الله", "ممم", "اهلا", "يا هلا ", "السلام عليكم"]
|
| 58 |
+
|
| 59 |
+
for sentence in sentences:
|
| 60 |
+
predicted_label, confidence = detector.predict(sentence)
|
| 61 |
+
result = "End of Turn" if predicted_label == 1 else "Not End of Turn"
|
| 62 |
+
print(f"'{sentence}' -> {result} (confidence: {confidence:.3f})")
|
| 63 |
+
```
|
| 64 |
+
|
| 65 |
+
|
| 66 |
+
This example shows how to load the **SaudiEOU** ONNX model from the Hugging Face Hub and predict if a sentence is an end-of-turn utterance.
|
| 67 |
+
The model runs on GPU if available, and prints the latency per sentence.
|