Spaces:
Sleeping
Sleeping
Update src/streamlit_app.py
Browse files- src/streamlit_app.py +45 -39
src/streamlit_app.py
CHANGED
|
@@ -20,43 +20,43 @@ st.markdown(
|
|
| 20 |
<style>
|
| 21 |
/* Main app background and text color */
|
| 22 |
.stApp {
|
| 23 |
-
background-color: #
|
| 24 |
-
color: #
|
| 25 |
}
|
|
|
|
| 26 |
/* Sidebar background color */
|
| 27 |
.css-1d36184 {
|
| 28 |
-
background-color: #
|
| 29 |
-
secondary-background-color: #
|
| 30 |
-
}
|
| 31 |
-
|
| 32 |
-
/* Expander background color */
|
| 33 |
-
.streamlit-expanderContent {
|
| 34 |
-
background-color: #E0FFFF;
|
| 35 |
}
|
| 36 |
-
|
| 37 |
-
/* Expander
|
| 38 |
-
.streamlit-expanderHeader {
|
| 39 |
-
background-color: #
|
| 40 |
}
|
|
|
|
| 41 |
/* Text Area background and text color */
|
| 42 |
.stTextArea textarea {
|
| 43 |
-
background-color: #
|
| 44 |
-
color: #
|
| 45 |
}
|
|
|
|
| 46 |
/* Button background and text color */
|
| 47 |
.stButton > button {
|
| 48 |
-
background-color: #
|
| 49 |
-
color: #
|
| 50 |
}
|
|
|
|
| 51 |
/* Warning box background and text color */
|
| 52 |
.stAlert.st-warning {
|
| 53 |
-
background-color: #
|
| 54 |
-
color: #
|
| 55 |
}
|
|
|
|
| 56 |
/* Success box background and text color */
|
| 57 |
.stAlert.st-success {
|
| 58 |
-
background-color: #
|
| 59 |
-
color: #
|
| 60 |
}
|
| 61 |
</style>
|
| 62 |
""",
|
|
@@ -66,22 +66,28 @@ st.markdown(
|
|
| 66 |
|
| 67 |
|
| 68 |
|
| 69 |
-
|
| 70 |
# --- Page Configuration and UI Elements ---
|
| 71 |
st.set_page_config(layout="wide", page_title="Named Entity Recognition App")
|
| 72 |
-
st.subheader("
|
| 73 |
st.link_button("by nlpblogs", "https://nlpblogs.com", type="tertiary")
|
| 74 |
expander = st.expander("**Important notes**")
|
| 75 |
-
expander.write("""**Named Entities:** This
|
|
|
|
|
|
|
| 76 |
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.
|
|
|
|
| 77 |
**How to Use:** Type or paste your text into the text area below, then press Ctrl + Enter. Click the 'Results' button to extract and tag entities in your text data.
|
|
|
|
| 78 |
**Usage Limits:** You can request results unlimited times for one (1) month.
|
|
|
|
| 79 |
**Supported Languages:** English
|
|
|
|
| 80 |
**Technical issues:** If your connection times out, please refresh the page or reopen the app's URL.
|
|
|
|
| 81 |
For any errors or inquiries, please contact us at info@nlpblogs.com""")
|
| 82 |
|
| 83 |
with st.sidebar:
|
| 84 |
-
st.write("Use the following code to embed the
|
| 85 |
code = '''
|
| 86 |
<iframe
|
| 87 |
src="https://aiecosystem-storycraft.hf.space"
|
|
@@ -94,7 +100,7 @@ with st.sidebar:
|
|
| 94 |
st.text("")
|
| 95 |
st.text("")
|
| 96 |
st.divider()
|
| 97 |
-
st.subheader("🚀 Ready to build your own NER Web App?", divider="
|
| 98 |
st.link_button("NER Builder", "https://nlpblogs.com", type="primary")
|
| 99 |
|
| 100 |
# --- Comet ML Setup ---
|
|
@@ -149,7 +155,7 @@ category_mapping = {
|
|
| 149 |
def load_ner_model():
|
| 150 |
"""Loads the GLiNER model and caches it."""
|
| 151 |
try:
|
| 152 |
-
return GLiNER.from_pretrained("
|
| 153 |
except Exception as e:
|
| 154 |
st.error(f"Failed to load NER model. Please check your internet connection or model availability: {e}")
|
| 155 |
st.stop()
|
|
@@ -189,7 +195,7 @@ if st.button("Results"):
|
|
| 189 |
experiment.log_parameter("input_text", text)
|
| 190 |
experiment.log_table("predicted_entities", df)
|
| 191 |
|
| 192 |
-
st.subheader("Grouped Entities by Category", divider = "
|
| 193 |
|
| 194 |
# Create tabs for each category
|
| 195 |
category_names = sorted(list(category_mapping.keys()))
|
|
@@ -217,9 +223,9 @@ if st.button("Results"):
|
|
| 217 |
st.divider()
|
| 218 |
|
| 219 |
# Tree map
|
| 220 |
-
st.subheader("Tree map", divider = "
|
| 221 |
fig_treemap = px.treemap(df, path=[px.Constant("all"), 'category', 'label', 'text'], values='score', color='category')
|
| 222 |
-
fig_treemap.update_layout(margin=dict(t=50, l=25, r=25, b=25), paper_bgcolor='#
|
| 223 |
st.plotly_chart(fig_treemap)
|
| 224 |
|
| 225 |
# Pie and Bar charts
|
|
@@ -228,12 +234,12 @@ if st.button("Results"):
|
|
| 228 |
col1, col2 = st.columns(2)
|
| 229 |
|
| 230 |
with col1:
|
| 231 |
-
st.subheader("Pie chart", divider = "
|
| 232 |
fig_pie = px.pie(grouped_counts, values='count', names='category', hover_data=['count'], labels={'count': 'count'}, title='Percentage of predicted categories')
|
| 233 |
fig_pie.update_traces(textposition='inside', textinfo='percent+label')
|
| 234 |
fig_pie.update_layout(
|
| 235 |
-
paper_bgcolor='#
|
| 236 |
-
plot_bgcolor='#
|
| 237 |
)
|
| 238 |
st.plotly_chart(fig_pie)
|
| 239 |
|
|
@@ -241,16 +247,16 @@ if st.button("Results"):
|
|
| 241 |
|
| 242 |
|
| 243 |
with col2:
|
| 244 |
-
st.subheader("Bar chart", divider = "
|
| 245 |
fig_bar = px.bar(grouped_counts, x="count", y="category", color="category", text_auto=True, title='Occurrences of predicted categories')
|
| 246 |
fig_bar.update_layout( # Changed from fig_pie to fig_bar
|
| 247 |
-
paper_bgcolor='#
|
| 248 |
-
plot_bgcolor='#
|
| 249 |
)
|
| 250 |
st.plotly_chart(fig_bar)
|
| 251 |
|
| 252 |
# Most Frequent Entities
|
| 253 |
-
st.subheader("Most Frequent Entities", divider="
|
| 254 |
word_counts = df['text'].value_counts().reset_index()
|
| 255 |
word_counts.columns = ['Entity', 'Count']
|
| 256 |
repeating_entities = word_counts[word_counts['Count'] > 1]
|
|
@@ -258,8 +264,8 @@ if st.button("Results"):
|
|
| 258 |
st.dataframe(repeating_entities, use_container_width=True)
|
| 259 |
fig_repeating_bar = px.bar(repeating_entities, x='Entity', y='Count', color='Entity')
|
| 260 |
fig_repeating_bar.update_layout(xaxis={'categoryorder': 'total descending'},
|
| 261 |
-
paper_bgcolor='#
|
| 262 |
-
plot_bgcolor='#
|
| 263 |
st.plotly_chart(fig_repeating_bar)
|
| 264 |
else:
|
| 265 |
st.warning("No entities were found that occur more than once.")
|
|
|
|
| 20 |
<style>
|
| 21 |
/* Main app background and text color */
|
| 22 |
.stApp {
|
| 23 |
+
background-color: #F3E5F5; /* A very light purple */
|
| 24 |
+
color: #1A0A26; /* Dark purple for the text */
|
| 25 |
}
|
| 26 |
+
|
| 27 |
/* Sidebar background color */
|
| 28 |
.css-1d36184 {
|
| 29 |
+
background-color: #D1C4E9; /* A medium light purple */
|
| 30 |
+
secondary-background-color: #D1C4E9;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 31 |
}
|
| 32 |
+
|
| 33 |
+
/* Expander background color and header */
|
| 34 |
+
.streamlit-expanderContent, .streamlit-expanderHeader {
|
| 35 |
+
background-color: #F3E5F5;
|
| 36 |
}
|
| 37 |
+
|
| 38 |
/* Text Area background and text color */
|
| 39 |
.stTextArea textarea {
|
| 40 |
+
background-color: #B39DDB; /* A slightly darker medium purple */
|
| 41 |
+
color: #1A0A26; /* Dark purple for text */
|
| 42 |
}
|
| 43 |
+
|
| 44 |
/* Button background and text color */
|
| 45 |
.stButton > button {
|
| 46 |
+
background-color: #B39DDB;
|
| 47 |
+
color: #1A0A26;
|
| 48 |
}
|
| 49 |
+
|
| 50 |
/* Warning box background and text color */
|
| 51 |
.stAlert.st-warning {
|
| 52 |
+
background-color: #9575CD; /* A medium-dark purple for the warning box */
|
| 53 |
+
color: #1A0A26;
|
| 54 |
}
|
| 55 |
+
|
| 56 |
/* Success box background and text color */
|
| 57 |
.stAlert.st-success {
|
| 58 |
+
background-color: #9575CD; /* A medium-dark purple for the success box */
|
| 59 |
+
color: #1A0A26;
|
| 60 |
}
|
| 61 |
</style>
|
| 62 |
""",
|
|
|
|
| 66 |
|
| 67 |
|
| 68 |
|
|
|
|
| 69 |
# --- Page Configuration and UI Elements ---
|
| 70 |
st.set_page_config(layout="wide", page_title="Named Entity Recognition App")
|
| 71 |
+
st.subheader("MediaTagger", divider="violet")
|
| 72 |
st.link_button("by nlpblogs", "https://nlpblogs.com", type="tertiary")
|
| 73 |
expander = st.expander("**Important notes**")
|
| 74 |
+
expander.write("""**Named Entities:** This MediaTagger web app predicts eighteen (18) labels: 'person', 'organization', 'location', 'date', 'time', 'event', 'title', 'product', 'law', 'policy', 'work of art', 'geopolitical entity', 'number', 'cause of death',
|
| 75 |
+
'weapon', 'vehicle', 'facility', 'temporal expression'
|
| 76 |
+
|
| 77 |
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.
|
| 78 |
+
|
| 79 |
**How to Use:** Type or paste your text into the text area below, then press Ctrl + Enter. Click the 'Results' button to extract and tag entities in your text data.
|
| 80 |
+
|
| 81 |
**Usage Limits:** You can request results unlimited times for one (1) month.
|
| 82 |
+
|
| 83 |
**Supported Languages:** English
|
| 84 |
+
|
| 85 |
**Technical issues:** If your connection times out, please refresh the page or reopen the app's URL.
|
| 86 |
+
|
| 87 |
For any errors or inquiries, please contact us at info@nlpblogs.com""")
|
| 88 |
|
| 89 |
with st.sidebar:
|
| 90 |
+
st.write("Use the following code to embed the MediaTagger web app on your website. Feel free to adjust the width and height values to fit your page.")
|
| 91 |
code = '''
|
| 92 |
<iframe
|
| 93 |
src="https://aiecosystem-storycraft.hf.space"
|
|
|
|
| 100 |
st.text("")
|
| 101 |
st.text("")
|
| 102 |
st.divider()
|
| 103 |
+
st.subheader("🚀 Ready to build your own NER Web App?", divider="violet")
|
| 104 |
st.link_button("NER Builder", "https://nlpblogs.com", type="primary")
|
| 105 |
|
| 106 |
# --- Comet ML Setup ---
|
|
|
|
| 155 |
def load_ner_model():
|
| 156 |
"""Loads the GLiNER model and caches it."""
|
| 157 |
try:
|
| 158 |
+
return GLiNER.from_pretrained("EmergentMethods/gliner_large_news-v2.1", nested_ner=True, num_gen_sequences=2, gen_constraints= labels)
|
| 159 |
except Exception as e:
|
| 160 |
st.error(f"Failed to load NER model. Please check your internet connection or model availability: {e}")
|
| 161 |
st.stop()
|
|
|
|
| 195 |
experiment.log_parameter("input_text", text)
|
| 196 |
experiment.log_table("predicted_entities", df)
|
| 197 |
|
| 198 |
+
st.subheader("Grouped Entities by Category", divider = "violet")
|
| 199 |
|
| 200 |
# Create tabs for each category
|
| 201 |
category_names = sorted(list(category_mapping.keys()))
|
|
|
|
| 223 |
st.divider()
|
| 224 |
|
| 225 |
# Tree map
|
| 226 |
+
st.subheader("Tree map", divider = "violet")
|
| 227 |
fig_treemap = px.treemap(df, path=[px.Constant("all"), 'category', 'label', 'text'], values='score', color='category')
|
| 228 |
+
fig_treemap.update_layout(margin=dict(t=50, l=25, r=25, b=25), paper_bgcolor='#F3E5F5', plot_bgcolor='#F3E5F5')
|
| 229 |
st.plotly_chart(fig_treemap)
|
| 230 |
|
| 231 |
# Pie and Bar charts
|
|
|
|
| 234 |
col1, col2 = st.columns(2)
|
| 235 |
|
| 236 |
with col1:
|
| 237 |
+
st.subheader("Pie chart", divider = "violet")
|
| 238 |
fig_pie = px.pie(grouped_counts, values='count', names='category', hover_data=['count'], labels={'count': 'count'}, title='Percentage of predicted categories')
|
| 239 |
fig_pie.update_traces(textposition='inside', textinfo='percent+label')
|
| 240 |
fig_pie.update_layout(
|
| 241 |
+
paper_bgcolor='#F3E5F5',
|
| 242 |
+
plot_bgcolor='#F3E5F5'
|
| 243 |
)
|
| 244 |
st.plotly_chart(fig_pie)
|
| 245 |
|
|
|
|
| 247 |
|
| 248 |
|
| 249 |
with col2:
|
| 250 |
+
st.subheader("Bar chart", divider = "violet")
|
| 251 |
fig_bar = px.bar(grouped_counts, x="count", y="category", color="category", text_auto=True, title='Occurrences of predicted categories')
|
| 252 |
fig_bar.update_layout( # Changed from fig_pie to fig_bar
|
| 253 |
+
paper_bgcolor='#F3E5F5',
|
| 254 |
+
plot_bgcolor='#F3E5F5'
|
| 255 |
)
|
| 256 |
st.plotly_chart(fig_bar)
|
| 257 |
|
| 258 |
# Most Frequent Entities
|
| 259 |
+
st.subheader("Most Frequent Entities", divider="violet")
|
| 260 |
word_counts = df['text'].value_counts().reset_index()
|
| 261 |
word_counts.columns = ['Entity', 'Count']
|
| 262 |
repeating_entities = word_counts[word_counts['Count'] > 1]
|
|
|
|
| 264 |
st.dataframe(repeating_entities, use_container_width=True)
|
| 265 |
fig_repeating_bar = px.bar(repeating_entities, x='Entity', y='Count', color='Entity')
|
| 266 |
fig_repeating_bar.update_layout(xaxis={'categoryorder': 'total descending'},
|
| 267 |
+
paper_bgcolor='#F3E5F5',
|
| 268 |
+
plot_bgcolor='#F3E5F5')
|
| 269 |
st.plotly_chart(fig_repeating_bar)
|
| 270 |
else:
|
| 271 |
st.warning("No entities were found that occur more than once.")
|