Spaces:
Sleeping
Sleeping
Update src/streamlit_app.py
Browse files- src/streamlit_app.py +44 -59
src/streamlit_app.py
CHANGED
|
@@ -58,45 +58,34 @@ st.markdown(
|
|
| 58 |
unsafe_allow_html=True
|
| 59 |
)
|
| 60 |
|
| 61 |
-
|
| 62 |
# --- Page Configuration and UI Elements ---
|
| 63 |
st.set_page_config(layout="wide", page_title="Named Entity Recognition App")
|
| 64 |
st.subheader("InfoFinder", divider="violet")
|
| 65 |
st.link_button("by nlpblogs", "https://nlpblogs.com", type="tertiary")
|
| 66 |
|
| 67 |
expander = st.expander("**Important notes**")
|
| 68 |
-
expander.write("""
|
| 69 |
-
|
|
|
|
| 70 |
2. Click the 'Add Question' button to add your question to the Record of Questions. You can manage your questions by deleting them one by one.
|
| 71 |
-
3. Click the 'Extract Answers' button to extract the answer to your question.
|
| 72 |
-
|
| 73 |
-
Results are presented in an easy-to-read table, visualized in an interactive tree map and are available for download.
|
| 74 |
|
| 75 |
**Usage Limits:** You can request results unlimited times for one (1) month.
|
| 76 |
-
|
| 77 |
-
**
|
| 78 |
-
|
| 79 |
-
**Technical issues:** If your connection times out, please refresh the page or reopen the app's URL.
|
| 80 |
-
|
| 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 InfoFinder web app on your website. Feel free to adjust the width and height values to fit your page.")
|
| 85 |
code = '''
|
| 86 |
-
<iframe
|
| 87 |
-
src="https://aiecosystem-infofinder.hf.space"
|
| 88 |
-
frameborder="0"
|
| 89 |
-
width="850"
|
| 90 |
-
height="450"
|
| 91 |
-
></iframe>
|
| 92 |
-
|
| 93 |
'''
|
| 94 |
st.code(code, language="html")
|
| 95 |
st.text("")
|
| 96 |
st.text("")
|
| 97 |
st.divider()
|
| 98 |
st.subheader("🚀 Ready to build your own AI Web App?", divider="violet")
|
| 99 |
-
st.link_button("AI Web App Builder", "
|
| 100 |
|
| 101 |
# --- Comet ML Setup ---
|
| 102 |
COMET_API_KEY = os.environ.get("COMET_API_KEY")
|
|
@@ -107,58 +96,64 @@ comet_initialized = bool(COMET_API_KEY and COMET_WORKSPACE and COMET_PROJECT_NAM
|
|
| 107 |
if not comet_initialized:
|
| 108 |
st.warning("Comet ML not initialized. Check environment variables.")
|
| 109 |
|
| 110 |
-
# --- Initialize session state for labels ---
|
| 111 |
if 'user_labels' not in st.session_state:
|
| 112 |
st.session_state.user_labels = []
|
|
|
|
|
|
|
|
|
|
|
|
|
| 113 |
|
| 114 |
# --- Model Loading and Caching ---
|
| 115 |
@st.cache_resource
|
| 116 |
def load_gliner_model():
|
| 117 |
-
"""
|
| 118 |
-
Initializes and caches the GLiNER model.
|
| 119 |
-
This ensures the model is only loaded once, improving performance.
|
| 120 |
-
"""
|
| 121 |
try:
|
| 122 |
return GLiNER.from_pretrained("knowledgator/gliner-multitask-large-v0.5", device="cpu")
|
| 123 |
except Exception as e:
|
| 124 |
st.error(f"Error loading the GLiNER model: {e}")
|
| 125 |
st.stop()
|
| 126 |
|
| 127 |
-
# Load the model
|
| 128 |
model = load_gliner_model()
|
| 129 |
|
| 130 |
-
|
| 131 |
-
|
| 132 |
-
def clear_text():
|
| 133 |
"""Clears the text area by resetting its value in session state."""
|
| 134 |
st.session_state['my_text_area'] = ""
|
| 135 |
-
st.button("Clear text", on_click=clear_text)
|
| 136 |
-
|
| 137 |
-
st.subheader("Question-Answering", divider = "violet")
|
| 138 |
|
| 139 |
-
|
| 140 |
-
|
| 141 |
|
|
|
|
| 142 |
|
| 143 |
-
|
| 144 |
-
|
| 145 |
-
|
| 146 |
-
|
| 147 |
-
st.
|
|
|
|
|
|
|
| 148 |
else:
|
| 149 |
st.warning("This question has already been added.")
|
| 150 |
else:
|
| 151 |
st.warning("Please enter a question.")
|
| 152 |
-
|
| 153 |
-
|
| 154 |
-
st.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 155 |
|
| 156 |
if st.session_state.user_labels:
|
| 157 |
def delete_label(label_to_delete):
|
| 158 |
"""Removes a label from the session state list."""
|
| 159 |
st.session_state.user_labels = [label for label in st.session_state.user_labels if label != label_to_delete]
|
| 160 |
st.rerun()
|
| 161 |
-
|
| 162 |
for label in st.session_state.user_labels:
|
| 163 |
col_list, col_delete = st.columns([0.9, 0.1])
|
| 164 |
with col_list:
|
|
@@ -204,14 +199,18 @@ if st.button("Extract Answers"):
|
|
| 204 |
df1 = pd.DataFrame(entities)
|
| 205 |
df2 = df1[['label', 'text', 'score']]
|
| 206 |
df = df2.rename(columns={'label': 'question', 'text': 'answer'})
|
|
|
|
|
|
|
|
|
|
| 207 |
|
| 208 |
-
st.subheader("Extracted Answers", divider
|
| 209 |
st.dataframe(df, use_container_width=True)
|
| 210 |
-
|
| 211 |
# Create Tree map
|
| 212 |
st.subheader("Tree map", divider="violet")
|
| 213 |
all_labels = df['question'].unique()
|
| 214 |
label_color_map = {label: get_stable_color(label) for label in all_labels}
|
|
|
|
| 215 |
fig_treemap = px.treemap(
|
| 216 |
df,
|
| 217 |
path=[px.Constant("all"), 'question', 'answer'],
|
|
@@ -222,8 +221,6 @@ if st.button("Extract Answers"):
|
|
| 222 |
fig_treemap.update_layout(margin=dict(t=50, l=25, r=25, b=25), paper_bgcolor='#F3E5F5', plot_bgcolor='#F3E5F5')
|
| 223 |
st.plotly_chart(fig_treemap)
|
| 224 |
|
| 225 |
-
|
| 226 |
-
|
| 227 |
csv_data = df.to_csv(index=False).encode('utf-8')
|
| 228 |
with stylable_container(
|
| 229 |
key="download_button",
|
|
@@ -235,12 +232,10 @@ if st.button("Extract Answers"):
|
|
| 235 |
file_name="nlpblogs_results.csv",
|
| 236 |
mime="text/csv",
|
| 237 |
)
|
| 238 |
-
|
| 239 |
if comet_initialized:
|
| 240 |
experiment.log_metric("processing_time_seconds", elapsed_time)
|
| 241 |
experiment.log_table("predicted_entities", df)
|
| 242 |
experiment.log_figure(figure=fig_treemap, figure_name="entity_treemap")
|
| 243 |
-
|
| 244 |
experiment.end()
|
| 245 |
else:
|
| 246 |
st.info("No answers were found in the text with the defined questions.")
|
|
@@ -272,16 +267,6 @@ if st.button("Extract Answers"):
|
|
| 272 |
|
| 273 |
|
| 274 |
|
| 275 |
-
|
| 276 |
-
|
| 277 |
-
|
| 278 |
-
|
| 279 |
-
|
| 280 |
-
|
| 281 |
-
|
| 282 |
-
|
| 283 |
-
|
| 284 |
-
|
| 285 |
|
| 286 |
|
| 287 |
|
|
|
|
| 58 |
unsafe_allow_html=True
|
| 59 |
)
|
| 60 |
|
|
|
|
| 61 |
# --- Page Configuration and UI Elements ---
|
| 62 |
st.set_page_config(layout="wide", page_title="Named Entity Recognition App")
|
| 63 |
st.subheader("InfoFinder", divider="violet")
|
| 64 |
st.link_button("by nlpblogs", "https://nlpblogs.com", type="tertiary")
|
| 65 |
|
| 66 |
expander = st.expander("**Important notes**")
|
| 67 |
+
expander.write("""
|
| 68 |
+
**How to Use:**
|
| 69 |
+
1. Type or paste your text into the text area below, then press Ctrl + Enter.
|
| 70 |
2. Click the 'Add Question' button to add your question to the Record of Questions. You can manage your questions by deleting them one by one.
|
| 71 |
+
3. Click the 'Extract Answers' button to extract the answer to your question. Results are presented in an easy-to-read table, visualized in an interactive tree map and are available for download.
|
|
|
|
|
|
|
| 72 |
|
| 73 |
**Usage Limits:** You can request results unlimited times for one (1) month.
|
| 74 |
+
**Supported Languages:** English
|
| 75 |
+
**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 info@nlpblogs.com
|
| 76 |
+
""")
|
|
|
|
|
|
|
|
|
|
| 77 |
|
| 78 |
with st.sidebar:
|
| 79 |
st.write("Use the following code to embed the InfoFinder web app on your website. Feel free to adjust the width and height values to fit your page.")
|
| 80 |
code = '''
|
| 81 |
+
<iframe src="https://aiecosystem-infofinder.hf.space" frameborder="0" width="850" height="450"></iframe>
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 82 |
'''
|
| 83 |
st.code(code, language="html")
|
| 84 |
st.text("")
|
| 85 |
st.text("")
|
| 86 |
st.divider()
|
| 87 |
st.subheader("🚀 Ready to build your own AI Web App?", divider="violet")
|
| 88 |
+
st.link_button("AI Web App Builder", "https://nlpblogs.com/custom-web-app-development/", type="primary")
|
| 89 |
|
| 90 |
# --- Comet ML Setup ---
|
| 91 |
COMET_API_KEY = os.environ.get("COMET_API_KEY")
|
|
|
|
| 96 |
if not comet_initialized:
|
| 97 |
st.warning("Comet ML not initialized. Check environment variables.")
|
| 98 |
|
| 99 |
+
# --- Initialize session state for labels and inputs ---
|
| 100 |
if 'user_labels' not in st.session_state:
|
| 101 |
st.session_state.user_labels = []
|
| 102 |
+
if 'my_text_area' not in st.session_state:
|
| 103 |
+
st.session_state['my_text_area'] = ""
|
| 104 |
+
if 'question_input' not in st.session_state:
|
| 105 |
+
st.session_state['question_input'] = ""
|
| 106 |
|
| 107 |
# --- Model Loading and Caching ---
|
| 108 |
@st.cache_resource
|
| 109 |
def load_gliner_model():
|
| 110 |
+
"""Initializes and caches the GLiNER model."""
|
|
|
|
|
|
|
|
|
|
| 111 |
try:
|
| 112 |
return GLiNER.from_pretrained("knowledgator/gliner-multitask-large-v0.5", device="cpu")
|
| 113 |
except Exception as e:
|
| 114 |
st.error(f"Error loading the GLiNER model: {e}")
|
| 115 |
st.stop()
|
| 116 |
|
|
|
|
| 117 |
model = load_gliner_model()
|
| 118 |
|
| 119 |
+
# --- Text Area and Clear Button ---
|
| 120 |
+
def clear_text_area():
|
|
|
|
| 121 |
"""Clears the text area by resetting its value in session state."""
|
| 122 |
st.session_state['my_text_area'] = ""
|
|
|
|
|
|
|
|
|
|
| 123 |
|
| 124 |
+
user_text = st.text_area("Type or paste your text below, and then press Ctrl + Enter", height=250, key='my_text_area')
|
| 125 |
+
st.button("Clear text", on_click=clear_text_area)
|
| 126 |
|
| 127 |
+
st.subheader("Question-Answering", divider="violet")
|
| 128 |
|
| 129 |
+
def add_question_callback():
|
| 130 |
+
"""Callback to add the question and clear the input."""
|
| 131 |
+
if st.session_state.question_input:
|
| 132 |
+
if st.session_state.question_input not in st.session_state.user_labels:
|
| 133 |
+
st.session_state.user_labels.append(st.session_state.question_input)
|
| 134 |
+
st.success(f"Added question: {st.session_state.question_input}")
|
| 135 |
+
st.session_state.question_input = ""
|
| 136 |
else:
|
| 137 |
st.warning("This question has already been added.")
|
| 138 |
else:
|
| 139 |
st.warning("Please enter a question.")
|
| 140 |
+
|
| 141 |
+
# The `st.columns` call has been removed to place widgets on separate lines
|
| 142 |
+
question_input = st.text_input(
|
| 143 |
+
"Ask wh-questions. **Wh-questions begin with what, when, where, who, whom, which, whose, why and how. We use them to ask for specific information.**",
|
| 144 |
+
key='question_input'
|
| 145 |
+
)
|
| 146 |
+
st.button("Add Question", on_click=add_question_callback)
|
| 147 |
+
|
| 148 |
+
st.markdown("---")
|
| 149 |
+
st.subheader("Record of Questions", divider="violet")
|
| 150 |
|
| 151 |
if st.session_state.user_labels:
|
| 152 |
def delete_label(label_to_delete):
|
| 153 |
"""Removes a label from the session state list."""
|
| 154 |
st.session_state.user_labels = [label for label in st.session_state.user_labels if label != label_to_delete]
|
| 155 |
st.rerun()
|
| 156 |
+
|
| 157 |
for label in st.session_state.user_labels:
|
| 158 |
col_list, col_delete = st.columns([0.9, 0.1])
|
| 159 |
with col_list:
|
|
|
|
| 199 |
df1 = pd.DataFrame(entities)
|
| 200 |
df2 = df1[['label', 'text', 'score']]
|
| 201 |
df = df2.rename(columns={'label': 'question', 'text': 'answer'})
|
| 202 |
+
|
| 203 |
+
# Drop duplicates based on 'question' and 'answer' columns
|
| 204 |
+
df.drop_duplicates(subset=['question', 'answer'], keep='first', inplace=True)
|
| 205 |
|
| 206 |
+
st.subheader("Extracted Answers", divider="violet")
|
| 207 |
st.dataframe(df, use_container_width=True)
|
| 208 |
+
|
| 209 |
# Create Tree map
|
| 210 |
st.subheader("Tree map", divider="violet")
|
| 211 |
all_labels = df['question'].unique()
|
| 212 |
label_color_map = {label: get_stable_color(label) for label in all_labels}
|
| 213 |
+
|
| 214 |
fig_treemap = px.treemap(
|
| 215 |
df,
|
| 216 |
path=[px.Constant("all"), 'question', 'answer'],
|
|
|
|
| 221 |
fig_treemap.update_layout(margin=dict(t=50, l=25, r=25, b=25), paper_bgcolor='#F3E5F5', plot_bgcolor='#F3E5F5')
|
| 222 |
st.plotly_chart(fig_treemap)
|
| 223 |
|
|
|
|
|
|
|
| 224 |
csv_data = df.to_csv(index=False).encode('utf-8')
|
| 225 |
with stylable_container(
|
| 226 |
key="download_button",
|
|
|
|
| 232 |
file_name="nlpblogs_results.csv",
|
| 233 |
mime="text/csv",
|
| 234 |
)
|
|
|
|
| 235 |
if comet_initialized:
|
| 236 |
experiment.log_metric("processing_time_seconds", elapsed_time)
|
| 237 |
experiment.log_table("predicted_entities", df)
|
| 238 |
experiment.log_figure(figure=fig_treemap, figure_name="entity_treemap")
|
|
|
|
| 239 |
experiment.end()
|
| 240 |
else:
|
| 241 |
st.info("No answers were found in the text with the defined questions.")
|
|
|
|
| 267 |
|
| 268 |
|
| 269 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 270 |
|
| 271 |
|
| 272 |
|