Spaces:
Running
Running
Update src/streamlit_app.py
Browse files- src/streamlit_app.py +45 -16
src/streamlit_app.py
CHANGED
|
@@ -333,41 +333,69 @@ def generate_entity_csv(df):
|
|
| 333 |
return csv_buffer
|
| 334 |
# -----------------------------------
|
| 335 |
# --- Existing App Functionality (HTML) ---
|
| 336 |
-
|
| 337 |
def generate_html_report(df, text_input, elapsed_time, df_topic_data):
|
| 338 |
"""
|
| 339 |
-
Generates a full HTML report containing all analysis results and
|
| 340 |
-
|
| 341 |
"""
|
| 342 |
-
# 1. Generate Visualizations (Plotly HTML
|
| 343 |
-
#
|
| 344 |
-
|
| 345 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 346 |
# 1f. Topic Charts HTML
|
| 347 |
topic_charts_html = '<h3>Topic Word Weights (Bubble Chart)</h3>'
|
| 348 |
if df_topic_data is not None and not df_topic_data.empty:
|
| 349 |
bubble_figure = create_topic_word_bubbles(df_topic_data)
|
| 350 |
if bubble_figure:
|
| 351 |
-
|
| 352 |
topic_charts_html += f'<div class="chart-box">{bubble_figure.to_html(full_html=False, include_plotlyjs="cdn", config={"responsive": True})}</div>'
|
| 353 |
else:
|
| 354 |
topic_charts_html += '<p style="color: red;">Error: Topic modeling data was available but visualization failed.</p>'
|
| 355 |
else:
|
| 356 |
-
topic_charts_html += '<div class="chart-box" style="text-align: center; padding: 50px; background-color: #fff; border: 1px dashed #888888;">'
|
| 357 |
topic_charts_html += '<p><strong>Topic Modeling requires more unique input.</strong></p>'
|
| 358 |
topic_charts_html += '<p>Please enter text containing at least two unique entities to generate the Topic Bubble Chart.</p>'
|
| 359 |
topic_charts_html += '</div>'
|
| 360 |
-
|
| 361 |
# 2. Get Highlighted Text
|
| 362 |
highlighted_text_html = highlight_entities(text_input, df).replace("div style", "div class='highlighted-text' style")
|
| 363 |
-
|
| 364 |
# 3. Entity Tables (Pandas to HTML)
|
| 365 |
entity_table_html = df[['text', 'label', 'score', 'start', 'end', 'category']].to_html(
|
| 366 |
classes='table table-striped',
|
| 367 |
index=False
|
| 368 |
)
|
| 369 |
-
|
| 370 |
-
# 4. Construct the Final HTML with Mobile CSS Fixes
|
| 371 |
html_content = f"""<!DOCTYPE html><html lang="en"><head>
|
| 372 |
<meta charset="UTF-8">
|
| 373 |
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
|
@@ -450,12 +478,11 @@ white-space: pre-wrap; margin-bottom: 20px; }}
|
|
| 450 |
</div></body></html>
|
| 451 |
"""
|
| 452 |
return html_content
|
| 453 |
-
|
| 454 |
-
|
| 455 |
|
| 456 |
|
| 457 |
|
| 458 |
|
|
|
|
| 459 |
# --- Page Configuration and Styling (No Sidebar) ---
|
| 460 |
st.set_page_config(layout="wide", page_title="NER & Topic Report App")
|
| 461 |
|
|
@@ -802,4 +829,6 @@ if st.session_state.show_results:
|
|
| 802 |
file_name="extracted_entities.csv",
|
| 803 |
mime="text/csv",
|
| 804 |
type="secondary"
|
| 805 |
-
)
|
|
|
|
|
|
|
|
|
| 333 |
return csv_buffer
|
| 334 |
# -----------------------------------
|
| 335 |
# --- Existing App Functionality (HTML) ---
|
|
|
|
| 336 |
def generate_html_report(df, text_input, elapsed_time, df_topic_data):
|
| 337 |
"""
|
| 338 |
+
Generates a full HTML report containing all analysis results and visualizations.
|
| 339 |
+
(Content omitted for brevity but assumed to be here).
|
| 340 |
"""
|
| 341 |
+
# 1. Generate Visualizations (Plotly HTML)
|
| 342 |
+
# 1a. Treemap
|
| 343 |
+
fig_treemap = px.treemap(
|
| 344 |
+
df,
|
| 345 |
+
path=[px.Constant("All Entities"), 'category', 'label', 'text'],
|
| 346 |
+
values='score',
|
| 347 |
+
color='category',
|
| 348 |
+
title="Entity Distribution by Category and Label",
|
| 349 |
+
color_discrete_sequence=px.colors.qualitative.Dark24
|
| 350 |
+
)
|
| 351 |
+
fig_treemap.update_layout(margin=dict(t=50, l=25, r=25, b=25))
|
| 352 |
+
treemap_html = fig_treemap.to_html(full_html=False, include_plotlyjs='cdn')
|
| 353 |
+
# 1b. Pie Chart
|
| 354 |
+
grouped_counts = df['category'].value_counts().reset_index()
|
| 355 |
+
grouped_counts.columns = ['Category', 'Count']
|
| 356 |
+
# Changed color_discrete_sequence from sequential.RdBu (which has reds) to sequential.Cividis
|
| 357 |
+
fig_pie = px.pie(grouped_counts, values='Count', names='Category',title='Distribution of Entities by Category',color_discrete_sequence=px.colors.sequential.Cividis)
|
| 358 |
+
fig_pie.update_layout(margin=dict(t=50, b=10))
|
| 359 |
+
pie_html = fig_pie.to_html(full_html=False, include_plotlyjs='cdn')
|
| 360 |
+
# 1c. Bar Chart (Category Count)
|
| 361 |
+
fig_bar_category = px.bar(grouped_counts, x='Category', y='Count',color='Category', title='Total Entities per Category',color_discrete_sequence=px.colors.qualitative.Pastel)
|
| 362 |
+
fig_bar_category.update_layout(xaxis={'categoryorder': 'total descending'},margin=dict(t=50, b=100))
|
| 363 |
+
bar_category_html = fig_bar_category.to_html(full_html=False,include_plotlyjs='cdn')
|
| 364 |
+
# 1d. Bar Chart (Most Frequent Entities)
|
| 365 |
+
word_counts = df['text'].value_counts().reset_index()
|
| 366 |
+
word_counts.columns = ['Entity', 'Count']
|
| 367 |
+
repeating_entities = word_counts[word_counts['Count'] > 1].head(10)
|
| 368 |
+
bar_freq_html = '<p>No entities appear more than once in the text for visualization.</p>'
|
| 369 |
+
if not repeating_entities.empty:
|
| 370 |
+
# Changed color_discrete_sequence from sequential.Plasma (which has pink/magenta) to sequential.Viridis
|
| 371 |
+
fig_bar_freq = px.bar(repeating_entities, x='Entity', y='Count',color='Entity', title='Top 10 Most Frequent Entities',color_discrete_sequence=px.colors.sequential.Viridis)
|
| 372 |
+
fig_bar_freq.update_layout(xaxis={'categoryorder': 'total descending'},margin=dict(t=50, b=100))
|
| 373 |
+
bar_freq_html = fig_bar_freq.to_html(full_html=False, include_plotlyjs='cdn')
|
| 374 |
+
# 1e. Network Graph HTML
|
| 375 |
+
network_fig = generate_network_graph(df, text_input)
|
| 376 |
+
network_html = network_fig.to_html(full_html=False, include_plotlyjs='cdn')
|
| 377 |
# 1f. Topic Charts HTML
|
| 378 |
topic_charts_html = '<h3>Topic Word Weights (Bubble Chart)</h3>'
|
| 379 |
if df_topic_data is not None and not df_topic_data.empty:
|
| 380 |
bubble_figure = create_topic_word_bubbles(df_topic_data)
|
| 381 |
if bubble_figure:
|
| 382 |
+
|
| 383 |
topic_charts_html += f'<div class="chart-box">{bubble_figure.to_html(full_html=False, include_plotlyjs="cdn", config={"responsive": True})}</div>'
|
| 384 |
else:
|
| 385 |
topic_charts_html += '<p style="color: red;">Error: Topic modeling data was available but visualization failed.</p>'
|
| 386 |
else:
|
| 387 |
+
topic_charts_html += '<div class="chart-box" style="text-align: center; padding: 50px; background-color: #fff; border: 1px dashed #888888;">' # Changed border color
|
| 388 |
topic_charts_html += '<p><strong>Topic Modeling requires more unique input.</strong></p>'
|
| 389 |
topic_charts_html += '<p>Please enter text containing at least two unique entities to generate the Topic Bubble Chart.</p>'
|
| 390 |
topic_charts_html += '</div>'
|
|
|
|
| 391 |
# 2. Get Highlighted Text
|
| 392 |
highlighted_text_html = highlight_entities(text_input, df).replace("div style", "div class='highlighted-text' style")
|
|
|
|
| 393 |
# 3. Entity Tables (Pandas to HTML)
|
| 394 |
entity_table_html = df[['text', 'label', 'score', 'start', 'end', 'category']].to_html(
|
| 395 |
classes='table table-striped',
|
| 396 |
index=False
|
| 397 |
)
|
| 398 |
+
# 4. Construct the Final HTML
|
|
|
|
| 399 |
html_content = f"""<!DOCTYPE html><html lang="en"><head>
|
| 400 |
<meta charset="UTF-8">
|
| 401 |
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
|
|
|
| 478 |
</div></body></html>
|
| 479 |
"""
|
| 480 |
return html_content
|
|
|
|
|
|
|
| 481 |
|
| 482 |
|
| 483 |
|
| 484 |
|
| 485 |
+
|
| 486 |
# --- Page Configuration and Styling (No Sidebar) ---
|
| 487 |
st.set_page_config(layout="wide", page_title="NER & Topic Report App")
|
| 488 |
|
|
|
|
| 829 |
file_name="extracted_entities.csv",
|
| 830 |
mime="text/csv",
|
| 831 |
type="secondary"
|
| 832 |
+
)
|
| 833 |
+
|
| 834 |
+
|