Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,5 +1,7 @@
|
|
| 1 |
import streamlit as st
|
| 2 |
import pandas as pd
|
|
|
|
|
|
|
| 3 |
from sklearn.model_selection import train_test_split
|
| 4 |
from sklearn.preprocessing import LabelEncoder
|
| 5 |
from sklearn.ensemble import RandomForestClassifier
|
|
@@ -10,7 +12,6 @@ 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 |
from tabulate import tabulate
|
| 13 |
-
import io
|
| 14 |
|
| 15 |
# Function to convert DataFrame to Excel format
|
| 16 |
def to_excel(df):
|
|
@@ -20,6 +21,31 @@ def to_excel(df):
|
|
| 20 |
output.seek(0)
|
| 21 |
return output
|
| 22 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 23 |
# File uploader
|
| 24 |
st.title("Model Training with Metrics")
|
| 25 |
uploaded_file = st.file_uploader("Choose a CSV file", type=["csv"])
|
|
@@ -110,7 +136,7 @@ if uploaded_file is not None:
|
|
| 110 |
|
| 111 |
# Option to download the model performance metrics (Results Table)
|
| 112 |
st.download_button(
|
| 113 |
-
label="Download Model Report",
|
| 114 |
data=to_excel(metrics_df), # The metrics dataframe
|
| 115 |
file_name="model_report.xlsx",
|
| 116 |
mime="application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"
|
|
@@ -118,8 +144,18 @@ if uploaded_file is not None:
|
|
| 118 |
|
| 119 |
# Option to download the cleaned dataset
|
| 120 |
st.download_button(
|
| 121 |
-
label="Download Cleaned Dataset",
|
| 122 |
data=to_excel(df), # The cleaned dataset is 'df'
|
| 123 |
file_name="cleaned_dataset.xlsx",
|
| 124 |
mime="application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"
|
| 125 |
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
import streamlit as st
|
| 2 |
import pandas as pd
|
| 3 |
+
import matplotlib.pyplot as plt
|
| 4 |
+
import io
|
| 5 |
from sklearn.model_selection import train_test_split
|
| 6 |
from sklearn.preprocessing import LabelEncoder
|
| 7 |
from sklearn.ensemble import RandomForestClassifier
|
|
|
|
| 12 |
from sklearn.naive_bayes import GaussianNB
|
| 13 |
from sklearn.metrics import accuracy_score, precision_score, recall_score, f1_score
|
| 14 |
from tabulate import tabulate
|
|
|
|
| 15 |
|
| 16 |
# Function to convert DataFrame to Excel format
|
| 17 |
def to_excel(df):
|
|
|
|
| 21 |
output.seek(0)
|
| 22 |
return output
|
| 23 |
|
| 24 |
+
# Function to save table as PNG with bold headings
|
| 25 |
+
def save_table_as_png(df):
|
| 26 |
+
fig, ax = plt.subplots(figsize=(8, 6))
|
| 27 |
+
ax.axis('tight')
|
| 28 |
+
ax.axis('off')
|
| 29 |
+
|
| 30 |
+
# Create a table from the DataFrame
|
| 31 |
+
table = ax.table(cellText=df.values, colLabels=df.columns, loc='center', cellLoc='center')
|
| 32 |
+
|
| 33 |
+
# Set the font size and bold the header row
|
| 34 |
+
table.auto_set_font_size(False)
|
| 35 |
+
table.set_fontsize(10)
|
| 36 |
+
table.scale(1.2, 1.2)
|
| 37 |
+
|
| 38 |
+
# Bold the column headers
|
| 39 |
+
for (i, j) in zip(range(len(df.columns)), table[0]):
|
| 40 |
+
table[0, j].set_text_props(weight='bold') # Make column headers bold
|
| 41 |
+
|
| 42 |
+
# Save the table as a PNG image
|
| 43 |
+
img_path = "/tmp/model_report.png"
|
| 44 |
+
plt.savefig(img_path, format="png", bbox_inches="tight")
|
| 45 |
+
plt.close(fig)
|
| 46 |
+
|
| 47 |
+
return img_path
|
| 48 |
+
|
| 49 |
# File uploader
|
| 50 |
st.title("Model Training with Metrics")
|
| 51 |
uploaded_file = st.file_uploader("Choose a CSV file", type=["csv"])
|
|
|
|
| 136 |
|
| 137 |
# Option to download the model performance metrics (Results Table)
|
| 138 |
st.download_button(
|
| 139 |
+
label="Download Model Report (Excel)",
|
| 140 |
data=to_excel(metrics_df), # The metrics dataframe
|
| 141 |
file_name="model_report.xlsx",
|
| 142 |
mime="application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"
|
|
|
|
| 144 |
|
| 145 |
# Option to download the cleaned dataset
|
| 146 |
st.download_button(
|
| 147 |
+
label="Download Cleaned Dataset (Excel)",
|
| 148 |
data=to_excel(df), # The cleaned dataset is 'df'
|
| 149 |
file_name="cleaned_dataset.xlsx",
|
| 150 |
mime="application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"
|
| 151 |
)
|
| 152 |
+
|
| 153 |
+
# Option to download the report as PNG
|
| 154 |
+
img_path = save_table_as_png(metrics_df)
|
| 155 |
+
with open(img_path, "rb") as file:
|
| 156 |
+
st.download_button(
|
| 157 |
+
label="Download Model Report (PNG)",
|
| 158 |
+
data=file,
|
| 159 |
+
file_name="model_report.png",
|
| 160 |
+
mime="image/png"
|
| 161 |
+
)
|