Nucha commited on
Commit
c547c8a
·
verified ·
1 Parent(s): 6c2499f

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +33 -0
app.py CHANGED
@@ -66,3 +66,36 @@ with col2:
66
 
67
  with col3:
68
  st.header("Anotation")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
66
 
67
  with col3:
68
  st.header("Anotation")
69
+ # สร้าง NER Pipeline
70
+ ner_pipeline = pipeline("ner", model=model, tokenizer=tokenizer)
71
+
72
+ def annotate_text(text):
73
+ ner_results = ner_pipeline(text)
74
+
75
+ # สร้าง Annotation Output
76
+ annotations = []
77
+ highlighted_text = text
78
+
79
+ for entity in ner_results:
80
+ word = entity['word']
81
+ entity_label = entity['entity']
82
+ score = round(entity['score'], 4)
83
+
84
+ # สร้าง Dictionary ของ Entity
85
+ annotations.append({"Entity": word, "Label": entity_label, "Score": score})
86
+
87
+ # Highlight คำที่เป็น Entity
88
+ highlighted_text = highlighted_text.replace(word, f'<mark style="background-color: yellow">{word} ({entity_label})</mark>')
89
+
90
+ return highlighted_text, json.dumps(annotations, indent=2)
91
+
92
+ # UI ด้วย Gradio
93
+ demo = gr.Interface(
94
+ fn=annotate_text,
95
+ inputs=gr.Textbox(lines=10, placeholder="Enter text for NER analysis..."),
96
+ outputs=[gr.HTML(label="Annotated Text"), gr.JSON(label="Entities List")],
97
+ title="NER Annotation Tool",
98
+ description="Highlight Named Entities and display them in a structured format."
99
+ )
100
+
101
+ demo.launch()