dwb2023 commited on
Commit
9f9688c
·
verified ·
1 Parent(s): e38f582

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +53 -55
app.py CHANGED
@@ -1,3 +1,4 @@
 
1
  import re
2
  import PIL.Image
3
  import gradio as gr
@@ -36,12 +37,62 @@ def parse_segmentation(input_image, input_text, max_new_tokens=100):
36
  has_annotations = bool(annotated_img[1])
37
  return annotated_img
38
 
39
- INTRO_TEXT="🔬🧠 CellVision AI -- Intelligent Cell Imaging Analysis 🤖🧫"
40
- IMAGE_PROMPT="""
41
  Describe the morphological characteristics and visible interactions between different cell types.
42
  Assess the biological context to identify signs of cancer and the presence of antigens.
43
  """
44
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
45
  with gr.Blocks(css="style.css") as demo:
46
  gr.Markdown(INTRO_TEXT)
47
  with gr.Tab("Segment/Detect"):
@@ -117,58 +168,5 @@ with gr.Blocks(css="style.css") as demo:
117
  inputs=chat_inputs,
118
  )
119
 
120
- ### Postprocessing Utils for Segmentation Tokens
121
- ### Segmentation tokens are passed to another VAE which decodes them to a mask
122
-
123
- def extract_objs(text, width, height, unique_labels=False):
124
- """Returns objs for a string with "<loc>" and "<seg>" tokens."""
125
- objs = []
126
- seen = set()
127
- while text:
128
- m = _SEGMENT_DETECT_RE.match(text)
129
- if not m:
130
- break
131
- print("m", m)
132
- gs = list(m.groups())
133
- before = gs.pop(0)
134
- name = gs.pop()
135
- y1, x1, y2, x2 = [int(x) / 1024 for x in gs[:4]]
136
-
137
- y1, x1, y2, x2 = map(round, (y1*height, x1*width, y2*height, x2*width))
138
- seg_indices = gs[4:20]
139
- if seg_indices[0] is None:
140
- mask = None
141
- else:
142
- seg_indices = np.array([int(x) for x in seg_indices], dtype=np.int32)
143
- m64, = vae_model.reconstruct_masks(seg_indices[None])[..., 0]
144
- m64 = np.clip(np.array(m64) * 0.5 + 0.5, 0, 1)
145
- m64 = PIL.Image.fromarray((m64 * 255).astype('uint8'))
146
- mask = np.zeros([height, width])
147
- if y2 > y1 and x2 > x1:
148
- mask[y1:y2, x1:x2] = np.array(m64.resize([x2 - x1, y2 - y1])) / 255.0
149
-
150
- content = m.group()
151
- if before:
152
- objs.append(dict(content=before))
153
- content = content[len(before):]
154
- while unique_labels and name in seen:
155
- name = (name or '') + "'"
156
- seen.add(name)
157
- objs.append(dict(
158
- content=content, xyxy=(x1, y1, x2, y2), mask=mask, name=name))
159
- text = text[len(before) + len(content):]
160
-
161
- if text:
162
- objs.append(dict(content=text))
163
-
164
- return objs
165
-
166
- _SEGMENT_DETECT_RE = re.compile(
167
- r'(.*?)' +
168
- r'<loc(\d{4})>' * 4 + r'\s*' +
169
- '(?:%s)?' % (r'<seg(\d{3})>' * 16) +
170
- r'\s*([^;<>]+)? ?(?:; )?',
171
- )
172
-
173
  if __name__ == "__main__":
174
  demo.queue(max_size=10).launch(debug=True)
 
1
+ import functools
2
  import re
3
  import PIL.Image
4
  import gradio as gr
 
37
  has_annotations = bool(annotated_img[1])
38
  return annotated_img
39
 
40
+ INTRO_TEXT = "🔬🧠 CellVision AI -- Intelligent Cell Imaging Analysis 🤖🧫"
41
+ IMAGE_PROMPT = """
42
  Describe the morphological characteristics and visible interactions between different cell types.
43
  Assess the biological context to identify signs of cancer and the presence of antigens.
44
  """
45
 
46
+ def extract_objs(text, width, height, unique_labels=False):
47
+ """Returns objs for a string with "<loc>" and "<seg>" tokens."""
48
+ objs = []
49
+ seen = set()
50
+ while text:
51
+ m = _SEGMENT_DETECT_RE.match(text)
52
+ if not m:
53
+ break
54
+ print("m", m)
55
+ gs = list(m.groups())
56
+ before = gs.pop(0)
57
+ name = gs.pop()
58
+ y1, x1, y2, x2 = [int(x) / 1024 for x in gs[:4]]
59
+
60
+ y1, x1, y2, x2 = map(round, (y1 * height, x1 * width, y2 * height, x2 * width))
61
+ seg_indices = gs[4:20]
62
+ if seg_indices[0] is None:
63
+ mask = None
64
+ else:
65
+ seg_indices = np.array([int(x) for x in seg_indices], dtype=np.int32)
66
+ m64, = vae_model.reconstruct_masks(seg_indices[None])[..., 0]
67
+ m64 = np.clip(np.array(m64) * 0.5 + 0.5, 0, 1)
68
+ m64 = PIL.Image.fromarray((m64 * 255).astype('uint8'))
69
+ mask = np.zeros([height, width])
70
+ if y2 > y1 and x2 > x1:
71
+ mask[y1:y2, x1:x2] = np.array(m64.resize([x2 - x1, y2 - y1])) / 255.0
72
+
73
+ content = m.group()
74
+ if before:
75
+ objs.append(dict(content=before))
76
+ content = content[len(before):]
77
+ while unique_labels and name in seen:
78
+ name = (name or '') + "'"
79
+ seen.add(name)
80
+ objs.append(dict(
81
+ content=content, xyxy=(x1, y1, x2, y2), mask=mask, name=name))
82
+ text = text[len(before) + len(content):]
83
+
84
+ if text:
85
+ objs.append(dict(content=text))
86
+
87
+ return objs
88
+
89
+ _SEGMENT_DETECT_RE = re.compile(
90
+ r'(.*?)' +
91
+ r'<loc(\d{4})>' * 4 + r'\s*' +
92
+ '(?:%s)?' % (r'<seg(\d{3})>' * 16) +
93
+ r'\s*([^;<>]+)? ?(?:; )?',
94
+ )
95
+
96
  with gr.Blocks(css="style.css") as demo:
97
  gr.Markdown(INTRO_TEXT)
98
  with gr.Tab("Segment/Detect"):
 
168
  inputs=chat_inputs,
169
  )
170
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
171
  if __name__ == "__main__":
172
  demo.queue(max_size=10).launch(debug=True)