Spaces:
Sleeping
Sleeping
Update src/streamlit_app.py
Browse files- src/streamlit_app.py +40 -45
src/streamlit_app.py
CHANGED
|
@@ -1,5 +1,4 @@
|
|
| 1 |
import os
|
| 2 |
-
os.environ['HF_HOME'] = '/tmp'
|
| 3 |
import time
|
| 4 |
import streamlit as st
|
| 5 |
import pandas as pd
|
|
@@ -26,8 +25,7 @@ st.markdown(
|
|
| 26 |
background-color: #B2F2B2; /* A pale green for the sidebar */
|
| 27 |
secondary-background-color: #B2F2B2;
|
| 28 |
}
|
| 29 |
-
|
| 30 |
-
/* Expander background color */
|
| 31 |
.streamlit-expanderContent {
|
| 32 |
background-color: #F5FFFA;
|
| 33 |
}
|
|
@@ -65,30 +63,17 @@ st.set_page_config(layout="wide", page_title="Named Entity Recognition App")
|
|
| 65 |
st.subheader("HR.ai", divider="orange")
|
| 66 |
st.link_button("by nlpblogs", "https://nlpblogs.com", type="tertiary")
|
| 67 |
expander = st.expander("**Important notes**")
|
| 68 |
-
expander.write("""**Named Entities:** This HR.ai predicts sixty (60) labels:"Email", "Phone_number", "Street_address", "City", "State", "Zip_code", "Country", "Date_of_birth", "Gender", "Marital_status", "Person", "Full_time", "Part_time", "Contract", "Temporary", "Terminated", "Active", "Retired", "Job_title", "Employment_type", "Year", "Date", "Company", "Organization", "Role", "Position","Performance_review", "Performance_rating", "Performance_score", "Sick_days", "Vacation_days", "Leave_of_absence", "Holidays", "Pension", "Retirement_plan", "Bonus", "Stock_options", "Health_insurance", "Retire_date", "Pay_rate", "Hourly_wage", "Annual_salary", "Overtime_pay", "Tax", "Social_security", "Deductions", "Job_posting", "Job_description", "Interview_type", "Applicant", "Candidate", "Referral", "Job_board", "Recruiter","Contract", "Offer_letter", "Agreement", "Training_course", "Certification", "Skill"
|
| 69 |
-
|
| 70 |
-
|
| 71 |
-
|
| 72 |
-
**
|
| 73 |
-
|
| 74 |
-
**Usage Limits:** You can request results unlimited times for one (1) month.
|
| 75 |
-
|
| 76 |
-
**Supported Languages:** English, German, French, Italian, Spanish, Portuguese
|
| 77 |
-
|
| 78 |
-
**Technical issues:** If your connection times out, please refresh the page or reopen the app's URL.
|
| 79 |
-
|
| 80 |
-
For any errors or inquiries, please contact us at [email protected]""")
|
| 81 |
|
| 82 |
with st.sidebar:
|
| 83 |
st.write("Use the following code to embed the HR.ai web app on your website. Feel free to adjust the width and height values to fit your page.")
|
| 84 |
code = '''
|
| 85 |
-
<iframe
|
| 86 |
-
src="https://aiecosystem-hr-ai.hf.space"
|
| 87 |
-
frameborder="0"
|
| 88 |
-
width="850"
|
| 89 |
-
height="450"
|
| 90 |
-
></iframe>
|
| 91 |
-
|
| 92 |
'''
|
| 93 |
st.code(code, language="html")
|
| 94 |
st.text("")
|
|
@@ -102,7 +87,6 @@ COMET_API_KEY = os.environ.get("COMET_API_KEY")
|
|
| 102 |
COMET_WORKSPACE = os.environ.get("COMET_WORKSPACE")
|
| 103 |
COMET_PROJECT_NAME = os.environ.get("COMET_PROJECT_NAME")
|
| 104 |
comet_initialized = bool(COMET_API_KEY and COMET_WORKSPACE and COMET_PROJECT_NAME)
|
| 105 |
-
|
| 106 |
if not comet_initialized:
|
| 107 |
st.warning("Comet ML not initialized. Check environment variables.")
|
| 108 |
|
|
@@ -148,7 +132,6 @@ def clear_text():
|
|
| 148 |
|
| 149 |
st.button("Clear text", on_click=clear_text)
|
| 150 |
|
| 151 |
-
|
| 152 |
# --- Results Section ---
|
| 153 |
if st.button("Results"):
|
| 154 |
start_time = time.time()
|
|
@@ -158,7 +141,6 @@ if st.button("Results"):
|
|
| 158 |
with st.spinner("Extracting entities...", show_time=True):
|
| 159 |
entities = model.predict_entities(text, labels)
|
| 160 |
df = pd.DataFrame(entities)
|
| 161 |
-
|
| 162 |
if not df.empty:
|
| 163 |
df['category'] = df['label'].map(reverse_category_mapping)
|
| 164 |
if comet_initialized:
|
|
@@ -169,13 +151,13 @@ if st.button("Results"):
|
|
| 169 |
)
|
| 170 |
experiment.log_parameter("input_text", text)
|
| 171 |
experiment.log_table("predicted_entities", df)
|
| 172 |
-
|
| 173 |
st.subheader("Extracted Entities", divider = "orange")
|
| 174 |
-
|
| 175 |
# Create tabs for each category
|
| 176 |
category_names = sorted(list(category_mapping.keys()))
|
| 177 |
category_tabs = st.tabs(category_names)
|
| 178 |
-
|
| 179 |
for i, category_name in enumerate(category_names):
|
| 180 |
with category_tabs[i]:
|
| 181 |
df_category_filtered = df[df['category'] == category_name]
|
|
@@ -183,9 +165,7 @@ if st.button("Results"):
|
|
| 183 |
st.dataframe(df_category_filtered.drop(columns=['category']), use_container_width=True)
|
| 184 |
else:
|
| 185 |
st.info(f"No entities found for the '{category_name}' category.")
|
| 186 |
-
|
| 187 |
-
|
| 188 |
-
|
| 189 |
with st.expander("See Glossary of tags"):
|
| 190 |
st.write('''
|
| 191 |
- **text**: ['entity extracted from your text data']
|
|
@@ -196,29 +176,41 @@ if st.button("Results"):
|
|
| 196 |
- **end**: ['index of the end of the corresponding entity']
|
| 197 |
''')
|
| 198 |
st.divider()
|
| 199 |
-
|
| 200 |
# Tree map
|
| 201 |
st.subheader("Tree map", divider = "orange")
|
| 202 |
fig_treemap = px.treemap(df, path=[px.Constant("all"), 'category', 'label', 'text'], values='score', color='category')
|
| 203 |
-
fig_treemap.update_layout(
|
|
|
|
|
|
|
|
|
|
|
|
|
| 204 |
st.plotly_chart(fig_treemap)
|
| 205 |
-
|
| 206 |
# Pie and Bar charts
|
| 207 |
grouped_counts = df['category'].value_counts().reset_index()
|
| 208 |
grouped_counts.columns = ['category', 'count']
|
| 209 |
col1, col2 = st.columns(2)
|
| 210 |
-
|
| 211 |
with col1:
|
| 212 |
st.subheader("Pie chart", divider = "orange")
|
| 213 |
fig_pie = px.pie(grouped_counts, values='count', names='category', hover_data=['count'], labels={'count': 'count'}, title='Percentage of predicted categories')
|
| 214 |
fig_pie.update_traces(textposition='inside', textinfo='percent+label')
|
|
|
|
|
|
|
|
|
|
|
|
|
| 215 |
st.plotly_chart(fig_pie)
|
| 216 |
-
|
| 217 |
with col2:
|
| 218 |
st.subheader("Bar chart", divider = "orange")
|
| 219 |
fig_bar = px.bar(grouped_counts, x="count", y="category", color="category", text_auto=True, title='Occurrences of predicted categories')
|
|
|
|
|
|
|
|
|
|
|
|
|
| 220 |
st.plotly_chart(fig_bar)
|
| 221 |
-
|
| 222 |
# Most Frequent Entities
|
| 223 |
st.subheader("Most Frequent Entities", divider="orange")
|
| 224 |
word_counts = df['text'].value_counts().reset_index()
|
|
@@ -227,14 +219,18 @@ if st.button("Results"):
|
|
| 227 |
if not repeating_entities.empty:
|
| 228 |
st.dataframe(repeating_entities, use_container_width=True)
|
| 229 |
fig_repeating_bar = px.bar(repeating_entities, x='Entity', y='Count', color='Entity')
|
| 230 |
-
fig_repeating_bar.update_layout(
|
|
|
|
|
|
|
|
|
|
|
|
|
| 231 |
st.plotly_chart(fig_repeating_bar)
|
| 232 |
else:
|
| 233 |
st.warning("No entities were found that occur more than once.")
|
| 234 |
-
|
| 235 |
# Download Section
|
| 236 |
st.divider()
|
| 237 |
-
|
| 238 |
dfa = pd.DataFrame(
|
| 239 |
data={
|
| 240 |
'Column Name': ['text', 'label', 'score', 'start', 'end', 'category'],
|
|
@@ -252,7 +248,7 @@ if st.button("Results"):
|
|
| 252 |
with zipfile.ZipFile(buf, "w") as myzip:
|
| 253 |
myzip.writestr("Summary of the results.csv", df.to_csv(index=False))
|
| 254 |
myzip.writestr("Glossary of tags.csv", dfa.to_csv(index=False))
|
| 255 |
-
|
| 256 |
with stylable_container(
|
| 257 |
key="download_button",
|
| 258 |
css_styles="""button { background-color: red; border: 1px solid black; padding: 5px; color: white; }""",
|
|
@@ -263,14 +259,13 @@ if st.button("Results"):
|
|
| 263 |
file_name="nlpblogs_results.zip",
|
| 264 |
mime="application/zip",
|
| 265 |
)
|
| 266 |
-
|
| 267 |
if comet_initialized:
|
| 268 |
experiment.log_figure(figure=fig_treemap, figure_name="entity_treemap_categories")
|
| 269 |
experiment.end()
|
| 270 |
else: # If df is empty
|
| 271 |
st.warning("No entities were found in the provided text.")
|
| 272 |
-
|
| 273 |
-
end_time = time.time()
|
| 274 |
elapsed_time = end_time - start_time
|
| 275 |
st.text("")
|
| 276 |
st.text("")
|
|
|
|
| 1 |
import os
|
|
|
|
| 2 |
import time
|
| 3 |
import streamlit as st
|
| 4 |
import pandas as pd
|
|
|
|
| 25 |
background-color: #B2F2B2; /* A pale green for the sidebar */
|
| 26 |
secondary-background-color: #B2F2B2;
|
| 27 |
}
|
| 28 |
+
/* Expander background color */
|
|
|
|
| 29 |
.streamlit-expanderContent {
|
| 30 |
background-color: #F5FFFA;
|
| 31 |
}
|
|
|
|
| 63 |
st.subheader("HR.ai", divider="orange")
|
| 64 |
st.link_button("by nlpblogs", "https://nlpblogs.com", type="tertiary")
|
| 65 |
expander = st.expander("**Important notes**")
|
| 66 |
+
expander.write("""**Named Entities:** This HR.ai predicts sixty (60) labels:"Email", "Phone_number", "Street_address", "City", "State", "Zip_code", "Country", "Date_of_birth", "Gender", "Marital_status", "Person", "Full_time", "Part_time", "Contract", "Temporary", "Terminated", "Active", "Retired", "Job_title", "Employment_type", "Year", "Date", "Company", "Organization", "Role", "Position","Performance_review", "Performance_rating", "Performance_score", "Sick_days", "Vacation_days", "Leave_of_absence", "Holidays", "Pension", "Retirement_plan", "Bonus", "Stock_options", "Health_insurance", "Retire_date", "Pay_rate", "Hourly_wage", "Annual_salary", "Overtime_pay", "Tax", "Social_security", "Deductions", "Job_posting", "Job_description", "Interview_type", "Applicant", "Candidate", "Referral", "Job_board", "Recruiter","Contract", "Offer_letter", "Agreement", "Training_course", "Certification", "Skill"
|
| 67 |
+
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.
|
| 68 |
+
**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.
|
| 69 |
+
**Usage Limits:** You can request results unlimited times for one (1) month.
|
| 70 |
+
**Supported Languages:** English, German, French, Italian, Spanish, Portuguese
|
| 71 |
+
**Technical issues:** If your connection times out, please refresh the page or reopen the app's URL. For any errors or inquiries, please contact us at [email protected]""")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 72 |
|
| 73 |
with st.sidebar:
|
| 74 |
st.write("Use the following code to embed the HR.ai web app on your website. Feel free to adjust the width and height values to fit your page.")
|
| 75 |
code = '''
|
| 76 |
+
<iframe src="https://aiecosystem-hr-ai.hf.space" frameborder="0" width="850" height="450" ></iframe>
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 77 |
'''
|
| 78 |
st.code(code, language="html")
|
| 79 |
st.text("")
|
|
|
|
| 87 |
COMET_WORKSPACE = os.environ.get("COMET_WORKSPACE")
|
| 88 |
COMET_PROJECT_NAME = os.environ.get("COMET_PROJECT_NAME")
|
| 89 |
comet_initialized = bool(COMET_API_KEY and COMET_WORKSPACE and COMET_PROJECT_NAME)
|
|
|
|
| 90 |
if not comet_initialized:
|
| 91 |
st.warning("Comet ML not initialized. Check environment variables.")
|
| 92 |
|
|
|
|
| 132 |
|
| 133 |
st.button("Clear text", on_click=clear_text)
|
| 134 |
|
|
|
|
| 135 |
# --- Results Section ---
|
| 136 |
if st.button("Results"):
|
| 137 |
start_time = time.time()
|
|
|
|
| 141 |
with st.spinner("Extracting entities...", show_time=True):
|
| 142 |
entities = model.predict_entities(text, labels)
|
| 143 |
df = pd.DataFrame(entities)
|
|
|
|
| 144 |
if not df.empty:
|
| 145 |
df['category'] = df['label'].map(reverse_category_mapping)
|
| 146 |
if comet_initialized:
|
|
|
|
| 151 |
)
|
| 152 |
experiment.log_parameter("input_text", text)
|
| 153 |
experiment.log_table("predicted_entities", df)
|
| 154 |
+
|
| 155 |
st.subheader("Extracted Entities", divider = "orange")
|
| 156 |
+
|
| 157 |
# Create tabs for each category
|
| 158 |
category_names = sorted(list(category_mapping.keys()))
|
| 159 |
category_tabs = st.tabs(category_names)
|
| 160 |
+
|
| 161 |
for i, category_name in enumerate(category_names):
|
| 162 |
with category_tabs[i]:
|
| 163 |
df_category_filtered = df[df['category'] == category_name]
|
|
|
|
| 165 |
st.dataframe(df_category_filtered.drop(columns=['category']), use_container_width=True)
|
| 166 |
else:
|
| 167 |
st.info(f"No entities found for the '{category_name}' category.")
|
| 168 |
+
|
|
|
|
|
|
|
| 169 |
with st.expander("See Glossary of tags"):
|
| 170 |
st.write('''
|
| 171 |
- **text**: ['entity extracted from your text data']
|
|
|
|
| 176 |
- **end**: ['index of the end of the corresponding entity']
|
| 177 |
''')
|
| 178 |
st.divider()
|
| 179 |
+
|
| 180 |
# Tree map
|
| 181 |
st.subheader("Tree map", divider = "orange")
|
| 182 |
fig_treemap = px.treemap(df, path=[px.Constant("all"), 'category', 'label', 'text'], values='score', color='category')
|
| 183 |
+
fig_treemap.update_layout(
|
| 184 |
+
margin=dict(t=50, l=25, r=25, b=25),
|
| 185 |
+
paper_bgcolor='#F5FFFA',
|
| 186 |
+
plot_bgcolor='#F5FFFA'
|
| 187 |
+
)
|
| 188 |
st.plotly_chart(fig_treemap)
|
| 189 |
+
|
| 190 |
# Pie and Bar charts
|
| 191 |
grouped_counts = df['category'].value_counts().reset_index()
|
| 192 |
grouped_counts.columns = ['category', 'count']
|
| 193 |
col1, col2 = st.columns(2)
|
| 194 |
+
|
| 195 |
with col1:
|
| 196 |
st.subheader("Pie chart", divider = "orange")
|
| 197 |
fig_pie = px.pie(grouped_counts, values='count', names='category', hover_data=['count'], labels={'count': 'count'}, title='Percentage of predicted categories')
|
| 198 |
fig_pie.update_traces(textposition='inside', textinfo='percent+label')
|
| 199 |
+
fig_pie.update_layout(
|
| 200 |
+
paper_bgcolor='#F5FFFA',
|
| 201 |
+
plot_bgcolor='#F5FFFA'
|
| 202 |
+
)
|
| 203 |
st.plotly_chart(fig_pie)
|
| 204 |
+
|
| 205 |
with col2:
|
| 206 |
st.subheader("Bar chart", divider = "orange")
|
| 207 |
fig_bar = px.bar(grouped_counts, x="count", y="category", color="category", text_auto=True, title='Occurrences of predicted categories')
|
| 208 |
+
fig_bar.update_layout(
|
| 209 |
+
paper_bgcolor='#F5FFFA',
|
| 210 |
+
plot_bgcolor='#F5FFFA'
|
| 211 |
+
)
|
| 212 |
st.plotly_chart(fig_bar)
|
| 213 |
+
|
| 214 |
# Most Frequent Entities
|
| 215 |
st.subheader("Most Frequent Entities", divider="orange")
|
| 216 |
word_counts = df['text'].value_counts().reset_index()
|
|
|
|
| 219 |
if not repeating_entities.empty:
|
| 220 |
st.dataframe(repeating_entities, use_container_width=True)
|
| 221 |
fig_repeating_bar = px.bar(repeating_entities, x='Entity', y='Count', color='Entity')
|
| 222 |
+
fig_repeating_bar.update_layout(
|
| 223 |
+
xaxis={'categoryorder': 'total descending'},
|
| 224 |
+
paper_bgcolor='#F5FFFA',
|
| 225 |
+
plot_bgcolor='#F5FFFA'
|
| 226 |
+
)
|
| 227 |
st.plotly_chart(fig_repeating_bar)
|
| 228 |
else:
|
| 229 |
st.warning("No entities were found that occur more than once.")
|
| 230 |
+
|
| 231 |
# Download Section
|
| 232 |
st.divider()
|
| 233 |
+
|
| 234 |
dfa = pd.DataFrame(
|
| 235 |
data={
|
| 236 |
'Column Name': ['text', 'label', 'score', 'start', 'end', 'category'],
|
|
|
|
| 248 |
with zipfile.ZipFile(buf, "w") as myzip:
|
| 249 |
myzip.writestr("Summary of the results.csv", df.to_csv(index=False))
|
| 250 |
myzip.writestr("Glossary of tags.csv", dfa.to_csv(index=False))
|
| 251 |
+
|
| 252 |
with stylable_container(
|
| 253 |
key="download_button",
|
| 254 |
css_styles="""button { background-color: red; border: 1px solid black; padding: 5px; color: white; }""",
|
|
|
|
| 259 |
file_name="nlpblogs_results.zip",
|
| 260 |
mime="application/zip",
|
| 261 |
)
|
| 262 |
+
|
| 263 |
if comet_initialized:
|
| 264 |
experiment.log_figure(figure=fig_treemap, figure_name="entity_treemap_categories")
|
| 265 |
experiment.end()
|
| 266 |
else: # If df is empty
|
| 267 |
st.warning("No entities were found in the provided text.")
|
| 268 |
+
end_time = time.time()
|
|
|
|
| 269 |
elapsed_time = end_time - start_time
|
| 270 |
st.text("")
|
| 271 |
st.text("")
|