Spaces:
Sleeping
Sleeping
Commit
·
7b7636a
1
Parent(s):
4e4c0a1
Refactor main.py to reorganize functions and enhance demo interface with additional examples
Browse files
main.py
CHANGED
@@ -28,20 +28,52 @@ from inference_utils.inference import interactive_infer_image
|
|
28 |
gr.set_static_paths(["assets"])
|
29 |
|
30 |
|
31 |
-
def
|
32 |
-
|
33 |
-
|
34 |
-
for mask, color in zip(masks, colors):
|
35 |
-
overlay[mask > 0] = (overlay[mask > 0] * 0.4 + np.array(color) * 0.6).astype(
|
36 |
-
np.uint8
|
37 |
-
)
|
38 |
-
return Image.fromarray(overlay)
|
39 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
40 |
|
41 |
-
|
42 |
-
cmap = plt.get_cmap("tab10")
|
43 |
-
colors = [tuple(int(255 * val) for val in cmap(i)[:3]) for i in range(n)]
|
44 |
-
return colors
|
45 |
|
46 |
|
47 |
def init_model():
|
@@ -112,52 +144,20 @@ This Space is based on the [BiomedParse model](https://microsoft.github.io/Biome
|
|
112 |
"""
|
113 |
|
114 |
|
115 |
-
def
|
116 |
-
|
117 |
-
|
|
|
118 |
|
119 |
-
demo = gr.Interface(
|
120 |
-
fn=predict,
|
121 |
-
inputs=[
|
122 |
-
gr.Image(type="pil", label="Input Image"),
|
123 |
-
gr.Textbox(
|
124 |
-
label="Prompts",
|
125 |
-
placeholder="Enter prompts separated by commas (e.g., neoplastic cells, inflammatory cells)",
|
126 |
-
),
|
127 |
-
],
|
128 |
-
outputs=gr.Image(type="pil", label="Prediction"),
|
129 |
-
title="BiomedParse Demo",
|
130 |
-
description=description,
|
131 |
-
allow_flagging="never",
|
132 |
-
examples=[
|
133 |
-
["examples/144DME_as_F.jpeg", "edema"],
|
134 |
-
["examples/C3_EndoCV2021_00462.jpg", "polyp"],
|
135 |
-
["examples/CT-abdomen.png", "liver, pancreas, spleen"],
|
136 |
-
["examples/covid_1585.png", "left lung"],
|
137 |
-
["examples/covid_1585.png", "right lung"],
|
138 |
-
["examples/covid_1585.png", "COVID-19 infection"],
|
139 |
-
["examples/ISIC_0015551.jpg", "lesion"],
|
140 |
-
["examples/LIDC-IDRI-0140_143_280_CT_lung.png", "lung nodule"],
|
141 |
-
["examples/LIDC-IDRI-0140_143_280_CT_lung.png", "COVID-19 infection"],
|
142 |
-
[
|
143 |
-
"examples/Part_1_516_pathology_breast.png",
|
144 |
-
"connective tissue cells",
|
145 |
-
],
|
146 |
-
[
|
147 |
-
"examples/Part_1_516_pathology_breast.png",
|
148 |
-
"neoplastic cells",
|
149 |
-
],
|
150 |
-
[
|
151 |
-
"examples/Part_1_516_pathology_breast.png",
|
152 |
-
"neoplastic cells, inflammatory cells",
|
153 |
-
],
|
154 |
-
["examples/T0011.jpg", "optic disc"],
|
155 |
-
["examples/T0011.jpg", "optic cup"],
|
156 |
-
["examples/TCGA_HT_7856_19950831_8_MRI-FLAIR_brain.png", "glioma"],
|
157 |
-
],
|
158 |
-
)
|
159 |
|
160 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
161 |
|
162 |
|
163 |
if __name__ == "__main__":
|
|
|
28 |
gr.set_static_paths(["assets"])
|
29 |
|
30 |
|
31 |
+
def run():
|
32 |
+
global model
|
33 |
+
model = init_model()
|
|
|
|
|
|
|
|
|
|
|
34 |
|
35 |
+
demo = gr.Interface(
|
36 |
+
fn=predict,
|
37 |
+
inputs=[
|
38 |
+
gr.Image(type="pil", label="Input Image"),
|
39 |
+
gr.Textbox(
|
40 |
+
label="Prompts",
|
41 |
+
placeholder="Enter prompts separated by commas (e.g., neoplastic cells, inflammatory cells)",
|
42 |
+
),
|
43 |
+
],
|
44 |
+
outputs=gr.Image(type="pil", label="Prediction"),
|
45 |
+
title="BiomedParse Demo",
|
46 |
+
description=description,
|
47 |
+
allow_flagging="never",
|
48 |
+
examples=[
|
49 |
+
["examples/144DME_as_F.jpeg", "edema"],
|
50 |
+
["examples/C3_EndoCV2021_00462.jpg", "polyp"],
|
51 |
+
["examples/CT-abdomen.png", "liver, pancreas, spleen"],
|
52 |
+
["examples/covid_1585.png", "left lung"],
|
53 |
+
["examples/covid_1585.png", "right lung"],
|
54 |
+
["examples/covid_1585.png", "COVID-19 infection"],
|
55 |
+
["examples/ISIC_0015551.jpg", "lesion"],
|
56 |
+
["examples/LIDC-IDRI-0140_143_280_CT_lung.png", "lung nodule"],
|
57 |
+
["examples/LIDC-IDRI-0140_143_280_CT_lung.png", "COVID-19 infection"],
|
58 |
+
[
|
59 |
+
"examples/Part_1_516_pathology_breast.png",
|
60 |
+
"connective tissue cells",
|
61 |
+
],
|
62 |
+
[
|
63 |
+
"examples/Part_1_516_pathology_breast.png",
|
64 |
+
"neoplastic cells",
|
65 |
+
],
|
66 |
+
[
|
67 |
+
"examples/Part_1_516_pathology_breast.png",
|
68 |
+
"neoplastic cells, inflammatory cells",
|
69 |
+
],
|
70 |
+
["examples/T0011.jpg", "optic disc"],
|
71 |
+
["examples/T0011.jpg", "optic cup"],
|
72 |
+
["examples/TCGA_HT_7856_19950831_8_MRI-FLAIR_brain.png", "glioma"],
|
73 |
+
],
|
74 |
+
)
|
75 |
|
76 |
+
demo.launch(server_name="0.0.0.0", server_port=7860)
|
|
|
|
|
|
|
77 |
|
78 |
|
79 |
def init_model():
|
|
|
144 |
"""
|
145 |
|
146 |
|
147 |
+
def generate_colors(n):
|
148 |
+
cmap = plt.get_cmap("tab10")
|
149 |
+
colors = [tuple(int(255 * val) for val in cmap(i)[:3]) for i in range(n)]
|
150 |
+
return colors
|
151 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
152 |
|
153 |
+
def overlay_masks(image, masks, colors):
|
154 |
+
overlay = image.copy()
|
155 |
+
overlay = np.array(overlay, dtype=np.uint8)
|
156 |
+
for mask, color in zip(masks, colors):
|
157 |
+
overlay[mask > 0] = (overlay[mask > 0] * 0.4 + np.array(color) * 0.6).astype(
|
158 |
+
np.uint8
|
159 |
+
)
|
160 |
+
return Image.fromarray(overlay)
|
161 |
|
162 |
|
163 |
if __name__ == "__main__":
|