SamaaliWhisper / test_api.py
MuhammadHijazii's picture
Upload 17 files
dfdd9cb verified
#!/usr/bin/env python3
"""
اختبار API لمعالج النصوص
"""
import requests
import json
import time
import logging
# إعداد التسجيل
logging.basicConfig(level=logging.INFO)
logger = logging.getLogger(__name__)
BASE_URL = "http://localhost:8000"
def test_health_endpoint():
"""اختبار نقطة فحص الحالة"""
try:
response = requests.get(f"{BASE_URL}/health")
if response.status_code == 200:
data = response.json()
logger.info(f"✅ Health check: {data['message']}")
return True
else:
logger.error(f"❌ Health check failed: {response.status_code}")
return False
except Exception as e:
logger.error(f"❌ Health check error: {str(e)}")
return False
def test_text_correction():
"""اختبار تصحيح النصوص"""
try:
data = {
"text": "يحتاز غشاء الطبل تنتقل عظيمات السماء الاحتزازات",
"reference_texts": [
"يهتز غشاء الطبل تنتقل عظيمات السمع الاهتزازات إلى النافذة البيضية",
"يهتز غشاء النافذة البيضية يهتز اللمف الخارجي في القناة الدهليزية"
],
"max_distance": 3,
"threshold_freq": 1
}
response = requests.post(
f"{BASE_URL}/correct-text",
json=data,
headers={"Content-Type": "application/json"}
)
if response.status_code == 200:
result = response.json()
logger.info(f"✅ Text correction successful")
logger.info(f" Original: {result['data']['original_text']}")
logger.info(f" Corrected: {result['data']['corrected_text']}")
logger.info(f" Corrections: {result['data']['corrections_count']}")
return True
else:
logger.error(f"❌ Text correction failed: {response.status_code}")
logger.error(f" Response: {response.text}")
return False
except Exception as e:
logger.error(f"❌ Text correction error: {str(e)}")
return False
def test_text_comparison():
"""اختبار مقارنة النصوص"""
try:
data = {
"reference_text": "يهتز غشاء الطبل تنتقل عظيمات السمع الاهتزازات إلى النافذة البيضية",
"transcribed_text": "يحتاز غشاء الطبل تنتقل عظيمات السماء الاحتزازات إلى النافذة البيضية"
}
response = requests.post(
f"{BASE_URL}/compare-texts",
json=data,
headers={"Content-Type": "application/json"}
)
if response.status_code == 200:
result = response.json()
logger.info(f"✅ Text comparison successful")
logger.info(f" WER: {result['data']['wer_percentage']}")
logger.info(f" CER: {result['data']['cer_percentage']}")
return True
else:
logger.error(f"❌ Text comparison failed: {response.status_code}")
logger.error(f" Response: {response.text}")
return False
except Exception as e:
logger.error(f"❌ Text comparison error: {str(e)}")
return False
def main():
"""الدالة الرئيسية للاختبار"""
logger.info("🚀 بدء اختبار API لمعالج النصوص")
# انتظار تشغيل الخادم
logger.info("انتظار تشغيل الخادم...")
time.sleep(2)
tests_passed = 0
total_tests = 3
# اختبار فحص الحالة
if test_health_endpoint():
tests_passed += 1
# اختبار تصحيح النصوص
if test_text_correction():
tests_passed += 1
# اختبار مقارنة النصوص
if test_text_comparison():
tests_passed += 1
# عرض النتائج
logger.info(f"\n📊 نتائج الاختبارات: {tests_passed}/{total_tests}")
if tests_passed == total_tests:
logger.info("✅ جميع اختبارات API نجحت!")
return True
else:
logger.error("❌ فشلت بعض اختبارات API")
return False
if __name__ == "__main__":
success = main()
exit(0 if success else 1)