Spaces:
				
			
			
	
			
			
		Paused
		
	
	
	
			
			
	
	
	
	
		
		
		Paused
		
	Upload 2 files
Browse files- start-ngrok.py +109 -0
- start.sh +47 -0
    	
        start-ngrok.py
    ADDED
    
    | @@ -0,0 +1,109 @@ | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | 
|  | |
| 1 | 
            +
            import argparse
         | 
| 2 | 
            +
            import json
         | 
| 3 | 
            +
            from pyngrok import ngrok, conf
         | 
| 4 | 
            +
            import os
         | 
| 5 | 
            +
            import psutil
         | 
| 6 | 
            +
            import signal
         | 
| 7 | 
            +
            import socket
         | 
| 8 | 
            +
            import sys
         | 
| 9 | 
            +
            import subprocess
         | 
| 10 | 
            +
             | 
| 11 | 
            +
            def get_saved_data():
         | 
| 12 | 
            +
                try:
         | 
| 13 | 
            +
                    with open('data.json', 'r') as file:
         | 
| 14 | 
            +
                        data = json.load(file)
         | 
| 15 | 
            +
                    return data
         | 
| 16 | 
            +
                except (FileNotFoundError, json.JSONDecodeError):
         | 
| 17 | 
            +
                    return None
         | 
| 18 | 
            +
             | 
| 19 | 
            +
            def save_data(data):
         | 
| 20 | 
            +
                with open('data.json', 'w') as file:
         | 
| 21 | 
            +
                    json.dump(data, file)
         | 
| 22 | 
            +
             | 
| 23 | 
            +
            def signal_handler(sig, frame):
         | 
| 24 | 
            +
                print('You pressed Ctrl+C!')
         | 
| 25 | 
            +
                sys.exit(0)
         | 
| 26 | 
            +
                
         | 
| 27 | 
            +
            def is_port_in_use(port):
         | 
| 28 | 
            +
                with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s:
         | 
| 29 | 
            +
                    return s.connect_ex(('127.0.0.1', port)) == 0
         | 
| 30 | 
            +
                
         | 
| 31 | 
            +
            def find_and_terminate_process(port):
         | 
| 32 | 
            +
                for process in psutil.process_iter(['pid', 'name', 'connections']):
         | 
| 33 | 
            +
                    for conn in process.info.get('connections', []):
         | 
| 34 | 
            +
                        if conn.laddr.port == port:
         | 
| 35 | 
            +
                            print(f"Port {port} is in use by process {process.info['name']} (PID {process.info['pid']})")
         | 
| 36 | 
            +
                            try:
         | 
| 37 | 
            +
                                process.terminate()
         | 
| 38 | 
            +
                                print(f"Terminated process with PID {process.info['pid']}")
         | 
| 39 | 
            +
                            except psutil.NoSuchProcess:
         | 
| 40 | 
            +
                                print(f"Process with PID {process.info['pid']} not found")
         | 
| 41 | 
            +
                    
         | 
| 42 | 
            +
            def main():
         | 
| 43 | 
            +
                target_port = 7860
         | 
| 44 | 
            +
                
         | 
| 45 | 
            +
                if is_port_in_use(target_port):
         | 
| 46 | 
            +
                    find_and_terminate_process(target_port)
         | 
| 47 | 
            +
                else:
         | 
| 48 | 
            +
                    print(f"Port {target_port} is free.")
         | 
| 49 | 
            +
                
         | 
| 50 | 
            +
                parser = argparse.ArgumentParser(description='Console app with token and domain arguments')
         | 
| 51 | 
            +
                parser.add_argument('--token', help='Specify the token')
         | 
| 52 | 
            +
                parser.add_argument('--domain', help='Specify the domain')
         | 
| 53 | 
            +
                parser.add_argument('--reset', action='store_true', help='Reset saved data')
         | 
| 54 | 
            +
             | 
| 55 | 
            +
                args = parser.parse_args()
         | 
| 56 | 
            +
             | 
| 57 | 
            +
                saved_data = get_saved_data()
         | 
| 58 | 
            +
             | 
| 59 | 
            +
                if args.reset:
         | 
| 60 | 
            +
                    if saved_data is not None:
         | 
| 61 | 
            +
                        saved_data = { 'token': '', 'domain': ''}
         | 
| 62 | 
            +
                else:
         | 
| 63 | 
            +
                    if saved_data is not None:
         | 
| 64 | 
            +
                        if args.token:
         | 
| 65 | 
            +
                            saved_data['token'] = args.token
         | 
| 66 | 
            +
                        if args.domain:
         | 
| 67 | 
            +
                            saved_data['domain'] = args.domain
         | 
| 68 | 
            +
                    else:
         | 
| 69 | 
            +
                        saved_data = { 'token': '', 'domain': ''}
         | 
| 70 | 
            +
             | 
| 71 | 
            +
                if args.token is None:
         | 
| 72 | 
            +
                        if saved_data and saved_data['token']:
         | 
| 73 | 
            +
                            args.token = saved_data['token']
         | 
| 74 | 
            +
                        else:
         | 
| 75 | 
            +
                            args.token = input('Enter the token: ')
         | 
| 76 | 
            +
                            if args.token == '':
         | 
| 77 | 
            +
                                args.token = input('Enter the token: ')
         | 
| 78 | 
            +
                            saved_data['token'] = args.token
         | 
| 79 | 
            +
             | 
| 80 | 
            +
                if args.domain is None:
         | 
| 81 | 
            +
                        args.domain = ''
         | 
| 82 | 
            +
                        if saved_data and saved_data['domain']:
         | 
| 83 | 
            +
                            args.domain = saved_data['domain']
         | 
| 84 | 
            +
                        else:
         | 
| 85 | 
            +
                            args.domain = input('Enter the domain: ')
         | 
| 86 | 
            +
                            saved_data['domain'] = args.domain
         | 
| 87 | 
            +
             | 
| 88 | 
            +
                save_data(saved_data)
         | 
| 89 | 
            +
             | 
| 90 | 
            +
                print(f'Token: {args.token}')
         | 
| 91 | 
            +
                print(f'Domain: {args.domain}')
         | 
| 92 | 
            +
                
         | 
| 93 | 
            +
                if args.token != '':
         | 
| 94 | 
            +
                  ngrok.kill()
         | 
| 95 | 
            +
                  srv = ngrok.connect(target_port, pyngrok_config=conf.PyngrokConfig(auth_token=args.token),
         | 
| 96 | 
            +
                                bind_tls=True, domain=args.domain).public_url
         | 
| 97 | 
            +
                  print(srv)
         | 
| 98 | 
            +
             | 
| 99 | 
            +
                  signal.signal(signal.SIGINT, signal_handler)
         | 
| 100 | 
            +
                  print('Press Ctrl+C to exit')
         | 
| 101 | 
            +
                  cmd = 'cd facefusion; python run.py --execution-providers cuda'
         | 
| 102 | 
            +
                  env = os.environ.copy()
         | 
| 103 | 
            +
                  subprocess.run(cmd, shell=True, env=env)
         | 
| 104 | 
            +
                  signal.pause()
         | 
| 105 | 
            +
                else:
         | 
| 106 | 
            +
                  print('An ngrok token is required. You can get one on https://ngrok.com')
         | 
| 107 | 
            +
                
         | 
| 108 | 
            +
            if __name__ == '__main__':
         | 
| 109 | 
            +
                main()
         | 
    	
        start.sh
    ADDED
    
    | @@ -0,0 +1,47 @@ | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | 
|  | |
| 1 | 
            +
            #!/bin/bash
         | 
| 2 | 
            +
            eval "$(conda shell.bash hook)"
         | 
| 3 | 
            +
             | 
| 4 | 
            +
            # Create the Conda environment
         | 
| 5 | 
            +
            env_exists=1
         | 
| 6 | 
            +
            if [ ! -d ~/.conda/envs/SwapFace2Pon ]
         | 
| 7 | 
            +
            then
         | 
| 8 | 
            +
              env_exists=0
         | 
| 9 | 
            +
              conda create -y -n SwapFace2Pon python=3.10
         | 
| 10 | 
            +
            fi 
         | 
| 11 | 
            +
             | 
| 12 | 
            +
            conda activate facefusion
         | 
| 13 | 
            +
             | 
| 14 | 
            +
            # Get SwapFace2Pon from GitHub
         | 
| 15 | 
            +
            if [ ! -d "SwapFace2Pon" ]
         | 
| 16 | 
            +
            then
         | 
| 17 | 
            +
              git clone https://huggingface.co/spaces/victorisgeek/SwapFace2Pon
         | 
| 18 | 
            +
            fi
         | 
| 19 | 
            +
             | 
| 20 | 
            +
            # Update the installation if the parameter "update" was passed by running
         | 
| 21 | 
            +
            # start.sh update
         | 
| 22 | 
            +
            if [ $# -eq 1 ] && [ $1 = "update" ]
         | 
| 23 | 
            +
            then
         | 
| 24 | 
            +
              cd SwapFace2Pon
         | 
| 25 | 
            +
              git pull
         | 
| 26 | 
            +
              cd ..
         | 
| 27 | 
            +
            fi
         | 
| 28 | 
            +
             | 
| 29 | 
            +
            # Install the required packages if the environment needs to be freshly installed or updated 
         | 
| 30 | 
            +
            if [ $# -eq 1 ] && [ $1 = "update" ] || [ $env_exists = 0 ]
         | 
| 31 | 
            +
            then
         | 
| 32 | 
            +
              cd SwapFace2Pon
         | 
| 33 | 
            +
              python install.py --torch cuda --onnxruntime cuda
         | 
| 34 | 
            +
              cd ..
         | 
| 35 | 
            +
              pip install pyngrok
         | 
| 36 | 
            +
              conda install opencv -y
         | 
| 37 | 
            +
              conda install ffmpeg
         | 
| 38 | 
            +
            fi
         | 
| 39 | 
            +
             | 
| 40 | 
            +
            # Start SwapFace2Pon with ngrok
         | 
| 41 | 
            +
            if [ $# -eq 0 ]
         | 
| 42 | 
            +
            then
         | 
| 43 | 
            +
              python start-ngrok.py 
         | 
| 44 | 
            +
            elif [ $1 = "reset" ]
         | 
| 45 | 
            +
            then
         | 
| 46 | 
            +
              python start-ngrok.py --reset 
         | 
| 47 | 
            +
            fi
         | 
 
			
