该项目提供了一个Web应用程序,该应用程序从通过网络摄像头或上传文件捕获的头像生成头像。它利用稳定扩散REST API的img2img
功能,该功能通过稳定的扩散WebUI集成。
该项目以python应用程序( app.py
)为中心,该应用程序暴露了REST API。此API将处理原始图像以及可选参数(幻想级别和性别偏好),并返回生成的头像。包括以下目录和文件:
.
├── app.py # Main application file exposing REST API
├── static # Static resources for the web application
│ ├── Hyland_logo.png
│ └── style.css
└── templates # HTML templates
└── index.html
Python应用程序配置为在http://localhost:7861/sdapi/v1/img2img
上连接到稳定的扩散REST API。请按照以下步骤安装并在本地设置REST API:
克隆稳定的扩散WebUI存储库
git clone https://github.com/AUTOMATIC1111/stable-diffusion-webui
cd stable-diffusion-webui
从拥抱脸下载稳定的扩散模型
cd models/Stable-diffusion
wget https://huggingface.co/lllyasviel/fav_models/resolve/main/fav/realisticVisionV51_v51VAE.safetensors
cd ../..
创建虚拟环境
python3 -m venv venv
source venv/bin/activate
安装依赖项并启动webui
运行脚本以安装要求并启动应用程序:
./webui.sh
应用程序在http://127.0.0.1:7860
上运行后,您可以使用Ctrl+C
停止它。
注意:仅在初始设置期间才需要此步骤。
启动剩下的API
如果您没有GPU,则可以使用CPU启动API:
python3 launch.py --nowebui --skip-python-version-check --always-cpu --skip-torch-cuda-test
API将通过您的浏览器访问http://localhost:7861/docs#/default/img2imgapi_sdapi_v1_img2img_post
。
在本地测试应用程序:
启动稳定的扩散REST API ,并确保http://localhost:7861/sdapi/v1/img2img
可以接触。
启动Web应用程序
设置Python环境并运行应用程序:
python3 -m venv venv
source venv/bin/activate
pip install flask requests Pillow boto3
python3 app.py
访问Web应用程序
打开Web浏览器并导航到http://localhost:5000
用于使用Avatar Generator。
要在AWS EC2实例上部署该应用程序,请遵循设置服务器的通常过程,以确保正确安装Python 3.10,稳定的扩散WebUI和其他依赖关系。根据是否使用GPU调整安全组和实例类型。有关详细说明,请参阅AWS文档。
本指南概述了在配备GPU的AWS EC2实例上部署Avatar Generator的步骤。
g4dn.xlarge
更新系统软件包
sudo apt-get update
sudo apt-get upgrade -y
验证NVIDIA设备的存在
sudo lshw -c video
您应该看到表明存在NVIDIA GPU的输出,例如:
*-display:1
description: 3D controller
product: TU104GL [Tesla T4]
vendor: NVIDIA Corporation
安装NVIDIA驱动程序
sudo apt install -y ubuntu-drivers-common
sudo ubuntu-drivers autoinstall
重新启动实例
sudo reboot
重新启动后,正确安装了NVIDIA驱动程序:
nvidia-smi
预期输出应显示NVIDIA驱动程序版本和CUDA版本。
有关安装NVIDIA驱动程序的更多详细信息,请参阅Ubuntu的文档。
克隆稳定的扩散WebUI存储库
git clone https://github.com/AUTOMATIC1111/stable-diffusion-webui
cd stable-diffusion-webui
下载稳定扩散模型
cd models/Stable-diffusion
wget https://huggingface.co/lllyasviel/fav_models/resolve/main/fav/realisticVisionV51_v51VAE.safetensors
cd ../..
安装Python软件包安装程序
sudo apt install python3-pip
创建和激活虚拟环境
python3 -m venv venv
source venv/bin/activate
安装依赖项并启动webui
./webui.sh
您可以在http://127.0.0.1:7860
开始运行后使用Ctrl+C
停止应用程序。
启动剩下的API
python3 launch.py --nowebui --skip-python-version-check --skip-torch-cuda-test
为REST API创建启动脚本
创建一个脚本以在将来轻松启动API:
$ cat start_api.sh
#! /bin/bash
# Navigate to the stable-diffusion-webui directory
cd stable-diffusion-webui
# Create a virtual environment if it doesn't exist
if [ ! -d " venv " ] ; then
python3 -m venv venv
fi
# Activate the virtual environment
source venv/bin/activate
# Launch the application and write output to api.log
python3 launch.py --nowebui --skip-python-version-check --skip-torch-cuda-test > ../api.log 2>&1 &
# Print a message indicating the application has started
echo " API has started. Output is being written to api.log "
克隆Web应用程序存储库
git clone https://github.com/aborroy/communitylive-avatar.git
cd communitylive-avatar
创建虚拟环境并启动应用程序
python3 -m venv venv
source venv/bin/activate
pip install flask requests Pillow boto3
python3 app.py
为Web应用程序创建启动脚本
创建一个脚本以轻松启动Web应用程序:
$ cat start_app.sh
#! /bin/bash
# Navigate to the app directory
cd communitylive-avatar
# Create a virtual environment if it doesn't exist
if [ ! -d " venv " ] ; then
python3 -m venv venv
fi
# Activate the virtual environment
source venv/bin/activate
# Launch the application and write output to app.log
python3 app.py > ../app.log 2>&1 &
# Print a message indicating the application has started
echo " Application has started. Output is being written to app.log "
由于应用程序使用网络摄像头,因此需要安全的HTTPS连接。使用AWS路线53将流量路由到EC2实例。示例DNS: communitylive-avatar.alfdemo.com
。
安装nginx
sudo apt-get install nginx
安装certbot和nginx插件
sudo apt-get install certbot python3-certbot-nginx
配置nginx
创建一个nginx配置文件:
sudo vi /etc/nginx/sites-available/app
添加以下配置:
server {
listen 80 ;
server_name communitylive-avatar.alfdemo.com;
client_max_body_size 64M ;
location / {
proxy_pass http://127.0.0.1:5000;
proxy_set_header Host $host ;
proxy_set_header X-Real-IP $remote_addr ;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for ;
proxy_set_header X-Forwarded-Proto $scheme ;
}
}
启用nginx配置
sudo ln -s /etc/nginx/sites-available/app /etc/nginx/sites-enabled
测试NGINX配置和重新加载
sudo nginx -t
sudo systemctl reload nginx
获得SSL证书
sudo certbot --nginx -d communitylive-avatar.alfdemo.com
Certbot将自动配置NGINX在端口443上使用TLS。
要使用AWS SES(简单的电子邮件服务)启用电子邮件交付,请按照以下步骤:
安装AWS CLI :
配置AWS CLI :
aws configure
设置AWS SES :
aws ses verify-email-identity --email-address [email protected]
测试SES电子邮件发送:
aws ses send-email --from [email protected] --destination [email protected] --message " Subject={Data=Test Email},Body={Text={Data=Hello!}} "
使用AWS SES配置,您的应用程序将能够通过电子邮件将生成的化身发送给指定的收件人。
如果所有步骤都成功完成,则可以在https://communitylive-avatar.alfdemo.com上访问您的应用程序。