import gradio as gr from transformers import AutoTokenizer, AutoModelForTokenClassification, pipeline def convert_hf_ents_to_gradio(hf_ents): gradio_ents = [] for hf_ent in hf_ents: gradio_ent = {"start" : hf_ent['start'], "end": hf_ent['end'], "entity": hf_ent['entity_group']} gradio_ents.append(gradio_ent) return gradio_ents def tag(text): hf_ents = nlp(text, aggregation_strategy="first") gradio_ents = convert_hf_ents_to_gradio(hf_ents) doc ={"text": text, "entities": gradio_ents} return doc if __name__ == "__main__": model_ckpt = "carolanderson/roberta-base-food-ner" model = AutoModelForTokenClassification.from_pretrained(model_ckpt) tokenizer = AutoTokenizer.from_pretrained("roberta-base", add_prefix_space=True) nlp = pipeline("ner", model=model, tokenizer=tokenizer) with open("app_text/blog_text.md", "r") as f: blog_text = f.read() examples=[ ["Saute the onions in olive oil until browned."], ["Add bourbon and sweet vermouth to the shaker."], ["Salt the water and butter the bread."], ["Add salt to the water and spread butter on the bread."]] with gr.Blocks() as demo: gr.Markdown("# Extracting Food Mentions from Text") html = ("