File size: 7,958 Bytes
e6bfe5c
 
d25abcf
ae36be9
 
 
 
e6bfe5c
c480c1f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1b711d9
 
ae36be9
 
 
 
 
 
 
 
 
 
 
 
 
 
2856362
e6bfe5c
ae36be9
 
 
 
 
 
f474e98
e6bfe5c
f474e98
 
a834bc3
81805e8
f474e98
81805e8
c480c1f
ae36be9
f474e98
2856362
 
 
81805e8
a34a8fb
ae36be9
 
 
 
81805e8
ae36be9
 
 
f193a60
e6bfe5c
a834bc3
 
e6bfe5c
5b3d11c
f193a60
1b711d9
4874aa0
 
 
 
32e294e
81805e8
3598e61
4874aa0
 
1b711d9
ae36be9
8c045a9
 
 
 
 
 
 
 
 
 
 
 
8bd5af2
 
 
 
 
 
 
20bef1c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
81805e8
e6bfe5c
81805e8
1b711d9
 
c480c1f
 
 
 
 
 
8bb7ed4
c480c1f
 
 
 
 
 
 
 
 
 
 
3547909
c480c1f
 
8c045a9
 
3547909
8bd5af2
8c045a9
 
 
 
 
 
 
 
73f645c
 
 
e6bfe5c
73f645c
 
20bef1c
 
c480c1f
8c045a9
81805e8
4874aa0
8c045a9
 
 
 
 
8bd5af2
 
 
 
 
 
 
 
 
 
 
 
 
c480c1f
8bd5af2
8c045a9
 
8bd5af2
20bef1c
8bd5af2
 
 
 
 
 
 
 
20bef1c
8c045a9
8bd5af2
 
 
 
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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
import streamlit as st
import pandas as pd
import spacy
from transformers import pipeline, AutoModelForTokenClassification, AutoTokenizer
import PyPDF2
import docx
import io

def chunk_text(text, chunk_size=128):
    words = text.split()
    chunks = []
    current_chunk = []
    current_length = 0

    for word in words:
        if current_length + len(word) + 1 > chunk_size:
            chunks.append(' '.join(current_chunk))
            current_chunk = [word]
            current_length = len(word)
        else:
            current_chunk.append(word)
            current_length += len(word) + 1

    if current_chunk:
        chunks.append(' '.join(current_chunk))

    return chunks

st.set_page_config(layout="wide")

# Function to read text from uploaded file
def read_file(file):
    if file.type == "text/plain":
        return file.getvalue().decode("utf-8")
    elif file.type == "application/pdf":
        pdf_reader = PyPDF2.PdfReader(io.BytesIO(file.getvalue()))
        return " ".join(page.extract_text() for page in pdf_reader.pages)
    elif file.type == "application/vnd.openxmlformats-officedocument.wordprocessingml.document":
        doc = docx.Document(io.BytesIO(file.getvalue()))
        return " ".join(paragraph.text for paragraph in doc.paragraphs)
    else:
        st.error("Unsupported file type")
        return None

st.title("Turkish NER Models Testing")

model_list = [
    'girayyagmur/bert-base-turkish-ner-cased',
    'savasy/bert-base-turkish-ner-cased',
    'xlm-roberta-large-finetuned-conll03-english',
    'asahi417/tner-xlm-roberta-base-ontonotes5'
]

st.sidebar.header("Select NER Model")
model_checkpoint = st.sidebar.radio("", model_list)

st.sidebar.write("For details of models: 'https://huggingface.co/akdeniz27/")
st.sidebar.write("Only PDF, DOCX, and TXT files are supported.")

# Determine aggregation strategy
aggregation = "simple" if model_checkpoint in ["akdeniz27/xlm-roberta-base-turkish-ner", "xlm-roberta-large-finetuned-conll03-english", "asahi417/tner-xlm-roberta-base-ontonotes5"] else "first"

st.subheader("Select Text Input Method")
input_method = st.radio("", ('Write or Paste New Text', 'Upload File'))

if input_method == "Write or Paste New Text":
    input_text = st.text_area('Write or Paste Text Below', value="", height=128)
else:
    uploaded_file = st.file_uploader("Choose a file", type=["txt", "pdf", "docx"])
    if uploaded_file is not None:
        input_text = read_file(uploaded_file)
        if input_text:
            st.text_area("Extracted Text", input_text, height=128)
    else:
        input_text = ""

@st.cache_resource
def setModel(model_checkpoint, aggregation):
    model = AutoModelForTokenClassification.from_pretrained(model_checkpoint)
    tokenizer = AutoTokenizer.from_pretrained(model_checkpoint)
    return pipeline('ner', model=model, tokenizer=tokenizer, aggregation_strategy=aggregation)

@st.cache_resource
def entity_comb(output):
    output_comb = []
    for ind, entity in enumerate(output):
        if ind == 0:
            output_comb.append(entity)
        elif output[ind]["start"] == output[ind-1]["end"] and output[ind]["entity_group"] == output[ind-1]["entity_group"]:
            output_comb[-1]["word"] += output[ind]["word"]
            output_comb[-1]["end"] = output[ind]["end"]
        else:
            output_comb.append(entity)
    return output_comb

def create_mask_dict(entities):
    mask_dict = {}
    entity_counters = {}
    for entity in entities:
        if entity['entity_group'] not in ['CARDINAL', 'EVENT']:
            if entity['word'] not in mask_dict:
                if entity['entity_group'] not in entity_counters:
                    entity_counters[entity['entity_group']] = 1
                else:
                    entity_counters[entity['entity_group']] += 1
                mask_dict[entity['word']] = f"{entity['entity_group']}_{entity_counters[entity['entity_group']]}"
    return mask_dict
def create_masked_text(input_text, entities, mask_dict):
    masked_text = input_text
    for entity in sorted(entities, key=lambda x: x['start'], reverse=True):
        if entity['entity_group'] not in ['CARDINAL', 'EVENT']:
            masked_text = masked_text[:entity['start']] + mask_dict[entity['word']] + masked_text[entity['end']:]
    return masked_text

def export_masked_text(masked_text, file_type):
    if file_type == "txt":
        return masked_text.encode("utf-8")
    elif file_type == "pdf":
        pdf_buffer = io.BytesIO()
        pdf = FPDF()
        pdf.add_page()
        pdf.set_font("Arial", size=12)
        pdf.multi_cell(0, 10, masked_text)
        pdf.output(pdf_buffer)
        pdf_buffer.seek(0)
        return pdf_buffer.getvalue()
    elif file_type == "docx":
        doc = docx.Document()
        doc.add_paragraph(masked_text)
        buffer = io.BytesIO()
        doc.save(buffer)
        buffer.seek(0)
        return buffer.getvalue()
    else:
        st.error("Unsupported file type for export")
        return None
Run_Button = st.button("Run")

if Run_Button and input_text:
    ner_pipeline = setModel(model_checkpoint, aggregation)
    
    # Chunk the input text
    chunks = chunk_text(input_text)
    
    # Process each chunk
    all_outputs = []
    for i, chunk in enumerate(chunks):
        output = ner_pipeline(chunk)
        
        # Adjust start and end positions for entities in chunks after the first
        if i > 0:
            offset = len(' '.join(chunks[:i])) + 1
            for entity in output:
                entity['start'] += offset
                entity['end'] += offset
        
        all_outputs.extend(output)
    
    # Combine entities
    
    output_comb = entity_comb(all_outputs)
    
    # Create mask dictionary
    mask_dict = create_mask_dict(output_comb)

    masked_text = create_masked_text(input_text, output_comb, mask_dict)
    
    # Apply masking and add masked_word column
    for entity in output_comb:
        if entity['entity_group'] not in ['CARDINAL', 'EVENT']:
            entity['masked_word'] = mask_dict.get(entity['word'], entity['word'])
        else:
            entity['masked_word'] = entity['word']
    
    #df = pd.DataFrame.from_dict(output_comb)
    #cols_to_keep = ['word', 'entity_group', 'score', 'start', 'end']
    #df_final = df[cols_to_keep].loc[:,~df.columns.duplicated()].copy()
    
    #st.subheader("Recognized Entities")
    #st.dataframe(df_final)

    
    
    # Spacy display logic with entity numbering
    spacy_display = {"ents": [], "text": input_text, "title": None}
    for entity in output_comb:
        if entity['entity_group'] not in ['CARDINAL', 'EVENT']:
            label = f"{entity['entity_group']}_{mask_dict[entity['word']].split('_')[1]}"
        else:
            label = entity['entity_group']
        spacy_display["ents"].append({"start": entity["start"], "end": entity["end"], "label": label})
        # Custom CSS to prevent label overlap
    custom_css = """
    <style>
    .entity-label {
        font-size: 0.7em;
        line-height: 1;
        padding: 0.25em;
        border-radius: 0.25em;
        top: -1.5em;
        position: relative;
    }
    </style>
    """
    
    html = custom_css + spacy.displacy.render(spacy_display, style="ent", minify=True, manual=True)
    st.write(html, unsafe_allow_html=True)

 # Download button
    export_file_type = uploaded_file.type.split("/")[-1] if uploaded_file is not None else "txt"
    masked_file_content = export_masked_text(masked_text, export_file_type)
    if masked_file_content:
        st.download_button(
            label="Download Masked Text",
            data=masked_file_content,
            file_name=f"masked_output.{export_file_type}",
            mime=f"application/{export_file_type}" if export_file_type != "txt" else "text/plain"
        )

    st.subheader("Masking Dictionary")
    st.json(mask_dict)

    st.subheader("Masked Text Preview")
    st.text(masked_text)