Pierre Chapuis commited on
Commit
17a4159
Β·
unverified Β·
1 Parent(s): afd8ef6

add application

Browse files
.python-version ADDED
@@ -0,0 +1 @@
 
 
1
+ 3.10.16
README.md CHANGED
@@ -1,13 +1,18 @@
1
  ---
2
  title: Finegrain Light Switcher
3
- emoji: ⚑
4
- colorFrom: red
5
- colorTo: yellow
6
  sdk: gradio
7
- sdk_version: 5.22.0
 
8
  app_file: app.py
9
  pinned: false
 
10
  short_description: Switch lamps on in your images.
 
 
 
11
  ---
12
 
13
- Check out the configuration reference at https://huggingface.co/docs/hub/spaces-config-reference
 
1
  ---
2
  title: Finegrain Light Switcher
3
+ emoji: πŸ’‘
4
+ colorFrom: yellow
5
+ colorTo: indigo
6
  sdk: gradio
7
+ sdk_version: 4.44.1
8
+ python_version: 3.10
9
  app_file: app.py
10
  pinned: false
11
+ license: other
12
  short_description: Switch lamps on in your images.
13
+ tags:
14
+ - refiners
15
+ - image-to-image
16
  ---
17
 
18
+ # Finegrain Light Switcher
app.py ADDED
@@ -0,0 +1,116 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import os
2
+ import random
3
+ import time
4
+ from typing import Any
5
+
6
+ import gradio as gr
7
+ import pillow_avif # noqa: F401
8
+ import pillow_heif
9
+ import spaces
10
+ import torch
11
+ from gradio_imageslider import ImageSlider
12
+ from PIL import Image
13
+
14
+
15
+ class ModuleInterface:
16
+ def __init__(self, d: dict[str, Any]):
17
+ self.d = d
18
+
19
+ @property
20
+ def Model(self) -> Any:
21
+ return self.d["Model"]
22
+
23
+ def process(self, input_image: Image.Image, model: Any, seed: int) -> Image.Image:
24
+ return self.d["process"](input_image, model, seed)
25
+
26
+
27
+ assert (src := os.getenv("LIGHT_SWITCHER_LITE")), "LIGHT_SWITCHER_LITE not set"
28
+ exec_globals: dict[str, Any] = {}
29
+ exec(src, exec_globals)
30
+ light_switcher_lite = ModuleInterface(exec_globals)
31
+
32
+ pillow_heif.register_avif_opener()
33
+
34
+
35
+ DEVICE_CPU = torch.device("cpu")
36
+ DEVICE = torch.device("cuda" if torch.cuda.is_available() else "cpu")
37
+ DTYPE = torch.bfloat16 if torch.cuda.is_bf16_supported() else torch.float32
38
+
39
+ # CPU -> GPU dance because of ZeroGPU
40
+
41
+ path = "finegrain/weights-light-switcher-space"
42
+ model = light_switcher_lite.Model.from_pretrained(path, device=DEVICE_CPU, dtype=DTYPE)
43
+
44
+ model.to(DEVICE)
45
+
46
+
47
+ @spaces.GPU
48
+ def process(input_image: Image.Image, seed: int = 42) -> tuple[tuple[Image.Image, Image.Image], dict[str, Any]]:
49
+ output_image = light_switcher_lite.process(input_image, model, seed)
50
+ resized_input_image = input_image.resize(output_image.size)
51
+ return ((resized_input_image, output_image), gr.update(value=random.choice(BUTTON_LABELS)))
52
+
53
+
54
+ TITLE = """
55
+ <h1>Finegrain Light Switcher</h1>
56
+ <p>
57
+ Given an image with a lamp switched off, the model should turn it on.
58
+ </p>
59
+ <p>
60
+ πŸ”Œ For higher resolution results with control over lighting intensity and warmth,&nbsp;
61
+ <a href="https://finegrain.ai">head to the Finegrain API</a> πŸ”Œ
62
+ </p>
63
+ <p>
64
+ <a href="https://discord.gg/zFKg5TjXub" target="_blank">[Discord]</a>
65
+ <a href="https://github.com/finegrain-ai" target="_blank">[GitHub]</a>
66
+ <a href="https://finegrain.ai">[Finegrain API]</a>
67
+ </p>
68
+ """
69
+
70
+ BUTTON_LABELS = [
71
+ "πŸ’‘",
72
+ "Let there be light!",
73
+ "Light it up like a Christmas tree!",
74
+ "Turn it on!",
75
+ "Aziz, Light!",
76
+ "Make it shine ✨",
77
+ "Flip the magic switch.",
78
+ ]
79
+
80
+ random.seed(time.time())
81
+
82
+ with gr.Blocks() as demo:
83
+ gr.HTML(TITLE)
84
+ with gr.Row():
85
+ with gr.Column():
86
+ input_image = gr.Image(type="pil", label="Input Image")
87
+ run_button = gr.ClearButton(components=None, value=random.choice(BUTTON_LABELS))
88
+ with gr.Column():
89
+ output_slider = ImageSlider(label="Before / After")
90
+ run_button.add(output_slider)
91
+
92
+ with gr.Accordion("Advanced Options", open=False):
93
+ seed = gr.Slider(minimum=0, maximum=999, value=42, step=1, label="Seed")
94
+
95
+ run_button.click(
96
+ fn=process,
97
+ inputs=[input_image, seed],
98
+ outputs=[output_slider, run_button],
99
+ )
100
+
101
+ gr.Examples(
102
+ examples=[
103
+ "examples/01.webp",
104
+ "examples/02.webp",
105
+ "examples/03.webp",
106
+ "examples/04.webp",
107
+ "examples/05.webp",
108
+ ],
109
+ inputs=[input_image],
110
+ outputs=[output_slider, run_button],
111
+ fn=process,
112
+ cache_examples=True,
113
+ run_on_click=False,
114
+ )
115
+
116
+ demo.launch(share=False)
examples/01.webp ADDED
examples/02.webp ADDED
examples/03.webp ADDED
examples/04.webp ADDED
examples/05.webp ADDED
pyproject.toml ADDED
@@ -0,0 +1,27 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ [project]
2
+ name = "light-switcher"
3
+ version = "0.1.0"
4
+ description = "Finegrain Light Switcher (Gradio)"
5
+ authors = [
6
+ { name = "Pierre Chapuis", email = "[email protected]" }
7
+ ]
8
+
9
+ [tool.ruff]
10
+ line-length = 120
11
+ target-version = "py310"
12
+
13
+ [tool.ruff.lint]
14
+ select = [
15
+ "E", # pycodestyle errors
16
+ "W", # pycodestyle warnings
17
+ "F", # pyflakes
18
+ "UP", # pyupgrade
19
+ "A", # flake8-builtins
20
+ "B", # flake8-bugbear
21
+ "Q", # flake8-quotes
22
+ "I", # isort
23
+ ]
24
+
25
+ [tool.pyright]
26
+ include = ["app.py"]
27
+ exclude = ["**/__pycache__"]
requirements.txt ADDED
@@ -0,0 +1,9 @@
 
 
 
 
 
 
 
 
 
 
1
+ git+https://github.com/huggingface/diffusers@97abdd2210a540c2e71aee63c80a22723031cd57
2
+ git+https://github.com/finegrain-ai/refiners@d288e94fa8eed1386bd28cd0d5ceb8109c3ff398
3
+ gradio_imageslider==0.0.20
4
+ spaces==0.32.0
5
+ numpy<2.0.0
6
+ pillow>=10.4.0
7
+ pillow-heif>=0.18.0
8
+ accelerate>=1.3.0
9
+ pillow-avif-plugin>=1.5.0