Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -17,38 +17,29 @@ ner_pipeline = pipeline("ner", model=model, tokenizer=tokenizer)
|
|
17 |
|
18 |
# กำหนดสีของ Entity แต่ละประเภท
|
19 |
ENTITY_COLORS = {
|
20 |
-
"B-
|
21 |
-
"I-
|
22 |
-
"B-
|
23 |
-
"I-
|
24 |
"O": "#D3D3D3" # สีเทา (Default)
|
25 |
}
|
26 |
|
27 |
-
#
|
28 |
-
def
|
29 |
-
|
30 |
-
|
31 |
-
|
32 |
-
|
33 |
-
|
34 |
-
|
35 |
-
|
36 |
-
|
37 |
-
|
38 |
-
|
39 |
-
|
40 |
-
|
41 |
-
|
42 |
-
|
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
|
112 |
-
|
113 |
-
|
114 |
-
|
115 |
-
|
116 |
-
|
117 |
-
|
118 |
-
|
119 |
-
|
120 |
-
|
121 |
-
|
122 |
-
|
123 |
-
|
124 |
-
|
125 |
-
|
126 |
-
|
127 |
-
st.
|
128 |
-
|
|
|
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 |
|