Upload example_usage.py
Browse files- example_usage.py +93 -0
example_usage.py
ADDED
|
@@ -0,0 +1,93 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
#!/usr/bin/env python3
|
| 2 |
+
"""
|
| 3 |
+
Example script to test the Arabic Message Classification Model
|
| 4 |
+
"""
|
| 5 |
+
|
| 6 |
+
from transformers import AutoTokenizer, AutoModelForSequenceClassification, pipeline
|
| 7 |
+
import torch
|
| 8 |
+
|
| 9 |
+
def main():
|
| 10 |
+
# Model name - replace with your actual model name on Hugging Face
|
| 11 |
+
model_name = "ahmedmajid92/Arabic_MI_Classifier"
|
| 12 |
+
|
| 13 |
+
print("Loading Arabic Message Classification Model...")
|
| 14 |
+
print(f"Model: {model_name}")
|
| 15 |
+
|
| 16 |
+
try:
|
| 17 |
+
# Load tokenizer and model
|
| 18 |
+
tokenizer = AutoTokenizer.from_pretrained(model_name)
|
| 19 |
+
model = AutoModelForSequenceClassification.from_pretrained(model_name)
|
| 20 |
+
|
| 21 |
+
# Create classification pipeline
|
| 22 |
+
device = 0 if torch.cuda.is_available() else -1
|
| 23 |
+
classifier = pipeline(
|
| 24 |
+
"text-classification",
|
| 25 |
+
model=model,
|
| 26 |
+
tokenizer=tokenizer,
|
| 27 |
+
device=device
|
| 28 |
+
)
|
| 29 |
+
|
| 30 |
+
print(f"Model loaded successfully!")
|
| 31 |
+
print(f"Using device: {'GPU' if device >= 0 else 'CPU'}")
|
| 32 |
+
print("-" * 50)
|
| 33 |
+
|
| 34 |
+
# Test examples
|
| 35 |
+
test_examples = [
|
| 36 |
+
"السلام عليكم ورحمة الله وبركاته", # greeting
|
| 37 |
+
"هلو شلونك اليوم؟", # greeting + question
|
| 38 |
+
"متى يبدأ الاجتماع؟", # question
|
| 39 |
+
"عندي مشكلة بالانترنت", # complaint
|
| 40 |
+
"أحب القراءة والكتابة", # general
|
| 41 |
+
"الكهرباء نفطت", # complaint (Iraqi)
|
| 42 |
+
"شنو الأخبار؟", # question (Iraqi)
|
| 43 |
+
"تحية طيبة", # greeting
|
| 44 |
+
"أعمل مهندساً في شركة تقنية", # general
|
| 45 |
+
"الطابعة ما تطبع" # complaint (Iraqi)
|
| 46 |
+
]
|
| 47 |
+
|
| 48 |
+
print("Testing with example messages:")
|
| 49 |
+
print("=" * 60)
|
| 50 |
+
|
| 51 |
+
for i, text in enumerate(test_examples, 1):
|
| 52 |
+
result = classifier(text)[0]
|
| 53 |
+
label = result['label']
|
| 54 |
+
confidence = result['score']
|
| 55 |
+
|
| 56 |
+
print(f"{i:2d}. Text: {text}")
|
| 57 |
+
print(f" → Label: {label}")
|
| 58 |
+
print(f" → Confidence: {confidence:.4f}")
|
| 59 |
+
print()
|
| 60 |
+
|
| 61 |
+
print("=" * 60)
|
| 62 |
+
print("Interactive mode - Enter your own text (or 'quit' to exit):")
|
| 63 |
+
|
| 64 |
+
while True:
|
| 65 |
+
user_input = input("\nEnter Arabic text: ").strip()
|
| 66 |
+
|
| 67 |
+
if user_input.lower() in ['quit', 'exit', 'q']:
|
| 68 |
+
print("Goodbye!")
|
| 69 |
+
break
|
| 70 |
+
|
| 71 |
+
if not user_input:
|
| 72 |
+
continue
|
| 73 |
+
|
| 74 |
+
try:
|
| 75 |
+
result = classifier(user_input)[0]
|
| 76 |
+
label = result['label']
|
| 77 |
+
confidence = result['score']
|
| 78 |
+
|
| 79 |
+
print(f"→ Label: {label}")
|
| 80 |
+
print(f"→ Confidence: {confidence:.4f}")
|
| 81 |
+
|
| 82 |
+
except Exception as e:
|
| 83 |
+
print(f"Error processing text: {e}")
|
| 84 |
+
|
| 85 |
+
except Exception as e:
|
| 86 |
+
print(f"Error loading model: {e}")
|
| 87 |
+
print("Make sure to:")
|
| 88 |
+
print("1. Install required packages: pip install transformers torch")
|
| 89 |
+
print("2. Update the model_name variable with your actual model name")
|
| 90 |
+
print("3. Check your internet connection")
|
| 91 |
+
|
| 92 |
+
if __name__ == "__main__":
|
| 93 |
+
main()
|