hanbinChen commited on
Commit
a952df1
·
1 Parent(s): 2981176
Files changed (4) hide show
  1. app.py +77 -61
  2. ie.py +0 -0
  3. nen.py +0 -0
  4. test.py +76 -0
app.py CHANGED
@@ -179,22 +179,14 @@ def find_relations_with_entities(entities, entities_data):
179
  return relations
180
 
181
 
182
- def main():
183
- """Main application"""
184
- st.title("Medical Report Review System")
185
-
186
- # Load data
187
- if 'data' not in st.session_state:
188
- st.session_state.data = load_data()
189
-
190
- # Create two columns layout
191
  col1, col2 = st.columns(2)
192
 
193
  with col1:
194
  st.subheader("Reports to Review")
195
- # Get unreviewed reports list
196
  unreviewed_reports = [
197
- report_id for report_id, content in st.session_state.data.items()
198
  if 'reviewed' not in content
199
  ]
200
  selected_report = st.selectbox(
@@ -205,9 +197,8 @@ def main():
205
 
206
  with col2:
207
  st.subheader("Reviewed Reports")
208
- # Get reviewed reports list
209
  reviewed_reports = [
210
- report_id for report_id, content in st.session_state.data.items()
211
  if content.get('reviewed', False)
212
  ]
213
  st.selectbox(
@@ -216,59 +207,84 @@ def main():
216
  key="reviewed"
217
  )
218
 
219
- if selected_report:
220
- report_data = st.session_state.data[selected_report]
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
221
 
222
- # Display report text
223
- st.subheader("Report Content:")
224
- st.markdown(report_data['text'])
225
 
226
- # Display entities and relations
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
227
  entities_data = report_data['entities']
228
 
229
- # Convert entities data to Selection objects
230
- entities = entities2Selection(report_data['text'], entities_data)
231
 
232
- # Display annotation results
233
- st.subheader("Entity Annotation:")
234
- selections = label_select(
235
- body=report_data['text'],
236
- labels=list(set(e.labels[0] for e in entities)),
237
- selections=entities,
238
- )
239
- # Display selections in text format
240
- st.write(selections)
241
-
242
- # Display relationship graph
243
- st.subheader("Entity Relationship Graph:")
244
-
245
- # Add update graph button
246
- if st.button("Update Graph"):
247
- # Update relations using current selections
248
- relations = find_relations_with_entities(selections, entities_data)
249
- create_graph(selections, relations)
250
- else:
251
- # Display original graph
252
- relations = find_relations_with_entities(entities, entities_data)
253
- create_graph(entities, relations)
254
-
255
- # Add review functionality
256
- if st.button("Mark as Reviewed"):
257
- # Convert selections back to entities data
258
- updated_entities = selection2entities(selections)
259
-
260
- # Keep original relations
261
- for entity_id, entity in updated_entities.items():
262
- if entity_id in entities_data:
263
- entity['relations'] = entities_data[entity_id]['relations']
264
-
265
- st.session_state.data[selected_report]['reviewed'] = {
266
- 'entities': updated_entities
267
- }
268
-
269
- save_data(st.session_state.data)
270
- st.success("Review status saved!")
271
- st.rerun()
272
 
273
 
274
  if __name__ == "__main__":
 
179
  return relations
180
 
181
 
182
+ def setup_report_selection():
183
+ """Setup report selection columns and return selected report"""
 
 
 
 
 
 
 
184
  col1, col2 = st.columns(2)
185
 
186
  with col1:
187
  st.subheader("Reports to Review")
 
188
  unreviewed_reports = [
189
+ report_id for report_id, content in st.session_state.reports_json.items()
190
  if 'reviewed' not in content
191
  ]
192
  selected_report = st.selectbox(
 
197
 
198
  with col2:
199
  st.subheader("Reviewed Reports")
 
200
  reviewed_reports = [
201
+ report_id for report_id, content in st.session_state.reports_json.items()
202
  if content.get('reviewed', False)
203
  ]
204
  st.selectbox(
 
207
  key="reviewed"
208
  )
209
 
210
+ return selected_report
211
+
212
+
213
+ def display_report_content(report_data):
214
+ """Display the report text content"""
215
+ st.subheader("Report Content:")
216
+ st.markdown(report_data['text'])
217
+
218
+
219
+ def display_entities(report_text, entities):
220
+ """Setup and display entity annotation interface"""
221
+
222
+ st.subheader("Entity Annotation:")
223
+ selections = label_select(
224
+ body=report_text,
225
+ labels=list(set(e.labels[0] for e in entities)),
226
+ selections=entities,
227
+ )
228
+ st.write(selections)
229
+ return selections, entities
230
+
231
+
232
+ def display_relationship_graph(entities, entities_data):
233
+ st.subheader("Entity Relationship Graph:")
234
+ relations = find_relations_with_entities(entities, entities_data)
235
+ create_graph(entities, relations)
236
+
237
+
238
+ def handle_review_submission(selected_report, selections, entities_data):
239
+ """Handle the review submission process"""
240
+ if st.button("Mark as Reviewed"):
241
+ updated_entities = selection2entities(selections)
242
 
243
+ for entity_id, entity in updated_entities.items():
244
+ if entity_id in entities_data:
245
+ entity['relations'] = entities_data[entity_id]['relations']
246
 
247
+ st.session_state.reports_json[selected_report]['reviewed'] = {
248
+ 'entities': updated_entities
249
+ }
250
+
251
+ save_data(st.session_state.reports_json)
252
+ st.success("Review status saved!")
253
+ st.rerun()
254
+
255
+
256
+ def main():
257
+ """Main application"""
258
+ st.title("Medical Report Review System")
259
+
260
+ # Load data
261
+ if 'reports_json' not in st.session_state:
262
+ st.session_state.reports_json = load_data()
263
+
264
+ # Setup report selection
265
+ selected_report = setup_report_selection()
266
+
267
+ if selected_report:
268
+ report_data = st.session_state.reports_json[selected_report]
269
  entities_data = report_data['entities']
270
 
271
+ # Display report content
272
+ display_report_content(report_data)
273
 
274
+ # Setup entity annotation
275
+ selections = entities2Selection(report_data['text'], entities_data)
276
+ # Display entities
277
+ display_entities(report_data['text'], selections)
278
+
279
+ # Display NEN
280
+
281
+ # 使用当前选择或原始实体创建关系图
282
+ current_entities = selections if st.button(
283
+ "Update Graph") else selections
284
+ display_relationship_graph(current_entities, entities_data)
285
+
286
+ # Handle review submission
287
+ handle_review_submission(selected_report, selections, entities_data)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
288
 
289
 
290
  if __name__ == "__main__":
ie.py ADDED
File without changes
nen.py ADDED
File without changes
test.py ADDED
@@ -0,0 +1,76 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import streamlit as st
2
+
3
+ # 存储标注信息
4
+ if "annotations" not in st.session_state:
5
+ st.session_state.annotations = []
6
+
7
+ # 添加标注
8
+
9
+
10
+ def add_annotation(selected_text, label):
11
+ start_idx = text.find(selected_text)
12
+ if start_idx != -1:
13
+ end_idx = start_idx + len(selected_text)
14
+ st.session_state.annotations.append(
15
+ {"text": selected_text, "label": label, "start": start_idx, "end": end_idx})
16
+
17
+ # 移除标注
18
+
19
+
20
+ def remove_annotation(annotation):
21
+ st.session_state.annotations = [
22
+ ann for ann in st.session_state.annotations if ann != annotation
23
+ ]
24
+
25
+ # 生成带按钮的高亮文本
26
+
27
+
28
+ def render_highlighted_text(text, annotations):
29
+ annotated_text = []
30
+ last_idx = 0
31
+ for idx, ann in enumerate(sorted(annotations, key=lambda x: x["start"])):
32
+ # 添加非标注的部分
33
+ annotated_text.append(text[last_idx:ann["start"]])
34
+ # 添加标注的按钮
35
+ if st.button(f"{ann['text']} ({ann['label']})", key=f"annotation-{idx}"):
36
+ st.session_state.clicked_annotation = ann
37
+ last_idx = ann["end"]
38
+ # 添加剩余的部分
39
+ annotated_text.append(text[last_idx:])
40
+ return "".join(annotated_text)
41
+
42
+
43
+ # 主界面
44
+ st.title("文本标注工具")
45
+ st.write("输入文本并对其中的部分内容进行标注。")
46
+
47
+ # 输入文本
48
+ text = st.text_area("输入文本:", height=200)
49
+
50
+ # 文本选择与标注
51
+ if text:
52
+ st.write("选中文字并进行标注:")
53
+ col1, col2 = st.columns([3, 1])
54
+
55
+ # 输入标注的标签
56
+ with col1:
57
+ selected_text = st.text_input("选中的文本:")
58
+ with col2:
59
+ label = st.text_input("标签:")
60
+ if st.button("添加标注"):
61
+ if selected_text and label and selected_text in text:
62
+ add_annotation(selected_text, label)
63
+ else:
64
+ st.warning("请选择有效的文本并输入标签!")
65
+
66
+ # 渲染高亮文本
67
+ st.markdown(render_highlighted_text(text, st.session_state.annotations))
68
+
69
+ # 点击标注的文本
70
+ if "clicked_annotation" in st.session_state:
71
+ clicked_annotation = st.session_state.clicked_annotation
72
+ st.write(
73
+ f"你点击了: **{clicked_annotation['text']}** (标签: {clicked_annotation['label']})")
74
+ if st.button("删除标注", key="delete"):
75
+ remove_annotation(clicked_annotation)
76
+ del st.session_state.clicked_annotation