Spaces:
Sleeping
Sleeping
Create gradio_utils.py
Browse files- gradio_utils.py +66 -0
gradio_utils.py
ADDED
|
@@ -0,0 +1,66 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
|
| 3 |
+
|
| 4 |
+
def add_gradio_component(config_dict, component_key):
|
| 5 |
+
"""
|
| 6 |
+
Creates a gradio component for the component_key component, based on the config_dict dictionary of parameters
|
| 7 |
+
"""
|
| 8 |
+
if config_dict[component_key]["comp_type"] == "Text":
|
| 9 |
+
new_component = gr.Text(
|
| 10 |
+
label=config_dict[component_key]["label"], placeholder=config_dict[component_key]["label"]
|
| 11 |
+
)
|
| 12 |
+
elif config_dict[component_key]["comp_type"] == "Number":
|
| 13 |
+
new_component = gr.Number(
|
| 14 |
+
label=config_dict[component_key]["label"],
|
| 15 |
+
precision=config_dict[component_key]["precision"],
|
| 16 |
+
)
|
| 17 |
+
elif config_dict[component_key]["comp_type"] == "Dropdown":
|
| 18 |
+
new_component = gr.Dropdown(
|
| 19 |
+
label=config_dict[component_key]["label"], choices=config_dict[component_key]["cat_values"]
|
| 20 |
+
)
|
| 21 |
+
elif config_dict[component_key]["comp_type"] == "Image":
|
| 22 |
+
new_component = gr.Image(elem_classes="image-preview")
|
| 23 |
+
elif config_dict[component_key]["comp_type"] == "CheckboxGroup":
|
| 24 |
+
new_component = gr.CheckboxGroup(
|
| 25 |
+
label=config_dict[component_key]["label"], choices=config_dict[component_key]["cat_values"]
|
| 26 |
+
)
|
| 27 |
+
elif config_dict[component_key]["comp_type"] == "Plot":
|
| 28 |
+
new_component = gr.Plot(label=config_dict[component_key]["label"], type="matplotlib")
|
| 29 |
+
elif config_dict[component_key]["comp_type"] == "Dataframe":
|
| 30 |
+
new_component = gr.Dataframe(wrap=True, type="pandas")
|
| 31 |
+
else:
|
| 32 |
+
print(
|
| 33 |
+
f"Found component type {config_dict[component_key]['comp_type']} for {component_key}, which is not supported"
|
| 34 |
+
)
|
| 35 |
+
new_component = None
|
| 36 |
+
return new_component
|
| 37 |
+
|
| 38 |
+
|
| 39 |
+
def load_theme():
|
| 40 |
+
"""
|
| 41 |
+
Loads the Osium AI color theme
|
| 42 |
+
"""
|
| 43 |
+
osium_theme_colors = gr.themes.Color(
|
| 44 |
+
c50="#e4f3fa", # Dataframe background cell content - light mode only
|
| 45 |
+
c100="#e4f3fa", # Top corner of clear button in light mode + markdown text in dark mode
|
| 46 |
+
c200="#a1c6db", # Component borders
|
| 47 |
+
c300="#FFFFFF", #
|
| 48 |
+
c400="#e4f3fa", # Footer text
|
| 49 |
+
c500="#0c1538", # Text of component headers in light mode only
|
| 50 |
+
c600="#a1c6db", # Top corner of button in dark mode
|
| 51 |
+
c700="#475383", # Button text in light mode + component borders in dark mode
|
| 52 |
+
c800="#0c1538", # Markdown text in light mode
|
| 53 |
+
c900="#a1c6db", # Background of dataframe - dark mode
|
| 54 |
+
c950="#0c1538",
|
| 55 |
+
) # Background in dark mode only
|
| 56 |
+
# secondary color used for highlight box content when typing in light mode, and download option in dark mode
|
| 57 |
+
# primary color used for login button in dark mode
|
| 58 |
+
osium_theme = gr.themes.Default(primary_hue="cyan", secondary_hue="cyan", neutral_hue=osium_theme_colors)
|
| 59 |
+
|
| 60 |
+
css_styling = """#submit {background: #1eccd8}
|
| 61 |
+
#submit:hover {background: #a2f1f6}
|
| 62 |
+
.output-image, .input-image, .image-preview {height: 250px !important}
|
| 63 |
+
.output-plot {height: 250px !important}
|
| 64 |
+
#interpretation {height: 250px !important}"""
|
| 65 |
+
|
| 66 |
+
return osium_theme, css_styling
|