Spaces:
Running
Running
Update src/streamlit_app.py
Browse files- src/streamlit_app.py +26 -19
src/streamlit_app.py
CHANGED
|
@@ -13,6 +13,7 @@ from streamlit_extras.stylable_container import stylable_container
|
|
| 13 |
from typing import Optional
|
| 14 |
from gliner import GLiNER
|
| 15 |
from comet_ml import Experiment
|
|
|
|
| 16 |
st.markdown(
|
| 17 |
"""
|
| 18 |
<style>
|
|
@@ -21,37 +22,31 @@ st.markdown(
|
|
| 21 |
background-color: #E8F5E9; /* A very light green */
|
| 22 |
color: #1B5E20; /* Dark green for the text */
|
| 23 |
}
|
| 24 |
-
|
| 25 |
-
/* Sidebar background color */
|
| 26 |
.css-1d36184 {
|
| 27 |
background-color: #A5D6A7; /* A medium light green */
|
| 28 |
secondary-background-color: #A5D6A7;
|
| 29 |
}
|
| 30 |
-
|
| 31 |
-
/* Expander background color and header */
|
| 32 |
.streamlit-expanderContent, .streamlit-expanderHeader {
|
| 33 |
background-color: #E8F5E9;
|
| 34 |
}
|
| 35 |
-
|
| 36 |
-
/* Text Area background and text color */
|
| 37 |
.stTextArea textarea {
|
| 38 |
background-color: #81C784; /* A slightly darker medium green */
|
| 39 |
color: #1B5E20; /* Dark green for text */
|
| 40 |
}
|
| 41 |
-
|
| 42 |
-
/* Button background and text color */
|
| 43 |
.stButton > button {
|
| 44 |
background-color: #81C784;
|
| 45 |
color: #1B5E20;
|
| 46 |
}
|
| 47 |
-
|
| 48 |
-
/* Warning box background and text color */
|
| 49 |
.stAlert.st-warning {
|
| 50 |
background-color: #66BB6A; /* A medium-dark green for the warning box */
|
| 51 |
color: #1B5E20;
|
| 52 |
}
|
| 53 |
-
|
| 54 |
-
/* Success box background and text color */
|
| 55 |
.stAlert.st-success {
|
| 56 |
background-color: #66BB6A; /* A medium-dark green for the success box */
|
| 57 |
color: #1B5E20;
|
|
@@ -59,6 +54,7 @@ st.markdown(
|
|
| 59 |
</style>
|
| 60 |
""",
|
| 61 |
unsafe_allow_html=True)
|
|
|
|
| 62 |
# --- Page Configuration and UI Elements ---
|
| 63 |
st.set_page_config(layout="wide", page_title="Named Entity Recognition App")
|
| 64 |
st.subheader("DataHarvest", divider="violet")
|
|
@@ -94,6 +90,7 @@ with st.sidebar:
|
|
| 94 |
st.divider()
|
| 95 |
st.subheader("π Ready to build your own AI Web App?", divider="violet")
|
| 96 |
st.link_button("AI Web App Builder", "https://nlpblogs.com/build-your-named-entity-recognition-app/", type="primary")
|
|
|
|
| 97 |
# --- Comet ML Setup ---
|
| 98 |
COMET_API_KEY = os.environ.get("COMET_API_KEY")
|
| 99 |
COMET_WORKSPACE = os.environ.get("COMET_WORKSPACE")
|
|
@@ -101,6 +98,7 @@ COMET_PROJECT_NAME = os.environ.get("COMET_PROJECT_NAME")
|
|
| 101 |
comet_initialized = bool(COMET_API_KEY and COMET_WORKSPACE and COMET_PROJECT_NAME)
|
| 102 |
if not comet_initialized:
|
| 103 |
st.warning("Comet ML not initialized. Check environment variables.")
|
|
|
|
| 104 |
# --- Label Definitions ---
|
| 105 |
labels = ["person", "country", "city", "organization", "date", "time", "cardinal", "money", "position"]
|
| 106 |
# Corrected mapping dictionary
|
|
@@ -110,6 +108,7 @@ category_mapping = {
|
|
| 110 |
"Locations": ["country", "city"],
|
| 111 |
"Time": ["date", "time"],
|
| 112 |
"Numbers": ["money", "cardinal"]}
|
|
|
|
| 113 |
# --- Model Loading ---
|
| 114 |
@st.cache_resource
|
| 115 |
def load_ner_model():
|
|
@@ -119,30 +118,36 @@ def load_ner_model():
|
|
| 119 |
except Exception as e:
|
| 120 |
st.error(f"Failed to load NER model. Please check your internet connection or model availability: {e}")
|
| 121 |
st.stop()
|
|
|
|
| 122 |
model = load_ner_model()
|
| 123 |
# Flatten the mapping to a single dictionary
|
| 124 |
reverse_category_mapping = {label: category for category, label_list in category_mapping.items() for label in label_list}
|
|
|
|
| 125 |
# --- Text Input and Clear Button ---
|
| 126 |
word_limit = 200
|
| 127 |
text = st.text_area(f"Type or paste your text below (max {word_limit} words), and then press Ctrl + Enter", height=250, key='my_text_area')
|
| 128 |
word_count = len(text.split())
|
| 129 |
st.markdown(f"**Word count:** {word_count}/{word_limit}")
|
|
|
|
| 130 |
def clear_text():
|
| 131 |
"""Clears the text area."""
|
| 132 |
st.session_state['my_text_area'] = ""
|
|
|
|
| 133 |
def remove_punctuation(text):
|
| 134 |
"""Removes punctuation from a string."""
|
| 135 |
translator = str.maketrans('', '', string.punctuation)
|
| 136 |
return text.translate(translator)
|
|
|
|
| 137 |
st.button("Clear text", on_click=clear_text)
|
|
|
|
| 138 |
# --- Results Section ---
|
| 139 |
if st.button("Results"):
|
| 140 |
-
start_time = time.time()
|
| 141 |
if not text.strip():
|
| 142 |
st.warning("Please enter some text to extract entities.")
|
| 143 |
elif word_count > word_limit:
|
| 144 |
st.warning(f"Your text exceeds the {word_limit} word limit. Please shorten it to continue.")
|
| 145 |
else:
|
|
|
|
| 146 |
# Call the new function to remove punctuation from the input text
|
| 147 |
cleaned_text = remove_punctuation(text)
|
| 148 |
with st.spinner("Extracting entities...", show_time=True):
|
|
@@ -245,10 +250,12 @@ if st.button("Results"):
|
|
| 245 |
if comet_initialized:
|
| 246 |
experiment.log_figure(figure=fig_treemap, figure_name="entity_treemap_categories")
|
| 247 |
experiment.end()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 248 |
else: # If df is empty
|
| 249 |
-
st.warning("No entities were found in the provided text.")
|
| 250 |
-
end_time = time.time()
|
| 251 |
-
elapsed_time = end_time - start_time
|
| 252 |
-
st.text("")
|
| 253 |
-
st.text("")
|
| 254 |
-
st.info(f"Results processed in **{elapsed_time:.2f} seconds**.")
|
|
|
|
| 13 |
from typing import Optional
|
| 14 |
from gliner import GLiNER
|
| 15 |
from comet_ml import Experiment
|
| 16 |
+
|
| 17 |
st.markdown(
|
| 18 |
"""
|
| 19 |
<style>
|
|
|
|
| 22 |
background-color: #E8F5E9; /* A very light green */
|
| 23 |
color: #1B5E20; /* Dark green for the text */
|
| 24 |
}
|
| 25 |
+
/* Sidebar background color */
|
|
|
|
| 26 |
.css-1d36184 {
|
| 27 |
background-color: #A5D6A7; /* A medium light green */
|
| 28 |
secondary-background-color: #A5D6A7;
|
| 29 |
}
|
| 30 |
+
/* Expander background color and header */
|
|
|
|
| 31 |
.streamlit-expanderContent, .streamlit-expanderHeader {
|
| 32 |
background-color: #E8F5E9;
|
| 33 |
}
|
| 34 |
+
/* Text Area background and text color */
|
|
|
|
| 35 |
.stTextArea textarea {
|
| 36 |
background-color: #81C784; /* A slightly darker medium green */
|
| 37 |
color: #1B5E20; /* Dark green for text */
|
| 38 |
}
|
| 39 |
+
/* Button background and text color */
|
|
|
|
| 40 |
.stButton > button {
|
| 41 |
background-color: #81C784;
|
| 42 |
color: #1B5E20;
|
| 43 |
}
|
| 44 |
+
/* Warning box background and text color */
|
|
|
|
| 45 |
.stAlert.st-warning {
|
| 46 |
background-color: #66BB6A; /* A medium-dark green for the warning box */
|
| 47 |
color: #1B5E20;
|
| 48 |
}
|
| 49 |
+
/* Success box background and text color */
|
|
|
|
| 50 |
.stAlert.st-success {
|
| 51 |
background-color: #66BB6A; /* A medium-dark green for the success box */
|
| 52 |
color: #1B5E20;
|
|
|
|
| 54 |
</style>
|
| 55 |
""",
|
| 56 |
unsafe_allow_html=True)
|
| 57 |
+
|
| 58 |
# --- Page Configuration and UI Elements ---
|
| 59 |
st.set_page_config(layout="wide", page_title="Named Entity Recognition App")
|
| 60 |
st.subheader("DataHarvest", divider="violet")
|
|
|
|
| 90 |
st.divider()
|
| 91 |
st.subheader("π Ready to build your own AI Web App?", divider="violet")
|
| 92 |
st.link_button("AI Web App Builder", "https://nlpblogs.com/build-your-named-entity-recognition-app/", type="primary")
|
| 93 |
+
|
| 94 |
# --- Comet ML Setup ---
|
| 95 |
COMET_API_KEY = os.environ.get("COMET_API_KEY")
|
| 96 |
COMET_WORKSPACE = os.environ.get("COMET_WORKSPACE")
|
|
|
|
| 98 |
comet_initialized = bool(COMET_API_KEY and COMET_WORKSPACE and COMET_PROJECT_NAME)
|
| 99 |
if not comet_initialized:
|
| 100 |
st.warning("Comet ML not initialized. Check environment variables.")
|
| 101 |
+
|
| 102 |
# --- Label Definitions ---
|
| 103 |
labels = ["person", "country", "city", "organization", "date", "time", "cardinal", "money", "position"]
|
| 104 |
# Corrected mapping dictionary
|
|
|
|
| 108 |
"Locations": ["country", "city"],
|
| 109 |
"Time": ["date", "time"],
|
| 110 |
"Numbers": ["money", "cardinal"]}
|
| 111 |
+
|
| 112 |
# --- Model Loading ---
|
| 113 |
@st.cache_resource
|
| 114 |
def load_ner_model():
|
|
|
|
| 118 |
except Exception as e:
|
| 119 |
st.error(f"Failed to load NER model. Please check your internet connection or model availability: {e}")
|
| 120 |
st.stop()
|
| 121 |
+
|
| 122 |
model = load_ner_model()
|
| 123 |
# Flatten the mapping to a single dictionary
|
| 124 |
reverse_category_mapping = {label: category for category, label_list in category_mapping.items() for label in label_list}
|
| 125 |
+
|
| 126 |
# --- Text Input and Clear Button ---
|
| 127 |
word_limit = 200
|
| 128 |
text = st.text_area(f"Type or paste your text below (max {word_limit} words), and then press Ctrl + Enter", height=250, key='my_text_area')
|
| 129 |
word_count = len(text.split())
|
| 130 |
st.markdown(f"**Word count:** {word_count}/{word_limit}")
|
| 131 |
+
|
| 132 |
def clear_text():
|
| 133 |
"""Clears the text area."""
|
| 134 |
st.session_state['my_text_area'] = ""
|
| 135 |
+
|
| 136 |
def remove_punctuation(text):
|
| 137 |
"""Removes punctuation from a string."""
|
| 138 |
translator = str.maketrans('', '', string.punctuation)
|
| 139 |
return text.translate(translator)
|
| 140 |
+
|
| 141 |
st.button("Clear text", on_click=clear_text)
|
| 142 |
+
|
| 143 |
# --- Results Section ---
|
| 144 |
if st.button("Results"):
|
|
|
|
| 145 |
if not text.strip():
|
| 146 |
st.warning("Please enter some text to extract entities.")
|
| 147 |
elif word_count > word_limit:
|
| 148 |
st.warning(f"Your text exceeds the {word_limit} word limit. Please shorten it to continue.")
|
| 149 |
else:
|
| 150 |
+
start_time = time.time()
|
| 151 |
# Call the new function to remove punctuation from the input text
|
| 152 |
cleaned_text = remove_punctuation(text)
|
| 153 |
with st.spinner("Extracting entities...", show_time=True):
|
|
|
|
| 250 |
if comet_initialized:
|
| 251 |
experiment.log_figure(figure=fig_treemap, figure_name="entity_treemap_categories")
|
| 252 |
experiment.end()
|
| 253 |
+
|
| 254 |
+
# Corrected placement for time calculation and display
|
| 255 |
+
end_time = time.time()
|
| 256 |
+
elapsed_time = end_time - start_time
|
| 257 |
+
st.text("")
|
| 258 |
+
st.text("")
|
| 259 |
+
st.info(f"Results processed in **{elapsed_time:.2f} seconds**.")
|
| 260 |
else: # If df is empty
|
| 261 |
+
st.warning("No entities were found in the provided text.")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|