Spaces:
Sleeping
Sleeping
File size: 4,921 Bytes
c15480b 8ca0469 36fd8c7 8ca0469 36fd8c7 8ff3ba1 7abeff1 8c3e6c9 6c2499f 8ff3ba1 8c3e6c9 c616fef 8ff3ba1 36fd8c7 7faf30e 36fd8c7 7faf30e 36fd8c7 c15480b 6b7575f 8ff3ba1 827a18f a0d334a 1b3feca 5cd743d 827a18f 6b5852a a0d334a 827a18f 6f6166a 156ce5d d9d5ef6 156ce5d 827a18f d9d5ef6 827a18f 6b2c99b 827a18f d9d5ef6 75d98e1 f1331fb 8ea3892 7faf30e c547c8a |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 |
import streamlit as st
import matplotlib
matplotlib.use('Agg') # ใช้ Agg เพื่อรองรับ Headless Environment
import matplotlib.pyplot as plt
import numpy as np
from transformers import AutoTokenizer, AutoModelForTokenClassification, pipeline
# โหลด Tokenizer และ Model
model_name = "Nucha/Nucha_ITSkillNER_BERT"
tokenizer = AutoTokenizer.from_pretrained(model_name)
model = AutoModelForTokenClassification.from_pretrained(model_name)
# สร้าง NER Pipeline
ner_pipeline = pipeline("ner", model=model, tokenizer=tokenizer)
# กำหนดสีของ Entity แต่ละประเภท
ENTITY_COLORS = {
"B-HSKILL": "#FFD700", # สีทอง
"I-HSKILL": "#FFA500", # สีส้ม
"B-SSKILL": "#FFD700", # สีทอง
"I-SSKILL": "#FFA500", # สีส้ม
"O": "#D3D3D3" # สีเทา (Default)
}
# ฟังก์ชันสร้าง HTML สำหรับแสดงผล Entity ที่ถูก Highlight พร้อม Tag
def highlight_entities(text, entities):
highlighted_text = text
for entity in sorted(entities, key=lambda e: e["start"], reverse=True):
entity_word = entity["word"]
entity_label = entity["entity"]
entity_color = ENTITY_COLORS.get(entity_label, "#D3D3D3") # ใช้สีที่กำหนดไว้
# ใช้ HTML + CSS ในการไฮไลต์ Entity และใส่ TAG
highlighted_text = (
highlighted_text[:entity["start"]]
+ f'<span style="background-color: {entity_color}; padding: 2px 5px; border-radius: 5px;">[{entity_word}] ({entity_label})</span>'
+ highlighted_text[entity["end"]:]
)
return f'<div style="font-size:16px; line-height:1.6;">{highlighted_text}</div>'
# UI ด้วย Streamlit
col1, col2, col3 = st.columns([4, 4, 4])
with col1:
st.header("Input")
default_text="""Job Description:
We are seeking a talented Software Engineer to join our dynamic team at Tech Innovations Inc. You will be responsible for designing, developing, and maintaining software applications that meet the needs of our clients.
Key Responsibilities:
Develop high-quality software design and architecture
Identify, prioritize, and execute tasks in the software development life cycle
Review and debug code
Collaborate with other developers and engineers to ensure software quality
Required Qualifications:
Bachelor’s degree in Computer Science or related field
Proven experience as a Software Engineer or similar role
Familiarity with Agile development methodologies
Proficiency in programming languages such as Java, Python, or C#
Strong problem-solving skills and the ability to work in a team
Preferred Qualifications:
"""
text = st.text_area("Enter text for NER analysis:", value=default_text, height=400, max_chars=None, key=None, help=None, placeholder=None)
analyze_button = st.button("Analyze")
st.write("""**Example Inputs:**
- Experience with cloud services (AWS, Azure)
- Knowledge of databases (SQL, NoSQL)
- Familiarity with front-end technologies (HTML, CSS, JavaScript)""")
with col2:
st.header("Result")
# ใช้ st.markdown กับ CSS เพื่อปรับขนาดฟอนต์
st.markdown("<span style='font-size: 14px;'>Press button [Analyze]</span>", unsafe_allow_html=True)
if analyze_button:
ner_results = ner_pipeline(text)
# Display results in a structured output block
if ner_results:
output_data = [{"Entity": entity['word'], "Label": entity['entity'], "Score": f"{entity['score']:.4f}"} for entity in ner_results]
st.table(output_data) # Display as a table
else:
st.write("No entities found.")
# ใช้ st.markdown กับ CSS เพื่อปรับขนาดฟอนต์
st.markdown("<span style='font-size: 14px;'>JSON</span>", unsafe_allow_html=True)
st.write(ner_results)
with col3:
st.header("Annotation")
if analyze_button:
ner_results = ner_pipeline(text)
if ner_results:
entity_list = [
{"word": entity['word'], "entity": entity['entity'], "start": entity['start'], "end": entity['end']}
for entity in ner_results
]
# แสดงผล Entity Highlighted Text พร้อม Tag
st.markdown("### Annotated Text (Tagged)")
highlighted_html = highlight_entities(text, entity_list)
st.markdown(highlighted_html, unsafe_allow_html=True)
# แสดงข้อมูล Entity เป็นตาราง
st.markdown("### Extracted Entities")
st.table(entity_list)
else:
st.write("No entities found.")
|