Update app.py
Browse files
app.py
CHANGED
|
@@ -0,0 +1,78 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
import pandas as pd
|
| 3 |
+
import plotly.express as px
|
| 4 |
+
|
| 5 |
+
def clean_and_analyze(file):
|
| 6 |
+
if file is None:
|
| 7 |
+
return None, "Please upload a file.", gr.update(choices=[])
|
| 8 |
+
|
| 9 |
+
# Load data
|
| 10 |
+
try:
|
| 11 |
+
if file.name.endswith('.csv'):
|
| 12 |
+
df_raw = pd.read_csv(file.name, header=None)
|
| 13 |
+
else:
|
| 14 |
+
df_raw = pd.read_excel(file.name, header=None)
|
| 15 |
+
|
| 16 |
+
# Smart Header Detection (Finding the 'real' table start)
|
| 17 |
+
non_null_counts = df_raw.notnull().sum(axis=1)
|
| 18 |
+
header_idx = non_null_counts.idxmax()
|
| 19 |
+
|
| 20 |
+
df = df_raw.iloc[header_idx + 1:].reset_index(drop=True)
|
| 21 |
+
df.columns = [str(c).strip() for c in df_raw.iloc[header_idx].values]
|
| 22 |
+
df = df.dropna(axis=1, how='all').dropna(axis=0, how='all')
|
| 23 |
+
|
| 24 |
+
# Convert numeric columns automatically
|
| 25 |
+
for col in df.columns:
|
| 26 |
+
numeric_conv = pd.to_numeric(df[col], errors='coerce')
|
| 27 |
+
if numeric_conv.notnull().sum() > (len(df) * 0.4):
|
| 28 |
+
df[col] = numeric_conv
|
| 29 |
+
|
| 30 |
+
cols = df.columns.tolist()
|
| 31 |
+
summary = f"✅ Successfully cleaned! Found {len(df)} rows and {len(cols)} columns."
|
| 32 |
+
|
| 33 |
+
return df, summary, gr.update(choices=cols, value=cols[0]), gr.update(choices=cols, value=cols[-1])
|
| 34 |
+
except Exception as e:
|
| 35 |
+
return None, f"Error: {str(e)}", gr.update(choices=[]), gr.update(choices=[])
|
| 36 |
+
|
| 37 |
+
def create_plot(df, x_col, y_col):
|
| 38 |
+
if df is None:
|
| 39 |
+
return None
|
| 40 |
+
fig = px.bar(df, x=x_col, y=y_col, color=y_col,
|
| 41 |
+
title=f"{y_col} Analysis", template="plotly_white")
|
| 42 |
+
return fig
|
| 43 |
+
|
| 44 |
+
# --- Gradio UI Layout ---
|
| 45 |
+
with gr.Blocks(theme=gr.themes.Soft()) as demo:
|
| 46 |
+
gr.Markdown("# 📊 Course Quality Tracker (Product Ops)")
|
| 47 |
+
gr.Markdown("Upload raw exports (Zoom, LMS, CSV) and generate instant quality reports.")
|
| 48 |
+
|
| 49 |
+
current_data = gr.State()
|
| 50 |
+
|
| 51 |
+
with gr.Row():
|
| 52 |
+
file_input = gr.File(label="Upload Messy CSV or Excel")
|
| 53 |
+
with gr.Column():
|
| 54 |
+
status_msg = gr.Textbox(label="System Status", interactive=False)
|
| 55 |
+
x_sel = gr.Dropdown(label="Select Course/Identity Column")
|
| 56 |
+
y_sel = gr.Dropdown(label="Select Quality Metric (Numeric)")
|
| 57 |
+
plot_btn = gr.Button("Generate Insights", variant="primary")
|
| 58 |
+
|
| 59 |
+
with gr.Tabs():
|
| 60 |
+
with gr.TabItem("Visualization"):
|
| 61 |
+
plot_output = gr.Plot()
|
| 62 |
+
with gr.TabItem("Cleaned Data"):
|
| 63 |
+
table_output = gr.DataFrame()
|
| 64 |
+
|
| 65 |
+
# Logic Flows
|
| 66 |
+
file_input.change(
|
| 67 |
+
clean_and_analyze,
|
| 68 |
+
inputs=[file_input],
|
| 69 |
+
outputs=[current_data, status_msg, x_sel, y_sel]
|
| 70 |
+
).then(lambda df: df, inputs=[current_data], outputs=[table_output])
|
| 71 |
+
|
| 72 |
+
plot_btn.click(
|
| 73 |
+
create_plot,
|
| 74 |
+
inputs=[current_data, x_sel, y_sel],
|
| 75 |
+
outputs=[plot_output]
|
| 76 |
+
)
|
| 77 |
+
|
| 78 |
+
demo.launch()
|