Skip to content

feat: customize gradio components with lightning colors #17054

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from 16 commits
Commits
Show all changes
69 commits
Select commit Hold shift + click to select a range
4ae6893
feat: customize gradio components with lightning colors
KogaiIrina Mar 13, 2023
4311beb
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Mar 13, 2023
44fc012
Merge branch 'Lightning-AI:master' into feature/customized-gradio-com…
KogaiIrina Mar 13, 2023
c22dda4
move demo app from test to examples, add css file
KogaiIrina Mar 13, 2023
60c1314
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Mar 13, 2023
b05d3ad
add option to customize the gradio components, add readme with example
KogaiIrina Mar 13, 2023
a7931b2
Merge branch 'feature/customized-gradio-components' of github.com:Kog…
KogaiIrina Mar 13, 2023
1291321
add example with path to css file
KogaiIrina Mar 13, 2023
a53ff4b
Merge branch 'master' into feature/customized-gradio-components
KogaiIrina Mar 14, 2023
c102a49
fix tests
KogaiIrina Mar 14, 2023
d92c5c6
Merge branch 'master' into feature/customized-gradio-components
KogaiIrina Mar 14, 2023
fe40554
Merge branch 'master' into feature/customized-gradio-components
KogaiIrina Mar 14, 2023
5490689
Apply suggestions from code review
Borda Mar 24, 2023
22f07cc
Merge branch 'master' into feature/customized-gradio-components
Borda Mar 24, 2023
32a8019
Merge branch 'master' into feature/customized-gradio-components
KogaiIrina Mar 27, 2023
07e6b4c
rename variable
KogaiIrina Mar 27, 2023
1a0b38a
Apply suggestions from code review
Borda Mar 29, 2023
b689872
Merge branch 'master' into feature/customized-gradio-components
Borda Mar 29, 2023
ff6e825
Merge branch 'master' into feature/customized-gradio-components
yurijmikhalevich Mar 30, 2023
3e0d1d7
Merge branch 'master' into feature/customized-gradio-components
yurijmikhalevich Apr 1, 2023
12e7556
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Apr 1, 2023
4fa244e
bump ci
KogaiIrina Apr 1, 2023
2c470a1
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Apr 1, 2023
c58fd6a
Merge branch 'master' into feature/customized-gradio-components
yurijmikhalevich Apr 3, 2023
321a946
Merge branch 'master' into feature/customized-gradio-components
Borda Apr 3, 2023
f40349e
Merge branch 'master' into feature/customized-gradio-components
yurijmikhalevich Apr 4, 2023
3337129
Merge branch 'master' into feature/customized-gradio-components
yurijmikhalevich Apr 4, 2023
52f33ff
Merge branch 'master' into feature/customized-gradio-components
Borda Apr 5, 2023
d7b6c85
Merge branch 'master' into feature/customized-gradio-components
KogaiIrina Apr 6, 2023
b4500cb
Merge branch 'master' into feature/customized-gradio-components
KogaiIrina Apr 7, 2023
844e553
create "native" Gradio theme
KogaiIrina Apr 21, 2023
424f3c7
Merge branch 'feature/customized-gradio-components' of github.com:Kog…
KogaiIrina Apr 21, 2023
48a44c1
remove commented code
KogaiIrina Apr 21, 2023
ca9b9ac
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Apr 21, 2023
0813803
Merge branch 'feature/customized-gradio-components' of github.com:Kog…
KogaiIrina Apr 21, 2023
22beaa4
Merge branch 'master' into feature/customized-gradio-components
KogaiIrina Apr 21, 2023
862209f
import gradio
KogaiIrina Apr 21, 2023
f3882c4
put gradio directory back
KogaiIrina Apr 21, 2023
8167eae
Merge branch 'feature/customized-gradio-components' of github.com:Kog…
KogaiIrina Apr 21, 2023
a76959a
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Apr 21, 2023
2cf0a31
remove elon img
KogaiIrina Apr 21, 2023
c159410
Merge branch 'feature/customized-gradio-components' of github.com:Kog…
KogaiIrina Apr 21, 2023
d8c4804
put comments back
KogaiIrina Apr 21, 2023
027f5c0
put copyright comments into gradio_server.py
KogaiIrina Apr 21, 2023
91eba51
remove tabs
KogaiIrina Apr 21, 2023
173a788
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Apr 21, 2023
08ef3bd
remove unused import
KogaiIrina Apr 21, 2023
6e8fe41
Merge branch 'feature/customized-gradio-components' of github.com:Kog…
KogaiIrina Apr 21, 2023
4707ec7
mock gradio colors and default
KogaiIrina Apr 21, 2023
c21ca38
Merge branch 'master' into feature/customized-gradio-components
KogaiIrina Apr 22, 2023
9aeacad
add DummyColor class for tests
KogaiIrina Apr 22, 2023
e45fc1c
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Apr 22, 2023
274b85e
make DummyDefault for test
KogaiIrina Apr 22, 2023
9f51c9e
Merge branch 'feature/customized-gradio-components' of github.com:Kog…
KogaiIrina Apr 22, 2023
62f5878
remove version.info
KogaiIrina Apr 22, 2023
2e23728
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Apr 22, 2023
4e818be
Merge branch 'feature/customized-gradio-components' of github.com:Kog…
KogaiIrina Apr 22, 2023
c9be8f6
fix tests
KogaiIrina Apr 22, 2023
aad346d
put version info back
KogaiIrina Apr 23, 2023
939932b
replace link for upload elon image in examples, move theme into serve…
KogaiIrina Apr 23, 2023
95abccc
fix test
KogaiIrina Apr 23, 2023
b52e9fb
replace BAse with Theme
KogaiIrina Apr 23, 2023
71c1fa6
attempt
KogaiIrina Apr 23, 2023
0478add
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Apr 23, 2023
71364c3
attempt at making CI happy
KogaiIrina Apr 23, 2023
ca197df
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Apr 23, 2023
d52720a
fix test
KogaiIrina Apr 23, 2023
fe22aa9
Merge branch 'master' into feature/customized-gradio-components
Borda Apr 24, 2023
6aa9fa7
chlog
awaelchli Apr 13, 2023
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 36 additions & 0 deletions examples/app_gradio_custom_css/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
# Customize Gradio components

If you want to customize gradio component by yourself, you need to pass `css` argument to ServeGradio like this:

```py
class AnimeGANv2UI(ServeGradio):
inputs = gr.inputs.Image(type="pil")
outputs = gr.outputs.Image(type="pil")
...
css='''
.gradio-container button.primary, .gradio-container button.plain {
background: red;
color: white;
font-size: 15;
font-weight: 500;
}
'''

def __init__(self):
super().__init__(css=self.css)
self.ready = False
```

You can also pass path to css file like this:

```py
class AnimeGANv2UI(ServeGradio):
inputs = gr.inputs.Image(type="pil")
outputs = gr.outputs.Image(type="pil")
...
css="styles.css"

def __init__(self):
super().__init__(css=self.css)
self.ready = False
```
57 changes: 57 additions & 0 deletions examples/app_gradio_custom_css/app.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
import gradio as gr

import lightning as L
from lightning.app.components.serve import ServeGradio


class LitGradio(ServeGradio):
inputs = [
gr.components.Textbox(label="print your prompt here", elem_id="label"),
gr.components.Dropdown(
label="choose you model",
choices=[
"PulpSciFiDiffusion",
"pixel_art_diffusion_hard_256",
"pixel_art_diffusion_soft_256",
],
),
gr.components.Number(value=250, label="number of steps"),
gr.components.Audio(label="Audio"),
gr.components.Image(label="Image"),
gr.components.Video(label="Video"),
gr.components.HTML(label="HTML"),
gr.components.BarPlot(label="Bar Plot"),
gr.components.Button(value="Hi"),
gr.components.Chatbot(label="Bot"),
gr.components.Checkbox(label="Checkbox"),
gr.components.ColorPicker(label="Color Picker"),
gr.components.File(label="File"),
gr.components.Dataframe(label="Data Frame"),
gr.components.Gallery(label="Gallery"),
gr.components.HighlightedText(
label="Highlighted Text",
),
gr.components.Image(label="Image"),
gr.components.JSON(label="JSON"),
gr.components.Label(label="Label"),
gr.components.LinePlot(label="Line Plot"),
gr.components.Markdown(label="Markdown"),
gr.components.Model3D(label="Model 3D"),
gr.components.Plot(label="Plot"),
gr.components.Radio(label="Radio"),
gr.components.ScatterPlot(label="Scatter Plot"),
gr.components.Slider(label="Slider"),
gr.components.Timeseries(label="Time Series"),
gr.components.UploadButton(label="Upload Button"),
gr.components.Video(label="Video"),
]
outputs = gr.outputs.Image(type="pil", label="Output Image")

def predict(self, label, model, steps):
return 0

def build_model(self):
return None


app = L.LightningApp(LitGradio())
77 changes: 77 additions & 0 deletions src/lightning/app/components/serve/gradio.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
:root {
--purple: rgb(121, 46, 229);
--purple-darker: rgb(109, 30, 229);
--button-primary-border-color-base: rgb(121, 46, 229);
--purple-gradient: linear-gradient(206.91deg, rgb(121, 46, 229) 16.83%, rgb(62, 171, 179) 144.59%);
--color-accent-base: var(--purple);
--color-border-highlight: var(--purple);
}

.gradio-container {
background: white;
}

.gradio-container input {
font-size: 15px;
border-radius: 6px;
box-shadow: none;
}

.gradio-container button.primary, .gradio-container button.plain {
background: var(--purple) !important;
color: white !important;
font-size: 15;
}

.gradio-container button.primary:hover, .gradio-container button.plain:hover {
background: var(--purple-darker);
color: white;
}

.gradio-container button.lg {
border-radius: 120px;
}

.gradio-container input:focus {
--tw-ring-opacity: 0;
--tw-border-opacity: 0.5;
box-shadow: none;
border-color: var(--purple);
}
.gradio-container textarea:focus {
--tw-ring-opacity: 0;
--tw-border-opacity: 0.5;
box-shadow: none;
border-color: var(--purple);
}

.gradio-container input:hover {
border-color: var(--purple);
}

.gradio-container input[type="range"] {
accent-color: var(--purple);
}

.gradio-container input[type="checkbox"]:checked {
background-color: var(--purple) !important;
border-color: var(--purple) !important;
}

.gradio-container input[type="checkbox"]:focus {
--ring-color: rgb(121, 46, 229 / .5) !important;
border-color: var(--purple) !important;
}

.gradio-container button.sm.plain:hover svg {
color: white !important;
}

.gradio-container .wrap:focus-within {
border-color: var(--purple) !important;
--ring-color: transparent !important;
}

.gradio-container th {
border-color: var(--purple);
}
7 changes: 6 additions & 1 deletion src/lightning/app/components/serve/gradio_server.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,16 @@
# limitations under the License.

import abc
import os.path
from functools import partial
from types import ModuleType
from typing import Any, List, Optional

from lightning.app.core.work import LightningWork
from lightning.app.utilities.imports import _is_gradio_available, requires

DEFAULT_CSS_FILE_PATH = os.path.join(os.path.dirname(os.path.realpath(__file__)), "gradio.css")

if _is_gradio_available():
import gradio
else:
Expand Down Expand Up @@ -49,11 +52,12 @@ class ServeGradio(LightningWork, abc.ABC):

_start_method = "spawn"

def __init__(self, *args: Any, **kwargs: Any):
def __init__(self, *args: Any, css=None, **kwargs: Any):
requires("gradio")(super().__init__(*args, **kwargs))
assert self.inputs
assert self.outputs
self._model = None
self._css = css

self.ready = False

Expand Down Expand Up @@ -85,6 +89,7 @@ def run(self, *args: Any, **kwargs: Any):
examples=self.examples,
title=self.title,
description=self.description,
css=self._css or DEFAULT_CSS_FILE_PATH,
).launch(
server_name=self.host,
server_port=self.port,
Expand Down
2 changes: 1 addition & 1 deletion tests/integrations_app/public/test_gradio.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,5 +28,5 @@ def predict(self, *args, **kwargs):
assert comp.model == "model"
assert comp.predict() == "prediction"
gradio_mock.Interface.assert_called_once_with(
fn=ANY, inputs=ANY, outputs=ANY, examples=ANY, title=None, description=None
fn=ANY, inputs=ANY, outputs=ANY, examples=ANY, title=None, description=None, css=ANY
)