Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
|
@@ -20,13 +20,15 @@ refined_model = load_model(selected_model_name)
|
|
| 20 |
|
| 21 |
# Helper functions
|
| 22 |
def get_wikidata_id(entity_string):
|
| 23 |
-
|
| 24 |
-
|
|
|
|
|
|
|
| 25 |
|
| 26 |
# Create the form
|
| 27 |
with st.form(key='my_form'):
|
| 28 |
text_input = st.text_input(label='Enter a sentence')
|
| 29 |
-
submit_button = st.form_submit_button(label='
|
| 30 |
|
| 31 |
# Process the text and extract the entities
|
| 32 |
if text_input:
|
|
@@ -37,23 +39,23 @@ if text_input:
|
|
| 37 |
for entity in entities:
|
| 38 |
single_entity_list = str(entity).strip('][').replace("\'", "").split(', ')
|
| 39 |
if len(single_entity_list) >= 2 and "wikidata" in single_entity_list[1]:
|
| 40 |
-
entities_map[
|
| 41 |
-
entities_link_descriptions[
|
| 42 |
|
| 43 |
combined_entity_info_dictionary = dict([(k, [entities_map[k], entities_link_descriptions[k]]) for k in entities_map])
|
| 44 |
|
| 45 |
-
def get_entity_description(
|
| 46 |
-
return combined_entity_info_dictionary[
|
| 47 |
|
| 48 |
if submit_button:
|
| 49 |
# Prepare a list to hold the final output
|
| 50 |
final_text = []
|
| 51 |
|
| 52 |
# Replace each entity in the text with its annotated version
|
| 53 |
-
for
|
| 54 |
-
description = get_entity_description(
|
| 55 |
-
entity_annotation = (
|
| 56 |
-
text_input = text_input.replace(
|
| 57 |
|
| 58 |
# Split the modified text_input into a list
|
| 59 |
text_list = text_input.split("{")
|
|
@@ -69,3 +71,6 @@ if text_input:
|
|
| 69 |
|
| 70 |
# Pass the final_text to the annotated_text function
|
| 71 |
annotated_text(*final_text)
|
|
|
|
|
|
|
|
|
|
|
|
| 20 |
|
| 21 |
# Helper functions
|
| 22 |
def get_wikidata_id(entity_string):
|
| 23 |
+
entity_list = entity_string.split("=")
|
| 24 |
+
entity_id = str(entity_list[1])
|
| 25 |
+
entity_link = "https://www.wikidata.org/wiki/" + entity_id
|
| 26 |
+
return {"id": entity_id, "link": entity_link}
|
| 27 |
|
| 28 |
# Create the form
|
| 29 |
with st.form(key='my_form'):
|
| 30 |
text_input = st.text_input(label='Enter a sentence')
|
| 31 |
+
submit_button = st.form_submit_button(label='Analyze')
|
| 32 |
|
| 33 |
# Process the text and extract the entities
|
| 34 |
if text_input:
|
|
|
|
| 39 |
for entity in entities:
|
| 40 |
single_entity_list = str(entity).strip('][').replace("\'", "").split(', ')
|
| 41 |
if len(single_entity_list) >= 2 and "wikidata" in single_entity_list[1]:
|
| 42 |
+
entities_map[single_entity_list[0].strip()] = get_wikidata_id(single_entity_list[1]).strip()
|
| 43 |
+
entities_link_descriptions[single_entity_list[0].strip()] = single_entity_list[2].strip().replace("(", "").replace(")", "")
|
| 44 |
|
| 45 |
combined_entity_info_dictionary = dict([(k, [entities_map[k], entities_link_descriptions[k]]) for k in entities_map])
|
| 46 |
|
| 47 |
+
def get_entity_description(entity_string, combined_entity_info_dictionary):
|
| 48 |
+
return combined_entity_info_dictionary[entity_string][1]
|
| 49 |
|
| 50 |
if submit_button:
|
| 51 |
# Prepare a list to hold the final output
|
| 52 |
final_text = []
|
| 53 |
|
| 54 |
# Replace each entity in the text with its annotated version
|
| 55 |
+
for entity_string, entity_info in entities_map.items():
|
| 56 |
+
description = get_entity_description(entity_string, combined_entity_info_dictionary)
|
| 57 |
+
entity_annotation = (entity_string, entity_info["id"], "#8ef") # Use the entity ID in the annotation
|
| 58 |
+
text_input = text_input.replace(entity_string, f'{{{str(entity_annotation)}}}', 1)
|
| 59 |
|
| 60 |
# Split the modified text_input into a list
|
| 61 |
text_list = text_input.split("{")
|
|
|
|
| 71 |
|
| 72 |
# Pass the final_text to the annotated_text function
|
| 73 |
annotated_text(*final_text)
|
| 74 |
+
|
| 75 |
+
with st.expander("See annotations"):
|
| 76 |
+
st.write(final_text)
|