som11 commited on
Commit
5e37eca
·
verified ·
1 Parent(s): 203a3f0

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +98 -98
app.py CHANGED
@@ -1,98 +1,98 @@
1
- import streamlit as st
2
- import spacy
3
- from spacy import displacy
4
- from transformers import AutoTokenizer, AutoModelForTokenClassification
5
- from transformers import pipeline
6
- import pandas as pd
7
-
8
-
9
- st.title('Named Entity Recognizer')
10
-
11
- st.write('Named Entity Recognition (NER) is like a smart highlighter that scans through text and highlights important words, such as people’s names, places, companies, and dates. It’s a tool used in language processing that helps computers understand and organize chunks of text by picking out these key terms and saying, “Hey, this word is a location, and this one is a person’s name.” By doing this, NER helps turn messy, unstructured text into neat, organized data that’s easier for machines to understand and work with. It’s like teaching computers to read a map of words, so they can help us find the information we need much faster.')
12
-
13
- st.write('')
14
-
15
- with st.form(key='form_named_entity_recognition'):
16
-
17
- input_from_user = st.text_area('enter your input')
18
-
19
- model_options = st.selectbox('choose a model', ('Choose a model', 'Spacy\'s en_core_web_sm model', 'dslim/bert-base-NER model'))
20
-
21
- submit_button = st.form_submit_button('Submit')
22
-
23
-
24
- if submit_button:
25
-
26
- if input_from_user == '':
27
-
28
- st.error('empty form submitted')
29
-
30
- else:
31
-
32
- if model_options == 'Choose a model':
33
-
34
- st.error('Please choose a model for named entity recognition')
35
-
36
- else:
37
-
38
- st.subheader('Result Analysis')
39
-
40
- if model_options == 'Choose a model':
41
-
42
- st.error('Please choose a model for Named Entity Recognition')
43
-
44
- elif model_options == 'Spacy\'s en_core_web_sm model':
45
-
46
- st.write('Model Used for Named Entity Recognition:')
47
- st.success(model_options)
48
-
49
- spacy_model = spacy.load('en_core_web_sm')
50
-
51
- res = spacy_model(input_from_user)
52
-
53
- st.write(f'Analysis of the detected entities from the text ==>')
54
- st.markdown(f'**{input_from_user}**')
55
-
56
- entities = [{'Entity': entity.text, 'Label of the Entity': entity.label_, 'Description of the Label': spacy.explain(entity.label_)} for entity in res.ents]
57
-
58
- df = pd.DataFrame(entities)
59
-
60
- st.table(df)
61
-
62
- st.write('Entites marked in the input text:')
63
- st.markdown(displacy.render(res, style='ent'), unsafe_allow_html=True)
64
-
65
- elif model_options == 'dslim/bert-base-NER model':
66
-
67
- st.write('Model Used for Named Entity Recognition:')
68
- st.success(model_options)
69
-
70
- tokenizer = AutoTokenizer.from_pretrained("dslim/bert-base-NER")
71
- model = AutoModelForTokenClassification.from_pretrained("dslim/bert-base-NER")
72
-
73
- bert_ner_model = pipeline('ner', model=model, tokenizer=tokenizer)
74
-
75
- res = bert_ner_model(input_from_user)
76
-
77
- abbreviations = {
78
- "O": "Outside of a named entity",
79
- "B-MISC": "Beginning of a miscellaneous entity right after another miscellaneous entity",
80
- "I-MISC": "Miscellaneous entity",
81
- "B-PER": "Beginning of a person’s name right after another person’s name",
82
- "I-PER": "Person’s name",
83
- "B-ORG": "Beginning of an organization right after another organization",
84
- "I-ORG": "Organization",
85
- "B-LOC": "Beginning of a location right after another location",
86
- "I-LOC": "Location"
87
- }
88
-
89
- st.write(f'Analysis of the detected entities from the text ==>')
90
- st.markdown(f'**{input_from_user}**')
91
-
92
- entities = [{'Entity': input_from_user[entity['start']:entity['end']], 'Label of the Entity': entity['entity'], 'Description of the Label': abbreviations.get(entity['entity'])} for entity in res]
93
-
94
- df = pd.DataFrame(entities)
95
-
96
- st.table(df)
97
-
98
-
 
1
+ import streamlit as st
2
+ import spacy
3
+ from spacy import displacy
4
+ from transformers import AutoTokenizer, AutoModelForTokenClassification
5
+ from transformers import pipeline
6
+ import pandas as pd
7
+
8
+
9
+ st.title('Named Entity Recognizer')
10
+
11
+ st.write('Named Entity Recognition (NER) is like a smart highlighter that scans through text and highlights important words, such as people’s names, places, companies, and dates.')
12
+
13
+ st.write('')
14
+
15
+ with st.form(key='form_named_entity_recognition'):
16
+
17
+ input_from_user = st.text_area('enter your input')
18
+
19
+ model_options = st.selectbox('choose a model', ('Choose a model', 'Spacy\'s en_core_web_sm model', 'dslim/bert-base-NER model'))
20
+
21
+ submit_button = st.form_submit_button('Submit')
22
+
23
+
24
+ if submit_button:
25
+
26
+ if input_from_user == '':
27
+
28
+ st.error('empty form submitted')
29
+
30
+ else:
31
+
32
+ if model_options == 'Choose a model':
33
+
34
+ st.error('Please choose a model for named entity recognition')
35
+
36
+ else:
37
+
38
+ st.subheader('Result Analysis')
39
+
40
+ if model_options == 'Choose a model':
41
+
42
+ st.error('Please choose a model for Named Entity Recognition')
43
+
44
+ elif model_options == 'Spacy\'s en_core_web_sm model':
45
+
46
+ st.write('Model Used for Named Entity Recognition:')
47
+ st.success(model_options)
48
+
49
+ spacy_model = spacy.load('en_core_web_sm')
50
+
51
+ res = spacy_model(input_from_user)
52
+
53
+ st.write(f'Analysis of the detected entities from the text ==>')
54
+ st.markdown(f'**{input_from_user}**')
55
+
56
+ entities = [{'Entity': entity.text, 'Label of the Entity': entity.label_, 'Description of the Label': spacy.explain(entity.label_)} for entity in res.ents]
57
+
58
+ df = pd.DataFrame(entities)
59
+
60
+ st.table(df)
61
+
62
+ st.write('Entites marked in the input text:')
63
+ st.markdown(displacy.render(res, style='ent'), unsafe_allow_html=True)
64
+
65
+ elif model_options == 'dslim/bert-base-NER model':
66
+
67
+ st.write('Model Used for Named Entity Recognition:')
68
+ st.success(model_options)
69
+
70
+ tokenizer = AutoTokenizer.from_pretrained("dslim/bert-base-NER")
71
+ model = AutoModelForTokenClassification.from_pretrained("dslim/bert-base-NER")
72
+
73
+ bert_ner_model = pipeline('ner', model=model, tokenizer=tokenizer)
74
+
75
+ res = bert_ner_model(input_from_user)
76
+
77
+ abbreviations = {
78
+ "O": "Outside of a named entity",
79
+ "B-MISC": "Beginning of a miscellaneous entity right after another miscellaneous entity",
80
+ "I-MISC": "Miscellaneous entity",
81
+ "B-PER": "Beginning of a person’s name right after another person’s name",
82
+ "I-PER": "Person’s name",
83
+ "B-ORG": "Beginning of an organization right after another organization",
84
+ "I-ORG": "Organization",
85
+ "B-LOC": "Beginning of a location right after another location",
86
+ "I-LOC": "Location"
87
+ }
88
+
89
+ st.write(f'Analysis of the detected entities from the text ==>')
90
+ st.markdown(f'**{input_from_user}**')
91
+
92
+ entities = [{'Entity': input_from_user[entity['start']:entity['end']], 'Label of the Entity': entity['entity'], 'Description of the Label': abbreviations.get(entity['entity'])} for entity in res]
93
+
94
+ df = pd.DataFrame(entities)
95
+
96
+ st.table(df)
97
+
98
+