Nucha commited on
Commit
2a9db15
·
verified ·
1 Parent(s): 7faf30e

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +65 -20
app.py CHANGED
@@ -17,29 +17,65 @@ ner_pipeline = pipeline("ner", model=model, tokenizer=tokenizer)
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])
@@ -103,19 +139,28 @@ with col3:
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
 
 
17
 
18
  # กำหนดสีของ Entity แต่ละประเภท
19
  ENTITY_COLORS = {
20
+ "hskill": "#FFD700", # สีทอง (Hard Skill)
21
+ "sskill": "#87CEFA", # สีฟ้าอ่อน (Soft Skill)
 
 
 
22
  }
23
 
24
+ # ฟังก์ชันรวม Entity (B-* และ I-*)
25
+ def merge_entities(entities):
26
+ merged = []
27
+ current_entity = None
28
+
29
+ for entity in entities:
30
+ word, label = entity["word"], entity["entity"]
31
+
32
+ if label.startswith("B-"): # ถ้าเป็น Entity ตัวแรก
33
+ if current_entity: # บันทึก Entity ก่อนหน้า
34
+ merged.append(current_entity)
35
+ current_entity = {"word": word, "entity": label[2:]} # ตัด B- ออก
36
+ elif label.startswith("I-") and current_entity: # ถ้าเป็น Entity ที่ต่อเนื่อง
37
+ current_entity["word"] += " " + word # รวมคำเข้าไป
38
+ else:
39
+ if current_entity:
40
+ merged.append(current_entity)
41
+ current_entity = {"word": word, "entity": label}
42
+
43
+ if current_entity:
44
+ merged.append(current_entity)
45
+
46
+ return merged
47
+
48
+ # ฟังก์ชันแยกเป็น Hard Skill (hskill) และ Soft Skill (sskill)
49
+ def categorize_entities(entities):
50
+ hskill = []
51
+ sskill = []
52
+
53
+ for entity in entities:
54
+ if entity["entity"] in ["SKILL", "TOOL"]: # รวม B-* และ I-* เป็นหมวดเดียว
55
+ hskill.append(entity)
56
+ else:
57
+ sskill.append(entity)
58
+
59
+ return hskill, sskill
60
+
61
  # ฟังก์ชันสร้าง HTML สำหรับแสดงผล Entity ที่ถูก Highlight พร้อม Tag
62
+ def highlight_entities(text, hskill, sskill):
63
+ all_entities = hskill + sskill # รวมทั้งหมดเพื่อไฮไลต์
64
  highlighted_text = text
65
+
66
+ for entity in sorted(all_entities, key=lambda e: text.find(e["word"]), reverse=True):
67
  entity_word = entity["word"]
68
+ entity_label = "hskill" if entity in hskill else "sskill"
69
+ entity_color = ENTITY_COLORS.get(entity_label, "#D3D3D3")
70
 
71
  # ใช้ HTML + CSS ในการไฮไลต์ Entity และใส่ TAG
72
+ highlighted_text = highlighted_text.replace(
73
+ entity_word,
74
+ f'<span style="background-color: {entity_color}; padding: 2px 5px; border-radius: 5px;">[{entity_word}] ({entity_label.upper()})</span>'
 
75
  )
76
 
77
  return f'<div style="font-size:16px; line-height:1.6;">{highlighted_text}</div>'
78
+
79
 
80
  # UI ด้วย Streamlit
81
  col1, col2, col3 = st.columns([4, 4, 4])
 
139
  ner_results = ner_pipeline(text)
140
 
141
  if ner_results:
142
+ # รวม Entity ที่มี B- และ I- ให้เป็นคำเดียวกัน
143
+ merged_entities = merge_entities([
144
+ {"word": entity['word'], "entity": entity['entity']}
145
  for entity in ner_results
146
+ ])
147
+
148
+ # แยกเป็น Hard Skill และ Soft Skill
149
+ hskill, sskill = categorize_entities(merged_entities)
150
 
151
  # แสดงผล Entity Highlighted Text พร้อม Tag
152
  st.markdown("### Annotated Text (Tagged)")
153
+ highlighted_html = highlight_entities(text, hskill, sskill)
154
  st.markdown(highlighted_html, unsafe_allow_html=True)
155
 
156
+ # แสดงข้อมูล Hard Skill
157
+ st.markdown("### Hard Skills (hskill)")
158
+ st.table(hskill)
159
+
160
+ # แสดงข้อมูล Soft Skill
161
+ st.markdown("### Soft Skills (sskill)")
162
+ st.table(sskill)
163
+
164
  else:
165
  st.write("No entities found.")
166