Spaces:
Sleeping
Sleeping
Update src/streamlit_app.py
Browse files- src/streamlit_app.py +133 -36
src/streamlit_app.py
CHANGED
|
@@ -211,45 +211,142 @@ if st.button("Results"):
|
|
| 211 |
fig_treemap = px.treemap(df, path=[px.Constant("all"), 'category', 'label', 'text'], values='score', color='category')
|
| 212 |
fig_treemap.update_layout(margin=dict(t=50, l=25, r=25, b=25), paper_bgcolor='#F5FFFA', plot_bgcolor='#F5FFFA')
|
| 213 |
st.plotly_chart(fig_treemap)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 214 |
|
| 215 |
-
|
| 216 |
-
|
| 217 |
-
|
| 218 |
-
|
| 219 |
-
|
| 220 |
-
|
| 221 |
-
|
| 222 |
-
|
| 223 |
-
|
| 224 |
-
|
| 225 |
-
|
| 226 |
-
|
| 227 |
-
|
| 228 |
-
|
| 229 |
-
|
| 230 |
-
|
| 231 |
-
|
| 232 |
-
fig_bar = px.bar(grouped_counts, x="count", y="category", color="category", text_auto=True, title='Occurrences of predicted categories')
|
| 233 |
-
fig_pie.update_layout(
|
| 234 |
-
paper_bgcolor='#F5FFFA',
|
| 235 |
-
plot_bgcolor='#F5FFFA'
|
| 236 |
-
)
|
| 237 |
-
st.plotly_chart(fig_bar)
|
| 238 |
|
| 239 |
-
|
| 240 |
-
|
| 241 |
-
|
| 242 |
-
|
| 243 |
-
|
| 244 |
-
|
| 245 |
-
|
| 246 |
-
|
| 247 |
-
|
| 248 |
-
|
| 249 |
-
|
| 250 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 251 |
else:
|
| 252 |
-
st.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 253 |
|
| 254 |
# Download Section
|
| 255 |
st.divider()
|
|
|
|
| 211 |
fig_treemap = px.treemap(df, path=[px.Constant("all"), 'category', 'label', 'text'], values='score', color='category')
|
| 212 |
fig_treemap.update_layout(margin=dict(t=50, l=25, r=25, b=25), paper_bgcolor='#F5FFFA', plot_bgcolor='#F5FFFA')
|
| 213 |
st.plotly_chart(fig_treemap)
|
| 214 |
+
|
| 215 |
+
@st.cache_resource
|
| 216 |
+
def load_gliner_model():
|
| 217 |
+
try:
|
| 218 |
+
return GLiNER.from_pretrained("knowledgator/gliner-multitask-v1.0", device="cpu")
|
| 219 |
+
except Exception as e:
|
| 220 |
+
st.error(f"Error loading the GLiNER model: {e}")
|
| 221 |
+
st.stop()
|
| 222 |
|
| 223 |
+
model = load_gliner_model()
|
| 224 |
+
st.subheader("Question-Answering", divider = "violet")
|
| 225 |
+
|
| 226 |
+
|
| 227 |
+
question_input = st.text_input("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.**")
|
| 228 |
+
|
| 229 |
+
|
| 230 |
+
if st.button("Add Question"):
|
| 231 |
+
if question_input:
|
| 232 |
+
if question_input not in st.session_state.user_labels:
|
| 233 |
+
st.session_state.user_labels.append(question_input)
|
| 234 |
+
st.success(f"Added question: {question_input}")
|
| 235 |
+
else:
|
| 236 |
+
st.warning("This question has already been added.")
|
| 237 |
+
else:
|
| 238 |
+
st.warning("Please enter a question.")
|
| 239 |
+
st.markdown("---")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 240 |
|
| 241 |
+
st.subheader("Record of Questions", divider = "violet")
|
| 242 |
+
|
| 243 |
+
if st.session_state.user_labels:
|
| 244 |
+
|
| 245 |
+
for i, label in enumerate(st.session_state.user_labels):
|
| 246 |
+
col_list, col_delete = st.columns([0.9, 0.1])
|
| 247 |
+
with col_list:
|
| 248 |
+
st.write(f"- {label}", key=f"label_{i}")
|
| 249 |
+
with col_delete:
|
| 250 |
+
# Create a unique key for each button using the index
|
| 251 |
+
if st.button("Delete", key=f"delete_{i}"):
|
| 252 |
+
# Remove the label at the specific index
|
| 253 |
+
st.session_state.user_labels.pop(i)
|
| 254 |
+
# Rerun to update the UI
|
| 255 |
+
st.rerun()
|
| 256 |
+
else:
|
| 257 |
+
st.info("No questions defined yet. Use the input above to add one.")
|
| 258 |
+
|
| 259 |
+
def get_stable_color(label):
|
| 260 |
+
"""Generates a consistent hexadecimal color from a given string."""
|
| 261 |
+
hash_object = hashlib.sha1(label.encode('utf-8'))
|
| 262 |
+
hex_dig = hash_object.hexdigest()
|
| 263 |
+
return '#' + hex_dig[:6]
|
| 264 |
+
|
| 265 |
+
st.divider()
|
| 266 |
+
|
| 267 |
+
# --- Main Processing Logic ---
|
| 268 |
+
if st.button("Extract Answers"):
|
| 269 |
+
if not user_text.strip():
|
| 270 |
+
st.warning("Please enter some text to analyze.")
|
| 271 |
+
elif not st.session_state.user_labels:
|
| 272 |
+
st.warning("Please define at least one question.")
|
| 273 |
+
else:
|
| 274 |
+
if comet_initialized:
|
| 275 |
+
experiment = Experiment(
|
| 276 |
+
api_key=COMET_API_KEY,
|
| 277 |
+
workspace=COMET_WORKSPACE,
|
| 278 |
+
project_name=COMET_PROJECT_NAME
|
| 279 |
+
)
|
| 280 |
+
experiment.log_parameter("input_text_length", len(user_text))
|
| 281 |
+
experiment.log_parameter("defined_labels", st.session_state.user_labels)
|
| 282 |
+
|
| 283 |
+
start_time = time.time()
|
| 284 |
+
with st.spinner("Analyzing text...", show_time=True):
|
| 285 |
+
try:
|
| 286 |
+
entities = model.predict_entities(user_text, st.session_state.user_labels)
|
| 287 |
+
end_time = time.time()
|
| 288 |
+
elapsed_time = end_time - start_time
|
| 289 |
+
st.info(f"Processing took **{elapsed_time:.2f} seconds**.")
|
| 290 |
+
|
| 291 |
+
if entities:
|
| 292 |
+
df1 = pd.DataFrame(entities)
|
| 293 |
+
df2 = df1[['label', 'text', 'score']]
|
| 294 |
+
df = df2.rename(columns={'label': 'question', 'text': 'answer'})
|
| 295 |
+
|
| 296 |
+
st.subheader("Extracted Answers", divider = "violet")
|
| 297 |
+
st.dataframe(df, use_container_width=True)
|
| 298 |
+
|
| 299 |
+
|
| 300 |
+
|
| 301 |
+
|
| 302 |
+
csv_data = df.to_csv(index=False).encode('utf-8')
|
| 303 |
+
with stylable_container(
|
| 304 |
+
key="download_button",
|
| 305 |
+
css_styles="""button { background-color: red; border: 1px solid black; padding: 5px; color: white; }""",
|
| 306 |
+
):
|
| 307 |
+
st.download_button(
|
| 308 |
+
label="Download CSV",
|
| 309 |
+
data=csv_data,
|
| 310 |
+
file_name="nlpblogs_results.csv",
|
| 311 |
+
mime="text/csv",
|
| 312 |
+
)
|
| 313 |
+
|
| 314 |
+
if comet_initialized:
|
| 315 |
+
experiment.log_metric("processing_time_seconds", elapsed_time)
|
| 316 |
+
experiment.log_table("predicted_entities", df)
|
| 317 |
+
experiment.log_figure(figure=fig_treemap, figure_name="entity_treemap")
|
| 318 |
+
|
| 319 |
+
experiment.end()
|
| 320 |
else:
|
| 321 |
+
st.info("No answers were found in the text with the defined questions.")
|
| 322 |
+
if comet_initialized:
|
| 323 |
+
experiment.end()
|
| 324 |
+
except Exception as e:
|
| 325 |
+
st.error(f"An error occurred during processing: {e}")
|
| 326 |
+
st.write(f"Error details: {e}")
|
| 327 |
+
if comet_initialized:
|
| 328 |
+
experiment.log_text(f"Error: {e}")
|
| 329 |
+
experiment.end()
|
| 330 |
+
|
| 331 |
+
|
| 332 |
+
|
| 333 |
+
|
| 334 |
+
|
| 335 |
+
|
| 336 |
+
|
| 337 |
+
|
| 338 |
+
|
| 339 |
+
|
| 340 |
+
|
| 341 |
+
|
| 342 |
+
|
| 343 |
+
|
| 344 |
+
|
| 345 |
+
|
| 346 |
+
|
| 347 |
+
|
| 348 |
+
|
| 349 |
+
|
| 350 |
|
| 351 |
# Download Section
|
| 352 |
st.divider()
|