Spaces:
Sleeping
Sleeping
Updated app.py directlt on browser
Browse files
app.py
CHANGED
@@ -1,4 +1,79 @@
|
|
1 |
import streamlit as st
|
|
|
|
|
|
|
2 |
|
3 |
x = st.slider('Select a value')
|
4 |
-
st.write(x, 'squared is', x * x)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
import streamlit as st
|
2 |
+
import wandb
|
3 |
+
from transformers import pipeline
|
4 |
+
from transformers import AutoTokenizer, AutoModelForTokenClassification
|
5 |
|
6 |
x = st.slider('Select a value')
|
7 |
+
st.write(x, 'squared is', x * x)
|
8 |
+
|
9 |
+
@st.cache_resource()
|
10 |
+
def load_trained_model():
|
11 |
+
|
12 |
+
tokenizer = AutoTokenizer.from_pretrained("LampOfSocrates/bert-base-cased-sourav")
|
13 |
+
model = AutoModelForTokenClassification.from_pretrained("LampOfSocrates/bert-base-cased-sourav")
|
14 |
+
# Mapping labels
|
15 |
+
label_map = model.config.id2label
|
16 |
+
# Print the label mapping
|
17 |
+
print(label_map)
|
18 |
+
|
19 |
+
# Load the NER model and tokenizer from Hugging Face
|
20 |
+
#ner_pipeline = pipeline("ner", model="dbmdz/bert-large-cased-finetuned-conll03-english")
|
21 |
+
ner_pipeline = pipeline("ner", model=model, tokenizer = tokenizer)
|
22 |
+
return ner_pipeline
|
23 |
+
|
24 |
+
def prep_page():
|
25 |
+
model = load_trained_model()
|
26 |
+
|
27 |
+
# Streamlit app
|
28 |
+
st.title("Named Entity Recognition with BERT on PLOD-CW")
|
29 |
+
st.write("Enter a sentence to see the named entities recognized by the model.")
|
30 |
+
|
31 |
+
# Text input
|
32 |
+
text = st.text_area("Enter your sentence here:")
|
33 |
+
|
34 |
+
# Perform NER and display results
|
35 |
+
if text:
|
36 |
+
st.write("Entities recognized:")
|
37 |
+
entities = model(text)
|
38 |
+
|
39 |
+
# Create a dictionary to map entity labels to colors
|
40 |
+
label_colors = {
|
41 |
+
'ORG': 'lightblue',
|
42 |
+
'PER': 'lightgreen',
|
43 |
+
'LOC': 'lightcoral',
|
44 |
+
'MISC': 'lightyellow'
|
45 |
+
}
|
46 |
+
|
47 |
+
# Prepare the HTML output with styled entities
|
48 |
+
def get_entity_html(text, entities):
|
49 |
+
html = ""
|
50 |
+
last_idx = 0
|
51 |
+
for entity in entities:
|
52 |
+
start = entity['start']
|
53 |
+
end = entity['end']
|
54 |
+
label = entity['entity']
|
55 |
+
entity_text = text[start:end]
|
56 |
+
color = label_colors.get(label, 'lightgray')
|
57 |
+
|
58 |
+
# Append the text before the entity
|
59 |
+
html += text[last_idx:start]
|
60 |
+
# Append the entity with styling
|
61 |
+
html += f'<mark style="background-color: {color}; border-radius: 3px;">{entity_text}</mark>'
|
62 |
+
last_idx = end
|
63 |
+
|
64 |
+
# Append any remaining text after the last entity
|
65 |
+
html += text[last_idx:]
|
66 |
+
return html
|
67 |
+
|
68 |
+
# Generate and display the styled HTML
|
69 |
+
styled_text = get_entity_html(text, entities)
|
70 |
+
|
71 |
+
st.markdown(styled_text, unsafe_allow_html=True)
|
72 |
+
|
73 |
+
|
74 |
+
|
75 |
+
if __name__ == '__main__':
|
76 |
+
models = load_model_from_wandb()
|
77 |
+
print(models)
|
78 |
+
|
79 |
+
prep_page()
|