Spaces:
Runtime error
Runtime error
Merge branch #sadickam/document-SDG-App' into 'sadickam/document-SDG-App-cpu'
Browse files
app.py
CHANGED
|
@@ -170,7 +170,7 @@ sdg_colors = {
|
|
| 170 |
|
| 171 |
# Function to plot SDG dominant bar graphs using Plotly
|
| 172 |
# Function to plot SDG dominant bar graphs using Plotly
|
| 173 |
-
def plot_sdg(df, title, pred_column, icons_folder='assets/icons/'):
|
| 174 |
"""
|
| 175 |
Plots a horizontal bar graph of SDG predictions and superimposes the icon of the most frequent SDG.
|
| 176 |
|
|
@@ -178,6 +178,8 @@ def plot_sdg(df, title, pred_column, icons_folder='assets/icons/'):
|
|
| 178 |
df (pd.DataFrame): DataFrame containing SDG predictions.
|
| 179 |
title (str): Title of the plot.
|
| 180 |
pred_column (str): Column name to use for plotting (e.g., 'pred1').
|
|
|
|
|
|
|
| 181 |
icons_folder (str): Path to the folder containing SDG icons.
|
| 182 |
|
| 183 |
Returns:
|
|
@@ -206,7 +208,7 @@ def plot_sdg(df, title, pred_column, icons_folder='assets/icons/'):
|
|
| 206 |
fig.update_traces(
|
| 207 |
texttemplate='%{x:.2f}%',
|
| 208 |
textposition='auto',
|
| 209 |
-
textfont=dict(size=
|
| 210 |
)
|
| 211 |
|
| 212 |
# Adjust layout for better visibility
|
|
@@ -216,16 +218,17 @@ def plot_sdg(df, title, pred_column, icons_folder='assets/icons/'):
|
|
| 216 |
),
|
| 217 |
yaxis=dict(
|
| 218 |
automargin=True,
|
| 219 |
-
title=
|
| 220 |
-
tickfont=dict(size=
|
| 221 |
),
|
| 222 |
-
margin=dict(l=20, r=
|
| 223 |
height=600,
|
| 224 |
-
#width=800,
|
| 225 |
showlegend=False,
|
| 226 |
template="simple_white",
|
| 227 |
xaxis=dict(
|
| 228 |
-
|
|
|
|
| 229 |
),
|
| 230 |
)
|
| 231 |
|
|
@@ -250,7 +253,7 @@ def plot_sdg(df, title, pred_column, icons_folder='assets/icons/'):
|
|
| 250 |
dict(
|
| 251 |
source='data:image/png;base64,' + encoded_image,
|
| 252 |
xref="paper", yref="paper",
|
| 253 |
-
x
|
| 254 |
sizex=0.2, sizey=0.2, # Size of the icon
|
| 255 |
xanchor="left",
|
| 256 |
yanchor="top",
|
|
@@ -264,7 +267,7 @@ def plot_sdg(df, title, pred_column, icons_folder='assets/icons/'):
|
|
| 264 |
|
| 265 |
def save_figure_as_jpeg(fig, filename):
|
| 266 |
"""Saves the Plotly figure as a high-resolution JPEG."""
|
| 267 |
-
pio.write_image(fig, filename, format='jpeg', width=
|
| 268 |
|
| 269 |
# Generate reports (page and sentence levels)
|
| 270 |
def generate_page_report(df_pages, report_file_name):
|
|
@@ -306,10 +309,17 @@ def generate_page_report(df_pages, report_file_name):
|
|
| 306 |
first_sdg_plot_path = f"{sanitized_doc_name}_first_sdg_page.jpeg"
|
| 307 |
second_sdg_plot_path = f"{sanitized_doc_name}_second_sdg_page.jpeg"
|
| 308 |
|
| 309 |
-
plot_sdg(
|
| 310 |
-
|
| 311 |
-
|
| 312 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 313 |
|
| 314 |
# Add plots to the Word document
|
| 315 |
doc.add_picture(first_sdg_plot_path, width=Inches(6))
|
|
@@ -358,10 +368,17 @@ def generate_sentence_report(df_sentences, report_file_name):
|
|
| 358 |
first_sdg_plot_path = f"{sanitized_doc_name}_first_sdg_sentence.jpeg"
|
| 359 |
second_sdg_plot_path = f"{sanitized_doc_name}_second_sdg_sentence.jpeg"
|
| 360 |
|
| 361 |
-
plot_sdg(
|
| 362 |
-
|
| 363 |
-
|
| 364 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 365 |
|
| 366 |
# Add plots to the Word document
|
| 367 |
doc.add_picture(first_sdg_plot_path, width=Inches(6))
|
|
@@ -546,7 +563,7 @@ def launch_interface():
|
|
| 546 |
gr.Markdown(
|
| 547 |
"When the analysis is done, the Primary SDGs bar graph on the left will show "+
|
| 548 |
"the percentage of pages that strongly align with each SDG. The icon for the most frequent "+
|
| 549 |
-
"SDG will be highlighted
|
| 550 |
label = "Note", container=True
|
| 551 |
)
|
| 552 |
|
|
@@ -594,7 +611,7 @@ def launch_interface():
|
|
| 594 |
gr.Markdown(
|
| 595 |
"When the analysis is done, the Primary SDGs bar graph on the left will show "+
|
| 596 |
"the percentage of sentences that strongly align with each SDG. The icon for the most frequent "+
|
| 597 |
-
"SDG will be highlighted
|
| 598 |
label = "Note", container=True
|
| 599 |
)
|
| 600 |
|
|
@@ -662,10 +679,16 @@ def launch_interface():
|
|
| 662 |
|
| 663 |
# Generate plots with icon overlay
|
| 664 |
first_plot = plot_sdg(
|
| 665 |
-
df_page_predictions, "
|
|
|
|
|
|
|
|
|
|
| 666 |
)
|
| 667 |
second_plot = plot_sdg(
|
| 668 |
-
df_page_predictions, "
|
|
|
|
|
|
|
|
|
|
| 669 |
)
|
| 670 |
|
| 671 |
# Define output file names
|
|
@@ -740,10 +763,16 @@ def launch_interface():
|
|
| 740 |
|
| 741 |
# Generate plots with icon overlay
|
| 742 |
first_plot = plot_sdg(
|
| 743 |
-
df_sentence_predictions, "
|
|
|
|
|
|
|
|
|
|
| 744 |
)
|
| 745 |
second_plot = plot_sdg(
|
| 746 |
-
df_sentence_predictions, "
|
|
|
|
|
|
|
|
|
|
| 747 |
)
|
| 748 |
|
| 749 |
# Define output file names
|
|
|
|
| 170 |
|
| 171 |
# Function to plot SDG dominant bar graphs using Plotly
|
| 172 |
# Function to plot SDG dominant bar graphs using Plotly
|
| 173 |
+
def plot_sdg(df, title, pred_column, x_axis_title=None, y_axis_title=None, icons_folder='assets/icons/'):
|
| 174 |
"""
|
| 175 |
Plots a horizontal bar graph of SDG predictions and superimposes the icon of the most frequent SDG.
|
| 176 |
|
|
|
|
| 178 |
df (pd.DataFrame): DataFrame containing SDG predictions.
|
| 179 |
title (str): Title of the plot.
|
| 180 |
pred_column (str): Column name to use for plotting (e.g., 'pred1').
|
| 181 |
+
x_axis_title (str): Title for the x-axis.
|
| 182 |
+
y_axis_title (str): Title for the y-axis.
|
| 183 |
icons_folder (str): Path to the folder containing SDG icons.
|
| 184 |
|
| 185 |
Returns:
|
|
|
|
| 208 |
fig.update_traces(
|
| 209 |
texttemplate='%{x:.2f}%',
|
| 210 |
textposition='auto',
|
| 211 |
+
textfont=dict(size=11)
|
| 212 |
)
|
| 213 |
|
| 214 |
# Adjust layout for better visibility
|
|
|
|
| 218 |
),
|
| 219 |
yaxis=dict(
|
| 220 |
automargin=True,
|
| 221 |
+
title=y_axis_title,
|
| 222 |
+
tickfont=dict(size=13)
|
| 223 |
),
|
| 224 |
+
margin=dict(l=20, r=100, t=30, b=20), # Increased right margin for icon
|
| 225 |
height=600,
|
| 226 |
+
#width=800,
|
| 227 |
showlegend=False,
|
| 228 |
template="simple_white",
|
| 229 |
xaxis=dict(
|
| 230 |
+
title=x_axis_title,
|
| 231 |
+
tickfont=dict(size=13) # Reduce x-axis font size
|
| 232 |
),
|
| 233 |
)
|
| 234 |
|
|
|
|
| 253 |
dict(
|
| 254 |
source='data:image/png;base64,' + encoded_image,
|
| 255 |
xref="paper", yref="paper",
|
| 256 |
+
x=.98, y=1.0, # Positioning: slightly to the right and top
|
| 257 |
sizex=0.2, sizey=0.2, # Size of the icon
|
| 258 |
xanchor="left",
|
| 259 |
yanchor="top",
|
|
|
|
| 267 |
|
| 268 |
def save_figure_as_jpeg(fig, filename):
|
| 269 |
"""Saves the Plotly figure as a high-resolution JPEG."""
|
| 270 |
+
pio.write_image(fig, filename, format='jpeg', width=700, height=650, scale=7, engine="kaleido")
|
| 271 |
|
| 272 |
# Generate reports (page and sentence levels)
|
| 273 |
def generate_page_report(df_pages, report_file_name):
|
|
|
|
| 309 |
first_sdg_plot_path = f"{sanitized_doc_name}_first_sdg_page.jpeg"
|
| 310 |
second_sdg_plot_path = f"{sanitized_doc_name}_second_sdg_page.jpeg"
|
| 311 |
|
| 312 |
+
plot_sdg(
|
| 313 |
+
df_doc, "", 'pred1',
|
| 314 |
+
x_axis_title="Percentage (%) of aligned pages",
|
| 315 |
+
y_axis_title="Primary SDGs"
|
| 316 |
+
).write_image(first_sdg_plot_path, format='jpeg', scale=7, engine="kaleido")
|
| 317 |
+
|
| 318 |
+
plot_sdg(
|
| 319 |
+
df_doc, "", 'pred2',
|
| 320 |
+
x_axis_title="Percentage (%) of aligned pages",
|
| 321 |
+
y_axis_title="Secondary SDGs"
|
| 322 |
+
).write_image(second_sdg_plot_path, format='jpeg', scale=7, engine="kaleido")
|
| 323 |
|
| 324 |
# Add plots to the Word document
|
| 325 |
doc.add_picture(first_sdg_plot_path, width=Inches(6))
|
|
|
|
| 368 |
first_sdg_plot_path = f"{sanitized_doc_name}_first_sdg_sentence.jpeg"
|
| 369 |
second_sdg_plot_path = f"{sanitized_doc_name}_second_sdg_sentence.jpeg"
|
| 370 |
|
| 371 |
+
plot_sdg(
|
| 372 |
+
df_doc, "", 'pred1',
|
| 373 |
+
x_axis_title="Percentage (%) of aligned sentences",
|
| 374 |
+
y_axis_title="Primary SDGs"
|
| 375 |
+
).write_image(first_sdg_plot_path, format='jpeg', scale=7, engine="kaleido")
|
| 376 |
+
|
| 377 |
+
plot_sdg(
|
| 378 |
+
df_doc, "", 'pred2',
|
| 379 |
+
x_axis_title="Percentage (%) of aligned sentences",
|
| 380 |
+
y_axis_title="Secondary SDGs"
|
| 381 |
+
).write_image(second_sdg_plot_path, format='jpeg', scale=7, engine="kaleido")
|
| 382 |
|
| 383 |
# Add plots to the Word document
|
| 384 |
doc.add_picture(first_sdg_plot_path, width=Inches(6))
|
|
|
|
| 563 |
gr.Markdown(
|
| 564 |
"When the analysis is done, the Primary SDGs bar graph on the left will show "+
|
| 565 |
"the percentage of pages that strongly align with each SDG. The icon for the most frequent "+
|
| 566 |
+
"SDG will be highlighted on the graph. Download the Page Predictions CVS for further details.",
|
| 567 |
label = "Note", container=True
|
| 568 |
)
|
| 569 |
|
|
|
|
| 611 |
gr.Markdown(
|
| 612 |
"When the analysis is done, the Primary SDGs bar graph on the left will show "+
|
| 613 |
"the percentage of sentences that strongly align with each SDG. The icon for the most frequent "+
|
| 614 |
+
"SDG will be highlighted on the graph. Download the Sentence Predictions CVS for further details.",
|
| 615 |
label = "Note", container=True
|
| 616 |
)
|
| 617 |
|
|
|
|
| 679 |
|
| 680 |
# Generate plots with icon overlay
|
| 681 |
first_plot = plot_sdg(
|
| 682 |
+
df_page_predictions, "",
|
| 683 |
+
'pred1',
|
| 684 |
+
x_axis_title="Percentage (%) of aligned pages",
|
| 685 |
+
y_axis_title="Primary SDGs"
|
| 686 |
)
|
| 687 |
second_plot = plot_sdg(
|
| 688 |
+
df_page_predictions, "",
|
| 689 |
+
'pred2',
|
| 690 |
+
x_axis_title="Percentage (%) of aligned pages",
|
| 691 |
+
y_axis_title="Secondary SDGs"
|
| 692 |
)
|
| 693 |
|
| 694 |
# Define output file names
|
|
|
|
| 763 |
|
| 764 |
# Generate plots with icon overlay
|
| 765 |
first_plot = plot_sdg(
|
| 766 |
+
df_sentence_predictions, "",
|
| 767 |
+
'pred1',
|
| 768 |
+
x_axis_title="Percentage (%) of aligned sentences",
|
| 769 |
+
y_axis_title="Primary SDGs"
|
| 770 |
)
|
| 771 |
second_plot = plot_sdg(
|
| 772 |
+
df_sentence_predictions, "",
|
| 773 |
+
'pred2',
|
| 774 |
+
x_axis_title="Percentage (%) of aligned sentences",
|
| 775 |
+
y_axis_title="Secondary SDGs"
|
| 776 |
)
|
| 777 |
|
| 778 |
# Define output file names
|