Nucha commited on
Commit
7faf30e
·
verified ·
1 Parent(s): 8ca0469

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +39 -47
app.py CHANGED
@@ -17,38 +17,29 @@ ner_pipeline = pipeline("ner", model=model, tokenizer=tokenizer)
17
 
18
  # กำหนดสีของ Entity แต่ละประเภท
19
  ENTITY_COLORS = {
20
- "B-SKILL": "#FFD700", # สีทอง
21
- "I-SKILL": "#FFA500", # สีส้ม
22
- "B-TOOL": "#87CEFA", # สีฟ้าอ่อน
23
- "I-TOOL": "#1E90FF", # สีฟ้าเข้ม
24
  "O": "#D3D3D3" # สีเทา (Default)
25
  }
26
 
27
- # ฟังก์ชันวาด Entity Annotation เป็นภาพ
28
- def draw_annotation(text, entities):
29
- fig, ax = plt.subplots(figsize=(12, len(text.split("\n")) * 0.5))
30
- ax.set_xlim(0, 1)
31
- ax.set_ylim(0, 1)
32
- ax.axis("off")
33
-
34
- # แยกบรรทัดของข้อความ
35
- lines = text.split("\n")
36
- y = 0.9 # ตำแหน่งเริ่มต้น
37
-
38
- for line in lines:
39
- words = line.split(" ")
40
- x = 0.05 # ระยะห่างซ้ายสุด
41
- for word in words:
42
- color = "#FFFFFF" # สีพื้นหลังปกติ
43
- for entity in entities:
44
- if entity["Entity"] == word:
45
- color = ENTITY_COLORS.get(entity["Label"], "#D3D3D3") # เลือกสีตาม Label
46
-
47
- ax.text(x, y, word, fontsize=12, bbox=dict(facecolor=color, edgecolor="black", boxstyle="round,pad=0.3"))
48
- x += (len(word) / 80) + 0.02 # ปรับระยะห่างของแต่ละคำ
49
- y -= 0.05 # ลดระดับบรรทัดลง
50
-
51
- return fig
52
 
53
  # UI ด้วย Streamlit
54
  col1, col2, col3 = st.columns([4, 4, 4])
@@ -108,22 +99,23 @@ with col2:
108
  with col3:
109
  st.header("Annotation")
110
 
111
- if analyze_button and ner_results:
112
- st.write("Edit or Add Entities:")
113
-
114
- annotated_entities = []
115
- for i, entity in enumerate(ner_results):
116
- entity_text = st.text_input(f"Entity {i+1}", value=entity['word'])
117
- entity_label = st.selectbox(f"Label {i+1}", ["O", "B-SKILL", "I-SKILL", "B-TOOL", "I-TOOL"], index=0)
118
- annotated_entities.append({"Entity": entity_text, "Label": entity_label})
119
-
120
- # เพิ่ม Entity ใหม่
121
- new_entity_text = st.text_input("New Entity")
122
- new_entity_label = st.selectbox("New Label", ["O", "B-SKILL", "I-SKILL", "B-TOOL", "I-TOOL"], index=0)
123
- if st.button("Add New Entity") and new_entity_text:
124
- annotated_entities.append({"Entity": new_entity_text, "Label": new_entity_label})
125
-
126
- if st.button("Save Annotation"):
127
- st.write("Saved Annotations:")
128
- st.json(annotated_entities)
 
129
 
 
17
 
18
  # กำหนดสีของ Entity แต่ละประเภท
19
  ENTITY_COLORS = {
20
+ "B-HSKILL": "#FFD700", # สีทอง
21
+ "I-HSKILL": "#FFA500", # สีส้ม
22
+ "B-SSKILL": "#FFD700", # สีทอง
23
+ "I-SSKILL": "#FFA500", # สีส้ม
24
  "O": "#D3D3D3" # สีเทา (Default)
25
  }
26
 
27
+ # ฟังก์ชันสร้าง HTML สำหรับแสดงผล Entity ที่ถูก Highlight พร้อม Tag
28
+ def highlight_entities(text, entities):
29
+ highlighted_text = text
30
+ for entity in sorted(entities, key=lambda e: e["start"], reverse=True):
31
+ entity_word = entity["word"]
32
+ entity_label = entity["entity"]
33
+ entity_color = ENTITY_COLORS.get(entity_label, "#D3D3D3") # ใช้สีที่กำหนดไว้
34
+
35
+ # ใช้ HTML + CSS ในการไฮไลต์ Entity และใส่ TAG
36
+ highlighted_text = (
37
+ highlighted_text[:entity["start"]]
38
+ + f'<span style="background-color: {entity_color}; padding: 2px 5px; border-radius: 5px;">[{entity_word}] ({entity_label})</span>'
39
+ + highlighted_text[entity["end"]:]
40
+ )
41
+
42
+ return f'<div style="font-size:16px; line-height:1.6;">{highlighted_text}</div>'
 
 
 
 
 
 
 
 
 
43
 
44
  # UI ด้วย Streamlit
45
  col1, col2, col3 = st.columns([4, 4, 4])
 
99
  with col3:
100
  st.header("Annotation")
101
 
102
+ if analyze_button:
103
+ ner_results = ner_pipeline(text)
104
+
105
+ if ner_results:
106
+ entity_list = [
107
+ {"word": entity['word'], "entity": entity['entity'], "start": entity['start'], "end": entity['end']}
108
+ for entity in ner_results
109
+ ]
110
+
111
+ # แสดงผล Entity Highlighted Text พร้อม Tag
112
+ st.markdown("### Annotated Text (Tagged)")
113
+ highlighted_html = highlight_entities(text, entity_list)
114
+ st.markdown(highlighted_html, unsafe_allow_html=True)
115
+
116
+ # แสดงข้อมูล Entity เป็นตาราง
117
+ st.markdown("### Extracted Entities")
118
+ st.table(entity_list)
119
+ else:
120
+ st.write("No entities found.")
121