Update README.md
Browse files
README.md
CHANGED
|
@@ -65,3 +65,39 @@ For a detailed example of how to load and use this model, please refer to the Co
|
|
| 65 |
## Ethical Considerations
|
| 66 |
|
| 67 |
As with any language model, care should be taken when deploying this for real-world applications. Potential biases present in the training data could be reflected in the translations. It's important to monitor its output and ensure fair and accurate use.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 65 |
## Ethical Considerations
|
| 66 |
|
| 67 |
As with any language model, care should be taken when deploying this for real-world applications. Potential biases present in the training data could be reflected in the translations. It's important to monitor its output and ensure fair and accurate use.
|
| 68 |
+
## 🚀 How to use
|
| 69 |
+
|
| 70 |
+
```python
|
| 71 |
+
from huggingface_hub import snapshot_download
|
| 72 |
+
import tensorflow as tf
|
| 73 |
+
import numpy as np
|
| 74 |
+
import os
|
| 75 |
+
from tensorflow.keras.preprocessing.text import tokenizer_from_json
|
| 76 |
+
from tensorflow.keras.preprocessing.sequence import pad_sequences
|
| 77 |
+
|
| 78 |
+
repo_id = "{repo_id}"
|
| 79 |
+
local_dir = snapshot_download(repo_id=repo_id)
|
| 80 |
+
|
| 81 |
+
model = tf.keras.models.load_model(os.path.join(local_dir, "Translation_model_for_hf.keras"))
|
| 82 |
+
|
| 83 |
+
with open(os.path.join(local_dir, "tokenizer/eng_tokenizer.json"), "r", encoding="utf-8") as f:
|
| 84 |
+
eng_tokenizer = tokenizer_from_json(f.read())
|
| 85 |
+
|
| 86 |
+
with open(os.path.join(local_dir, "tokenizer/ar_tokenizer.json"), "r", encoding="utf-8") as f:
|
| 87 |
+
ar_tokenizer = tokenizer_from_json(f.read())
|
| 88 |
+
|
| 89 |
+
def translate(sentences):
|
| 90 |
+
seq = eng_tokenizer.texts_to_sequences(sentences)
|
| 91 |
+
padded = pad_sequences(seq, maxlen=model.input_shape[1], padding='post')
|
| 92 |
+
preds = model.predict(padded)
|
| 93 |
+
preds = np.argmax(preds, axis=-1)
|
| 94 |
+
|
| 95 |
+
results = []
|
| 96 |
+
for s in preds:
|
| 97 |
+
text = [ar_tokenizer.index_word[i] for i in s if i != 0]
|
| 98 |
+
results.append(' '.join(text))
|
| 99 |
+
return results
|
| 100 |
+
|
| 101 |
+
# Example
|
| 102 |
+
print(translate(["Hello, how are you?"]))
|
| 103 |
+
"""
|