Spaces:
Sleeping
Sleeping
Update src/streamlit_app.py
Browse files- src/streamlit_app.py +11 -51
src/streamlit_app.py
CHANGED
|
@@ -12,7 +12,6 @@ from streamlit_extras.stylable_container import stylable_container
|
|
| 12 |
from typing import Optional
|
| 13 |
from gliner import GLiNER
|
| 14 |
from comet_ml import Experiment
|
| 15 |
-
|
| 16 |
# --- CSS Styling for the App ---
|
| 17 |
st.markdown(
|
| 18 |
"""
|
|
@@ -22,36 +21,30 @@ st.markdown(
|
|
| 22 |
background-color: #E8F5E9; /* A very light green */
|
| 23 |
color: #1B5E20; /* Dark green for the text */
|
| 24 |
}
|
| 25 |
-
|
| 26 |
/* Sidebar background color */
|
| 27 |
.css-1d36184 {
|
| 28 |
background-color: #A5D6A7; /* A medium light green */
|
| 29 |
secondary-background-color: #A5D6A7;
|
| 30 |
}
|
| 31 |
-
|
| 32 |
/* Expander background color and header */
|
| 33 |
.streamlit-expanderContent, .streamlit-expanderHeader {
|
| 34 |
background-color: #E8F5E9;
|
| 35 |
}
|
| 36 |
-
|
| 37 |
/* Text Area background and text color */
|
| 38 |
.stTextArea textarea {
|
| 39 |
background-color: #81C784; /* A slightly darker medium green */
|
| 40 |
color: #1B5E20; /* Dark green for text */
|
| 41 |
}
|
| 42 |
-
|
| 43 |
/* Button background and text color */
|
| 44 |
.stButton > button {
|
| 45 |
background-color: #81C784;
|
| 46 |
color: #1B5E20;
|
| 47 |
}
|
| 48 |
-
|
| 49 |
/* Warning box background and text color */
|
| 50 |
.stAlert.st-warning {
|
| 51 |
background-color: #66BB6A; /* A medium-dark green for the warning box */
|
| 52 |
color: #1B5E20;
|
| 53 |
}
|
| 54 |
-
|
| 55 |
/* Success box background and text color */
|
| 56 |
.stAlert.st-success {
|
| 57 |
background-color: #66BB6A; /* A medium-dark green for the success box */
|
|
@@ -59,18 +52,13 @@ st.markdown(
|
|
| 59 |
}
|
| 60 |
</style>
|
| 61 |
""",
|
| 62 |
-
unsafe_allow_html=True
|
| 63 |
-
)
|
| 64 |
-
|
| 65 |
# --- Page Configuration and UI Elements ---
|
| 66 |
st.set_page_config(layout="wide", page_title="Named Entity Recognition App")
|
| 67 |
st.subheader("RetailTag", divider="violet")
|
| 68 |
st.link_button("by nlpblogs", "https://nlpblogs.com", type="tertiary")
|
| 69 |
-
|
| 70 |
expander = st.expander("**Important notes**")
|
| 71 |
-
expander.write("""
|
| 72 |
-
**Named Entities:** This RetailTag web app predicts eighteen (18) labels:
|
| 73 |
-
"Product_Name", "Product_Type", "Brand", "Model_Number", "SKU", "Product_Attribute", "Service_Type", "Order_Number", "Monetary_Value", "Payment_Method", "Discount", "Shipping_Method", "Quantity", "Organization", "Location", "Person", "Date", "Time"
|
| 74 |
|
| 75 |
Results are presented in easy-to-read tables, visualized in an interactive tree map, pie chart and bar chart, and are available for download along with a Glossary of tags.
|
| 76 |
|
|
@@ -87,13 +75,7 @@ For any errors or inquiries, please contact us at info@nlpblogs.com""")
|
|
| 87 |
with st.sidebar:
|
| 88 |
st.write("Use the following code to embed the RetailTag web app on your website. Feel free to adjust the width and height values to fit your page.")
|
| 89 |
code = '''
|
| 90 |
-
<iframe
|
| 91 |
-
src="https://aiecosystem-retailtag.hf.space"
|
| 92 |
-
frameborder="0"
|
| 93 |
-
width="850"
|
| 94 |
-
height="450"
|
| 95 |
-
></iframe>
|
| 96 |
-
|
| 97 |
'''
|
| 98 |
st.code(code, language="html")
|
| 99 |
st.text("")
|
|
@@ -101,7 +83,6 @@ with st.sidebar:
|
|
| 101 |
st.divider()
|
| 102 |
st.subheader("🚀 Ready to build your own AI Web App?", divider="violet")
|
| 103 |
st.link_button("AI Web App Builder", "https://nlpblogs.com/build-your-named-entity-recognition-app/", type="primary")
|
| 104 |
-
|
| 105 |
# --- Comet ML Setup ---
|
| 106 |
COMET_API_KEY = os.environ.get("COMET_API_KEY")
|
| 107 |
COMET_WORKSPACE = os.environ.get("COMET_WORKSPACE")
|
|
@@ -109,7 +90,6 @@ COMET_PROJECT_NAME = os.environ.get("COMET_PROJECT_NAME")
|
|
| 109 |
comet_initialized = bool(COMET_API_KEY and COMET_WORKSPACE and COMET_PROJECT_NAME)
|
| 110 |
if not comet_initialized:
|
| 111 |
st.warning("Comet ML not initialized. Check environment variables.")
|
| 112 |
-
|
| 113 |
# --- Label Definitions ---
|
| 114 |
# The list of labels is assigned to a variable for use in the model loading function
|
| 115 |
labels = [
|
|
@@ -130,9 +110,7 @@ labels = [
|
|
| 130 |
"Location",
|
| 131 |
"Person",
|
| 132 |
"Date",
|
| 133 |
-
"Time"
|
| 134 |
-
]
|
| 135 |
-
|
| 136 |
# Create a mapping dictionary for labels to categories
|
| 137 |
category_mapping = {
|
| 138 |
"Product & Service Entities": [
|
|
@@ -158,9 +136,7 @@ category_mapping = {
|
|
| 158 |
"Person",
|
| 159 |
"Date",
|
| 160 |
"Time"
|
| 161 |
-
]
|
| 162 |
-
}
|
| 163 |
-
|
| 164 |
# --- Model Loading ---
|
| 165 |
@st.cache_resource
|
| 166 |
def load_ner_model():
|
|
@@ -171,33 +147,31 @@ def load_ner_model():
|
|
| 171 |
except Exception as e:
|
| 172 |
st.error(f"Failed to load NER model. Please check your internet connection or model availability: {e}")
|
| 173 |
st.stop()
|
| 174 |
-
|
| 175 |
model = load_ner_model()
|
| 176 |
-
|
| 177 |
# Flatten the mapping to a single dictionary
|
| 178 |
reverse_category_mapping = {label: category for category, label_list in category_mapping.items() for label in label_list}
|
| 179 |
-
|
| 180 |
# --- Text Input and Clear Button ---
|
| 181 |
-
|
| 182 |
-
|
|
|
|
|
|
|
| 183 |
def clear_text():
|
| 184 |
"""Clears the text area."""
|
| 185 |
st.session_state['my_text_area'] = ""
|
| 186 |
st.button("Clear text", on_click=clear_text)
|
| 187 |
-
|
| 188 |
# --- Results Section ---
|
| 189 |
if st.button("Results"):
|
| 190 |
start_time = time.time()
|
| 191 |
if not text.strip():
|
| 192 |
st.warning("Please enter some text to extract entities.")
|
|
|
|
|
|
|
| 193 |
else:
|
| 194 |
with st.spinner("Extracting entities...", show_time=True):
|
| 195 |
entities = model.predict_entities(text, labels)
|
| 196 |
df = pd.DataFrame(entities)
|
| 197 |
-
|
| 198 |
if not df.empty:
|
| 199 |
df['category'] = df['label'].map(reverse_category_mapping)
|
| 200 |
-
|
| 201 |
if comet_initialized:
|
| 202 |
experiment = Experiment(
|
| 203 |
api_key=COMET_API_KEY,
|
|
@@ -206,13 +180,10 @@ if st.button("Results"):
|
|
| 206 |
)
|
| 207 |
experiment.log_parameter("input_text", text)
|
| 208 |
experiment.log_table("predicted_entities", df)
|
| 209 |
-
|
| 210 |
st.subheader("Grouped Entities by Category", divider="violet")
|
| 211 |
-
|
| 212 |
# Create tabs for each category
|
| 213 |
category_names = sorted(list(category_mapping.keys()))
|
| 214 |
category_tabs = st.tabs(category_names)
|
| 215 |
-
|
| 216 |
for i, category_name in enumerate(category_names):
|
| 217 |
with category_tabs[i]:
|
| 218 |
df_category_filtered = df[df['category'] == category_name]
|
|
@@ -220,7 +191,6 @@ if st.button("Results"):
|
|
| 220 |
st.dataframe(df_category_filtered.drop(columns=['category']), use_container_width=True)
|
| 221 |
else:
|
| 222 |
st.info(f"No entities found for the '{category_name}' category.")
|
| 223 |
-
|
| 224 |
with st.expander("See Glossary of tags"):
|
| 225 |
st.write('''
|
| 226 |
- **text**: ['entity extracted from your text data']
|
|
@@ -230,18 +200,15 @@ if st.button("Results"):
|
|
| 230 |
- **end**: ['index of the end of the corresponding entity']
|
| 231 |
''')
|
| 232 |
st.divider()
|
| 233 |
-
|
| 234 |
# Tree map
|
| 235 |
st.subheader("Tree map", divider="violet")
|
| 236 |
fig_treemap = px.treemap(df, path=[px.Constant("all"), 'category', 'label', 'text'], values='score', color='category')
|
| 237 |
fig_treemap.update_layout(margin=dict(t=50, l=25, r=25, b=25), paper_bgcolor='#E8F5E9', plot_bgcolor='#E8F5E9')
|
| 238 |
st.plotly_chart(fig_treemap)
|
| 239 |
-
|
| 240 |
# Pie and Bar charts
|
| 241 |
grouped_counts = df['category'].value_counts().reset_index()
|
| 242 |
grouped_counts.columns = ['category', 'count']
|
| 243 |
col1, col2 = st.columns(2)
|
| 244 |
-
|
| 245 |
with col1:
|
| 246 |
st.subheader("Pie chart", divider="violet")
|
| 247 |
fig_pie = px.pie(grouped_counts, values='count', names='category', hover_data=['count'], labels={'count': 'count'}, title='Percentage of predicted categories')
|
|
@@ -251,7 +218,6 @@ if st.button("Results"):
|
|
| 251 |
plot_bgcolor='#E8F5E9'
|
| 252 |
)
|
| 253 |
st.plotly_chart(fig_pie)
|
| 254 |
-
|
| 255 |
with col2:
|
| 256 |
st.subheader("Bar chart", divider="violet")
|
| 257 |
fig_bar = px.bar(grouped_counts, x="count", y="category", color="category", text_auto=True, title='Occurrences of predicted categories')
|
|
@@ -260,7 +226,6 @@ if st.button("Results"):
|
|
| 260 |
plot_bgcolor='#E8F5E9'
|
| 261 |
)
|
| 262 |
st.plotly_chart(fig_bar)
|
| 263 |
-
|
| 264 |
# Most Frequent Entities
|
| 265 |
st.subheader("Most Frequent Entities", divider="violet")
|
| 266 |
word_counts = df['text'].value_counts().reset_index()
|
|
@@ -277,10 +242,8 @@ if st.button("Results"):
|
|
| 277 |
st.plotly_chart(fig_repeating_bar)
|
| 278 |
else:
|
| 279 |
st.warning("No entities were found that occur more than once.")
|
| 280 |
-
|
| 281 |
# Download Section
|
| 282 |
st.divider()
|
| 283 |
-
|
| 284 |
dfa = pd.DataFrame(
|
| 285 |
data={
|
| 286 |
'Column Name': ['text', 'label', 'score', 'start', 'end'],
|
|
@@ -297,7 +260,6 @@ if st.button("Results"):
|
|
| 297 |
with zipfile.ZipFile(buf, "w") as myzip:
|
| 298 |
myzip.writestr("Summary of the results.csv", df.to_csv(index=False))
|
| 299 |
myzip.writestr("Glossary of tags.csv", dfa.to_csv(index=False))
|
| 300 |
-
|
| 301 |
with stylable_container(
|
| 302 |
key="download_button",
|
| 303 |
css_styles="""button { background-color: #81C784; border: 1px solid black; padding: 5px; color: #1B5E20; }""",
|
|
@@ -308,13 +270,11 @@ if st.button("Results"):
|
|
| 308 |
file_name="nlpblogs_results.zip",
|
| 309 |
mime="application/zip",
|
| 310 |
)
|
| 311 |
-
|
| 312 |
if comet_initialized:
|
| 313 |
experiment.log_figure(figure=fig_treemap, figure_name="entity_treemap_categories")
|
| 314 |
experiment.end()
|
| 315 |
else: # If df is empty
|
| 316 |
st.warning("No entities were found in the provided text.")
|
| 317 |
-
|
| 318 |
end_time = time.time()
|
| 319 |
elapsed_time = end_time - start_time
|
| 320 |
st.text("")
|
|
|
|
| 12 |
from typing import Optional
|
| 13 |
from gliner import GLiNER
|
| 14 |
from comet_ml import Experiment
|
|
|
|
| 15 |
# --- CSS Styling for the App ---
|
| 16 |
st.markdown(
|
| 17 |
"""
|
|
|
|
| 21 |
background-color: #E8F5E9; /* A very light green */
|
| 22 |
color: #1B5E20; /* Dark green for the text */
|
| 23 |
}
|
|
|
|
| 24 |
/* Sidebar background color */
|
| 25 |
.css-1d36184 {
|
| 26 |
background-color: #A5D6A7; /* A medium light green */
|
| 27 |
secondary-background-color: #A5D6A7;
|
| 28 |
}
|
|
|
|
| 29 |
/* Expander background color and header */
|
| 30 |
.streamlit-expanderContent, .streamlit-expanderHeader {
|
| 31 |
background-color: #E8F5E9;
|
| 32 |
}
|
|
|
|
| 33 |
/* Text Area background and text color */
|
| 34 |
.stTextArea textarea {
|
| 35 |
background-color: #81C784; /* A slightly darker medium green */
|
| 36 |
color: #1B5E20; /* Dark green for text */
|
| 37 |
}
|
|
|
|
| 38 |
/* Button background and text color */
|
| 39 |
.stButton > button {
|
| 40 |
background-color: #81C784;
|
| 41 |
color: #1B5E20;
|
| 42 |
}
|
|
|
|
| 43 |
/* Warning box background and text color */
|
| 44 |
.stAlert.st-warning {
|
| 45 |
background-color: #66BB6A; /* A medium-dark green for the warning box */
|
| 46 |
color: #1B5E20;
|
| 47 |
}
|
|
|
|
| 48 |
/* Success box background and text color */
|
| 49 |
.stAlert.st-success {
|
| 50 |
background-color: #66BB6A; /* A medium-dark green for the success box */
|
|
|
|
| 52 |
}
|
| 53 |
</style>
|
| 54 |
""",
|
| 55 |
+
unsafe_allow_html=True)
|
|
|
|
|
|
|
| 56 |
# --- Page Configuration and UI Elements ---
|
| 57 |
st.set_page_config(layout="wide", page_title="Named Entity Recognition App")
|
| 58 |
st.subheader("RetailTag", divider="violet")
|
| 59 |
st.link_button("by nlpblogs", "https://nlpblogs.com", type="tertiary")
|
|
|
|
| 60 |
expander = st.expander("**Important notes**")
|
| 61 |
+
expander.write("""**Named Entities:** This RetailTag web app predicts eighteen (18) labels: "Product_Name", "Product_Type", "Brand", "Model_Number", "SKU", "Product_Attribute", "Service_Type", "Order_Number", "Monetary_Value", "Payment_Method", "Discount", "Shipping_Method", "Quantity", "Organization", "Location", "Person", "Date", "Time"
|
|
|
|
|
|
|
| 62 |
|
| 63 |
Results are presented in easy-to-read tables, visualized in an interactive tree map, pie chart and bar chart, and are available for download along with a Glossary of tags.
|
| 64 |
|
|
|
|
| 75 |
with st.sidebar:
|
| 76 |
st.write("Use the following code to embed the RetailTag web app on your website. Feel free to adjust the width and height values to fit your page.")
|
| 77 |
code = '''
|
| 78 |
+
<iframe src="https://aiecosystem-retailtag.hf.space" frameborder="0" width="850" height="450" ></iframe>
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 79 |
'''
|
| 80 |
st.code(code, language="html")
|
| 81 |
st.text("")
|
|
|
|
| 83 |
st.divider()
|
| 84 |
st.subheader("🚀 Ready to build your own AI Web App?", divider="violet")
|
| 85 |
st.link_button("AI Web App Builder", "https://nlpblogs.com/build-your-named-entity-recognition-app/", type="primary")
|
|
|
|
| 86 |
# --- Comet ML Setup ---
|
| 87 |
COMET_API_KEY = os.environ.get("COMET_API_KEY")
|
| 88 |
COMET_WORKSPACE = os.environ.get("COMET_WORKSPACE")
|
|
|
|
| 90 |
comet_initialized = bool(COMET_API_KEY and COMET_WORKSPACE and COMET_PROJECT_NAME)
|
| 91 |
if not comet_initialized:
|
| 92 |
st.warning("Comet ML not initialized. Check environment variables.")
|
|
|
|
| 93 |
# --- Label Definitions ---
|
| 94 |
# The list of labels is assigned to a variable for use in the model loading function
|
| 95 |
labels = [
|
|
|
|
| 110 |
"Location",
|
| 111 |
"Person",
|
| 112 |
"Date",
|
| 113 |
+
"Time"]
|
|
|
|
|
|
|
| 114 |
# Create a mapping dictionary for labels to categories
|
| 115 |
category_mapping = {
|
| 116 |
"Product & Service Entities": [
|
|
|
|
| 136 |
"Person",
|
| 137 |
"Date",
|
| 138 |
"Time"
|
| 139 |
+
]}
|
|
|
|
|
|
|
| 140 |
# --- Model Loading ---
|
| 141 |
@st.cache_resource
|
| 142 |
def load_ner_model():
|
|
|
|
| 147 |
except Exception as e:
|
| 148 |
st.error(f"Failed to load NER model. Please check your internet connection or model availability: {e}")
|
| 149 |
st.stop()
|
|
|
|
| 150 |
model = load_ner_model()
|
|
|
|
| 151 |
# Flatten the mapping to a single dictionary
|
| 152 |
reverse_category_mapping = {label: category for category, label_list in category_mapping.items() for label in label_list}
|
|
|
|
| 153 |
# --- Text Input and Clear Button ---
|
| 154 |
+
word_limit = 200
|
| 155 |
+
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')
|
| 156 |
+
word_count = len(text.split())
|
| 157 |
+
st.markdown(f"**Word count:** {word_count}/{word_limit}")
|
| 158 |
def clear_text():
|
| 159 |
"""Clears the text area."""
|
| 160 |
st.session_state['my_text_area'] = ""
|
| 161 |
st.button("Clear text", on_click=clear_text)
|
|
|
|
| 162 |
# --- Results Section ---
|
| 163 |
if st.button("Results"):
|
| 164 |
start_time = time.time()
|
| 165 |
if not text.strip():
|
| 166 |
st.warning("Please enter some text to extract entities.")
|
| 167 |
+
elif word_count > word_limit:
|
| 168 |
+
st.warning(f"Your text exceeds the {word_limit} word limit. Please shorten it to continue.")
|
| 169 |
else:
|
| 170 |
with st.spinner("Extracting entities...", show_time=True):
|
| 171 |
entities = model.predict_entities(text, labels)
|
| 172 |
df = pd.DataFrame(entities)
|
|
|
|
| 173 |
if not df.empty:
|
| 174 |
df['category'] = df['label'].map(reverse_category_mapping)
|
|
|
|
| 175 |
if comet_initialized:
|
| 176 |
experiment = Experiment(
|
| 177 |
api_key=COMET_API_KEY,
|
|
|
|
| 180 |
)
|
| 181 |
experiment.log_parameter("input_text", text)
|
| 182 |
experiment.log_table("predicted_entities", df)
|
|
|
|
| 183 |
st.subheader("Grouped Entities by Category", divider="violet")
|
|
|
|
| 184 |
# Create tabs for each category
|
| 185 |
category_names = sorted(list(category_mapping.keys()))
|
| 186 |
category_tabs = st.tabs(category_names)
|
|
|
|
| 187 |
for i, category_name in enumerate(category_names):
|
| 188 |
with category_tabs[i]:
|
| 189 |
df_category_filtered = df[df['category'] == category_name]
|
|
|
|
| 191 |
st.dataframe(df_category_filtered.drop(columns=['category']), use_container_width=True)
|
| 192 |
else:
|
| 193 |
st.info(f"No entities found for the '{category_name}' category.")
|
|
|
|
| 194 |
with st.expander("See Glossary of tags"):
|
| 195 |
st.write('''
|
| 196 |
- **text**: ['entity extracted from your text data']
|
|
|
|
| 200 |
- **end**: ['index of the end of the corresponding entity']
|
| 201 |
''')
|
| 202 |
st.divider()
|
|
|
|
| 203 |
# Tree map
|
| 204 |
st.subheader("Tree map", divider="violet")
|
| 205 |
fig_treemap = px.treemap(df, path=[px.Constant("all"), 'category', 'label', 'text'], values='score', color='category')
|
| 206 |
fig_treemap.update_layout(margin=dict(t=50, l=25, r=25, b=25), paper_bgcolor='#E8F5E9', plot_bgcolor='#E8F5E9')
|
| 207 |
st.plotly_chart(fig_treemap)
|
|
|
|
| 208 |
# Pie and Bar charts
|
| 209 |
grouped_counts = df['category'].value_counts().reset_index()
|
| 210 |
grouped_counts.columns = ['category', 'count']
|
| 211 |
col1, col2 = st.columns(2)
|
|
|
|
| 212 |
with col1:
|
| 213 |
st.subheader("Pie chart", divider="violet")
|
| 214 |
fig_pie = px.pie(grouped_counts, values='count', names='category', hover_data=['count'], labels={'count': 'count'}, title='Percentage of predicted categories')
|
|
|
|
| 218 |
plot_bgcolor='#E8F5E9'
|
| 219 |
)
|
| 220 |
st.plotly_chart(fig_pie)
|
|
|
|
| 221 |
with col2:
|
| 222 |
st.subheader("Bar chart", divider="violet")
|
| 223 |
fig_bar = px.bar(grouped_counts, x="count", y="category", color="category", text_auto=True, title='Occurrences of predicted categories')
|
|
|
|
| 226 |
plot_bgcolor='#E8F5E9'
|
| 227 |
)
|
| 228 |
st.plotly_chart(fig_bar)
|
|
|
|
| 229 |
# Most Frequent Entities
|
| 230 |
st.subheader("Most Frequent Entities", divider="violet")
|
| 231 |
word_counts = df['text'].value_counts().reset_index()
|
|
|
|
| 242 |
st.plotly_chart(fig_repeating_bar)
|
| 243 |
else:
|
| 244 |
st.warning("No entities were found that occur more than once.")
|
|
|
|
| 245 |
# Download Section
|
| 246 |
st.divider()
|
|
|
|
| 247 |
dfa = pd.DataFrame(
|
| 248 |
data={
|
| 249 |
'Column Name': ['text', 'label', 'score', 'start', 'end'],
|
|
|
|
| 260 |
with zipfile.ZipFile(buf, "w") as myzip:
|
| 261 |
myzip.writestr("Summary of the results.csv", df.to_csv(index=False))
|
| 262 |
myzip.writestr("Glossary of tags.csv", dfa.to_csv(index=False))
|
|
|
|
| 263 |
with stylable_container(
|
| 264 |
key="download_button",
|
| 265 |
css_styles="""button { background-color: #81C784; border: 1px solid black; padding: 5px; color: #1B5E20; }""",
|
|
|
|
| 270 |
file_name="nlpblogs_results.zip",
|
| 271 |
mime="application/zip",
|
| 272 |
)
|
|
|
|
| 273 |
if comet_initialized:
|
| 274 |
experiment.log_figure(figure=fig_treemap, figure_name="entity_treemap_categories")
|
| 275 |
experiment.end()
|
| 276 |
else: # If df is empty
|
| 277 |
st.warning("No entities were found in the provided text.")
|
|
|
|
| 278 |
end_time = time.time()
|
| 279 |
elapsed_time = end_time - start_time
|
| 280 |
st.text("")
|