File size: 2,921 Bytes
6123728
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
# 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()