該項目提供了一個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上訪問您的應用程序。