andreped commited on
Commit
5f4f6a7
1 Parent(s): a41b9f5

Added argparse to support setting cwd and disabling public link sharing

Browse files
Files changed (3) hide show
  1. README.md +25 -1
  2. app.py +14 -4
  3. neukit/gui.py +7 -5
README.md CHANGED
@@ -10,7 +10,7 @@ license: mit
10
  app_file: app.py
11
  ---
12
 
13
- <div align="center">
14
  <h1 align="center">neukit</h1>
15
  <h3 align="center">Automatic brain extraction and preoperative tumor segmentation from MRI</h3>
16
 
@@ -36,6 +36,8 @@ To access the live demo, click on the `Hugging Face` badge above. Below is a sna
36
 
37
  ## Development
38
 
 
 
39
  Alternatively, you can deploy the software locally. Note that this is only relevant for development purposes. Simply dockerize the app and run it:
40
 
41
  ```
@@ -45,6 +47,28 @@ docker run -it -p 7860:7860 neukit
45
 
46
  Then open `http://127.0.0.1:7860` in your favourite internet browser to view the demo.
47
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
48
  ## Citation
49
 
50
  If you found the tool useful in your research, please, cite the corresponding software paper:
 
10
  app_file: app.py
11
  ---
12
 
13
+ <div align="center">M
14
  <h1 align="center">neukit</h1>
15
  <h3 align="center">Automatic brain extraction and preoperative tumor segmentation from MRI</h3>
16
 
 
36
 
37
  ## Development
38
 
39
+ ### Docker
40
+
41
  Alternatively, you can deploy the software locally. Note that this is only relevant for development purposes. Simply dockerize the app and run it:
42
 
43
  ```
 
47
 
48
  Then open `http://127.0.0.1:7860` in your favourite internet browser to view the demo.
49
 
50
+ ### Python
51
+
52
+ It is also possible to run the app locally without Docker. Just setup a virtual environment and run the app.
53
+ Note that the current working directory would need to be adjusted based on where `neukit` is located on disk.
54
+
55
+ ```
56
+ git clone https://github.com/andreped/neukit.git
57
+ cd neukit/
58
+
59
+ virtualenv -ppython3 venv --clear
60
+ source venv/bin/activate
61
+ pip install -r requirements.txt
62
+
63
+ python app.py --cwd ./
64
+ ```
65
+
66
+ ## Troubleshooting
67
+
68
+ Note that due to `share=True` being enabled by default when launching the app,
69
+ internet access is required for the app to be launched. This can disabled by setting
70
+ the argument to `--share 0`.
71
+
72
  ## Citation
73
 
74
  If you found the tool useful in your research, please, cite the corresponding software paper:
app.py CHANGED
@@ -1,14 +1,24 @@
1
  from neukit.gui import WebUI
 
 
2
 
3
 
4
  def main():
5
- print("Launching demo...")
 
 
 
 
 
6
 
7
- # cwd = "/Users/andreped/workspace/neukit/" # local testing -> macOS
8
- cwd = "/home/user/app/" # production -> docker
 
 
9
 
10
  # initialize and run app
11
- app = WebUI(cwd=cwd)
 
12
  app.run()
13
 
14
 
 
1
  from neukit.gui import WebUI
2
+ from argparse import ArgumentParser
3
+ import os
4
 
5
 
6
  def main():
7
+ parser = ArgumentParser()
8
+ parser.add_argument("--cwd", type=str, default="/home/user/app/", help="Set current working directory (path to app.py).")
9
+ 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.")
10
+ args = parser.parse_args()
11
+
12
+ print("Current working directory:", args.cwd)
13
 
14
+ if not os.path.exists(args.cwd):
15
+ raise ValueError("Chosen 'cwd' is not a valid path!")
16
+ if not args.share in [0, 1]:
17
+ raise ValueError("The 'share' argument can only be set to 0 or 1 (boolean), but was:", args.share)
18
 
19
  # initialize and run app
20
+ print("Launching demo...")
21
+ app = WebUI(cwd=args.cwd, share=args.share)
22
  app.run()
23
 
24
 
neukit/gui.py CHANGED
@@ -1,4 +1,5 @@
1
  import gradio as gr
 
2
 
3
  from .inference import run_model
4
  from .utils import load_ct_to_numpy
@@ -7,7 +8,7 @@ from .utils import nifti_to_glb
7
 
8
 
9
  class WebUI:
10
- def __init__(self, model_name: str = None, cwd: str = "/home/user/app/"):
11
  # global states
12
  self.images = []
13
  self.pred_images = []
@@ -17,6 +18,7 @@ class WebUI:
17
 
18
  self.model_name = model_name
19
  self.cwd = cwd
 
20
 
21
  self.class_name = "meningioma" # default
22
  self.class_names = {
@@ -64,7 +66,7 @@ class WebUI:
64
  path = mesh_file_name.name
65
  run_model(
66
  path,
67
- model_path=self.cwd + "resources/models/",
68
  task=self.class_names[self.class_name],
69
  name=self.result_names[self.class_name],
70
  )
@@ -127,8 +129,8 @@ class WebUI:
127
  with gr.Row():
128
  gr.Examples(
129
  examples=[
130
- self.cwd + "RegLib_C01_1.nii",
131
- self.cwd + "RegLib_C01_2.nii",
132
  ],
133
  inputs=file_output,
134
  outputs=file_output,
@@ -164,4 +166,4 @@ class WebUI:
164
  # https://gradio.app/sharing-your-app/
165
  # inference times > 60 seconds -> need queue():
166
  # https://github.com/tloen/alpaca-lora/issues/60#issuecomment-1510006062
167
- demo.queue().launch(server_name="0.0.0.0", server_port=7860, share=True)
 
1
  import gradio as gr
2
+ import os
3
 
4
  from .inference import run_model
5
  from .utils import load_ct_to_numpy
 
8
 
9
 
10
  class WebUI:
11
+ def __init__(self, model_name: str = None, cwd: str = "/home/user/app/", share: int = 1):
12
  # global states
13
  self.images = []
14
  self.pred_images = []
 
18
 
19
  self.model_name = model_name
20
  self.cwd = cwd
21
+ self.share = share
22
 
23
  self.class_name = "meningioma" # default
24
  self.class_names = {
 
66
  path = mesh_file_name.name
67
  run_model(
68
  path,
69
+ model_path=os.path.join(self.cwd, "resources/models/"),
70
  task=self.class_names[self.class_name],
71
  name=self.result_names[self.class_name],
72
  )
 
129
  with gr.Row():
130
  gr.Examples(
131
  examples=[
132
+ os.path.join(self.cwd, "RegLib_C01_1.nii"),
133
+ os.path.join(self.cwd, "RegLib_C01_2.nii"),
134
  ],
135
  inputs=file_output,
136
  outputs=file_output,
 
166
  # https://gradio.app/sharing-your-app/
167
  # inference times > 60 seconds -> need queue():
168
  # https://github.com/tloen/alpaca-lora/issues/60#issuecomment-1510006062
169
+ demo.queue().launch(server_name="0.0.0.0", server_port=7860, share=self.share)