CodeFormer icon indicating copy to clipboard operation
CodeFormer copied to clipboard

Issues when building docker image with Web-UI.

Open hero-intelligent opened this issue 2 years ago • 5 comments
trafficstars

I've noticed that you have a web-ui demo on Huggingface at https://huggingface.co/spaces/sczhou/CodeFormer. When I tried to build a docker image local hosted, I've experienced some inconvenience. There is the same program in this repository. Hope you modify the program at the same time.

First and the most importantly, the program can't be reached using web browser without modification. If I run the app.py directly after installing dependencies, the program will finally be hosted at 127.0.0.1:7860. But the problem is, it can't be reached at anywhere else exept inside the container. It needs some modification so that the program can be reached outside the container. Secondly, the example images needs to be downloaded every single time I run the container. If I can't have access to network, the program will refuse to work. Now that the existence of models are checked, why not check the existence of images if it is essential to the program? Thirdly, the models needs to be mounted inside the container or predownloaded manually using wget. I tried to run app.py in building stage directly, but the building process refused to quit. I know the reason is on demo.launch().

hero-intelligent avatar Feb 15 '23 02:02 hero-intelligent

To solve these issues, would you like to have an eye on my version of solution? @sczhou

Solution:

Change the last line in app.py, which is demo.launch() into demo.launch(server_name="0.0.0.0")

change the line 44 - line 59 in app.py, which is:

# download images
torch.hub.download_url_to_file(
    'https://replicate.com/api/models/sczhou/codeformer/files/fa3fe3d1-76b0-4ca8-ac0d-0a925cb0ff54/06.png',
    '01.png')
torch.hub.download_url_to_file(
    'https://replicate.com/api/models/sczhou/codeformer/files/a1daba8e-af14-4b00-86a4-69cec9619b53/04.jpg',
    '02.jpg')
torch.hub.download_url_to_file(
    'https://replicate.com/api/models/sczhou/codeformer/files/542d64f9-1712-4de7-85f7-3863009a7c3d/03.jpg',
    '03.jpg')
torch.hub.download_url_to_file(
    'https://replicate.com/api/models/sczhou/codeformer/files/a11098b0-a18a-4c02-a19a-9a7045d68426/010.jpg',
    '04.jpg')
torch.hub.download_url_to_file(
    'https://replicate.com/api/models/sczhou/codeformer/files/7cf19c2c-e0cf-4712-9af8-cf5bdbb8d0ee/012.jpg',
    '05.jpg')

INTO:

# download images
images_url = {
    '01.png': 'https://replicate.com/api/models/sczhou/codeformer/files/fa3fe3d1-76b0-4ca8-ac0d-0a925cb0ff54/06.png',
    '02.jpg': 'https://replicate.com/api/models/sczhou/codeformer/files/a1daba8e-af14-4b00-86a4-69cec9619b53/04.jpg',
    '03.jpg': 'https://replicate.com/api/models/sczhou/codeformer/files/542d64f9-1712-4de7-85f7-3863009a7c3d/03.jpg',
    '04.jpg': 'https://replicate.com/api/models/sczhou/codeformer/files/a11098b0-a18a-4c02-a19a-9a7045d68426/010.jpg',
    '05.jpg': 'https://replicate.com/api/models/sczhou/codeformer/files/7cf19c2c-e0cf-4712-9af8-cf5bdbb8d0ee/012.jpg',
}
if not os.path.exists('01.png'):
    torch.hub.download_url_to_file(images_url['01.png'], '01.png')
if not os.path.exists('02.jpg'):
    torch.hub.download_url_to_file(images_url['02.jpg'], '02.jpg')
if not os.path.exists('03.jpg'):
    torch.hub.download_url_to_file(images_url['03.jpg'], '03.jpg')
if not os.path.exists('04.jpg'):
    torch.hub.download_url_to_file(images_url['04.jpg'], '04.jpg')
if not os.path.exists('05.jpg'):
    torch.hub.download_url_to_file(images_url['05.jpg'], '05.jpg')

and make a new file named downloader.py, the content of which is about the first 60 lines of app.py:


"""
This file is used for downloading models and images used in deploying hugging face demo:
https://huggingface.co/spaces/sczhou/CodeFormer
"""

import sys
sys.path.append('CodeFormer')
import os
import cv2
import torch
import torch.nn.functional as F
import gradio as gr

from torchvision.transforms.functional import normalize

from basicsr.utils import imwrite, img2tensor, tensor2img
from basicsr.utils.download_util import load_file_from_url
from facelib.utils.face_restoration_helper import FaceRestoreHelper
from facelib.utils.misc import is_gray
from basicsr.archs.rrdbnet_arch import RRDBNet
from basicsr.utils.realesrgan_utils import RealESRGANer

from basicsr.utils.registry import ARCH_REGISTRY


os.system("pip freeze")

pretrain_model_url = {
    'codeformer': 'https://github.com/sczhou/CodeFormer/releases/download/v0.1.0/codeformer.pth',
    'detection': 'https://github.com/sczhou/CodeFormer/releases/download/v0.1.0/detection_Resnet50_Final.pth',
    'parsing': 'https://github.com/sczhou/CodeFormer/releases/download/v0.1.0/parsing_parsenet.pth',
    'realesrgan': 'https://github.com/sczhou/CodeFormer/releases/download/v0.1.0/RealESRGAN_x2plus.pth'
}
# download weights
if not os.path.exists('CodeFormer/weights/CodeFormer/codeformer.pth'):
    load_file_from_url(url=pretrain_model_url['codeformer'], model_dir='CodeFormer/weights/CodeFormer', progress=True, file_name=None)
if not os.path.exists('CodeFormer/weights/facelib/detection_Resnet50_Final.pth'):
    load_file_from_url(url=pretrain_model_url['detection'], model_dir='CodeFormer/weights/facelib', progress=True, file_name=None)
if not os.path.exists('CodeFormer/weights/facelib/parsing_parsenet.pth'):
    load_file_from_url(url=pretrain_model_url['parsing'], model_dir='CodeFormer/weights/facelib', progress=True, file_name=None)
if not os.path.exists('CodeFormer/weights/realesrgan/RealESRGAN_x2plus.pth'):
    load_file_from_url(url=pretrain_model_url['realesrgan'], model_dir='CodeFormer/weights/realesrgan', progress=True, file_name=None)

 # download images
images_url = {
    '01.png': 'https://replicate.com/api/models/sczhou/codeformer/files/fa3fe3d1-76b0-4ca8-ac0d-0a925cb0ff54/06.png',
    '02.jpg': 'https://replicate.com/api/models/sczhou/codeformer/files/a1daba8e-af14-4b00-86a4-69cec9619b53/04.jpg',
    '03.jpg': 'https://replicate.com/api/models/sczhou/codeformer/files/542d64f9-1712-4de7-85f7-3863009a7c3d/03.jpg',
    '04.jpg': 'https://replicate.com/api/models/sczhou/codeformer/files/a11098b0-a18a-4c02-a19a-9a7045d68426/010.jpg',
    '05.jpg': 'https://replicate.com/api/models/sczhou/codeformer/files/7cf19c2c-e0cf-4712-9af8-cf5bdbb8d0ee/012.jpg',
}
if not os.path.exists('01.png'):
    torch.hub.download_url_to_file(images_url['01.png'], '01.png')
if not os.path.exists('02.jpg'):
    torch.hub.download_url_to_file(images_url['02.jpg'], '02.jpg')
if not os.path.exists('03.jpg'):
    torch.hub.download_url_to_file(images_url['03.jpg'], '03.jpg')
if not os.path.exists('04.jpg'):
    torch.hub.download_url_to_file(images_url['04.jpg'], '04.jpg')
if not os.path.exists('05.jpg'):
    torch.hub.download_url_to_file(images_url['05.jpg'], '05.jpg')

hero-intelligent avatar Feb 15 '23 02:02 hero-intelligent

This is my new version of Dockerfile:

FROM pytorch/pytorch:latest

RUN apt update && apt install -y git ffmpeg libgl1 libglib2.0-dev libsm6 libxext6 && \
    git clone https://huggingface.co/spaces/sczhou/CodeFormer /cf

WORKDIR /cf

RUN pip3 install -r requirements.txt -i https://pypi.mirrors.ustc.edu.cn/simple

RUN pip install gradio && python -m pip install markupsafe==2.0.1

RUN apt install -y wget

WORKDIR /cf/CodeFormer/weights/CodeFormer
RUN wget https://github.com/sczhou/CodeFormer/releases/download/v0.1.0/codeformer.pth 
WORKDIR /cf/CodeFormer/weights/facelib
RUN wget https://github.com/sczhou/CodeFormer/releases/download/v0.1.0/detection_Resnet50_Final.pth
RUN wget https://github.com/sczhou/CodeFormer/releases/download/v0.1.0/parsing_parsenet.pth
WORKDIR /cf/CodeFormer/weights/realesrgan
RUN wget https://github.com/sczhou/CodeFormer/releases/download/v0.1.0/RealESRGAN_x2plus.pth

WORKDIR /cf

RUN sed -i '$c demo.launch(server_name="0.0.0.0")' app.py

EXPOSE 7860

CMD [ "python", "app.py" ]

hero-intelligent avatar Feb 15 '23 02:02 hero-intelligent

Hello. Please can you tell me how to use your dockerfile? i have Codeformer installed locally so can this run it as well? If not, i'll clean install. i'm absolutely a noob. i don't know what a dockerfile is. Please can you help? Thanks in advance.

sao-g avatar Jan 06 '24 21:01 sao-g

Ah, this issue acts more like a note for me. Since you have already installed locally, you needn't run this dockerfile anymore. Still, this Dockerfile hasn't been updated for a while, so it may come with some issues. Let me know whether it can run successfully, if you would like to test it anyway.

Dockerfile is like a script to install a software in one line, but the software will be wrapped inside a container, which is a clean environment seperate from your system, instead of running directly on your system.

But i'm lying a little bit. Docker is a kind of virtualization. Instead of virtualize hardware like Supervisor for Virtual Machines, Docker virtualize system. The container acts like a clean system, for softwares to be installed onto. That's why I said Dockerfile is like a script to install a software in one line. Actually it is a script for Docker Engine to generate an image, which then be used to create container.

To try out this Dockerfile, install Docker following the official documentation, and then just copy and paste it into an empty folder, name it as Dockerfile, then run sudo docker build -t codeformer ., wait it to finish, then run sudo docker run -it --rm -p 7860:7860 --name=codeformer codeformer.

The terminal should output logs. minimize your terminal, and visit localhost:7860 in your web browser. This command creates a one time container. if you want to terminate the program, press Ctrl + C inside the terminal, or sudo docker stop codeformer inside a new terminal. Note: this will automatically delete all your working progress as well as outputs inside of the container, and the container itself. if you don't want this, omit --rm in the command line.

If you want this container run in the background as a service for a long time, change the command line into sudo docker run -d --restart=always -p 7860:7860 --name=codeformer codeformer

If you would like to delete the image, run sudo docker rmi codeformer.

To uninstall docker, please refer to official documentation.

sao-g @.***> 于2024年1月7日周日 05:57写道:

Hello. Please can you tell me how to use your dockerfile? i have Codeformer installed locally so can this run it as well? If not, i'll clean install. i'm absolutely a noob. i don't know what a dockerfile is. Please can you help? Thanks in advance.

— Reply to this email directly, view it on GitHub https://github.com/sczhou/CodeFormer/issues/143#issuecomment-1879840827, or unsubscribe https://github.com/notifications/unsubscribe-auth/A2XUP23RY7GJLZLZBJB4MELYNHCGPAVCNFSM6AAAAAAU4IUEN2VHI2DSMVQWIX3LMV43OSLTON2WKQ3PNVWWK3TUHMYTQNZZHA2DAOBSG4 . You are receiving this because you authored the thread.Message ID: @.***>

hero-intelligent avatar Jan 13 '24 17:01 hero-intelligent

Thank you very much for such a detailed and helpful reply! i had previously already installed Docker software. But i found out that codeformer can do batch upscale from within the input folder! Previously i had to manually enter my file directory and file names which made it so tedious. But someone reddit told me batch is possible if i just write the folder path.

i must apologise, it seems, i may not need a docker now. But in future if i do try it out, i will definitely reach out to you for feedback. Thank you once again, and best regards!

sao-g avatar Jan 13 '24 20:01 sao-g