File size: 1,990 Bytes
19d65ea
 
 
 
 
 
f62d856
19d65ea
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1eeaafa
19d65ea
 
 
 
 
 
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
import gradio as gr
import spacy
import medspacy
from medspacy.visualization import visualize_dep, visualize_ent
from spacy import displacy

med_ner = medspacy.load(r"./model-best")
def merge_tokens(tokens):
    merged_tokens = []
    for token in tokens:
        if merged_tokens and token['entity'].startswith('I-') and merged_tokens[-1]['entity'].endswith(token['entity'][2:]):
             # If current token continues the entity of the last one, merge them
            last_token = merged_tokens[-1]
            last_token['word'] += token['word'].replace('##', '')
            last_token['end'] = token['end']
#             last_token['score'] = (last_token['score'] + token['score']) / 2
        else:
            # Otherwise, add the token to the list
            merged_tokens.append(token)
            
    return merged_tokens

def ner(inp):
    output = med_ner(inp)
    formatted_ents = []
    for i in output.ents:
        ent = {}
        ent['entity']= i.label_
        ent['word']= i.text
        ent['start']= int(i.start_char)
        ent['end']= int(i.end_char)
        print(i.label_,"->",i.text,"->",i.start_char,"->",i.end_char,"->",type(i.start_char))
        formatted_ents.append(ent)
    print(formatted_ents)
    
    merged_tokens = merge_tokens(formatted_ents)
#     return {"text": str(inp), "entities": formatted_ents}
    return {"text": str(inp), "entities": merged_tokens}

demo = gr.Interface(fn=ner,
                   inputs=[gr.Textbox(label="Text to find entities", lines=2)],
                   outputs=[gr.HighlightedText(label="Text with entites")],
                   title="Custom-NER with Spacy3 and MedSpacy with v2 model",
                   description="Find medical entities using the NER model under the hood!",
                   allow_flagging = True,
                   examples=["Patient has hx of stroke. Mother diagnosed with diabetes. No evidence of pna.", "I have fever and cough since 2 days."]
                   )

demo.launch()