File size: 3,044 Bytes
e954acb
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import subprocess
from pathlib import Path


def check_openssl():
    """Check if OpenSSL is available."""
    try:
        subprocess.run(["openssl", "version"], capture_output=True, check=True)
        return True
    except (subprocess.CalledProcessError, FileNotFoundError):
        return False


def generate_ssl_certificates():
    """Generate self-signed SSL certificates for local development."""
    cert_dir = Path("")
    key_file = cert_dir / "key.pem"
    cert_file = cert_dir / "cert.pem"

    # Check if certificates already exist
    if key_file.exists() and cert_file.exists():
        print("βœ… SSL certificates already exist!")
        return True

    if not check_openssl():
        print("❌ OpenSSL not found. Please install OpenSSL first.")
        print("On Ubuntu/Debian: sudo apt-get install openssl")
        print("On macOS: brew install openssl")
        print(
            "On Windows: Download from https://slproweb.com/products/Win32OpenSSL.html"
        )
        return False

    print("πŸ” Generating self-signed SSL certificates...")

    # Generate private key
    key_cmd = ["openssl", "genrsa", "-out", str(key_file), "2048"]

    # Generate certificate
    cert_cmd = [
        "openssl",
        "req",
        "-new",
        "-x509",
        "-key",
        str(key_file),
        "-out",
        str(cert_file),
        "-days",
        "365",
        "-subj",
        "/C=US/ST=CA/L=Local/O=Dev/CN=localhost",
    ]

    try:
        subprocess.run(key_cmd, check=True)
        subprocess.run(cert_cmd, check=True)
        print("βœ… SSL certificates generated successfully!")
        print(f"   Private key: {key_file}")
        print(f"   Certificate: {cert_file}")
        return True
    except subprocess.CalledProcessError as e:
        print(f"❌ Failed to generate SSL certificates: {e}")
        return False


def main():
    """Main function to set up SSL certificates."""
    print("πŸš€ Setting up SSL certificates for local HTTPS...")

    if generate_ssl_certificates():
        print("\nπŸ“‹ Next steps:")
        print("1. Run your Gradio app with the generated certificates")
        print("2. Open https://localhost:7860 in your browser")
        print(
            "3. Accept the security warning (click 'Advanced' β†’ 'Proceed to localhost')"
        )
        print("4. Allow microphone access when prompted")
        print(
            "\n⚠️  Note: You'll see a security warning because these are self-signed certificates."
        )
        print(
            "   This is normal for local development - just click through the warning."
        )
    else:
        print("\n❌ SSL setup failed. Alternative options:")
        print(
            "1. Use Chrome with: --unsafely-treat-insecure-origin-as-secure=http://localhost:7860"
        )
        print(
            "2. Use Firefox and set media.devices.insecure.enabled=true in about:config"
        )
        print("3. Deploy to a server with proper SSL certificates")


if __name__ == "__main__":
    main()