Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,85 +1,101 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
import pandas as pd
|
|
|
|
| 3 |
import seaborn as sns
|
| 4 |
import matplotlib.pyplot as plt
|
| 5 |
import io
|
|
|
|
|
|
|
|
|
|
| 6 |
from docx import Document
|
| 7 |
-
from docx.shared import Inches
|
| 8 |
|
| 9 |
-
def
|
| 10 |
-
|
| 11 |
-
doc = Document()
|
| 12 |
-
doc.add_heading('Amrita Manthan: Preprocessing Report', 0)
|
| 13 |
-
doc.add_paragraph(f"Author: Prof. B. Santhi, SRC, SASTRA")
|
| 14 |
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
description = df.describe().to_string()
|
| 18 |
-
doc.add_paragraph(description)
|
| 19 |
|
| 20 |
-
# Missing
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
doc.add_paragraph(null_data)
|
| 24 |
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
return report_path
|
| 28 |
-
|
| 29 |
-
def process_data(file):
|
| 30 |
-
if file is None:
|
| 31 |
-
return None, "Please upload a CSV file.", None, None
|
| 32 |
|
| 33 |
-
#
|
| 34 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 35 |
|
| 36 |
-
|
| 37 |
-
|
|
|
|
| 38 |
|
| 39 |
-
# 2. EDA: Correlation Plot
|
| 40 |
-
plt.figure(figsize=(10, 6))
|
| 41 |
-
sns.heatmap(df.corr(numeric_only=True), annot=True, cmap='coolwarm')
|
| 42 |
plt.tight_layout()
|
| 43 |
-
plot_path = "
|
| 44 |
plt.savefig(plot_path)
|
| 45 |
plt.close()
|
| 46 |
-
|
| 47 |
-
# 3. Generate Docx
|
| 48 |
-
report = generate_report(df)
|
| 49 |
-
|
| 50 |
-
return df.head(10), desc, plot_path, report
|
| 51 |
|
| 52 |
-
#
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 60 |
|
| 61 |
-
|
| 62 |
-
|
| 63 |
-
|
| 64 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 65 |
|
| 66 |
-
with gr.TabItem("
|
| 67 |
with gr.Row():
|
| 68 |
-
out_raw = gr.DataFrame(label="
|
| 69 |
-
|
| 70 |
-
|
| 71 |
-
|
| 72 |
-
|
|
|
|
| 73 |
|
| 74 |
-
with gr.TabItem("
|
| 75 |
-
gr.
|
| 76 |
-
|
|
|
|
|
|
|
|
|
|
| 77 |
|
| 78 |
-
# Click Logic
|
| 79 |
process_btn.click(
|
| 80 |
-
fn=
|
| 81 |
inputs=[file_input],
|
| 82 |
-
outputs=[out_raw,
|
| 83 |
)
|
| 84 |
|
| 85 |
demo.launch()
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
import pandas as pd
|
| 3 |
+
import numpy as np
|
| 4 |
import seaborn as sns
|
| 5 |
import matplotlib.pyplot as plt
|
| 6 |
import io
|
| 7 |
+
import os
|
| 8 |
+
from sklearn.decomposition import PCA
|
| 9 |
+
from sklearn.preprocessing import StandardScaler
|
| 10 |
from docx import Document
|
|
|
|
| 11 |
|
| 12 |
+
def perform_eda_and_preprocess(file):
|
| 13 |
+
if file is None: return [None]*6
|
|
|
|
|
|
|
|
|
|
| 14 |
|
| 15 |
+
df = pd.read_csv(file.name)
|
| 16 |
+
numeric_df = df.select_dtypes(include=[np.number])
|
|
|
|
|
|
|
| 17 |
|
| 18 |
+
# 1. Preprocessing: Null/Missing Checks
|
| 19 |
+
null_info = df.isnull().sum().to_frame(name="Missing Values").reset_index()
|
| 20 |
+
df_clean = df.fillna(df.mean(numeric_only=True)) # Simple Imputation
|
|
|
|
| 21 |
|
| 22 |
+
# 2. Statistical Features
|
| 23 |
+
stats = df.describe(include='all').reset_index()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 24 |
|
| 25 |
+
# 3. PCA (Principal Component Analysis)
|
| 26 |
+
pca_result = "Not enough numeric data"
|
| 27 |
+
if numeric_df.shape[1] >= 2:
|
| 28 |
+
x = StandardScaler().fit_transform(numeric_df.dropna())
|
| 29 |
+
pca = PCA(n_components=2)
|
| 30 |
+
components = pca.fit_transform(x)
|
| 31 |
+
pca_df = pd.DataFrame(data=components, columns=['PC1', 'PC2'])
|
| 32 |
+
pca_result = f"Explained Variance: {pca.explained_variance_ratio_}"
|
| 33 |
+
|
| 34 |
+
# 4. Generate 15+ Plots
|
| 35 |
+
fig, axes = plt.subplots(5, 3, figsize=(20, 25))
|
| 36 |
+
axes = axes.flatten()
|
| 37 |
+
cols = numeric_df.columns[:15] # Take first 15 numeric columns
|
| 38 |
|
| 39 |
+
for i, col in enumerate(cols):
|
| 40 |
+
sns.histplot(df[col], kde=True, ax=axes[i], color='skyblue')
|
| 41 |
+
axes[i].set_title(f'Dist: {col}')
|
| 42 |
|
|
|
|
|
|
|
|
|
|
| 43 |
plt.tight_layout()
|
| 44 |
+
plot_path = "multi_plot_grid.png"
|
| 45 |
plt.savefig(plot_path)
|
| 46 |
plt.close()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 47 |
|
| 48 |
+
# 5. Generate Professional DOCX Report
|
| 49 |
+
doc_path = "Data_Mining_Report.docx"
|
| 50 |
+
doc = Document()
|
| 51 |
+
doc.add_heading('Amrita Manthan - Preprocessing Report', 0)
|
| 52 |
+
doc.add_paragraph("Institution: SRC, SASTRA | Faculty: Prof. B. Santhi")
|
| 53 |
+
doc.add_heading('Missing Value Analysis', level=1)
|
| 54 |
+
doc.add_paragraph(null_info.to_string())
|
| 55 |
+
doc.add_heading('PCA Summary', level=1)
|
| 56 |
+
doc.add_paragraph(str(pca_result))
|
| 57 |
+
doc.save(doc_path)
|
| 58 |
+
|
| 59 |
+
return df.head(10), stats, null_info, plot_path, pca_result, doc_path
|
| 60 |
|
| 61 |
+
# --- UI Layout ---
|
| 62 |
+
with gr.Blocks(theme=gr.themes.Default()) as demo:
|
| 63 |
+
# Header with Logo and Blue Bold Centered Title
|
| 64 |
+
with gr.Row():
|
| 65 |
+
with gr.Column(scale=1):
|
| 66 |
+
gr.Image("logo.jpg", width=100, show_label=False, container=False)
|
| 67 |
+
with gr.Column(scale=4):
|
| 68 |
+
gr.Markdown(
|
| 69 |
+
"<h1 style='text-align: center; color: #003366;'><b>Amrita Manthan</b></h1>"
|
| 70 |
+
"<h2 style='text-align: center; color: #003366;'><b>Preprocessing & Data Mining Portal</b></h2>"
|
| 71 |
+
"<p style='text-align: center;'><b>Prof. B. Santhi, SRC, SASTRA</b></p>"
|
| 72 |
+
)
|
| 73 |
+
|
| 74 |
+
with gr.Tabs() as ribbon:
|
| 75 |
+
with gr.TabItem("π Data Upload"):
|
| 76 |
+
file_input = gr.File(label="Upload CSV")
|
| 77 |
+
process_btn = gr.Button("START MINING", variant="primary")
|
| 78 |
|
| 79 |
+
with gr.TabItem("π Statistics & Nulls"):
|
| 80 |
with gr.Row():
|
| 81 |
+
out_raw = gr.DataFrame(label="Dataset Preview")
|
| 82 |
+
out_null = gr.DataFrame(label="Missing Value Check")
|
| 83 |
+
out_stat = gr.DataFrame(label="Statistical Summary")
|
| 84 |
+
|
| 85 |
+
with gr.TabItem("π 15-Plot EDA Grid"):
|
| 86 |
+
out_plot = gr.Image(label="Distribution Analysis (15 Plots)")
|
| 87 |
|
| 88 |
+
with gr.TabItem("𧬠PCA & Feature Selection"):
|
| 89 |
+
pca_out = gr.Textbox(label="PCA Explained Variance")
|
| 90 |
+
gr.Markdown("Note: PCA reduces dimensionality to 2 Principal Components for visualization.")
|
| 91 |
+
|
| 92 |
+
with gr.TabItem("π₯ Download Report"):
|
| 93 |
+
out_file = gr.File(label="Download DOCX Documentation")
|
| 94 |
|
|
|
|
| 95 |
process_btn.click(
|
| 96 |
+
fn=perform_eda_and_preprocess,
|
| 97 |
inputs=[file_input],
|
| 98 |
+
outputs=[out_raw, out_stat, out_null, out_plot, pca_out, out_file]
|
| 99 |
)
|
| 100 |
|
| 101 |
demo.launch()
|