Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
|
@@ -9,6 +9,8 @@ from sklearn.neighbors import KNeighborsClassifier
|
|
| 9 |
from sklearn.tree import DecisionTreeClassifier
|
| 10 |
from sklearn.naive_bayes import GaussianNB
|
| 11 |
from sklearn.metrics import accuracy_score, precision_score, recall_score, f1_score
|
|
|
|
|
|
|
| 12 |
|
| 13 |
# File uploader
|
| 14 |
st.title("Model Training with Metrics")
|
|
@@ -84,12 +86,98 @@ if uploaded_file is not None:
|
|
| 84 |
st.subheader("Model Performance Metrics")
|
| 85 |
st.dataframe(metrics_df)
|
| 86 |
|
| 87 |
-
#
|
|
|
|
|
|
|
|
|
|
| 88 |
st.download_button(
|
| 89 |
-
label="Download
|
| 90 |
data=metrics_df.to_csv(index=False),
|
| 91 |
file_name="model_report.csv",
|
| 92 |
mime="text/csv"
|
| 93 |
)
|
| 94 |
|
| 95 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 9 |
from sklearn.tree import DecisionTreeClassifier
|
| 10 |
from sklearn.naive_bayes import GaussianNB
|
| 11 |
from sklearn.metrics import accuracy_score, precision_score, recall_score, f1_score
|
| 12 |
+
import matplotlib.pyplot as plt
|
| 13 |
+
import seaborn as sns
|
| 14 |
|
| 15 |
# File uploader
|
| 16 |
st.title("Model Training with Metrics")
|
|
|
|
| 86 |
st.subheader("Model Performance Metrics")
|
| 87 |
st.dataframe(metrics_df)
|
| 88 |
|
| 89 |
+
# Download options
|
| 90 |
+
st.subheader("Download Model Performance Report in Different Formats")
|
| 91 |
+
|
| 92 |
+
# CSV
|
| 93 |
st.download_button(
|
| 94 |
+
label="Download as CSV",
|
| 95 |
data=metrics_df.to_csv(index=False),
|
| 96 |
file_name="model_report.csv",
|
| 97 |
mime="text/csv"
|
| 98 |
)
|
| 99 |
|
| 100 |
+
# Excel
|
| 101 |
+
st.download_button(
|
| 102 |
+
label="Download as Excel",
|
| 103 |
+
data=metrics_df.to_excel(index=False, engine='openpyxl'),
|
| 104 |
+
file_name="model_report.xlsx",
|
| 105 |
+
mime="application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"
|
| 106 |
+
)
|
| 107 |
+
|
| 108 |
+
# JSON
|
| 109 |
+
st.download_button(
|
| 110 |
+
label="Download as JSON",
|
| 111 |
+
data=metrics_df.to_json(orient='records'),
|
| 112 |
+
file_name="model_report.json",
|
| 113 |
+
mime="application/json"
|
| 114 |
+
)
|
| 115 |
+
|
| 116 |
+
# PDF (using `fpdf` library)
|
| 117 |
+
from fpdf import FPDF
|
| 118 |
+
|
| 119 |
+
def generate_pdf(df):
|
| 120 |
+
pdf = FPDF()
|
| 121 |
+
pdf.add_page()
|
| 122 |
+
pdf.set_font("Arial", size=12)
|
| 123 |
+
pdf.cell(200, 10, txt="Model Performance Report", ln=True, align="C")
|
| 124 |
+
pdf.ln(10)
|
| 125 |
+
|
| 126 |
+
# Add table header
|
| 127 |
+
pdf.set_font("Arial", style='B', size=10)
|
| 128 |
+
for header in df.columns:
|
| 129 |
+
pdf.cell(40, 10, header, border=1)
|
| 130 |
+
pdf.ln()
|
| 131 |
+
|
| 132 |
+
# Add table rows
|
| 133 |
+
pdf.set_font("Arial", size=10)
|
| 134 |
+
for row in df.values:
|
| 135 |
+
for value in row:
|
| 136 |
+
pdf.cell(40, 10, str(value), border=1)
|
| 137 |
+
pdf.ln()
|
| 138 |
+
|
| 139 |
+
return pdf.output(dest='S').encode('latin1')
|
| 140 |
+
|
| 141 |
+
# PDF download
|
| 142 |
+
st.download_button(
|
| 143 |
+
label="Download as PDF",
|
| 144 |
+
data=generate_pdf(metrics_df),
|
| 145 |
+
file_name="model_report.pdf",
|
| 146 |
+
mime="application/pdf"
|
| 147 |
+
)
|
| 148 |
+
|
| 149 |
+
# Option to download the dataset
|
| 150 |
+
st.download_button(
|
| 151 |
+
label="Download Dataset",
|
| 152 |
+
data=df.to_csv(index=False),
|
| 153 |
+
file_name="dataset.csv",
|
| 154 |
+
mime="text/csv"
|
| 155 |
+
)
|
| 156 |
+
|
| 157 |
+
# Generate and download PNG report
|
| 158 |
+
st.subheader("Download Report as PNG")
|
| 159 |
+
|
| 160 |
+
# Create table plot using matplotlib
|
| 161 |
+
fig, ax = plt.subplots(figsize=(12, 4)) # Adjust the figure size to match the table's layout
|
| 162 |
+
ax.axis('tight')
|
| 163 |
+
ax.axis('off')
|
| 164 |
+
table_data = metrics_df.values
|
| 165 |
+
table_columns = metrics_df.columns.tolist()
|
| 166 |
+
|
| 167 |
+
table = ax.table(cellText=table_data, colLabels=table_columns, loc='center', cellLoc='center', colLoc='center')
|
| 168 |
+
table.auto_set_font_size(False)
|
| 169 |
+
table.set_fontsize(10)
|
| 170 |
+
table.scale(1.2, 1.2) # Adjust the scale for better appearance
|
| 171 |
+
|
| 172 |
+
# Save the table as a PNG file
|
| 173 |
+
png_file = "model_report.png"
|
| 174 |
+
fig.savefig(png_file, bbox_inches='tight', dpi=300)
|
| 175 |
+
|
| 176 |
+
# Provide a download button for the PNG file
|
| 177 |
+
with open(png_file, "rb") as file:
|
| 178 |
+
st.download_button(
|
| 179 |
+
label="Download as PNG",
|
| 180 |
+
data=file,
|
| 181 |
+
file_name="model_report.png",
|
| 182 |
+
mime="image/png"
|
| 183 |
+
)
|