Spaces:
Runtime error
Runtime error
Update app.py
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:
|
|
@@ -216,15 +218,16 @@ 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=100, t=30, b=20), # Increased right margin for icon
|
| 223 |
height=600,
|
| 224 |
-
#width=800,
|
| 225 |
showlegend=False,
|
| 226 |
template="simple_white",
|
| 227 |
xaxis=dict(
|
|
|
|
| 228 |
tickfont=dict(size=13) # Reduce x-axis font size
|
| 229 |
),
|
| 230 |
)
|
|
@@ -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=700, height=650, scale=
|
| 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))
|
|
@@ -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:
|
|
|
|
| 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 |
)
|
|
|
|
| 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))
|
|
|
|
| 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
|