SanthiSastra commited on
Commit
df19938
Β·
verified Β·
1 Parent(s): b1a2e2c

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +74 -58
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 generate_report(df):
10
- # Create Word Document
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
- # Data Description
16
- doc.add_heading('1. Data Description', level=1)
17
- description = df.describe().to_string()
18
- doc.add_paragraph(description)
19
 
20
- # Missing Values
21
- doc.add_heading('2. Preprocessing: Missing Values', level=1)
22
- null_data = df.isnull().sum().to_string()
23
- doc.add_paragraph(null_data)
24
 
25
- report_path = "preprocessing_report.docx"
26
- doc.save(report_path)
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
- # Load Data
34
- df = pd.read_csv(file.name)
 
 
 
 
 
 
 
 
 
 
 
35
 
36
- # 1. EDA: Basic Stats
37
- desc = df.describe(include='all').reset_index()
 
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 = "eda_plot.png"
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
- # --- Gradio UI Design ---
53
- with gr.Blocks(theme=gr.themes.Soft()) as demo:
54
- # Header Section
55
- with gr.Column(elem_id="header"):
56
- gr.Image("sastra_logo.jpg", width=150, container=False) # Ensure file exists
57
- gr.Markdown("<h1 style='text-align: center;'>Amrita Manthan</h1>")
58
- gr.Markdown("<h3 style='text-align: center;'>Preprocessing & Data Mining Portal</h3>")
59
- gr.Markdown("<p style='text-align: center;'><i>Prof. B. Santhi, SRC, SASTRA</i></p>")
 
 
 
 
60
 
61
- with gr.Tabs():
62
- with gr.TabItem("Step 1: Upload & Process"):
63
- file_input = gr.File(label="Upload CSV Dataset", file_types=[".csv"])
64
- process_btn = gr.Button("Analyze Data", variant="primary")
 
 
 
 
 
 
 
 
 
 
 
 
 
65
 
66
- with gr.TabItem("Step 2: Data View"):
67
  with gr.Row():
68
- out_raw = gr.DataFrame(label="Raw Data (First 10 Rows)")
69
- out_desc = gr.DataFrame(label="Statistical Summary")
70
-
71
- with gr.TabItem("Step 3: Visual EDA"):
72
- out_plot = gr.Image(label="Correlation Heatmap")
 
73
 
74
- with gr.TabItem("Step 4: Download Report"):
75
- gr.Markdown("Click below to download the full preprocessing documentation.")
76
- out_file = gr.File(label="Download DOCX Report")
 
 
 
77
 
78
- # Click Logic
79
  process_btn.click(
80
- fn=process_data,
81
  inputs=[file_input],
82
- outputs=[out_raw, out_desc, out_plot, out_file]
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()