Spaces:
Running
Running
File size: 1,976 Bytes
ac736ed 5c91758 ac736ed 83e90d7 ac736ed 1aa7dda ac736ed 5c91758 83e90d7 9822204 3577a57 9822204 3577a57 9822204 3577a57 9822204 3577a57 9822204 5c91758 83e90d7 5c91758 cfadf58 83e90d7 cfadf58 83e90d7 3577a57 5c91758 3577a57 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 |
import streamlit as st
import spacy
from transformers import pipeline
from PIL import Image
nlp = spacy.load("./models/en_core_web_sm")
st.title("SpaGAN Demo")
st.write("Enter a text, and the system will highlight the geo-entities within it.")
# Define a color map and descriptions for different entity types
COLOR_MAP = {
'FAC': ('red', 'Facilities (e.g., buildings, airports)'),
'ORG': ('blue', 'Organizations (e.g., companies, institutions)'),
'LOC': ('purple', 'Locations (e.g., mountain ranges, water bodies)'),
'GPE': ('green', 'Geopolitical Entities (e.g., countries, cities)')
}
# Display the color key with descriptions
st.write("**Color Key:**")
for label, (color, description) in COLOR_MAP.items():
st.markdown(f"- **{label}**: <span style='color:{color}'>{color}</span> - {description}", unsafe_allow_html=True)
# Text input
user_input = st.text_area("Input Text", height=200)
# Process the text when the button is clicked
if st.button("Highlight Geo-Entities"):
if user_input.strip():
# Process the text using spaCy
doc = nlp(user_input)
# Generate highlighted text with different colors for each entity type
highlighted_text = ""
last_pos = 0
for ent in doc.ents:
color = COLOR_MAP.get(ent.label_, ('black', ''))[0] # Default to black if label not in map
# Add text before the entity
highlighted_text += user_input[last_pos:ent.start_char]
# Add the highlighted entity text
highlighted_text += f"<span style='color:{color}; font-weight:bold'>{ent.text}</span>"
# Update the position
last_pos = ent.end_char
# Add any remaining text after the last entity
highlighted_text += user_input[last_pos:]
# Display the highlighted text with HTML support
st.markdown(highlighted_text, unsafe_allow_html=True)
else:
st.error("Please enter some text.") |