Spaces:
Running
Running
import streamlit as st | |
from st_annotated_text import annotated_text | |
from refined.inference.processor import Refined | |
from PIL import Image | |
# Load WordLift Logo | |
image = Image.open('WordLift_logo.png') | |
# Initiate the model | |
model_options = {"aida_model", "wikipedia_model_with_numbers"} | |
selected_model = st.sidebar.selectbox("Select the Model", list(model_options)) | |
# Load the pretrained model | |
refined_model = Refined.from_pretrained(model_name=selected_model, entity_set="wikipedia") | |
# Create the form | |
with st.form(key='my_form'): | |
st.sidebar.image(image, caption='', use_column_width=True) | |
text_input = st.text_input(label='Enter a sentence') | |
submit_button = st.form_submit_button(label='Submit') | |
# Process the text and extract the entities | |
if text_input: | |
entities = refined_model.process_text(text_input) | |
entities_map = {} | |
entities_link_descriptions = {} | |
for entity in entities: | |
single_entity_list = str(entity).strip('][').replace("\'", "").split(', ') | |
if len(single_entity_list) >= 2 and "wikidata" in single_entity_list[1]: | |
entities_map[get_wikidata_id(single_entity_list[1]).strip()] = single_entity_list[0].strip() | |
entities_link_descriptions[get_wikidata_id(single_entity_list[1]).strip()] = single_entity_list[2].strip().replace("(", "").replace(")", "") | |
combined_entity_info_dictionary = dict([(k, [entities_map[k], entities_link_descriptions[k]]) for k in entities_map]) | |
def get_entity_description(entity_link, combined_entity_info_dictionary): | |
return combined_entity_info_dictionary[entity_link][1] | |
annotations = [] | |
for wikidata_link, entity in entities_map.items(): | |
description = get_entity_description(wikidata_link, combined_entity_info_dictionary) | |
annotations.append((entity, wikidata_link, description)) | |
st.write(entity + " , " + wikidata_link + " , " + description) | |
# Annotate text with entities | |
if submit_button: | |
annotated_text(*annotations) | |