Spaces:
Running
Running
Update src/streamlit_app.py
Browse files- src/streamlit_app.py +65 -50
src/streamlit_app.py
CHANGED
|
@@ -333,86 +333,95 @@ def generate_entity_csv(df):
|
|
| 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
|
| 339 |
-
|
| 340 |
"""
|
| 341 |
-
# 1. Generate Visualizations (Plotly HTML
|
| 342 |
-
#
|
| 343 |
-
|
| 344 |
-
|
| 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;">'
|
| 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 |
-
|
|
|
|
| 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">
|
| 402 |
<title>Entity and Topic Analysis Report</title>
|
| 403 |
<script src="https://cdn.plot.ly/plotly-latest.min.js"></script>
|
| 404 |
<style>
|
| 405 |
-
body {{ font-family: 'Inter', sans-serif; margin: 0; padding: 20px;
|
| 406 |
-
|
| 407 |
-
|
| 408 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 409 |
h3 {{ color: #555; margin-top: 20px; }}
|
| 410 |
-
.metadata {{ background-color: #e6f0ff; padding: 15px; border-radius:
|
| 411 |
-
|
|
|
|
|
|
|
|
|
|
| 412 |
table {{ width: 100%; border-collapse: collapse; margin-top: 15px; }}
|
| 413 |
-
table th, table td {{ border: 1px solid #ddd; padding: 8px; text-align:
|
|
|
|
| 414 |
table th {{ background-color: #f0f0f0; }}
|
| 415 |
-
.highlighted-text {{ border: 1px solid #888888; padding: 15px;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 416 |
</style></head><body>
|
| 417 |
<div class="container">
|
| 418 |
<h1>Entity and Topic Analysis Report</h1>
|
|
@@ -441,6 +450,12 @@ def generate_html_report(df, text_input, elapsed_time, df_topic_data):
|
|
| 441 |
</div></body></html>
|
| 442 |
"""
|
| 443 |
return html_content
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 444 |
# --- Page Configuration and Styling (No Sidebar) ---
|
| 445 |
st.set_page_config(layout="wide", page_title="NER & Topic Report App")
|
| 446 |
|
|
@@ -787,4 +802,4 @@ if st.session_state.show_results:
|
|
| 787 |
file_name="extracted_entities.csv",
|
| 788 |
mime="text/csv",
|
| 789 |
type="secondary"
|
| 790 |
-
)
|
|
|
|
| 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 |
+
visualizations, with CSS media queries for mobile responsiveness.
|
| 341 |
"""
|
| 342 |
+
# 1. Generate Visualizations (Plotly HTML generation code remains the same,
|
| 343 |
+
# ensuring bubble chart to_html includes config={'responsive': True})
|
| 344 |
+
# ... (Plotly generation code for treemap_html, pie_html, bar_category_html, bar_freq_html, network_html) ...
|
| 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 |
+
# IMPORTANT: Ensure config={'responsive': True} is here to make the plot resize
|
| 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">
|
| 374 |
<title>Entity and Topic Analysis Report</title>
|
| 375 |
<script src="https://cdn.plot.ly/plotly-latest.min.js"></script>
|
| 376 |
<style>
|
| 377 |
+
body {{ font-family: 'Inter', sans-serif; margin: 0; padding: 20px;
|
| 378 |
+
background-color: #f4f4f9; color: #333; }}
|
| 379 |
+
.container {{ max-width: 1200px; margin: 0 auto; background-color:
|
| 380 |
+
#ffffff; padding: 30px; border-radius: 12px; box-shadow: 0 4px 12px
|
| 381 |
+
rgba(0,0,0,0.1); }}
|
| 382 |
+
h1 {{ color: #007bff; border-bottom: 3px solid #007bff; padding-bottom:
|
| 383 |
+
10px; margin-top: 0; }}
|
| 384 |
+
h2 {{ color: #007bff; margin-top: 30px; border-bottom: 1px solid #ddd;
|
| 385 |
+
padding-bottom: 5px; }}
|
| 386 |
h3 {{ color: #555; margin-top: 20px; }}
|
| 387 |
+
.metadata {{ background-color: #e6f0ff; padding: 15px; border-radius:
|
| 388 |
+
8px; margin-bottom: 20px; font-size: 0.9em; }}
|
| 389 |
+
.chart-box {{ background-color: #f9f9f9; padding: 15px; border-radius:
|
| 390 |
+
8px; box-shadow: 0 2px 4px rgba(0,0,0,0.05); min-width: 0; margin-bottom: 20px;
|
| 391 |
+
}}
|
| 392 |
table {{ width: 100%; border-collapse: collapse; margin-top: 15px; }}
|
| 393 |
+
table th, table td {{ border: 1px solid #ddd; padding: 8px; text-align:
|
| 394 |
+
left; }}
|
| 395 |
table th {{ background-color: #f0f0f0; }}
|
| 396 |
+
.highlighted-text {{ border: 1px solid #888888; padding: 15px;
|
| 397 |
+
border-radius: 5px; background-color: #ffffff; font-family: monospace;
|
| 398 |
+
white-space: pre-wrap; margin-bottom: 20px; }}
|
| 399 |
+
|
| 400 |
+
/* === MOBILE-SPECIFIC FIXES FOR REPORT OVERLAP === */
|
| 401 |
+
@media (max-width: 600px) {
|
| 402 |
+
body {
|
| 403 |
+
padding: 10px;
|
| 404 |
+
}
|
| 405 |
+
.container {
|
| 406 |
+
padding: 10px;
|
| 407 |
+
border-radius: 0;
|
| 408 |
+
}
|
| 409 |
+
.chart-box {
|
| 410 |
+
padding: 5px;
|
| 411 |
+
overflow-x: auto; /* Allow horizontal scrolling for wide charts */
|
| 412 |
+
}
|
| 413 |
+
/* Ensures the Plotly chart inside has a minimum width */
|
| 414 |
+
.chart-box > div {
|
| 415 |
+
min-width: 400px;
|
| 416 |
+
}
|
| 417 |
+
/* Force tables to be scrollable */
|
| 418 |
+
table {
|
| 419 |
+
display: block;
|
| 420 |
+
overflow-x: auto;
|
| 421 |
+
white-space: nowrap;
|
| 422 |
+
}
|
| 423 |
+
}
|
| 424 |
+
/* ============================================== */
|
| 425 |
</style></head><body>
|
| 426 |
<div class="container">
|
| 427 |
<h1>Entity and Topic Analysis Report</h1>
|
|
|
|
| 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 |
file_name="extracted_entities.csv",
|
| 803 |
mime="text/csv",
|
| 804 |
type="secondary"
|
| 805 |
+
)
|