switch to api config
Browse files
app.py
CHANGED
@@ -11,49 +11,48 @@ import fastai
|
|
11 |
from deoldify import device
|
12 |
from deoldify.device_id import DeviceId
|
13 |
from deoldify.visualize import *
|
14 |
-
from huggingface_hub import snapshot_download
|
|
|
15 |
|
16 |
os.system("pip freeze")
|
17 |
from collections.abc import Sized # Import Sized from collections.abc
|
|
|
18 |
warnings.filterwarnings("ignore", category=UserWarning, message=".*?Your .*? set is empty.*?")
|
19 |
|
|
|
|
|
20 |
|
21 |
-
|
|
|
|
|
22 |
token = os.getenv("HF_TOKEN")
|
23 |
repo_id = "afondiel/image-colorizer-deoldify"
|
24 |
repo_type = "space"
|
25 |
|
26 |
# Download the snapshot from the space repository
|
27 |
-
snapshot_folder = snapshot_download(repo_id=repo_id, repo_type=repo_type, token=token)
|
28 |
-
|
29 |
-
# Set the device to use for computation
|
30 |
-
# choices: CPU, GPU0...GPU7
|
31 |
-
if torch.cuda.is_available():
|
32 |
-
device.set(device=DeviceId.GPU0)
|
33 |
-
else:
|
34 |
-
device.set(device=DeviceId.CPU)
|
35 |
|
|
|
36 |
# Load the pre-trained model
|
37 |
_colorizer = get_image_colorizer(root_folder=Path(snapshot_folder), artistic=True)
|
38 |
|
39 |
-
|
40 |
def colorizer_fn(input_img, render_factor):
|
41 |
-
|
42 |
-
|
43 |
-
|
44 |
-
|
45 |
-
|
46 |
-
|
47 |
-
|
48 |
-
|
49 |
-
|
50 |
-
|
51 |
-
|
52 |
-
|
53 |
-
|
54 |
-
|
55 |
|
56 |
-
|
57 |
|
58 |
title = "AI Image Colorizer"
|
59 |
description = "Colorize old images with AI"
|
@@ -61,7 +60,7 @@ examples = [["./demo.jpg"],]
|
|
61 |
|
62 |
demo = gr.Interface(
|
63 |
fn=colorizer_fn,
|
64 |
-
inputs=[gr.Image(type="filepath"
|
65 |
outputs=ImageSlider(type="pil", label="Old vs Colored image"),
|
66 |
examples=examples,
|
67 |
title=title,
|
@@ -69,6 +68,5 @@ demo = gr.Interface(
|
|
69 |
)
|
70 |
|
71 |
# Launch the demo
|
72 |
-
|
73 |
if __name__ == "__main__":
|
74 |
-
|
|
|
11 |
from deoldify import device
|
12 |
from deoldify.device_id import DeviceId
|
13 |
from deoldify.visualize import *
|
14 |
+
# from huggingface_hub import HfApi, snapshot_download
|
15 |
+
from huggingface_hub import HfApi
|
16 |
|
17 |
os.system("pip freeze")
|
18 |
from collections.abc import Sized # Import Sized from collections.abc
|
19 |
+
# Suppress warnings
|
20 |
warnings.filterwarnings("ignore", category=UserWarning, message=".*?Your .*? set is empty.*?")
|
21 |
|
22 |
+
# Initialize Hugging Face API
|
23 |
+
api = HfApi()
|
24 |
|
25 |
+
device.set(device=DeviceId.CPU)
|
26 |
+
|
27 |
+
# Private repo settings
|
28 |
token = os.getenv("HF_TOKEN")
|
29 |
repo_id = "afondiel/image-colorizer-deoldify"
|
30 |
repo_type = "space"
|
31 |
|
32 |
# Download the snapshot from the space repository
|
33 |
+
snapshot_folder = api.snapshot_download(repo_id=repo_id, repo_type=repo_type, token=token)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
34 |
|
35 |
+
device.set(device=DeviceId.GPU0)
|
36 |
# Load the pre-trained model
|
37 |
_colorizer = get_image_colorizer(root_folder=Path(snapshot_folder), artistic=True)
|
38 |
|
|
|
39 |
def colorizer_fn(input_img, render_factor):
|
40 |
+
"""
|
41 |
+
Colorize grayscale images/photos
|
42 |
+
- @param input_img old (grayscale) image
|
43 |
+
- @param render_factor render_factor
|
44 |
+
"""
|
45 |
+
if input_img is not None and input_img != '':
|
46 |
+
output_img = _colorizer.get_transformed_image(
|
47 |
+
path=input_img,
|
48 |
+
render_factor=int(render_factor),
|
49 |
+
watermarked=watermarked,
|
50 |
+
post_process=True,
|
51 |
+
)
|
52 |
+
else:
|
53 |
+
print('Provide an image and try again.')
|
54 |
|
55 |
+
return (input_img, output_img) # Return a tuple of old and color Image to be plotted with ImageSlider()
|
56 |
|
57 |
title = "AI Image Colorizer"
|
58 |
description = "Colorize old images with AI"
|
|
|
60 |
|
61 |
demo = gr.Interface(
|
62 |
fn=colorizer_fn,
|
63 |
+
inputs=[gr.Image(type="filepath", label="Old image"), gr.Slider(0, 40, label="Render Factor", value=10)],
|
64 |
outputs=ImageSlider(type="pil", label="Old vs Colored image"),
|
65 |
examples=examples,
|
66 |
title=title,
|
|
|
68 |
)
|
69 |
|
70 |
# Launch the demo
|
|
|
71 |
if __name__ == "__main__":
|
72 |
+
demo.launch()
|