syncmaster3 / test_server_translation.py
aseelflihan's picture
move actual project files into root folder
1138072
# test_server_translation.py - Test server translation response
import requests
import json
import tempfile
import wave
import numpy as np
def create_test_audio():
"""Create a simple test audio file"""
sample_rate = 16000
duration = 2 # seconds
frequency = 440 # Hz (A note)
# Generate sine wave
t = np.linspace(0, duration, int(sample_rate * duration))
audio_data = (0.3 * np.sin(2 * np.pi * frequency * t) * 32767).astype(np.int16)
# Create temporary WAV file
with tempfile.NamedTemporaryFile(delete=False, suffix='.wav') as tmp_file:
with wave.open(tmp_file.name, 'w') as wav_file:
wav_file.setnchannels(1) # Mono
wav_file.setsampwidth(2) # 2 bytes per sample
wav_file.setframerate(sample_rate)
wav_file.writeframes(audio_data.tobytes())
return tmp_file.name
def test_server_translation():
print("πŸ” Testing Server Translation Response...")
# Create test audio
audio_file = create_test_audio()
print(f"πŸ“ Created test audio: {audio_file}")
try:
# Prepare request data
with open(audio_file, 'rb') as f:
files = {'audio': f}
data = {
'target_language': 'ar',
'enable_translation': 'true',
'markers': '[]'
}
# Send request to server
response = requests.post('http://localhost:5001/record', files=files, data=data)
if response.status_code == 200:
result = response.json()
print(f"βœ… Server Response Received")
print(f"πŸ“Š Success: {result.get('success')}")
print(f"πŸ”€ Original text: {result.get('original_text', 'None')}")
print(f"πŸ”„ Translated text: {result.get('translated_text', 'None')}")
print(f"🌍 Translation enabled: {result.get('translation_enabled')}")
print(f"βœ… Translation success: {result.get('translation_success')}")
print(f"🌐 Target language: {result.get('target_language')}")
print(f"πŸ” Detected language: {result.get('language_detected')}")
# Check if texts are different
original = result.get('original_text', '')
translated = result.get('translated_text', '')
print(f"πŸ”„ Texts are different: {original != translated}")
else:
print(f"❌ Server error: {response.status_code}")
print(f"Response: {response.text}")
except Exception as e:
print(f"❌ Test failed: {str(e)}")
finally:
# Clean up
import os
if os.path.exists(audio_file):
os.unlink(audio_file)
if __name__ == "__main__":
test_server_translation()