import gradio as gr from spacy import displacy import spacy med7 = spacy.load("en_core_med7_lg") def get_med7_ent(text): # create distinct colours for labels col_dict = {} seven_colours = ['#e6194B', '#3cb44b', '#ffe119', '#ffd8b1', '#f58231', '#f032e6', '#42d4f4'] for label, colour in zip(med7.pipe_labels['ner'], seven_colours): col_dict[label] = colour options = {'ents': med7.pipe_labels['ner'], 'colors':col_dict} # text = 'A patient was prescribed Magnesium hydroxide 400mg/5ml suspension PO of total 30ml bid for the next 5 days.' doc = med7(text) # spacy.displacy.render(doc, style='ent', jupyter=True, options=options) # [(ent.text, ent.label_) for ent in doc.ents] html = displacy.render(doc, style="ent", jupyter=True, options=options) return html exp=["A patient was prescribed Magnesium hydroxide 400mg/5ml suspension PO of total 30ml bid for the next 5 days."] desc="Med7 — an information extraction model for clinical natural language processing" inp=gr.inputs.Textbox(lines=5, placeholder=None, default="", label="text to extract Med7 Entities") out=gr.outputs.HTML(label=None) iface = gr.Interface(fn=get_med7_ent, inputs=inp, outputs=out,examples=exp,article=desc,title="Med7",theme="huggingface",layout='horizontal') iface.launch()