-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_translation_service.py
More file actions
270 lines (217 loc) · 9.39 KB
/
Copy pathtest_translation_service.py
File metadata and controls
270 lines (217 loc) · 9.39 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
#!/usr/bin/env python3
"""
Test script for the Translation Service
This script tests the translation processor functionality including:
- Single text translation
- Batch translation
- Language detection
- API endpoints integration
"""
import sys
import time
import requests
from pathlib import Path
# Add src to path for imports
sys.path.append(str(Path(__file__).parent / "src"))
from src.file_processors.translation_processor import TranslationProcessor
from src.config import Config
def test_processor_initialization():
"""Test if translation processor can be initialized."""
print("🔧 Testing Translation Processor Initialization...")
print(f" OpenAI API Key configured: {bool(Config.OPENAI_API_KEY)}")
print(f" Gemini API Key configured: {bool(Config.GOOGLE_API_KEY)}")
if not Config.OPENAI_API_KEY and not Config.GOOGLE_API_KEY:
print(" ⚠️ No API keys configured - processor will have limited functionality")
return None
try:
processor = TranslationProcessor()
print(" ✅ Translation processor initialized successfully")
# Test supported languages
languages = processor.get_supported_languages()
print(f" 📋 Supported languages: {len(languages)}")
print(f" 🌍 Sample languages: {', '.join(list(languages.keys())[:5])}")
return processor
except Exception as e:
print(f" ❌ Error initializing processor: {e}")
return None
def test_language_detection(processor):
"""Test language detection functionality."""
if not processor:
print("⏭️ Skipping language detection tests (no processor)")
return
print("\n🔍 Testing Language Detection...")
test_texts = [
("Hello, this is English text.", "en"),
("Hola, esto es texto en español.", "es"),
("Bonjour, ceci est du texte français.", "fr"),
("Guten Tag, das ist deutscher Text.", "de"),
]
for text, expected_lang in test_texts:
print(f" Text: '{text[:30]}...'")
print(f" Expected: {expected_lang}")
try:
if Config.OPENAI_API_KEY:
result = processor.detect_language(text, use_openai=True)
if result["success"]:
detected = result["detected_language"]
print(f" 📍 OpenAI detected: {detected} (expected: {expected_lang})")
else:
print(f" ❌ OpenAI detection failed: {result.get('error')}")
if Config.GOOGLE_API_KEY:
result = processor.detect_language(text, use_openai=False)
if result["success"]:
detected = result["detected_language"]
print(f" 📍 Gemini detected: {detected} (expected: {expected_lang})")
else:
print(f" ❌ Gemini detection failed: {result.get('error')}")
except Exception as e:
print(f" ❌ Error in detection: {e}")
def test_single_translation(processor):
"""Test single text translation."""
if not processor:
print("⏭️ Skipping single translation tests (no processor)")
return
print("\n🌐 Testing Single Text Translation...")
test_cases = [
("Hello world!", "en", "es"),
("How are you today?", "en", "fr"),
("Thank you very much", "en", "de"),
]
for text, source_lang, target_lang in test_cases:
print(f" Translating: '{text}' ({source_lang} → {target_lang})")
try:
if Config.OPENAI_API_KEY:
result = processor.translate_text(text, source_lang, target_lang, use_openai=True)
if result["success"]:
translation = result["translated_text"]
print(f" ✅ OpenAI: '{translation}'")
else:
print(f" ❌ OpenAI failed: {result.get('error')}")
if Config.GOOGLE_API_KEY:
result = processor.translate_text(text, source_lang, target_lang, use_openai=False)
if result["success"]:
translation = result["translated_text"]
print(f" ✅ Gemini: '{translation}'")
else:
print(f" ❌ Gemini failed: {result.get('error')}")
except Exception as e:
print(f" ❌ Error in translation: {e}")
def test_batch_translation(processor):
"""Test batch translation functionality."""
if not processor:
print("⏭️ Skipping batch translation tests (no processor)")
return
print("\n📦 Testing Batch Translation...")
texts = [
"Good morning!",
"How can I help you?",
"Thank you for your time.",
"Have a great day!"
]
print(f" Translating {len(texts)} texts (en → es)")
try:
if Config.OPENAI_API_KEY:
result = processor.translate_batch(texts, "en", "es", use_openai=True)
if result["success"]:
print(f" ✅ OpenAI batch: {result['successful']}/{result['total_texts']} successful")
for item in result["results"][:2]: # Show first 2 results
if item["success"]:
print(f" '{item['original_text']}' → '{item['translated_text']}'")
else:
print(f" ❌ OpenAI batch failed")
if Config.GOOGLE_API_KEY:
result = processor.translate_batch(texts, "en", "es", use_openai=False)
if result["success"]:
print(f" ✅ Gemini batch: {result['successful']}/{result['total_texts']} successful")
else:
print(f" ❌ Gemini batch failed")
except Exception as e:
print(f" ❌ Error in batch translation: {e}")
def test_api_endpoints():
"""Test API endpoints if server is running."""
print("\n🌐 Testing API Endpoints...")
base_url = "http://localhost:8000"
# Check if server is running
try:
response = requests.get(f"{base_url}/health", timeout=5)
if response.status_code != 200:
print(" ⏭️ API server not running, skipping endpoint tests")
return
except requests.exceptions.RequestException:
print(" ⏭️ API server not accessible, skipping endpoint tests")
return
print(" 🟢 API server detected, testing endpoints...")
# Test supported languages endpoint
try:
response = requests.get(f"{base_url}/supported-languages")
if response.status_code == 200:
data = response.json()
print(f" ✅ Supported languages: {data.get('total_languages', 0)} languages")
else:
print(f" ❌ Supported languages endpoint failed: {response.status_code}")
except Exception as e:
print(f" ❌ Error testing supported languages: {e}")
# Test single translation endpoint
try:
payload = {
"text": "Hello API test",
"source_language": "en",
"target_language": "es",
"use_openai": True
}
response = requests.post(f"{base_url}/translate", json=payload)
if response.status_code == 200:
data = response.json()
if data.get("success"):
print(f" ✅ Translation endpoint: '{data.get('translated_text')}'")
else:
print(f" ❌ Translation failed: {data.get('error')}")
else:
print(f" ❌ Translation endpoint failed: {response.status_code}")
except Exception as e:
print(f" ❌ Error testing translation endpoint: {e}")
# Test language detection endpoint
try:
payload = {
"text": "Bonjour, comment ça va?",
"use_openai": True
}
response = requests.post(f"{base_url}/detect-language", json=payload)
if response.status_code == 200:
data = response.json()
if data.get("success"):
print(f" ✅ Detection endpoint: '{data.get('detected_language')}'")
else:
print(f" ❌ Detection failed: {data.get('error')}")
else:
print(f" ❌ Detection endpoint failed: {response.status_code}")
except Exception as e:
print(f" ❌ Error testing detection endpoint: {e}")
def main():
"""Run all translation service tests."""
print("🧪 AI Content Processing - Translation Service Tests")
print("=" * 60)
# Test processor initialization
processor = test_processor_initialization()
# Test core functionality
test_language_detection(processor)
test_single_translation(processor)
test_batch_translation(processor)
# Test API endpoints
test_api_endpoints()
print("\n" + "=" * 60)
print("🎉 Translation service tests completed!")
# Show configuration recommendations
print("\n💡 Configuration Notes:")
if not Config.OPENAI_API_KEY:
print(" • Set OPENAI_API_KEY for OpenAI translations")
if not Config.GOOGLE_API_KEY:
print(" • Set GOOGLE_API_KEY for Gemini translations")
if Config.OPENAI_API_KEY and Config.GOOGLE_API_KEY:
print(" ✅ Both APIs configured - full functionality available")
print("\n📖 Usage Examples:")
print(" • python translation_examples.py")
print(" • curl examples in examples/TRANSLATION_CURL_EXAMPLES.md")
print(" • Start API: python api_server.py")
if __name__ == "__main__":
main()