cyberandy commited on
Commit
c9574f5
1 Parent(s): e985afd

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +16 -11
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
- entity_list = entity_string.split("=")
24
- return "https://www.wikidata.org/wiki/" + str(entity_list[1])
 
 
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='Submit')
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[get_wikidata_id(single_entity_list[1]).strip()] = single_entity_list[0].strip()
41
- entities_link_descriptions[get_wikidata_id(single_entity_list[1]).strip()] = single_entity_list[2].strip().replace("(", "").replace(")", "")
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(entity_link, combined_entity_info_dictionary):
46
- return combined_entity_info_dictionary[entity_link][1]
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 wikidata_link, entity in entities_map.items():
54
- description = get_entity_description(wikidata_link, combined_entity_info_dictionary)
55
- entity_annotation = (entity, description, "#8ef")
56
- text_input = text_input.replace(entity, f'{{{str(entity_annotation)}}}', 1)
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)