Spaces:
Runtime error
Runtime error
File size: 4,179 Bytes
e6bfe5c d25abcf ae36be9 e6bfe5c 1b711d9 ae36be9 e6bfe5c ae36be9 f474e98 e6bfe5c f474e98 a834bc3 81805e8 f474e98 81805e8 ae36be9 f474e98 ae36be9 81805e8 1eab2d6 81805e8 e6bfe5c 81805e8 ae36be9 81805e8 ae36be9 f193a60 e6bfe5c a834bc3 e6bfe5c 5b3d11c f193a60 1b711d9 4874aa0 32e294e 81805e8 3598e61 4874aa0 1b711d9 ae36be9 81805e8 e6bfe5c 81805e8 1b711d9 4874aa0 81805e8 e6bfe5c d25abcf e6bfe5c d25abcf 81805e8 4874aa0 bc06b7e c558c48 81805e8 |
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 |
import streamlit as st
import pandas as pd
import spacy
from transformers import pipeline, AutoModelForTokenClassification, AutoTokenizer
import PyPDF2
import docx
import io
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("Demo for Turkish NER Models")
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("", ('Select from Examples', 'Write or Paste New Text', 'Upload File'))
example_list = [
"Mustafa Kemal Atatürk 1919 yılında Samsun'a çıktı.",
"""Mustafa Kemal Atatürk, Türk asker, devlet adamı ve Türkiye Cumhuriyeti'nin kurucusudur."""
]
if input_method == 'Select from Examples':
selected_text = st.selectbox('Select Text from List', example_list, index=0)
input_text = st.text_area("Selected Text", selected_text, height=128)
elif 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
Run_Button = st.button("Run")
if Run_Button and input_text:
ner_pipeline = setModel(model_checkpoint, aggregation)
output = ner_pipeline(input_text)
output_comb = entity_comb(output)
df = pd.DataFrame.from_dict(output_comb)
cols_to_keep = ['word', 'entity_group', 'score', 'start', 'end']
df_final = df[cols_to_keep]
st.subheader("Recognized Entities")
st.dataframe(df_final)
# Spacy display logic
spacy_display = {"ents": [], "text": input_text, "title": None}
for entity in output_comb:
spacy_display["ents"].append({"start": entity["start"], "end": entity["end"], "label": entity["entity_group"]})
html = spacy.displacy.render(spacy_display, style="ent", minify=True, manual=True)
st.write(html, unsafe_allow_html=True)
|