Skip to content

Commit 0fdd76d

Browse files
KogaiIrinapre-commit-ci[bot]Bordayurijmikhalevich
authored
feat: customize gradio components with lightning colors (#17054)
Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com> Co-authored-by: Jirka Borovec <[email protected]> Co-authored-by: Yurij Mikhalevich <[email protected]>
1 parent 20303e4 commit 0fdd76d

File tree

5 files changed

+108
-4
lines changed

5 files changed

+108
-4
lines changed

examples/app/components/serve/gradio/app.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
class AnimeGANv2UI(ServeGradio):
1515
inputs = gr.inputs.Image(type="pil")
1616
outputs = gr.outputs.Image(type="pil")
17-
elon = "https://upload.wikimedia.org/wikipedia/commons/3/34/Elon_Musk_Royal_Society_%28crop2%29.jpg"
17+
elon = "https://upload.wikimedia.org/wikipedia/commons/thumb/3/34/Elon_Musk_Royal_Society_%28crop2%29.jpg/330px-Elon_Musk_Royal_Society_%28crop2%29.jpg"
1818
img = Image.open(requests.get(elon, stream=True).raw)
1919
img.save("elon.jpg")
2020
examples = [["elon.jpg"]]

src/lightning/app/CHANGELOG.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ The format is based on [Keep a Changelog](http://keepachangelog.com/en/1.0.0/).
99

1010
### Added
1111

12-
-
12+
- Allow customize `gradio` components with lightning colors ([#17054](https://github.com/Lightning-AI/lightning/pull/17054))
1313

1414

1515
### Changed

src/lightning/app/components/serve/gradio_server.py

Lines changed: 104 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,12 @@
2424
import gradio
2525
else:
2626
gradio = ModuleType("gradio")
27+
gradio.themes = ModuleType("gradio.themes")
28+
29+
class __DummyBase:
30+
pass
31+
32+
gradio.themes.Base = __DummyBase
2733

2834

2935
class ServeGradio(LightningWork, abc.ABC):
@@ -49,11 +55,12 @@ class ServeGradio(LightningWork, abc.ABC):
4955

5056
_start_method = "spawn"
5157

52-
def __init__(self, *args: Any, **kwargs: Any):
58+
def __init__(self, *args: Any, theme: Optional[gradio.themes.Base] = None, **kwargs: Any):
5359
requires("gradio")(super().__init__(*args, **kwargs))
5460
assert self.inputs
5561
assert self.outputs
5662
self._model = None
63+
self._theme = theme or ServeGradio.__get_lightning_gradio_theme()
5764

5865
self.ready = False
5966

@@ -85,6 +92,7 @@ def run(self, *args: Any, **kwargs: Any):
8592
examples=self.examples,
8693
title=self.title,
8794
description=self.description,
95+
theme=self._theme,
8896
).launch(
8997
server_name=self.host,
9098
server_port=self.port,
@@ -93,3 +101,98 @@ def run(self, *args: Any, **kwargs: Any):
93101

94102
def configure_layout(self) -> str:
95103
return self.url
104+
105+
@staticmethod
106+
def __get_lightning_gradio_theme():
107+
return gradio.themes.Default(
108+
primary_hue=gradio.themes.Color(
109+
"#ffffff",
110+
"#e9d5ff",
111+
"#d8b4fe",
112+
"#c084fc",
113+
"#fcfcfc",
114+
"#a855f7",
115+
"#9333ea",
116+
"#8823e1",
117+
"#6b21a8",
118+
"#2c2730",
119+
"#1c1c1c",
120+
),
121+
secondary_hue=gradio.themes.Color(
122+
"#c3a1e8",
123+
"#e9d5ff",
124+
"#d3bbec",
125+
"#c795f9",
126+
"#9174af",
127+
"#a855f7",
128+
"#9333ea",
129+
"#6700c2",
130+
"#000000",
131+
"#991ef1",
132+
"#33243d",
133+
),
134+
neutral_hue=gradio.themes.Color(
135+
"#ede9fe",
136+
"#ddd6fe",
137+
"#c4b5fd",
138+
"#a78bfa",
139+
"#fafafa",
140+
"#8b5cf6",
141+
"#7c3aed",
142+
"#6d28d9",
143+
"#6130b0",
144+
"#8a4ce6",
145+
"#3b3348",
146+
),
147+
).set(
148+
body_background_fill="*primary_50",
149+
body_background_fill_dark="*primary_950",
150+
body_text_color_dark="*primary_100",
151+
body_text_size="*text_sm",
152+
body_text_color_subdued_dark="*primary_100",
153+
background_fill_primary="*primary_50",
154+
background_fill_primary_dark="*primary_950",
155+
background_fill_secondary="*primary_50",
156+
background_fill_secondary_dark="*primary_950",
157+
border_color_accent="*primary_400",
158+
border_color_accent_dark="*primary_900",
159+
border_color_primary="*primary_600",
160+
border_color_primary_dark="*primary_800",
161+
color_accent="*primary_400",
162+
color_accent_soft="*primary_300",
163+
color_accent_soft_dark="*primary_700",
164+
link_text_color="*primary_500",
165+
link_text_color_dark="*primary_50",
166+
link_text_color_active="*secondary_800",
167+
link_text_color_active_dark="*primary_500",
168+
link_text_color_hover="*primary_400",
169+
link_text_color_hover_dark="*primary_400",
170+
link_text_color_visited="*primary_500",
171+
link_text_color_visited_dark="*secondary_100",
172+
block_background_fill="*primary_50",
173+
block_background_fill_dark="*primary_900",
174+
block_border_color_dark="*primary_800",
175+
checkbox_background_color="*primary_50",
176+
checkbox_background_color_dark="*primary_50",
177+
checkbox_background_color_focus="*primary_100",
178+
checkbox_background_color_focus_dark="*primary_100",
179+
checkbox_background_color_hover="*primary_400",
180+
checkbox_background_color_hover_dark="*primary_500",
181+
checkbox_background_color_selected="*primary_300",
182+
checkbox_background_color_selected_dark="*primary_500",
183+
checkbox_border_color_dark="*primary_200",
184+
checkbox_border_radius="*radius_md",
185+
input_background_fill="*primary_50",
186+
input_background_fill_dark="*primary_900",
187+
input_radius="*radius_xxl",
188+
slider_color="*primary_600",
189+
slider_color_dark="*primary_700",
190+
button_large_radius="*radius_xxl",
191+
button_large_text_size="*text_md",
192+
button_small_radius="*radius_xxl",
193+
button_primary_background_fill_dark="*primary_800",
194+
button_primary_background_fill_hover_dark="*primary_700",
195+
button_primary_border_color_dark="*primary_800",
196+
button_secondary_background_fill="*neutral_200",
197+
button_secondary_background_fill_dark="*primary_600",
198+
)

src/lightning/version.info

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
2.1.0dev

tests/integrations_app/public/test_gradio.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,5 +28,5 @@ def predict(self, *args, **kwargs):
2828
assert comp.model == "model"
2929
assert comp.predict() == "prediction"
3030
gradio_mock.Interface.assert_called_once_with(
31-
fn=ANY, inputs=ANY, outputs=ANY, examples=ANY, title=None, description=None
31+
fn=ANY, inputs=ANY, outputs=ANY, examples=ANY, title=None, description=None, theme=ANY
3232
)

0 commit comments

Comments
 (0)