avatarify.ipynb
<a href="https://colab.research.google.com/github/alievk/avatarify/blob/master/avatarify.ipynb" target="_parent"></a>
This Colab notebook is for running Avatarify rendering server. It allows you to run Avatarify on your computer without GPU in this way:
To this end, all the heavy work is offloaded from your computer to this notebook so you don't need to have a beafy hardware on your PC anymore.
Run the cells below (Shift+Enter) sequentially and pay attention to the hints and instructions included in this notebook.
At the end you will get a command for running the client on your computer.
Make sure you have installed the latest version of Avatarify on your computer. Refer to the README for the instructions.
When it's ready execute this notebook and get the command for running the client on your computer.
The client on your computer connects to the server via ngrok TCP tunnel or a reverse ssh tunnel.
ngrok, while easy to use, can induce a considerable network lag ranging from dozens of milliseconds to a second. This can lead to a poor experience.
A more stable connection could be established using a reverse ssh tunnel to a host with a public IP, like an AWS t3.micro (free) instance. This notebook provides a script for creating a tunnel, but launching an instance in a cloud is on your own (find the manual below).
Follow the steps below to clone Avatarify and install the dependencies.
!cd /content
!rm -rf *
!git clone https://github.com/alievk/avatarify.git
cd avatarify
!git clone https://github.com/alievk/first-order-model.git fomm
!pip install face-alignment==1.0.0 msgpack_numpy pyyaml==5.1
!scripts/download_data.sh
Follow the steps below to setup ngrok. You will also need to sign up on the ngrok site and get your authtoken (free).
# Download ngrok
!scripts/get_ngrok.sh
Start here if the runtime was restarted after installation.
cd /content/avatarify
#!git pull origin
from subprocess import Popen, PIPE
import shlex
import json
import time
def run_with_pipe(command):
commands = list(map(shlex.split,command.split("|")))
ps = Popen(commands[0], stdout=PIPE, stderr=PIPE)
for command in commands[1:]:
ps = Popen(command, stdin=ps.stdout, stdout=PIPE, stderr=PIPE)
return ps.stdout.readlines()
def get_tunnel_adresses():
info = run_with_pipe("curl http://localhost:4040/api/tunnels")
assert info
info = json.loads(info[0])
for tunnel in info['tunnels']:
url = tunnel['public_url']
port = url.split(':')[-1]
local_port = tunnel['config']['addr'].split(':')[-1]
print(f'{url} -> {local_port} [{tunnel["name"]}]')
if tunnel['name'] == 'input':
in_addr = url
elif tunnel['name'] == 'output':
out_addr = url
else:
print(f'unknown tunnel: {tunnel["name"]}')
return in_addr, out_addr
# Input and output ports for communication
local_in_port = 5557
local_out_port = 5558
# (Re)Start the worker
with open('/tmp/run.txt', 'w') as f:
ps = Popen(
shlex.split(f'./run.sh --is-worker --in-port {local_in_port} --out-port {local_out_port} --no-vcam --no-conda'),
stdout=f, stderr=f)
time.sleep(3)
This command should print lines if the worker is successfully started
!ps aux | grep 'python3 afy/cam_fomm.py' | grep -v grep | tee /tmp/ps_run
!if [[ $(cat /tmp/ps_run | wc -l) == "0" ]]; then echo "Worker failed to start"; cat /tmp/run.txt; else echo "Worker started"; fi
Go to https://dashboard.ngrok.com/auth/your-authtoken (sign up if required), copy your authtoken and put it below.
# Paste your authtoken here in quotes
authtoken = "1cBzFFwzSlaLhlRPXIHJiVLqtiQ_2cVsonJXe52B6DDyp8su7"
Set your region
| Code | Region |
|---|---|
| us | United States |
| eu | Europe |
| ap | Asia/Pacific |
| au | Australia |
| sa | South America |
| jp | Japan |
| in | India |
# Set your region here in quotes
region = "eu"
config =\
f"""
version: 2
authtoken: {authtoken}
region: {region}
console_ui: False
tunnels:
input:
addr: {local_in_port}
proto: tcp
output:
addr: {local_out_port}
proto: tcp
"""
with open('ngrok.conf', 'w') as f:
f.write(config)
# (Re)Open tunnel
ps = Popen('./scripts/open_tunnel_ngrok.sh', stdout=PIPE, stderr=PIPE)
time.sleep(3)
# Get tunnel addresses
try:
in_addr, out_addr = get_tunnel_adresses()
print("Tunnel opened")
except Exception as e:
[print(l.decode(), end='') for l in ps.stdout.readlines()]
print("Something went wrong, reopen the tunnel")
Alternatively you can create a ssh reverse tunnel to an AWS t3.micro instance (it's free). It has lower latency than ngrok.
Ubuntu Server 18.04 LTS AMI;t3.micro instance type and press Review and launch;/etc/ssh/sshd_config:GatewayPorts yes
then restart sshd
sudo service sshd restart
key_pair.pem by dragging and dropping it into avatarify folder in this notebook;run_mac.sh with run_windows.bat or run.sh)./run_mac.sh --is-client --in-addr tcp://instace.compute.amazonaws.com:5557 --out-addr tcp://instance.compute.amazonaws.com:5558
# Open reverse ssh tunnel (uncomment line below)
# !./scripts/open_tunnel_ssh.sh key_pair.pem [email protected]
When you run the cell below it will print a command. Run this command on your computer:
Anaconda Prompt);avatarify directory:</br>C:\path\to\avatarify to your path)</br>
cd C:\path\to\avatarify</br></br>/path/to/avatarify to your path)</br>
cd /path/to/avatarifyprint('Copy-paste to the terminal the command below and run (press Enter)\n')
print('Mac:')
print(f'./run_mac.sh --is-client --in-addr {in_addr} --out-addr {out_addr}')
print('\nWindows:')
print(f'run_windows.bat --is-client --in-addr {in_addr} --out-addr {out_addr}')
print('\nLinux:')
print(f'./run.sh --is-client --in-addr {in_addr} --out-addr {out_addr}')
If something doesn't work as expected, please run the cells below and include the logs in your report.
#@title
!cat ./var/log/cam_fomm.log | head -100
#@title
!cat ./var/log/recv_worker.log | tail -100
#@title
!cat ./var/log/predictor_worker.log | tail -100
#@title
!cat ./var/log/send_worker.log | tail -100