File size: 3,905 Bytes
86fce4f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
#!/usr/bin/env python3
"""

Local runner for DOCX to PDF converter with Arabic support

Run this script to test the converter locally before deploying to Hugging Face Spaces

"""

import subprocess
import sys
import os
from pathlib import Path

def check_system_requirements():
    """Check if all system requirements are installed"""
    print("🔍 Checking system requirements...")
    
    requirements = {
        "LibreOffice": ["libreoffice", "--version"],
        "Font Cache": ["fc-cache", "--version"],
        "Font List": ["fc-list", "--help"]
    }
    
    missing = []
    for name, cmd in requirements.items():
        try:
            result = subprocess.run(cmd, capture_output=True, timeout=5)
            if result.returncode == 0:
                print(f"✅ {name}: Available")
            else:
                print(f"❌ {name}: Not working properly")
                missing.append(name)
        except (subprocess.TimeoutExpired, FileNotFoundError):
            print(f"❌ {name}: Not found")
            missing.append(name)
    
    if missing:
        print(f"\n⚠️ Missing requirements: {', '.join(missing)}")
        print("\nTo install on Ubuntu/Debian:")
        print("sudo apt-get update")
        print("sudo apt-get install libreoffice libreoffice-writer fonts-liberation fonts-dejavu fonts-noto fontconfig")
        return False
    
    print("✅ All system requirements are available")
    return True

def install_python_requirements():
    """Install Python requirements"""
    print("\n📦 Installing Python requirements...")
    try:
        subprocess.run([sys.executable, "-m", "pip", "install", "-r", "requirements.txt"], 
                      check=True)
        print("✅ Python requirements installed successfully")
        return True
    except subprocess.CalledProcessError as e:
        print(f"❌ Failed to install Python requirements: {e}")
        return False

def setup_arabic_fonts():
    """Setup Arabic fonts if the script exists"""
    script_path = Path("arabic_fonts_setup.sh")
    if script_path.exists():
        print("\n🔤 Setting up Arabic fonts...")
        try:
            # Make script executable
            os.chmod(script_path, 0o755)
            subprocess.run(["bash", str(script_path)], check=True)
            print("✅ Arabic fonts setup completed")
            return True
        except subprocess.CalledProcessError as e:
            print(f"⚠️ Arabic fonts setup failed: {e}")
            print("Continuing without additional Arabic fonts...")
            return False
    else:
        print("⚠️ Arabic fonts setup script not found, skipping...")
        return False

def run_app():
    """Run the main application"""
    print("\n🚀 Starting DOCX to PDF converter...")
    print("The application will be available at: http://localhost:7860")
    print("Press Ctrl+C to stop the application")
    
    try:
        subprocess.run([sys.executable, "app.py"], check=True)
    except KeyboardInterrupt:
        print("\n👋 Application stopped by user")
    except subprocess.CalledProcessError as e:
        print(f"❌ Application failed to start: {e}")

def main():
    """Main function"""
    print("🔧 DOCX to PDF Converter - Local Setup")
    print("=" * 50)
    
    # Check system requirements
    if not check_system_requirements():
        print("\n❌ System requirements not met. Please install missing components.")
        return 1
    
    # Install Python requirements
    if not install_python_requirements():
        print("\n❌ Failed to install Python requirements.")
        return 1
    
    # Setup Arabic fonts (optional)
    setup_arabic_fonts()
    
    # Run the application
    run_app()
    
    return 0

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