Spaces:
Running
Running
update app
Browse files- src/app.py +211 -0
- src/theme/carbon.py +147 -0
- src/theme/research_monochrome.py +152 -0
src/app.py
ADDED
@@ -0,0 +1,211 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
"""Template Demo for IBM Granite Hugging Face spaces."""
|
2 |
+
|
3 |
+
from collections.abc import Iterator
|
4 |
+
from datetime import datetime
|
5 |
+
from pathlib import Path
|
6 |
+
from threading import Thread
|
7 |
+
|
8 |
+
import gradio as gr
|
9 |
+
import spaces
|
10 |
+
import torch
|
11 |
+
from transformers import AutoModelForCausalLM, AutoTokenizer, TextIteratorStreamer
|
12 |
+
|
13 |
+
from themes.research_monochrome import theme
|
14 |
+
|
15 |
+
today_date = datetime.today().strftime("%B %-d, %Y") # noqa: DTZ002
|
16 |
+
|
17 |
+
SYS_PROMPT = f"""Knowledge Cutoff Date: April 2024.
|
18 |
+
Today's Date: {today_date}.
|
19 |
+
You are Granite, developed by IBM. You are a helpful AI assistant"""
|
20 |
+
TITLE = "IBM Granite 3.1 8b Instruct"
|
21 |
+
DESCRIPTION = """
|
22 |
+
<p>Granite 3.1 8b instruct is an open-source LLM supporting a 128k context window. Start with one of the sample prompts
|
23 |
+
or enter your own. Keep in mind that AI can occasionally make mistakes.
|
24 |
+
<span class="gr_docs_link">
|
25 |
+
<a href="https://www.ibm.com/granite/docs/">View Documentation <i class="fa fa-external-link"></i></a>
|
26 |
+
</span>
|
27 |
+
</p>
|
28 |
+
"""
|
29 |
+
MAX_INPUT_TOKEN_LENGTH = 128_000
|
30 |
+
MAX_NEW_TOKENS = 1024
|
31 |
+
TEMPERATURE = 0.7
|
32 |
+
TOP_P = 0.85
|
33 |
+
TOP_K = 50
|
34 |
+
REPETITION_PENALTY = 1.05
|
35 |
+
|
36 |
+
if not torch.cuda.is_available():
|
37 |
+
print("This demo may not work on CPU.")
|
38 |
+
|
39 |
+
model = AutoModelForCausalLM.from_pretrained(
|
40 |
+
"ibm-granite/granite-3.1-8b-instruct", torch_dtype=torch.float16, device_map="auto"
|
41 |
+
)
|
42 |
+
tokenizer = AutoTokenizer.from_pretrained("ibm-granite/granite-3.1-8b-instruct")
|
43 |
+
tokenizer.use_default_system_prompt = False
|
44 |
+
|
45 |
+
|
46 |
+
@spaces.GPU
|
47 |
+
def generate(
|
48 |
+
message: str,
|
49 |
+
chat_history: list[dict],
|
50 |
+
temperature: float = TEMPERATURE,
|
51 |
+
repetition_penalty: float = REPETITION_PENALTY,
|
52 |
+
top_p: float = TOP_P,
|
53 |
+
top_k: float = TOP_K,
|
54 |
+
max_new_tokens: int = MAX_NEW_TOKENS,
|
55 |
+
) -> Iterator[str]:
|
56 |
+
"""Generate function for chat demo."""
|
57 |
+
# Build messages
|
58 |
+
conversation = []
|
59 |
+
conversation.append({"role": "system", "content": SYS_PROMPT})
|
60 |
+
conversation += chat_history
|
61 |
+
conversation.append({"role": "user", "content": message})
|
62 |
+
|
63 |
+
# Convert messages to prompt format
|
64 |
+
input_ids = tokenizer.apply_chat_template(
|
65 |
+
conversation,
|
66 |
+
return_tensors="pt",
|
67 |
+
add_generation_prompt=True,
|
68 |
+
truncation=True,
|
69 |
+
max_length=MAX_INPUT_TOKEN_LENGTH - max_new_tokens,
|
70 |
+
)
|
71 |
+
|
72 |
+
input_ids = input_ids.to(model.device)
|
73 |
+
streamer = TextIteratorStreamer(tokenizer, skip_prompt=True, skip_special_tokens=True)
|
74 |
+
generate_kwargs = dict(
|
75 |
+
{"input_ids": input_ids},
|
76 |
+
streamer=streamer,
|
77 |
+
max_new_tokens=max_new_tokens,
|
78 |
+
do_sample=True,
|
79 |
+
top_p=top_p,
|
80 |
+
top_k=top_k,
|
81 |
+
temperature=temperature,
|
82 |
+
num_beams=1,
|
83 |
+
repetition_penalty=repetition_penalty,
|
84 |
+
)
|
85 |
+
|
86 |
+
t = Thread(target=model.generate, kwargs=generate_kwargs)
|
87 |
+
t.start()
|
88 |
+
|
89 |
+
outputs = []
|
90 |
+
for text in streamer:
|
91 |
+
outputs.append(text)
|
92 |
+
yield "".join(outputs)
|
93 |
+
|
94 |
+
|
95 |
+
css_file_path = Path(Path(__file__).parent / "app.css")
|
96 |
+
head_file_path = Path(Path(__file__).parent / "app_head.html")
|
97 |
+
|
98 |
+
# advanced settings (displayed in Accordion)
|
99 |
+
temperature_slider = gr.Slider(
|
100 |
+
minimum=0, maximum=1.0, value=TEMPERATURE, step=0.1, label="Temperature", elem_classes=["gr_accordion_element"]
|
101 |
+
)
|
102 |
+
top_p_slider = gr.Slider(
|
103 |
+
minimum=0, maximum=1.0, value=TOP_P, step=0.05, label="Top P", elem_classes=["gr_accordion_element"]
|
104 |
+
)
|
105 |
+
top_k_slider = gr.Slider(
|
106 |
+
minimum=0, maximum=100, value=TOP_K, step=1, label="Top K", elem_classes=["gr_accordion_element"]
|
107 |
+
)
|
108 |
+
repetition_penalty_slider = gr.Slider(
|
109 |
+
minimum=0,
|
110 |
+
maximum=2.0,
|
111 |
+
value=REPETITION_PENALTY,
|
112 |
+
step=0.05,
|
113 |
+
label="Repetition Penalty",
|
114 |
+
elem_classes=["gr_accordion_element"],
|
115 |
+
)
|
116 |
+
max_new_tokens_slider = gr.Slider(
|
117 |
+
minimum=1,
|
118 |
+
maximum=2000,
|
119 |
+
value=MAX_NEW_TOKENS,
|
120 |
+
step=1,
|
121 |
+
label="Max New Tokens",
|
122 |
+
elem_classes=["gr_accordion_element"],
|
123 |
+
)
|
124 |
+
chat_interface_accordion = gr.Accordion(label="Advanced Settings", open=False)
|
125 |
+
|
126 |
+
with gr.Blocks(fill_height=True, css_paths=css_file_path, head_paths=head_file_path, theme=theme, title=TITLE) as demo:
|
127 |
+
gr.HTML(f"<h1>{TITLE}</h1>", elem_classes=["gr_title"])
|
128 |
+
gr.HTML(DESCRIPTION)
|
129 |
+
chat_interface = gr.ChatInterface(
|
130 |
+
fn=generate,
|
131 |
+
examples=[
|
132 |
+
["Explain the concept of quantum computing to someone with no background in physics or computer science."],
|
133 |
+
["What is OpenShift?"],
|
134 |
+
["What's the importance of low latency inference?"],
|
135 |
+
["Help me boost productivity habits."],
|
136 |
+
[
|
137 |
+
"""Explain the following code in a concise manner:
|
138 |
+
|
139 |
+
```java
|
140 |
+
import java.util.ArrayList;
|
141 |
+
import java.util.List;
|
142 |
+
|
143 |
+
public class Main {
|
144 |
+
|
145 |
+
public static void main(String[] args) {
|
146 |
+
int[] arr = {1, 5, 3, 4, 2};
|
147 |
+
int diff = 3;
|
148 |
+
List<Pair> pairs = findPairs(arr, diff);
|
149 |
+
for (Pair pair : pairs) {
|
150 |
+
System.out.println(pair.x + " " + pair.y);
|
151 |
+
}
|
152 |
+
}
|
153 |
+
|
154 |
+
public static List<Pair> findPairs(int[] arr, int diff) {
|
155 |
+
List<Pair> pairs = new ArrayList<>();
|
156 |
+
for (int i = 0; i < arr.length; i++) {
|
157 |
+
for (int j = i + 1; j < arr.length; j++) {
|
158 |
+
if (Math.abs(arr[i] - arr[j]) < diff) {
|
159 |
+
pairs.add(new Pair(arr[i], arr[j]));
|
160 |
+
}
|
161 |
+
}
|
162 |
+
}
|
163 |
+
|
164 |
+
return pairs;
|
165 |
+
}
|
166 |
+
}
|
167 |
+
|
168 |
+
class Pair {
|
169 |
+
int x;
|
170 |
+
int y;
|
171 |
+
public Pair(int x, int y) {
|
172 |
+
this.x = x;
|
173 |
+
this.y = y;
|
174 |
+
}
|
175 |
+
}
|
176 |
+
```"""
|
177 |
+
],
|
178 |
+
[
|
179 |
+
"""Generate a Java code block from the following explanation:
|
180 |
+
|
181 |
+
The code in the Main class finds all pairs in an array whose absolute difference is less than a given value.
|
182 |
+
|
183 |
+
The findPairs method takes two arguments: an array of integers and a difference value. It iterates over the array and compares each element to every other element in the array. If the absolute difference between the two elements is less than the difference value, a new Pair object is created and added to a list.
|
184 |
+
|
185 |
+
The Pair class is a simple data structure that stores two integers.
|
186 |
+
|
187 |
+
The main method creates an array of integers, initializes the difference value, and calls the findPairs method to find all pairs in the array. Finally, the code iterates over the list of pairs and prints each pair to the console.""" # noqa: E501
|
188 |
+
],
|
189 |
+
],
|
190 |
+
example_labels=[
|
191 |
+
"Explain quantum computing",
|
192 |
+
"What is OpenShift?",
|
193 |
+
"Importance of low latency inference",
|
194 |
+
"Boosting productivity habits",
|
195 |
+
"Explain and document your code",
|
196 |
+
"Generate Java Code",
|
197 |
+
],
|
198 |
+
cache_examples=False,
|
199 |
+
type="messages",
|
200 |
+
additional_inputs=[
|
201 |
+
temperature_slider,
|
202 |
+
repetition_penalty_slider,
|
203 |
+
top_p_slider,
|
204 |
+
top_k_slider,
|
205 |
+
max_new_tokens_slider,
|
206 |
+
],
|
207 |
+
additional_inputs_accordion=chat_interface_accordion,
|
208 |
+
)
|
209 |
+
|
210 |
+
if __name__ == "__main__":
|
211 |
+
demo.queue().launch()
|
src/theme/carbon.py
ADDED
@@ -0,0 +1,147 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
"""IBM Carbon theme for gradio demos.
|
2 |
+
|
3 |
+
This version builds on top of the Carbon theme to make it more playful with rounded corners, a larger font family to
|
4 |
+
enhance readability, and the IBM Cool Gray color palette for better consistency with other IBM Research demos, such as
|
5 |
+
Bee.
|
6 |
+
"""
|
7 |
+
|
8 |
+
import gradio as gr
|
9 |
+
from gradio.themes.utils import sizes
|
10 |
+
|
11 |
+
theme = gr.themes.Base(
|
12 |
+
primary_hue=gr.themes.Color(
|
13 |
+
c100="#EDF5FF",
|
14 |
+
c200="#D0E2FF",
|
15 |
+
c300="#A6C8FF",
|
16 |
+
c400="#78A9FF",
|
17 |
+
c50="#F9F9FB",
|
18 |
+
c500="#4589FF",
|
19 |
+
c600="#0F62FE",
|
20 |
+
c700="#0043CE",
|
21 |
+
c800="#002D9C",
|
22 |
+
c900="#001D6C",
|
23 |
+
c950="#001141",
|
24 |
+
),
|
25 |
+
secondary_hue=gr.themes.Color(
|
26 |
+
c100="#EDF5FF",
|
27 |
+
c200="#D0E2FF",
|
28 |
+
c300="#A6C8FF",
|
29 |
+
c400="#78A9FF",
|
30 |
+
c50="#F9F9FB",
|
31 |
+
c500="#4589FF",
|
32 |
+
c600="#0F62FE",
|
33 |
+
c700="#0043CE",
|
34 |
+
c800="#002D9C",
|
35 |
+
c900="#001D6C",
|
36 |
+
c950="#001141",
|
37 |
+
),
|
38 |
+
neutral_hue=gr.themes.Color(
|
39 |
+
c100="#F2F4F8",
|
40 |
+
c200="#DDE1E6",
|
41 |
+
c300="#C1C7CD",
|
42 |
+
c400="#A2A9B0",
|
43 |
+
c50="#F9F9FB",
|
44 |
+
c500="#878D96",
|
45 |
+
c600="#697077",
|
46 |
+
c700="#4D5358",
|
47 |
+
c800="#393939",
|
48 |
+
c900="#21272A",
|
49 |
+
c950="#121619",
|
50 |
+
),
|
51 |
+
spacing_size=sizes.spacing_md, # change spacing to default size
|
52 |
+
radius_size=sizes.radius_md, # change spacing to default size and Keep Radius to make demo feel more playful
|
53 |
+
text_size=sizes.text_lg, # change fontsize to default size
|
54 |
+
# spacing_size: sizes.Size | str = sizes.spacing_md, #change spacing to default size
|
55 |
+
# radius_size: sizes.Size | str = sizes.radius_md, #change spacing to default size and Keep Radius to make
|
56 |
+
# demo feel more playful
|
57 |
+
# text_size: sizes.Size | str = sizes.text_lg, #change fontsize to default size
|
58 |
+
font=["IBM Plex Sans", "ui-sans-serif", "system-ui", "sans-serif"], # update font
|
59 |
+
font_mono=["IBM Plex Mono", "ui-monospace", "Consolas", "monospace"], # update font
|
60 |
+
).set(
|
61 |
+
# Colors
|
62 |
+
background_fill_primary="*neutral_100", # Coolgray10 background
|
63 |
+
background_fill_primary_dark="*neutral_950", # Coolgray95 background for dark mode
|
64 |
+
slider_color="*primary_600", # Blue60
|
65 |
+
slider_color_dark="*primary_500", # Blue50
|
66 |
+
# Shadows
|
67 |
+
shadow_drop="0 1px 4px 0 rgb(0 0 0 / 0.1)",
|
68 |
+
shadow_drop_lg="0 2px 5px 0 rgb(0 0 0 / 0.1)",
|
69 |
+
# Block Labels
|
70 |
+
block_background_fill="white",
|
71 |
+
block_label_background_fill="white", # same color as blockback gound fill
|
72 |
+
block_label_radius="*radius_md",
|
73 |
+
block_label_text_size="*text_md",
|
74 |
+
block_label_text_weight="600",
|
75 |
+
block_label_text_color="black",
|
76 |
+
block_label_text_color_dark="white",
|
77 |
+
block_title_radius="*block_label_radius",
|
78 |
+
block_title_background_fill="*block_label_background_fill",
|
79 |
+
block_title_text_weight="600",
|
80 |
+
block_title_text_color="black",
|
81 |
+
block_title_text_color_dark="white",
|
82 |
+
block_label_margin="*spacing_md",
|
83 |
+
# Inputs
|
84 |
+
input_background_fill="white",
|
85 |
+
input_background_fill_dark="*block-background-fill",
|
86 |
+
input_border_color="*neutral_100",
|
87 |
+
input_shadow="*shadow_drop",
|
88 |
+
input_shadow_focus="*shadow_drop_lg",
|
89 |
+
checkbox_shadow="none",
|
90 |
+
# Buttons
|
91 |
+
shadow_spread="6px",
|
92 |
+
button_primary_shadow="*shadow_drop_lg",
|
93 |
+
button_primary_shadow_hover="*shadow_drop_lg",
|
94 |
+
button_primary_shadow_active="*shadow_inset",
|
95 |
+
button_secondary_shadow="*shadow_drop_lg",
|
96 |
+
button_secondary_shadow_hover="*shadow_drop_lg",
|
97 |
+
button_secondary_shadow_active="*shadow_inset",
|
98 |
+
checkbox_label_shadow="*shadow_drop_lg",
|
99 |
+
button_primary_background_fill="*primary_600",
|
100 |
+
button_primary_background_fill_hover="*primary_500",
|
101 |
+
button_primary_background_fill_hover_dark="*primary_500",
|
102 |
+
button_primary_text_color="white",
|
103 |
+
button_secondary_background_fill="white",
|
104 |
+
button_secondary_background_fill_hover="*neutral_100",
|
105 |
+
button_secondary_background_fill_dark="*neutral_800", # Secondary cool gray 80
|
106 |
+
button_secondary_background_fill_hover_dark="*primary_500",
|
107 |
+
button_secondary_text_color="*neutral_800",
|
108 |
+
button_cancel_background_fill="*button_secondary_background_fill",
|
109 |
+
button_cancel_background_fill_hover="*button_secondary_background_fill_hover",
|
110 |
+
button_cancel_background_fill_hover_dark="*button_secondary_background_fill_hover",
|
111 |
+
button_cancel_text_color="*button_secondary_text_color",
|
112 |
+
checkbox_label_background_fill_selected="*primary_200",
|
113 |
+
checkbox_label_background_fill_selected_dark="*primary_500",
|
114 |
+
checkbox_border_width="1px",
|
115 |
+
checkbox_border_color="*neutral_200",
|
116 |
+
checkbox_background_color_dark="*neutral_700", # Jan 18 test to fix checkbox, radio button background color
|
117 |
+
checkbox_background_color_selected="*primary_600",
|
118 |
+
checkbox_background_color_selected_dark="*primary_500",
|
119 |
+
checkbox_border_color_focus="*primary_600",
|
120 |
+
checkbox_border_color_focus_dark="*primary_500",
|
121 |
+
checkbox_border_color_selected="*primary_600",
|
122 |
+
checkbox_border_color_selected_dark="*primary_500",
|
123 |
+
checkbox_label_text_color_selected="black",
|
124 |
+
# Borders
|
125 |
+
block_border_width="1px", # test example border
|
126 |
+
panel_border_width="1px",
|
127 |
+
# Chatbubble related colors
|
128 |
+
# light
|
129 |
+
# color_accent = "*secondary_400",
|
130 |
+
border_color_accent_subdued="*color_accent_soft", # chatbubble human border color, use Blue 20 as an accent color
|
131 |
+
color_accent_soft="*secondary_200", # chatbubble human color
|
132 |
+
# darkmode
|
133 |
+
# chatbubble human border color in darkmode, use Blue 20 as an accent color
|
134 |
+
border_color_accent_subdued_dark="*secondary_500",
|
135 |
+
color_accent_soft_dark="*secondary_500", # chatbubble human color in dark mode
|
136 |
+
# Chatbot related font
|
137 |
+
chatbot_text_size="*text_md", # make it larger
|
138 |
+
# additional dark mode related tweaks:
|
139 |
+
# block_background_fill_dark="*neutral_950", # Jan 18 test coolgray95 background for dark mode
|
140 |
+
block_label_background_fill_dark="*neutral_800", # same color as blockback gound fill
|
141 |
+
block_title_background_fill_dark="*block_label_background_fill",
|
142 |
+
# input_background_fill_dark="*neutral_800", #This attribute help match fill color cool gray 80 to match background
|
143 |
+
# however cause the problem for the general theme.
|
144 |
+
# input_shadow_dark="*shadow_drop", #Test if it could make the border without the color
|
145 |
+
# input_border_color_dark="*neutral_200",#add attribute for border Jan 18
|
146 |
+
checkbox_border_color_dark="*neutral_600", # Jan 18 test to fix border
|
147 |
+
)
|
src/theme/research_monochrome.py
ADDED
@@ -0,0 +1,152 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
"""IBM Research Monochrome theme for gradio demos.
|
2 |
+
|
3 |
+
This version is a variation of CarbonSoft style, where the primary button is dark gray to create monochrome style. This
|
4 |
+
version uses the style from Research demos such as Bee to make it more playful with rounded corners, a larger font
|
5 |
+
family to enhance readability, and the IBM Cool Gray color palette for better consistency with other IBM Research demos,
|
6 |
+
such as Bee.
|
7 |
+
"""
|
8 |
+
|
9 |
+
import gradio as gr
|
10 |
+
from gradio.themes.utils import sizes
|
11 |
+
|
12 |
+
theme = gr.themes.Base(
|
13 |
+
primary_hue=gr.themes.Color(
|
14 |
+
c100="#EDF5FF",
|
15 |
+
c200="#D0E2FF",
|
16 |
+
c300="#A6C8FF",
|
17 |
+
c400="#78A9FF",
|
18 |
+
c50="#F9F9FB",
|
19 |
+
c500="#4589FF",
|
20 |
+
c600="#0F62FE",
|
21 |
+
c700="#0043CE",
|
22 |
+
c800="#002D9C",
|
23 |
+
c900="#001D6C",
|
24 |
+
c950="#001141",
|
25 |
+
),
|
26 |
+
secondary_hue=gr.themes.Color(
|
27 |
+
c100="#EDF5FF",
|
28 |
+
c200="#D0E2FF",
|
29 |
+
c300="#A6C8FF",
|
30 |
+
c400="#78A9FF",
|
31 |
+
c50="#F9F9FB",
|
32 |
+
c500="#4589FF",
|
33 |
+
c600="#0F62FE",
|
34 |
+
c700="#0043CE",
|
35 |
+
c800="#002D9C",
|
36 |
+
c900="#001D6C",
|
37 |
+
c950="#001141",
|
38 |
+
),
|
39 |
+
neutral_hue=gr.themes.Color(
|
40 |
+
c100="#F2F4F8",
|
41 |
+
c200="#DDE1E6",
|
42 |
+
c300="#C1C7CD",
|
43 |
+
c400="#A2A9B0",
|
44 |
+
c50="#F9F9FB",
|
45 |
+
c500="#878D96",
|
46 |
+
c600="#697077",
|
47 |
+
c700="#4D5358",
|
48 |
+
c800="#393939",
|
49 |
+
c900="#21272A",
|
50 |
+
c950="#121619",
|
51 |
+
),
|
52 |
+
spacing_size=sizes.spacing_md, # change spacing to default size
|
53 |
+
radius_size=sizes.radius_md, # change spacing to default size and Keep Radius to make demo feel more playful
|
54 |
+
text_size=sizes.text_md, # change fontsize to default size
|
55 |
+
# spacing_size: sizes.Size | str = sizes.spacing_md, #change spacing to default size
|
56 |
+
# radius_size: sizes.Size | str = sizes.radius_md, #change spacing to default size and Keep Radius to make
|
57 |
+
# demo feel more playful
|
58 |
+
# text_size: sizes.Size | str = sizes.text_lg, #change fontsize to default size
|
59 |
+
font=["IBM Plex Sans", "ui-sans-serif", "system-ui", "sans-serif"], # update font
|
60 |
+
font_mono=["IBM Plex Mono", "ui-monospace", "Consolas", "monospace"], # update font
|
61 |
+
).set(
|
62 |
+
# Colors
|
63 |
+
background_fill_primary="*neutral_100", # Coolgray10 background
|
64 |
+
background_fill_primary_dark="*neutral_950", # Coolgray95 background for dark mode
|
65 |
+
# Change blue to black to create monochrome style
|
66 |
+
slider_color="*neutral_900",
|
67 |
+
slider_color_dark="*primary_500",
|
68 |
+
# Shadows
|
69 |
+
shadow_drop="0 1px 4px 0 rgb(0 0 0 / 0.1)",
|
70 |
+
shadow_drop_lg="0 2px 5px 0 rgb(0 0 0 / 0.1)",
|
71 |
+
# Block Labels
|
72 |
+
block_background_fill="white",
|
73 |
+
block_label_background_fill="white", # same color as blockback gound fill
|
74 |
+
block_label_radius="*radius_md",
|
75 |
+
block_label_text_size="*text_md",
|
76 |
+
block_label_text_weight="600",
|
77 |
+
block_label_text_color="black",
|
78 |
+
block_label_text_color_dark="white",
|
79 |
+
block_title_radius="*block_label_radius",
|
80 |
+
block_title_background_fill="*block_label_background_fill",
|
81 |
+
block_title_text_weight="400",
|
82 |
+
block_title_text_color="black",
|
83 |
+
block_title_text_color_dark="white",
|
84 |
+
block_label_margin="*spacing_md",
|
85 |
+
# Inputs
|
86 |
+
input_background_fill="white",
|
87 |
+
input_background_fill_dark="*block-background-fill",
|
88 |
+
input_border_color="*neutral_100",
|
89 |
+
input_shadow="*shadow_drop",
|
90 |
+
input_shadow_dark="0 1px 4px #000",
|
91 |
+
input_shadow_focus="*shadow_drop_lg",
|
92 |
+
checkbox_shadow="none",
|
93 |
+
# Buttons
|
94 |
+
shadow_spread="6px",
|
95 |
+
button_primary_shadow="*shadow_drop_lg",
|
96 |
+
button_primary_shadow_hover="*shadow_drop_lg",
|
97 |
+
button_primary_shadow_active="*shadow_inset",
|
98 |
+
button_secondary_shadow="*shadow_drop_lg",
|
99 |
+
button_secondary_shadow_hover="*shadow_drop_lg",
|
100 |
+
button_secondary_shadow_active="*shadow_inset",
|
101 |
+
checkbox_label_shadow="*shadow_drop_lg",
|
102 |
+
# Change blue to black to create monochrome style
|
103 |
+
button_primary_background_fill="*neutral_900",
|
104 |
+
button_primary_background_fill_dark="*neutral_600",
|
105 |
+
button_primary_background_fill_hover="*neutral_700",
|
106 |
+
button_primary_background_fill_hover_dark="*primary_500", # hover to be blue
|
107 |
+
button_primary_text_color="white",
|
108 |
+
button_secondary_background_fill="white",
|
109 |
+
button_secondary_background_fill_hover="*neutral_100",
|
110 |
+
button_secondary_background_fill_dark="*neutral_800", # Secondary cool gray 80
|
111 |
+
button_secondary_background_fill_hover_dark="*primary_500",
|
112 |
+
button_secondary_text_color="*neutral_800",
|
113 |
+
button_cancel_background_fill="*button_secondary_background_fill",
|
114 |
+
button_cancel_background_fill_hover="*button_secondary_background_fill_hover",
|
115 |
+
button_cancel_background_fill_hover_dark="*button_secondary_background_fill_hover",
|
116 |
+
button_cancel_text_color="*button_secondary_text_color",
|
117 |
+
checkbox_label_background_fill_selected="*primary_200",
|
118 |
+
checkbox_label_background_fill_selected_dark="*primary_500",
|
119 |
+
checkbox_border_width="1px",
|
120 |
+
checkbox_border_color="*neutral_200",
|
121 |
+
checkbox_background_color_dark="*neutral_700", # Jan 18 test to fix checkbox, radio button background color
|
122 |
+
checkbox_background_color_selected="*primary_600",
|
123 |
+
checkbox_background_color_selected_dark="*primary_500",
|
124 |
+
checkbox_border_color_focus="*primary_600",
|
125 |
+
checkbox_border_color_focus_dark="*primary_500",
|
126 |
+
checkbox_border_color_selected="*primary_600",
|
127 |
+
checkbox_border_color_selected_dark="*primary_500",
|
128 |
+
checkbox_label_text_color_selected="black",
|
129 |
+
# Borders
|
130 |
+
block_border_width="1px", # test example border
|
131 |
+
panel_border_width="1px",
|
132 |
+
# Chatbubble related colors
|
133 |
+
# light
|
134 |
+
# color_accent = "*secondary_400",
|
135 |
+
border_color_accent_subdued="*color_accent_soft", # chatbubble human border color, use Blue 20 as an accent color
|
136 |
+
color_accent_soft="*secondary_200", # chatbubble human color
|
137 |
+
# darkmode
|
138 |
+
# chatbubble human border color in darkmode, use Blue 20 as an accent color
|
139 |
+
border_color_accent_subdued_dark="*secondary_500",
|
140 |
+
color_accent_soft_dark="*secondary_500", # chatbubble human color in dark mode
|
141 |
+
# Chatbot related font
|
142 |
+
chatbot_text_size="*text_md", # make it larger
|
143 |
+
# additional dark mode related tweaks:
|
144 |
+
# block_background_fill_dark="*neutral_950", # Jan 18 test coolgray95 background for dark mode
|
145 |
+
block_label_background_fill_dark="*neutral_800", # same color as blockback gound fill
|
146 |
+
block_title_background_fill_dark="*block_label_background_fill",
|
147 |
+
# input_background_fill_dark="*neutral_800", #This attribute help match fill color cool gray 80 to match background
|
148 |
+
# however cause the problem for the general theme.
|
149 |
+
# input_shadow_dark="*shadow_drop", #Test if it could make the border without the color
|
150 |
+
# input_border_color_dark="*neutral_200",#add attribute for border Jan 18
|
151 |
+
checkbox_border_color_dark="*neutral_600", # Jan 18 test to fix border
|
152 |
+
)
|