Subject: [bug#59607] [PATCH 8/8] gnu: Add python-real-esrgan.

From: Liliana Marie Prikler <[email-masked]>
Date: Tue, 22 Nov 2022 21:55:02 +0100

* gnu/packages/machine-learning.scm (python-real-esrgan): New variable.
---
 gnu/packages/machine-learning.scm | 62 +++++++++++++++++++++++++++++++
 1 file changed, 62 insertions(+)

diff --git a/gnu/packages/machine-learning.scm b/gnu/packages/machine-learning.scm
index 73263962f5..c49886f50d 100644
--- a/gnu/packages/machine-learning.scm
+++ b/gnu/packages/machine-learning.scm
@@ -930,6 +930,68 @@ (define-public python-gfpgan
 images.")
     (license license:asl2.0)))
 
+(define-public python-real-esrgan
+  (package
+    (name "python-real-esrgan")
+    (version "0.3.0")
+    (source (origin
+              (method git-fetch)
+              (uri (git-reference
+                    (url "https://github.com/xinntao/Real-ESRGAN")
+                    (commit (string-append "v" version))))
+              (file-name (git-file-name name version))
+              (modules '((guix build utils)))
+              (snippet
+               #~(begin (substitute* (find-files "." "\\.py")
+                          (("\\.cuda\\(\\)") ""))))
+              (sha256
+               (base32
+                "19qp8af1m50zawcnb31hq41nicad5k4hz7ffwizana17w069ilx5"))))
+    (build-system python-build-system)
+    (arguments
+     (list
+      #:phases
+      #~(modify-phases %standard-phases
+          (add-after 'unpack 'fix-api
+            (lambda _
+              (substitute* (find-files "." "\\.py$")
+                (("from basicsr\\.losses\\.losses import .*")
+                 "import basicsr.losses\n")
+                (("GANLoss")
+                 "basicsr.losses.gan_loss.GANLoss")
+                (("(L1|Perceptual)Loss" loss)
+                 (string-append "basicsr.losses.basic_loss." loss)))))
+          (add-after 'unpack 'fix-requirements
+            (lambda _
+              (substitute* "requirements.txt"
+                (("opencv-python") "")))) ; installed without egg-info
+          (add-before 'check 'pre-check
+            (lambda _
+              (setenv "HOME" (getcwd))
+              ;; Test requires pretrained model.
+              (delete-file "tests/test_utils.py")))
+          (replace 'check
+            (lambda* (#:key tests? #:allow-other-keys)
+              (when tests?
+                (invoke "pytest" "-vv")))))))
+    (propagated-inputs (list opencv
+                             python-basicsr
+                             python-facexlib
+                             python-gfpgan
+                             python-numpy
+                             python-pillow
+                             python-pytorch
+                             python-torchvision
+                             python-tqdm))
+    (native-inputs (list python-cython python-pytest))
+    (home-page "https://github.com/xinntao/Real-ESRGAN")
+    (synopsis "Restore low-resolution images")
+    (description "Real-ESRGAN is a @acronym{GAN, Generative Adversarial Network}
+aiming to restore low-resolution images.  The techniques used are described in
+the paper 'Real-ESRGAN: Training Real-World Blind Super-Resolution with Pure
+Synthetic Data' by Xintao Wang, Liangbin Xie, Chao Dong, and Ying Shan.")
+    (license license:bsd-3)))
+
 (define-public ncnn
   (package
     (name "ncnn")
-- 
2.38.1