Spaces:
Running
Running
Create the first version
Browse files
app.py
ADDED
@@ -0,0 +1,48 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import streamlit as st
|
2 |
+
from st_annotated_text import annotated_text
|
3 |
+
from refined.inference.processor import Refined
|
4 |
+
|
5 |
+
from PIL import Image
|
6 |
+
|
7 |
+
# Load WordLift Logo
|
8 |
+
image = Image.open('WordLift_logo.png')
|
9 |
+
|
10 |
+
# Initiate the model
|
11 |
+
model_options = {"aida_model", "wikipedia_model_with_numbers"}
|
12 |
+
selected_model = st.sidebar.selectbox("Select the Model", list(model_options))
|
13 |
+
|
14 |
+
# Load the pretrained model
|
15 |
+
refined_model = Refined.from_pretrained(model_name=selected_model, entity_set="wikipedia")
|
16 |
+
|
17 |
+
# Create the form
|
18 |
+
with st.form(key='my_form'):
|
19 |
+
st.sidebar.image(image, caption='', use_column_width=True)
|
20 |
+
text_input = st.text_input(label='Enter a sentence')
|
21 |
+
submit_button = st.form_submit_button(label='Submit')
|
22 |
+
|
23 |
+
# Process the text and extract the entities
|
24 |
+
if text_input:
|
25 |
+
entities = refined_model.process_text(text_input)
|
26 |
+
|
27 |
+
entities_map = {}
|
28 |
+
entities_link_descriptions = {}
|
29 |
+
for entity in entities:
|
30 |
+
single_entity_list = str(entity).strip('][').replace("\'", "").split(', ')
|
31 |
+
if len(single_entity_list) >= 2 and "wikidata" in single_entity_list[1]:
|
32 |
+
entities_map[get_wikidata_id(single_entity_list[1]).strip()] = single_entity_list[0].strip()
|
33 |
+
entities_link_descriptions[get_wikidata_id(single_entity_list[1]).strip()] = single_entity_list[2].strip().replace("(", "").replace(")", "")
|
34 |
+
|
35 |
+
combined_entity_info_dictionary = dict([(k, [entities_map[k], entities_link_descriptions[k]]) for k in entities_map])
|
36 |
+
|
37 |
+
def get_entity_description(entity_link, combined_entity_info_dictionary):
|
38 |
+
return combined_entity_info_dictionary[entity_link][1]
|
39 |
+
|
40 |
+
annotations = []
|
41 |
+
for wikidata_link, entity in entities_map.items():
|
42 |
+
description = get_entity_description(wikidata_link, combined_entity_info_dictionary)
|
43 |
+
annotations.append((entity, wikidata_link, description))
|
44 |
+
st.write(entity + " , " + wikidata_link + " , " + description)
|
45 |
+
|
46 |
+
# Annotate text with entities
|
47 |
+
if submit_button:
|
48 |
+
annotated_text(*annotations)
|