Beehzod commited on
Commit
bfa4c9d
·
verified ·
1 Parent(s): c0fa9a7

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +60 -11
app.py CHANGED
@@ -1,24 +1,73 @@
1
- # import streamlit as st
2
 
 
3
  # from transformers import pipeline
4
 
5
- # pipe = pipeline("text-classification", model="Beehzod/smart-finetuned-ner")
 
6
 
 
7
  # text = st.text_area('enter text: ')
 
 
 
 
 
 
8
 
9
- # if text:
10
- # out = pipe(text)
11
- # st.json(out)
12
  import streamlit as st
13
  from transformers import pipeline
14
 
15
  # Load the model from the Hugging Face Hub
16
  ner_pipeline = pipeline("ner", model="Beehzod/smart-finetuned-ner")
17
 
18
- # Example predictions
19
- text = st.text_area('enter text: ')
20
- results = ner_pipeline(text)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
21
 
22
- for entity in results:
23
- print(f"Entity: {entity['word']}, Label: {entity['entity']}, Score: {entity['score']:.4f}")
24
- st.json(entity)
 
 
 
 
 
 
 
 
 
1
 
2
+ # import streamlit as st
3
  # from transformers import pipeline
4
 
5
+ # # Load the model from the Hugging Face Hub
6
+ # ner_pipeline = pipeline("ner", model="Beehzod/smart-finetuned-ner")
7
 
8
+ # # Example predictions
9
  # text = st.text_area('enter text: ')
10
+ # results = ner_pipeline(text)
11
+
12
+ # for entity in results:
13
+ # print(f"Entity: {entity['word']}, Label: {entity['entity']}, Score: {entity['score']:.4f}")
14
+ # st.json(entity)
15
+
16
 
 
 
 
17
  import streamlit as st
18
  from transformers import pipeline
19
 
20
  # Load the model from the Hugging Face Hub
21
  ner_pipeline = pipeline("ner", model="Beehzod/smart-finetuned-ner")
22
 
23
+ # Helper function to combine subword tokens
24
+ def merge_entities(entities):
25
+ merged_entities = []
26
+ current_entity = {"word": "", "entity": None, "score": 0.0, "start": None, "end": None}
27
+
28
+ for token in entities:
29
+ # Check if it's a new entity or a continuation of the current one
30
+ if token['entity'].startswith('B-') or (current_entity['entity'] and token['entity'] != current_entity['entity']):
31
+ # Add the current entity to the list if it exists
32
+ if current_entity['entity']:
33
+ current_entity['score'] /= current_entity['count'] # average the score
34
+ del current_entity['count'] # remove helper key
35
+ merged_entities.append(current_entity)
36
+
37
+ # Start a new entity
38
+ current_entity = {
39
+ "word": token['word'].replace("##", ""),
40
+ "entity": token['entity'],
41
+ "score": token['score'],
42
+ "start": token['start'],
43
+ "end": token['end'],
44
+ "count": 1 # for averaging score later
45
+ }
46
+ else:
47
+ # Continue adding to the current entity
48
+ current_entity["word"] += token['word'].replace("##", "")
49
+ current_entity["end"] = token['end']
50
+ current_entity["score"] += token['score']
51
+ current_entity["count"] += 1
52
+
53
+ # Add the last entity
54
+ if current_entity['entity']:
55
+ current_entity['score'] /= current_entity['count']
56
+ del current_entity['count']
57
+ merged_entities.append(current_entity)
58
+
59
+ return merged_entities
60
+
61
+ # Get input text
62
+ text = st.text_area('Enter text: ')
63
 
64
+ # Run NER model if there is input text
65
+ if text:
66
+ results = ner_pipeline(text)
67
+ # Merge entities for clean output
68
+ merged_results = merge_entities(results)
69
+
70
+ # Display merged results
71
+ for entity in merged_results:
72
+ st.write(f"Entity: {entity['word']}, Label: {entity['entity']}, Score: {entity['score']:.4f}")
73
+ st.json(entity)