Skip to content

Commit

Permalink
Added argparse to support setting cwd and disabling public link sharing
Browse files Browse the repository at this point in the history
  • Loading branch information
andreped committed Jun 7, 2023
1 parent a41b9f5 commit 5f4f6a7
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 10 deletions.
26 changes: 25 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ license: mit
app_file: app.py
---

<div align="center">
<div align="center">M
<h1 align="center">neukit</h1>
<h3 align="center">Automatic brain extraction and preoperative tumor segmentation from MRI</h3>

Expand All @@ -36,6 +36,8 @@ To access the live demo, click on the `Hugging Face` badge above. Below is a sna

## Development

### Docker

Alternatively, you can deploy the software locally. Note that this is only relevant for development purposes. Simply dockerize the app and run it:

```
Expand All @@ -45,6 +47,28 @@ docker run -it -p 7860:7860 neukit

Then open `http://127.0.0.1:7860` in your favourite internet browser to view the demo.

### Python

It is also possible to run the app locally without Docker. Just setup a virtual environment and run the app.
Note that the current working directory would need to be adjusted based on where `neukit` is located on disk.

```
git clone https://github.com/andreped/neukit.git
cd neukit/
virtualenv -ppython3 venv --clear
source venv/bin/activate
pip install -r requirements.txt
python app.py --cwd ./
```

## Troubleshooting

Note that due to `share=True` being enabled by default when launching the app,
internet access is required for the app to be launched. This can disabled by setting
the argument to `--share 0`.

## Citation

If you found the tool useful in your research, please, cite the corresponding software paper:
Expand Down
18 changes: 14 additions & 4 deletions app.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,24 @@
from neukit.gui import WebUI
from argparse import ArgumentParser
import os


def main():
print("Launching demo...")
parser = ArgumentParser()
parser.add_argument("--cwd", type=str, default="/home/user/app/", help="Set current working directory (path to app.py).")
parser.add_argument("--share", type=int, default=1, help="Whether to enable the app to be accessible online -> setups a public link which requires internet access.")
args = parser.parse_args()

print("Current working directory:", args.cwd)

# cwd = "/Users/andreped/workspace/neukit/" # local testing -> macOS
cwd = "/home/user/app/" # production -> docker
if not os.path.exists(args.cwd):
raise ValueError("Chosen 'cwd' is not a valid path!")
if not args.share in [0, 1]:
raise ValueError("The 'share' argument can only be set to 0 or 1 (boolean), but was:", args.share)

# initialize and run app
app = WebUI(cwd=cwd)
print("Launching demo...")
app = WebUI(cwd=args.cwd, share=args.share)
app.run()


Expand Down
12 changes: 7 additions & 5 deletions neukit/gui.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import gradio as gr
import os

from .inference import run_model
from .utils import load_ct_to_numpy
Expand All @@ -7,7 +8,7 @@


class WebUI:
def __init__(self, model_name: str = None, cwd: str = "/home/user/app/"):
def __init__(self, model_name: str = None, cwd: str = "/home/user/app/", share: int = 1):
# global states
self.images = []
self.pred_images = []
Expand All @@ -17,6 +18,7 @@ def __init__(self, model_name: str = None, cwd: str = "/home/user/app/"):

self.model_name = model_name
self.cwd = cwd
self.share = share

self.class_name = "meningioma" # default
self.class_names = {
Expand Down Expand Up @@ -64,7 +66,7 @@ def process(self, mesh_file_name):
path = mesh_file_name.name
run_model(
path,
model_path=self.cwd + "resources/models/",
model_path=os.path.join(self.cwd, "resources/models/"),
task=self.class_names[self.class_name],
name=self.result_names[self.class_name],
)
Expand Down Expand Up @@ -127,8 +129,8 @@ def run(self):
with gr.Row():
gr.Examples(
examples=[
self.cwd + "RegLib_C01_1.nii",
self.cwd + "RegLib_C01_2.nii",
os.path.join(self.cwd, "RegLib_C01_1.nii"),
os.path.join(self.cwd, "RegLib_C01_2.nii"),
],
inputs=file_output,
outputs=file_output,
Expand Down Expand Up @@ -164,4 +166,4 @@ def run(self):
# https://gradio.app/sharing-your-app/
# inference times > 60 seconds -> need queue():
# https://github.com/tloen/alpaca-lora/issues/60#issuecomment-1510006062
demo.queue().launch(server_name="0.0.0.0", server_port=7860, share=True)
demo.queue().launch(server_name="0.0.0.0", server_port=7860, share=self.share)

0 comments on commit 5f4f6a7

Please sign in to comment.