WorkTimer's picture
Upload folder using huggingface_hub
1e1e45e verified
import gradio as gr
from app import demo as app
import os
_docs = {'NiiVueViewer': {'description': 'Renders NIfTI (.nii / .nii.gz) volumes in the browser using NiiVue (WebGL).\nPass a list of file paths: first entry is the main volume, second (optional)\nis a segmentation overlay.', 'members': {'__init__': {'value': {'type': 'list[str] | None', 'default': 'value = None', 'description': 'List of NIfTI file paths. First path = main volume; second path'}, 'seg_labels': {'type': 'dict[int, str] | None', 'default': 'value = None', 'description': 'Mapping of integer label value → display name for the'}, 'label': {'type': 'str | I18nData | None', 'default': 'value = None', 'description': 'Label displayed above the component.'}, 'every': {'type': "'Timer | float | None'", 'default': 'value = None', 'description': 'Timer or interval (seconds) to periodically refresh a dynamic value.'}, 'inputs': {'type': "'Component | Sequence[Component] | set[Component] | None'", 'default': 'value = None', 'description': 'Components whose changes trigger a value recalculation.'}, 'show_label': {'type': 'bool | None', 'default': 'value = None', 'description': 'Whether to display the label.'}, 'container': {'type': 'bool', 'default': 'value = True', 'description': 'Wrap in a padded container.'}, 'scale': {'type': 'int | None', 'default': 'value = None', 'description': 'Relative width when placed in a Row.'}, 'min_width': {'type': 'int', 'default': 'value = 160', 'description': 'Minimum pixel width.'}, 'visible': {'type': "bool | Literal['hidden']", 'default': 'value = True', 'description': 'Visibility of the component.'}, 'elem_id': {'type': 'str | None', 'default': 'value = None', 'description': "HTML id attribute for the component's root element."}, 'elem_classes': {'type': 'list[str] | str | None', 'default': 'value = None', 'description': "HTML class(es) for the component's root element."}, 'render': {'type': 'bool', 'default': 'value = True', 'description': 'If False, defer rendering until later.'}, 'key': {'type': 'int | str | tuple[int | str, ...] | None', 'default': 'value = None', 'description': 'Stable key for gr.render() re-render identity.'}, 'preserved_by_key': {'type': 'list[str] | str | None', 'default': 'value = "value"', 'description': 'Constructor parameters preserved across re-renders.'}}, 'postprocess': {'value': {'type': 'list[str]| None', 'description': 'List of NIfTI file paths (absolute or relative to cwd).'}}, 'preprocess': {'return': {'type': 'list[str]| None', 'description': 'List of local file paths, or None.'}, 'value': None}}, 'events': {'change': {'type': None, 'default': None, 'description': 'Triggered when the value of the NiiVueViewer changes either because of user input (e.g. a user types in a textbox) OR because of a function update (e.g. an image receives a value from the output of an event trigger). See `.input()` for a listener that is only triggered by user input.'}}}, '__meta__': {'additional_interfaces': {}, 'user_fn_refs': {'NiiVueViewer': []}}}
abs_path = os.path.join(os.path.dirname(__file__), "css.css")
with gr.Blocks(
css=abs_path,
theme=gr.themes.Default(
font_mono=[
gr.themes.GoogleFont("Inconsolata"),
"monospace",
],
),
) as demo:
gr.Markdown(
"""
# `gradio_niivueviewer`
<div style="display: flex; gap: 7px;">
<img alt="Static Badge" src="https://img.shields.io/badge/version%20-%200.0.1%20-%20orange">
</div>
Python library for easily interacting with trained machine learning models
""", elem_classes=["md-custom"], header_links=True)
app.render()
gr.Markdown(
"""
## Installation
```bash
pip install gradio_niivueviewer
```
## Usage
```python
import os
import gradio as gr
from gradio_niivueviewer import NiiVueViewer
ROOT = os.path.abspath(os.path.join(os.path.dirname(__file__), "../.."))
nii_path = os.path.join(ROOT, "input/FLARE_02510_0000.nii.gz")
seg_path = os.path.join(ROOT, "output/FLARE_02510.nii.gz")
# FLARE22 standard organ labels
FLARE_LABELS = {
1: "Liver",
2: "Right Kidney",
3: "Spleen",
4: "Pancreas",
5: "Aorta",
6: "IVC",
7: "Right Adrenal",
8: "Left Adrenal",
9: "Gallbladder",
10: "Esophagus",
11: "Stomach",
12: "Duodenum",
13: "Left Kidney",
}
with gr.Blocks(title="NiiVue Medical Image Viewer") as demo:
gr.Markdown("## NiiVue — FLARE_02510")
NiiVueViewer(value=[nii_path, seg_path], seg_labels=FLARE_LABELS)
if __name__ == "__main__":
demo.launch(
server_port=7870,
allowed_paths=[os.path.dirname(nii_path), os.path.dirname(seg_path)],
)
```
""", elem_classes=["md-custom"], header_links=True)
gr.Markdown("""
## `NiiVueViewer`
### Initialization
""", elem_classes=["md-custom"], header_links=True)
gr.ParamViewer(value=_docs["NiiVueViewer"]["members"]["__init__"], linkify=[])
gr.Markdown("### Events")
gr.ParamViewer(value=_docs["NiiVueViewer"]["events"], linkify=['Event'])
gr.Markdown("""
### User function
The impact on the users predict function varies depending on whether the component is used as an input or output for an event (or both).
- When used as an Input, the component only impacts the input signature of the user function.
- When used as an output, the component only impacts the return signature of the user function.
The code snippet below is accurate in cases where the component is used as both an input and an output.
- **As input:** Is passed, list of local file paths, or None.
- **As output:** Should return, list of NIfTI file paths (absolute or relative to cwd).
```python
def predict(
value: list[str]| None
) -> list[str]| None:
return value
```
""", elem_classes=["md-custom", "NiiVueViewer-user-fn"], header_links=True)
demo.load(None, js=r"""function() {
const refs = {};
const user_fn_refs = {
NiiVueViewer: [], };
requestAnimationFrame(() => {
Object.entries(user_fn_refs).forEach(([key, refs]) => {
if (refs.length > 0) {
const el = document.querySelector(`.${key}-user-fn`);
if (!el) return;
refs.forEach(ref => {
el.innerHTML = el.innerHTML.replace(
new RegExp("\\b"+ref+"\\b", "g"),
`<a href="#h-${ref.toLowerCase()}">${ref}</a>`
);
})
}
})
Object.entries(refs).forEach(([key, refs]) => {
if (refs.length > 0) {
const el = document.querySelector(`.${key}`);
if (!el) return;
refs.forEach(ref => {
el.innerHTML = el.innerHTML.replace(
new RegExp("\\b"+ref+"\\b", "g"),
`<a href="#h-${ref.toLowerCase()}">${ref}</a>`
);
})
}
})
})
}
""")
demo.launch()