pangyuteng commited on
Commit
ac6e0fb
·
1 Parent(s): a05675c

added barebones example for viewer with slider

Browse files
.gitattributes CHANGED
@@ -33,3 +33,4 @@ saved_model/**/* filter=lfs diff=lfs merge=lfs -text
33
  *.zip filter=lfs diff=lfs merge=lfs -text
34
  *.zst filter=lfs diff=lfs merge=lfs -text
35
  *tfevents* filter=lfs diff=lfs merge=lfs -text
 
 
33
  *.zip filter=lfs diff=lfs merge=lfs -text
34
  *.zst filter=lfs diff=lfs merge=lfs -text
35
  *tfevents* filter=lfs diff=lfs merge=lfs -text
36
+ *.nii.gz filter=lfs diff=lfs merge=lfs -text
NOTES.md CHANGED
@@ -7,4 +7,6 @@ https://github.com/gradio-app/gradio/issues/4511
7
 
8
  https://www.gradio.app/guides/custom-components-in-five-minutes
9
 
10
- https://www.gradio.app/guides/pdf-component-example
 
 
 
7
 
8
  https://www.gradio.app/guides/custom-components-in-five-minutes
9
 
10
+ https://www.gradio.app/guides/pdf-component-example
11
+
12
+ https://github.com/gradio-app/gradio/issues/6821
README.md CHANGED
@@ -1,5 +1,5 @@
1
  ---
2
- title: Papaya Image Viewer
3
  emoji: 🏢
4
  colorFrom: pink
5
  colorTo: purple
 
1
  ---
2
+ title: Not-A-Papaya Image Viewer
3
  emoji: 🏢
4
  colorFrom: pink
5
  colorTo: purple
app.py CHANGED
@@ -1,22 +1,124 @@
1
- import gradio as gr
2
 
3
- head = f"""
4
  <script src="https://cdn.jsdelivr.net/npm/[email protected]/release/current/standard/papaya.min.js"></script>
5
  <link href="https://cdn.jsdelivr.net/npm/[email protected]/release/current/standard/papaya.min.css" rel="stylesheet">
6
-
7
- """
8
- papayadiv = f"""
9
  <div class="papaya" data-params="params"></div>
 
 
 
 
 
10
  """
11
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
12
  with gr.Blocks() as demo:
13
- input_mic = gr.HTML(papayadiv)
14
- demo.load(None,None,None,_js=head)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
15
 
16
- demo.launch(server_name='0.0.0.0')
 
 
17
 
18
  '''
19
- docker run --runtime=nvidia -it -u $(id -u):$(id -g) -p 7860:7860 -w $PWD -v /mnt:/mnt -v ~/.cache:/.cache pangyuteng/ml:tf-hf-latest bash
 
 
20
 
21
  python app.py
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
22
  '''
 
 
1
 
2
+ """
3
  <script src="https://cdn.jsdelivr.net/npm/[email protected]/release/current/standard/papaya.min.js"></script>
4
  <link href="https://cdn.jsdelivr.net/npm/[email protected]/release/current/standard/papaya.min.css" rel="stylesheet">
 
 
 
5
  <div class="papaya" data-params="params"></div>
6
+
7
+ TODO:
8
+ https://www.gradio.app/guides/custom-components-in-five-minutes
9
+ https://github.com/gradio-app/gradio/issues/6821
10
+
11
  """
12
 
13
+
14
+ import os
15
+ import gradio as gr
16
+
17
+ import numpy as np
18
+ import PIL
19
+ import SimpleITK as sitk
20
+
21
+ MINVAL,MAXVAL = -1000,1000
22
+
23
+ THIS_DIR = os.path.dirname(os.path.abspath(__file__))
24
+ example_image_path = os.path.join(THIS_DIR,'files','promis12-test-Case00.nii.gz') # from promise12
25
+ example_mask_path = os.path.join(THIS_DIR,'files','promis12-test-Case00-mask.nii.gz') # from promise12
26
+
27
+ mydict = {}
28
+
29
+ def load_data(input1,input2):
30
+ global mydict
31
+ mydict['image_path'] = input1.name
32
+ mydict['mask_path'] = input2.name
33
+ img = sitk.GetArrayFromImage(sitk.ReadImage(mydict['image_path']))
34
+ mask = sitk.GetArrayFromImage(sitk.ReadImage(mydict['mask_path']))
35
+ img = ((img-MINVAL)/(MAXVAL-MINVAL)).clip(0,1)*255
36
+ mydict['img'] = img.astype(np.uint8)
37
+ mydict['mask'] = mask.astype(np.uint8)
38
+ return f"Welcome to Gradio, {input1.name,input2.name}!"
39
+
40
+ def render(x, state):
41
+ print(x,state)
42
+ if len(mydict)==4:
43
+ if x > mydict['img'].shape[0]-1:
44
+ x = mydict['img'].shape[0]-1
45
+ if x < 0:
46
+ x = 0
47
+ img = mydict['img'][x,:,:]
48
+ mask = mydict['mask'][x,:,:]
49
+ im = PIL.Image.fromarray(img)
50
+ value = (im,[(mask,"prostate")])
51
+ else:
52
+ im = np.zeros(10,10)
53
+ value = (im,[])
54
+
55
+ return value,f'z-value: {x}'
56
+
57
  with gr.Blocks() as demo:
58
+ with gr.Column():
59
+ gr.Markdown(
60
+ """
61
+ # Hello World!
62
+ Start typing below to see the output.
63
+ """)
64
+ with gr.Row():
65
+ image_file = gr.File()
66
+ mask_file = gr.File()
67
+
68
+ with gr.Column():
69
+ btn = gr.Button("upload")
70
+ slider = gr.Slider(0, 100, step=1)
71
+ state = gr.State(value=0)
72
+ out = gr.Textbox(label='text-comment')
73
+ with gr.Row():
74
+ myimage = gr.AnnotatedImage(height=512)
75
+ gr.Examples(
76
+ [[example_image_path,example_mask_path]],
77
+ [image_file, mask_file],
78
+ )
79
+ btn.click(fn=load_data,
80
+ inputs=[image_file,mask_file], outputs=out,
81
+ )
82
+ slider.change(
83
+ render,
84
+ inputs=[slider, state],
85
+ outputs=[myimage,out],
86
+ api_name="hohoho"
87
+ )
88
 
89
+ if __name__ == "__main__":
90
+ #demo.launch()
91
+ demo.launch(server_name='0.0.0.0')
92
 
93
  '''
94
+
95
+ docker run --runtime=nvidia -it -u $(id -u):$(id -g) -p 7860:7860 \
96
+ -w $PWD -v /mnt:/mnt -v ~/.cache:/.cache pangyuteng/ml:tf-hf-latest bash
97
 
98
  python app.py
99
+
100
+ '''
101
+
102
+ '''
103
+ import numpy as np
104
+ import SimpleITK as sitk
105
+
106
+ sys.path.append(os.path.dirname(utils_py_file))
107
+ from utils import inference
108
+
109
+ model = keras.models.load_model(model_file,compile=False)
110
+
111
+ def mainfunc(file_obj):
112
+ image_path = file_obj.name
113
+ tmpdir = os.path.dirname(image_path)
114
+ mask_path = os.path.join(tmpdir,"mask.nii.gz")
115
+ img_obj = sitk.ReadImage(image_path)
116
+
117
+ mask_obj = inference(model,img_obj)
118
+ sitk.WriteImage(mask_obj,mask_path)
119
+ return [image_path,mask_path]
120
+
121
+ if __name__ == "__main__":
122
+
123
+ demo.queue().launch(debug=True,show_api=True,share=False)
124
  '''
files/promis12-test-Case00-mask.nii.gz ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:14b159df22eb0b404a7a8851315a0269b37ad9bff1517577d681abb3514fc742
3
+ size 7774
files/promis12-test-Case00.nii.gz ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:813dda4c7e60436017707cf7d371c311a1401750681e338fe29dac6bb087185c
3
+ size 2832200