File size: 5,781 Bytes
51e419e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
#!/usr/bin/env python3
"""
Test script for Sinhala Subtitle Hardcoder
Run this to test functionality before deploying to Hugging Face
"""

import os
import sys

def test_imports():
    """Test if all required packages are installed"""
    print("Testing imports...")
    try:
        import gradio as gr
        print("✅ Gradio imported successfully")
    except ImportError:
        print("❌ Gradio not found. Run: pip install gradio")
        return False
    
    try:
        import requests
        print("✅ Requests imported successfully")
    except ImportError:
        print("❌ Requests not found. Run: pip install requests")
        return False
    
    return True

def test_ffmpeg():
    """Test if FFmpeg is installed"""
    print("\nTesting FFmpeg...")
    import subprocess
    try:
        result = subprocess.run(['ffmpeg', '-version'], 
                              capture_output=True, 
                              text=True, 
                              timeout=5)
        if result.returncode == 0:
            version = result.stdout.split('\n')[0]
            print(f"✅ FFmpeg found: {version}")
            return True
        else:
            print("❌ FFmpeg found but returned error")
            return False
    except FileNotFoundError:
        print("❌ FFmpeg not found. Install it:")
        print("   Ubuntu/Debian: sudo apt-get install ffmpeg")
        print("   macOS: brew install ffmpeg")
        print("   Windows: Download from https://ffmpeg.org/")
        return False
    except Exception as e:
        print(f"❌ Error testing FFmpeg: {e}")
        return False

def test_fonts():
    """Test if Sinhala fonts are available"""
    print("\nTesting Sinhala fonts...")
    import subprocess
    try:
        # Check for common Sinhala fonts
        result = subprocess.run(['fc-list', ':lang=si'], 
                              capture_output=True, 
                              text=True, 
                              timeout=5)
        if 'Noto' in result.stdout or 'Sinhala' in result.stdout:
            print("✅ Sinhala fonts found")
            fonts = [line for line in result.stdout.split('\n') if 'Sinhala' in line or 'Noto' in line]
            for font in fonts[:3]:
                print(f"   - {font[:80]}...")
            return True
        else:
            print("⚠️  No Sinhala fonts found. Install Noto fonts:")
            print("   Ubuntu/Debian: sudo apt-get install fonts-noto-core fonts-noto-extra")
            print("   macOS: brew install font-noto-sans")
            return False
    except FileNotFoundError:
        print("⚠️  fontconfig not found, cannot check fonts")
        return False
    except Exception as e:
        print(f"⚠️  Error checking fonts: {e}")
        return False

def test_subtitle_encoding():
    """Test subtitle encoding handling"""
    print("\nTesting subtitle encoding...")
    test_content = """1
00:00:01,000 --> 00:00:04,000
හෙලෝ, මෙය සිංහල උපසිරැසි පරීක්ෂණයකි

2
00:00:05,000 --> 00:00:08,000
Unicode test: \u0D9A\u0DCA\u200D\u0DBB\u0DD2\u0DBA\u0DCF
"""
    
    import tempfile
    try:
        # Create test file
        with tempfile.NamedTemporaryFile(mode='w', encoding='utf-8', 
                                        suffix='.srt', delete=False) as f:
            f.write(test_content)
            test_file = f.name
        
        # Try to read it back
        with open(test_file, 'r', encoding='utf-8') as f:
            content = f.read()
        
        # Check for Sinhala characters
        if '\u0D80' <= content[50] <= '\u0DFF':  # Check if Sinhala unicode present
            print("✅ Subtitle encoding test passed")
            os.unlink(test_file)
            return True
        else:
            print("❌ Subtitle encoding test failed")
            os.unlink(test_file)
            return False
    except Exception as e:
        print(f"❌ Subtitle encoding test failed: {e}")
        return False

def test_app_import():
    """Test if app.py can be imported"""
    print("\nTesting app.py import...")
    try:
        sys.path.insert(0, os.path.dirname(os.path.abspath(__file__)))
        import app
        print("✅ app.py imported successfully")
        return True
    except Exception as e:
        print(f"❌ Failed to import app.py: {e}")
        return False

def main():
    """Run all tests"""
    print("=" * 60)
    print("Sinhala Subtitle Hardcoder - Environment Test")
    print("=" * 60)
    
    tests = [
        ("Imports", test_imports),
        ("FFmpeg", test_ffmpeg),
        ("Sinhala Fonts", test_fonts),
        ("Subtitle Encoding", test_subtitle_encoding),
        ("App Import", test_app_import),
    ]
    
    results = {}
    for name, test_func in tests:
        results[name] = test_func()
    
    print("\n" + "=" * 60)
    print("Test Results Summary")
    print("=" * 60)
    
    for name, passed in results.items():
        status = "✅ PASS" if passed else "❌ FAIL"
        print(f"{name:20s}: {status}")
    
    print("=" * 60)
    
    if all(results.values()):
        print("\n🎉 All tests passed! Ready to deploy to Hugging Face.")
        print("\nNext steps:")
        print("1. Create a new Space on Hugging Face")
        print("2. Upload app.py, requirements.txt, packages.txt, README.md")
        print("3. Wait for build to complete")
        print("4. Test with real videos!")
        return 0
    else:
        print("\n⚠️  Some tests failed. Fix the issues above before deploying.")
        failed = [name for name, passed in results.items() if not passed]
        print(f"\nFailed tests: {', '.join(failed)}")
        return 1

if __name__ == "__main__":
    sys.exit(main())