Back to Avatarify Python

Avatarify Colab Server

avatarify.ipynb

0.2.07.8 KB
Original Source

<a href="https://colab.research.google.com/github/alievk/avatarify/blob/master/avatarify.ipynb" target="_parent"></a>

Avatarify Colab Server

This Colab notebook is for running Avatarify rendering server. It allows you to run Avatarify on your computer without GPU in this way:

  1. When this notebook is executed, it starts listening for incoming requests from your computer;
  2. You start the client on your computer and it connects to the notebook and starts sending requests;
  3. This notebooks receives the requests from your computer, renders avatar images and sends them back;

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.

Start the server

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.

Start the client

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.

Technical details

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).

Install

Avatarify

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

ngrok

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

Run

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

Start the worker

# (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

Open ngrok tunnel

Get ngrok token

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

CodeRegion
usUnited States
euEurope
apAsia/Pacific
auAustralia
saSouth America
jpJapan
inIndia
# 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")

[Optional] AWS proxy

Alternatively you can create a ssh reverse tunnel to an AWS t3.micro instance (it's free). It has lower latency than ngrok.

  1. In your AWS console go to Services -> EC2 -> Instances -> Launch Instance;
  2. Choose Ubuntu Server 18.04 LTS AMI;
  3. Choose t3.micro instance type and press Review and launch;
  4. Confirm your key pair and press Launch instances;
  5. Go to the security group of this instance and edit inbound rules. Add TCP ports 5557 and 5558 and set Source to Anywhere. Press Save rules;
  6. ssh into the instance (you can find the command in the Instances if you click on the Connect button) and add this line in the end of /etc/ssh/sshd_config:
GatewayPorts yes

then restart sshd

sudo service sshd restart
  1. Copy your key_pair.pem by dragging and dropping it into avatarify folder in this notebook;
  2. Use the command below to open the tunnel;
  3. Start client with a command (substitute 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]

Start the client

When you run the cell below it will print a command. Run this command on your computer:

  1. Open a terminal (in Windows open Anaconda Prompt);
  2. Change working directory to the avatarify directory:</br>
  • Windows (change C:\path\to\avatarify to your path)</br> cd C:\path\to\avatarify</br></br>
  • Mac/Linux (change /path/to/avatarify to your path)</br> cd /path/to/avatarify
  1. Copy-paste to the terminal the command below and run;
  2. It can take some time to connect (usually up to 10 seconds). If the preview window doesn't appear in a minute or two, look for the errors above in this notebook and report in the issues or Slack.
print('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}')

Logs

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