Hello everyone, I introduced and ran Tencent’s open source old photo repair algorithm (AI hacking technology, old photo repair, blur to HD) before, and I also mentioned that the experience of the three online trial versions provided by the official is not good. So I tweaked the official Demo to use the latest V1.3 pre-training model and output whole-image, so you can get a feel for it

GFPGAN + Gradio + Huggingface are all new to me, and it’s fun to mix them together.

Now I will introduce the whole implementation process in detail

Clone official Demo

GFPGAN’s official Demo was a good Huggingface experience, with the disadvantage of only exporting faces and using an old model.

Install Git LFS before clone

LFS is short for Large File Storage and uses git to help manage Large files

GFPGAN git clone sudo apt-get install git- LFS git init https://huggingface.co/spaces/akhaliq/GFPGANCopy the code

After cloning, I directly run python app.py, and there are two errors. If you have encountered them, please refer to them. PIP install aiofiles PIP install aiofiles

Error 2: There is no current event loop in thread ‘thread-2’

To solve: Refer to this article

www.cnblogs.com/SunshineKim…

Add a sentence to uvicorn’s server.pynew_loop = asyncio.new_event_loop()

Gradio

GFPGAN Demo on Huggingface is implemented with Gradio.

Gradio, an open source project from MIT, automatically generates interactive Web pages by adding a few lines to the original code and supports a variety of input and output formats, such as graphs >> labels in image categories, graphs >> graphs in super-resolution, and more. It also supports generating links that can be accessed by external networks, allowing others to quickly experience your algorithm.

Gradio is positioned similarly to Streamlit, but more lightweight (one line of code) because it recommends applications that call “single functions” and do not require callbacks to components.

By the way, I’ve covered Streamlit in detail and developed a few little things:

  • Building machine learning apps, it’s so easy
  • I created a website for this GIF
  • Stop it, Huaqiang! I’m using machine learning to help you pick out watermelons
  • It took a month to make a pure machine learning website

I’m also new to Gradio, and its installation is simple: PIP install Gradio

I only read the official documents and the usage of Interface from zero, which took half an hour.

# getting_started
https://www.gradio.app/getting_started/
# docs
https://www.gradio.app/docs/
# github
https://github.com/gradio-app/gradio
Copy the code

Code changes

The official Demo code I have only modified a few:

  • Modify model_path to add V1.3 pretraining models directly to Experiments/Pretrained_models.
  • Inference (IMG) was modified to present restored_img
Restorer = GFPGANer(model_path='experiments/pretrained_models/ gfpganv1.3. PTH ', Upscale =2, arch='clean', channel_multiplier=2, bg_upsampler=bg_upsampler) def inference(img): input_img = cv2.imread(img, cv2.IMREAD_COLOR) cropped_faces, restored_faces, restored_img = restorer.enhance( input_img, has_aligned=False, only_center_face=False, paste_back=True) return Image.fromarray(restored_img[:, :, ::-1])Copy the code

Python app.py can be changed to python app.py.

It automatically generates a local static interaction page, and the browser automatically redirects to http://127.0.0.1:7860

That Gradio.app link is shareable for 72 hours.

Upload Huggingface

Step1: sign up for Huggingface

Step2: create Space, and remember to select Gradio for the SDK

Step3: Clone the newly created space code, and then push the modified code

git lfs install 
git add .
git commit -m "commit from $beihai"
git push
Copy the code

Git config –global credential. Helper Store git config –global credential. Helper store git config –global credential

Push is done, go back to your space page and see the effect.

But Huggingface often 500, everyone try, after all, free.