Nucha commited on
Commit
36fd8c7
·
verified ·
1 Parent(s): 8ea3892

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +42 -9
app.py CHANGED
@@ -1,4 +1,6 @@
1
  import streamlit as st
 
 
2
  from transformers import AutoTokenizer, AutoModelForTokenClassification, pipeline
3
 
4
  # โหลด Tokenizer และ Model
@@ -9,6 +11,41 @@ model = AutoModelForTokenClassification.from_pretrained(model_name)
9
  # สร้าง NER Pipeline
10
  ner_pipeline = pipeline("ner", model=model, tokenizer=tokenizer)
11
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
12
  # UI ด้วย Streamlit
13
  col1, col2, col3 = st.columns([4, 4, 4])
14
 
@@ -69,23 +106,19 @@ with col3:
69
 
70
  if analyze_button and ner_results:
71
  st.write("Edit or Add Entities:")
72
-
73
  annotated_entities = []
74
  for i, entity in enumerate(ner_results):
75
  entity_text = st.text_input(f"Entity {i+1}", value=entity['word'])
76
  entity_label = st.selectbox(f"Label {i+1}", ["O", "B-SKILL", "I-SKILL", "B-TOOL", "I-TOOL"], index=0)
77
- entity_start = entity["start"]
78
- entity_end = entity["end"]
79
- annotated_entities.append({"Entity": entity_text, "Label": entity_label, "Start": entity_start, "End": entity_end})
80
-
81
  # เพิ่ม Entity ใหม่
82
  new_entity_text = st.text_input("New Entity")
83
  new_entity_label = st.selectbox("New Label", ["O", "B-SKILL", "I-SKILL", "B-TOOL", "I-TOOL"], index=0)
84
  if st.button("Add New Entity") and new_entity_text:
85
- start_pos = text.find(new_entity_text)
86
- if start_pos != -1:
87
- annotated_entities.append({"Entity": new_entity_text, "Label": new_entity_label, "Start": start_pos, "End": start_pos + len(new_entity_text)})
88
-
89
  if st.button("Save Annotation"):
90
  st.write("Saved Annotations:")
91
  st.json(annotated_entities)
 
1
  import streamlit as st
2
+ import matplotlib.pyplot as plt
3
+ import numpy as np
4
  from transformers import AutoTokenizer, AutoModelForTokenClassification, pipeline
5
 
6
  # โหลด Tokenizer และ Model
 
11
  # สร้าง NER Pipeline
12
  ner_pipeline = pipeline("ner", model=model, tokenizer=tokenizer)
13
 
14
+ # กำหนดสีของ Entity แต่ละประเภท
15
+ ENTITY_COLORS = {
16
+ "B-SKILL": "#FFD700", # สีทอง
17
+ "I-SKILL": "#FFA500", # สีส้ม
18
+ "B-TOOL": "#87CEFA", # สีฟ้าอ่อน
19
+ "I-TOOL": "#1E90FF", # สีฟ้าเข้ม
20
+ "O": "#D3D3D3" # สีเทา (Default)
21
+ }
22
+
23
+ # ฟังก์ชันวาด Entity Annotation เป็นภาพ
24
+ def draw_annotation(text, entities):
25
+ fig, ax = plt.subplots(figsize=(12, len(text.split("\n")) * 0.5))
26
+ ax.set_xlim(0, 1)
27
+ ax.set_ylim(0, 1)
28
+ ax.axis("off")
29
+
30
+ # แยกบรรทัดของข้อความ
31
+ lines = text.split("\n")
32
+ y = 0.9 # ตำแหน่งเริ่มต้น
33
+
34
+ for line in lines:
35
+ words = line.split(" ")
36
+ x = 0.05 # ระยะห่างซ้ายสุด
37
+ for word in words:
38
+ color = "#FFFFFF" # สีพื้นหลังปกติ
39
+ for entity in entities:
40
+ if entity["Entity"] == word:
41
+ color = ENTITY_COLORS.get(entity["Label"], "#D3D3D3") # เลือกสีตาม Label
42
+
43
+ ax.text(x, y, word, fontsize=12, bbox=dict(facecolor=color, edgecolor="black", boxstyle="round,pad=0.3"))
44
+ x += (len(word) / 80) + 0.02 # ปรับระยะห่างของแต่ละคำ
45
+ y -= 0.05 # ลดระดับบรรทัดลง
46
+
47
+ return fig
48
+
49
  # UI ด้วย Streamlit
50
  col1, col2, col3 = st.columns([4, 4, 4])
51
 
 
106
 
107
  if analyze_button and ner_results:
108
  st.write("Edit or Add Entities:")
109
+
110
  annotated_entities = []
111
  for i, entity in enumerate(ner_results):
112
  entity_text = st.text_input(f"Entity {i+1}", value=entity['word'])
113
  entity_label = st.selectbox(f"Label {i+1}", ["O", "B-SKILL", "I-SKILL", "B-TOOL", "I-TOOL"], index=0)
114
+ annotated_entities.append({"Entity": entity_text, "Label": entity_label})
115
+
 
 
116
  # เพิ่ม Entity ใหม่
117
  new_entity_text = st.text_input("New Entity")
118
  new_entity_label = st.selectbox("New Label", ["O", "B-SKILL", "I-SKILL", "B-TOOL", "I-TOOL"], index=0)
119
  if st.button("Add New Entity") and new_entity_text:
120
+ annotated_entities.append({"Entity": new_entity_text, "Label": new_entity_label})
121
+
 
 
122
  if st.button("Save Annotation"):
123
  st.write("Saved Annotations:")
124
  st.json(annotated_entities)