Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,6 +1,7 @@
|
|
| 1 |
import streamlit as st
|
| 2 |
from annotated_text import annotated_text
|
| 3 |
from refined.inference.processor import Refined
|
|
|
|
| 4 |
|
| 5 |
# Sidebar
|
| 6 |
st.sidebar.image("logo-wordlift.png")
|
|
@@ -29,6 +30,14 @@ def get_wikidata_id(entity_string):
|
|
| 29 |
entity_link = "https://www.wikidata.org/wiki/" + entity_id
|
| 30 |
return {"id": entity_id, "link": entity_link}
|
| 31 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 32 |
# Create the form
|
| 33 |
with st.form(key='my_form'):
|
| 34 |
text_input = st.text_area(label='Enter a sentence')
|
|
@@ -39,17 +48,16 @@ if text_input:
|
|
| 39 |
entities = refined_model.process_text(text_input)
|
| 40 |
|
| 41 |
entities_map = {}
|
| 42 |
-
|
| 43 |
for entity in entities:
|
| 44 |
single_entity_list = str(entity).strip('][').replace("\'", "").split(', ')
|
| 45 |
if len(single_entity_list) >= 2 and "wikidata" in single_entity_list[1]:
|
| 46 |
entities_map[single_entity_list[0].strip()] = get_wikidata_id(single_entity_list[1])
|
| 47 |
-
|
|
|
|
|
|
|
| 48 |
|
| 49 |
-
combined_entity_info_dictionary = dict([(k, [entities_map[k],
|
| 50 |
-
|
| 51 |
-
def get_entity_description(entity_string, combined_entity_info_dictionary):
|
| 52 |
-
return combined_entity_info_dictionary[entity_string][1]
|
| 53 |
|
| 54 |
if submit_button:
|
| 55 |
# Prepare a list to hold the final output
|
|
@@ -57,13 +65,26 @@ if text_input:
|
|
| 57 |
|
| 58 |
# Replace each entity in the text with its annotated version
|
| 59 |
for entity_string, entity_info in entities_map.items():
|
| 60 |
-
|
| 61 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 62 |
text_input = text_input.replace(entity_string, f'{{{str(entity_annotation)}}}', 1)
|
| 63 |
|
| 64 |
# Split the modified text_input into a list
|
| 65 |
text_list = text_input.split("{")
|
| 66 |
-
|
| 67 |
for item in text_list:
|
| 68 |
if "}" in item:
|
| 69 |
item_list = item.split("}")
|
|
|
|
| 1 |
import streamlit as st
|
| 2 |
from annotated_text import annotated_text
|
| 3 |
from refined.inference.processor import Refined
|
| 4 |
+
import requests
|
| 5 |
|
| 6 |
# Sidebar
|
| 7 |
st.sidebar.image("logo-wordlift.png")
|
|
|
|
| 30 |
entity_link = "https://www.wikidata.org/wiki/" + entity_id
|
| 31 |
return {"id": entity_id, "link": entity_link}
|
| 32 |
|
| 33 |
+
def get_entity_data(entity_link):
|
| 34 |
+
try:
|
| 35 |
+
response = requests.get(f'https://api.wordlift.io/id/{entity_link}')
|
| 36 |
+
return response.json()
|
| 37 |
+
except Exception as e:
|
| 38 |
+
print(f"Exception when fetching data for entity: {entity_link}. Exception: {e}")
|
| 39 |
+
return None
|
| 40 |
+
|
| 41 |
# Create the form
|
| 42 |
with st.form(key='my_form'):
|
| 43 |
text_input = st.text_area(label='Enter a sentence')
|
|
|
|
| 48 |
entities = refined_model.process_text(text_input)
|
| 49 |
|
| 50 |
entities_map = {}
|
| 51 |
+
entities_data = {}
|
| 52 |
for entity in entities:
|
| 53 |
single_entity_list = str(entity).strip('][').replace("\'", "").split(', ')
|
| 54 |
if len(single_entity_list) >= 2 and "wikidata" in single_entity_list[1]:
|
| 55 |
entities_map[single_entity_list[0].strip()] = get_wikidata_id(single_entity_list[1])
|
| 56 |
+
entity_data = get_entity_data(entities_map[single_entity_list[0].strip()]["link"])
|
| 57 |
+
if entity_data is not None:
|
| 58 |
+
entities_data[single_entity_list[0].strip()] = entity_data
|
| 59 |
|
| 60 |
+
combined_entity_info_dictionary = dict([(k, [entities_map[k], entities_data[k] if k in entities_data else None]) for k in entities_map])
|
|
|
|
|
|
|
|
|
|
| 61 |
|
| 62 |
if submit_button:
|
| 63 |
# Prepare a list to hold the final output
|
|
|
|
| 65 |
|
| 66 |
# Replace each entity in the text with its annotated version
|
| 67 |
for entity_string, entity_info in entities_map.items():
|
| 68 |
+
entity_data = entities_data.get(entity_string, None)
|
| 69 |
+
entity_type = None
|
| 70 |
+
if entity_data is not None:
|
| 71 |
+
entity_type = entity_data.get("@type", None)
|
| 72 |
+
|
| 73 |
+
# Use different colors based on the entity's type
|
| 74 |
+
color = "#8ef" # Default color
|
| 75 |
+
if entity_type == "Place":
|
| 76 |
+
color = "#f00"
|
| 77 |
+
elif entity_type == "Organization":
|
| 78 |
+
color = "#0f0"
|
| 79 |
+
elif entity_type == "Person":
|
| 80 |
+
color = "#00f"
|
| 81 |
+
|
| 82 |
+
entity_annotation = (entity_string, entity_info["id"], color)
|
| 83 |
text_input = text_input.replace(entity_string, f'{{{str(entity_annotation)}}}', 1)
|
| 84 |
|
| 85 |
# Split the modified text_input into a list
|
| 86 |
text_list = text_input.split("{")
|
| 87 |
+
|
| 88 |
for item in text_list:
|
| 89 |
if "}" in item:
|
| 90 |
item_list = item.split("}")
|