pdf / run_local.py
fokan's picture
Upload 35 files
86fce4f verified
#!/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())