Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -2,52 +2,73 @@ import gradio as gr
|
|
| 2 |
import pandas as pd
|
| 3 |
import matplotlib.pyplot as plt
|
| 4 |
|
| 5 |
-
# ---------- CLEAN DATA ----------
|
| 6 |
def clean_data(file):
|
| 7 |
-
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 25 |
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
for col in required:
|
| 29 |
-
if col not in df.columns:
|
| 30 |
-
df[col] = None
|
| 31 |
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
|
|
|
|
|
|
|
|
|
| 35 |
|
| 36 |
-
|
| 37 |
|
| 38 |
-
if df.empty:
|
| 39 |
return df
|
| 40 |
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
(df["NPS Score"] * 0.4) +
|
| 44 |
-
(df["Completion Rate (%)"] * 0.3) +
|
| 45 |
-
(df["Satisfaction (1-5)"] * 20 * 0.3)
|
| 46 |
-
)
|
| 47 |
-
|
| 48 |
-
df["Needs Attention"] = df["Health Score"] < 60
|
| 49 |
-
|
| 50 |
-
return df
|
| 51 |
|
| 52 |
|
| 53 |
# ---------- CHARTS ----------
|
|
@@ -65,36 +86,13 @@ def charts(df):
|
|
| 65 |
return fig1, fig2
|
| 66 |
|
| 67 |
|
| 68 |
-
# ----------
|
| 69 |
def process(file):
|
| 70 |
-
|
| 71 |
-
if file is None:
|
| 72 |
-
df = pd.read_csv("sample.csv")
|
| 73 |
-
df = clean_data("sample.csv")
|
| 74 |
-
else:
|
| 75 |
-
df = clean_data(file)
|
| 76 |
|
| 77 |
-
|
| 78 |
-
return (
|
| 79 |
-
pd.DataFrame({"Message": ["No valid data found. Check your CSV."]}),
|
| 80 |
-
pd.DataFrame(),
|
| 81 |
-
pd.DataFrame(),
|
| 82 |
-
pd.DataFrame(),
|
| 83 |
-
None,
|
| 84 |
-
None
|
| 85 |
-
)
|
| 86 |
-
|
| 87 |
-
top_courses = df.sort_values(by="Health Score", ascending=False).head(3)
|
| 88 |
-
worst_courses = df.sort_values(by="Health Score").head(3)
|
| 89 |
-
attention = df[df["Needs Attention"] == True]
|
| 90 |
-
|
| 91 |
-
fig1, fig2 = charts(df)
|
| 92 |
-
|
| 93 |
-
return df, top_courses, worst_courses, attention, fig1, fig2
|
| 94 |
-
|
| 95 |
-
except Exception as e:
|
| 96 |
return (
|
| 97 |
-
pd.DataFrame({"
|
| 98 |
pd.DataFrame(),
|
| 99 |
pd.DataFrame(),
|
| 100 |
pd.DataFrame(),
|
|
@@ -102,12 +100,25 @@ def process(file):
|
|
| 102 |
None
|
| 103 |
)
|
| 104 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 105 |
|
| 106 |
# ---------- UI ----------
|
| 107 |
with gr.Blocks() as app:
|
| 108 |
gr.Markdown("# 📊 Course Quality Tracker")
|
| 109 |
|
| 110 |
-
gr.Markdown("Upload a CSV or
|
| 111 |
|
| 112 |
file_input = gr.File(label="Upload CSV")
|
| 113 |
|
|
@@ -133,10 +144,9 @@ with gr.Blocks() as app:
|
|
| 133 |
outputs=[table, top_table, worst_table, attention_table, chart1, chart2]
|
| 134 |
)
|
| 135 |
|
| 136 |
-
# load default data on start
|
| 137 |
app.load(
|
| 138 |
-
fn=
|
| 139 |
-
inputs=
|
| 140 |
outputs=[table, top_table, worst_table, attention_table, chart1, chart2]
|
| 141 |
)
|
| 142 |
|
|
|
|
| 2 |
import pandas as pd
|
| 3 |
import matplotlib.pyplot as plt
|
| 4 |
|
| 5 |
+
# ---------- CLEAN DATA (ROBUST VERSION) ----------
|
| 6 |
def clean_data(file):
|
| 7 |
+
try:
|
| 8 |
+
# read raw lines
|
| 9 |
+
with open(file.name if file else "sample.csv", "r", encoding="utf-8") as f:
|
| 10 |
+
lines = f.readlines()
|
| 11 |
+
|
| 12 |
+
# detect header row
|
| 13 |
+
header_index = None
|
| 14 |
+
for i, line in enumerate(lines):
|
| 15 |
+
if "course" in line.lower() and ("nps" in line.lower() or "completion" in line.lower()):
|
| 16 |
+
header_index = i
|
| 17 |
+
break
|
| 18 |
+
|
| 19 |
+
if header_index is None:
|
| 20 |
+
return pd.DataFrame()
|
| 21 |
+
|
| 22 |
+
# read actual data
|
| 23 |
+
df = pd.read_csv(file.name if file else "sample.csv", skiprows=header_index)
|
| 24 |
+
|
| 25 |
+
# normalize column names
|
| 26 |
+
df.columns = df.columns.str.strip().str.lower()
|
| 27 |
+
|
| 28 |
+
# flexible mapping
|
| 29 |
+
rename_map = {}
|
| 30 |
+
for col in df.columns:
|
| 31 |
+
if "course" in col:
|
| 32 |
+
rename_map[col] = "Course Name"
|
| 33 |
+
elif "nps" in col:
|
| 34 |
+
rename_map[col] = "NPS Score"
|
| 35 |
+
elif "completion" in col:
|
| 36 |
+
rename_map[col] = "Completion Rate (%)"
|
| 37 |
+
elif "satisfaction" in col or "rating" in col:
|
| 38 |
+
rename_map[col] = "Satisfaction (1-5)"
|
| 39 |
+
|
| 40 |
+
df = df.rename(columns=rename_map)
|
| 41 |
+
|
| 42 |
+
# ensure required columns
|
| 43 |
+
required = ["Course Name", "NPS Score", "Completion Rate (%)", "Satisfaction (1-5)"]
|
| 44 |
+
for col in required:
|
| 45 |
+
if col not in df.columns:
|
| 46 |
+
df[col] = None
|
| 47 |
+
|
| 48 |
+
# clean numeric values
|
| 49 |
+
for col in ["NPS Score", "Completion Rate (%)", "Satisfaction (1-5)"]:
|
| 50 |
+
df[col] = df[col].astype(str).str.replace('%', '', regex=False)
|
| 51 |
+
df[col] = df[col].astype(str).str.replace('/5', '', regex=False)
|
| 52 |
+
df[col] = pd.to_numeric(df[col], errors="coerce")
|
| 53 |
+
|
| 54 |
+
df = df.dropna()
|
| 55 |
|
| 56 |
+
if df.empty:
|
| 57 |
+
return df
|
|
|
|
|
|
|
|
|
|
| 58 |
|
| 59 |
+
# health score
|
| 60 |
+
df["Health Score"] = (
|
| 61 |
+
(df["NPS Score"] * 0.4) +
|
| 62 |
+
(df["Completion Rate (%)"] * 0.3) +
|
| 63 |
+
(df["Satisfaction (1-5)"] * 20 * 0.3)
|
| 64 |
+
)
|
| 65 |
|
| 66 |
+
df["Needs Attention"] = df["Health Score"] < 60
|
| 67 |
|
|
|
|
| 68 |
return df
|
| 69 |
|
| 70 |
+
except:
|
| 71 |
+
return pd.DataFrame()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 72 |
|
| 73 |
|
| 74 |
# ---------- CHARTS ----------
|
|
|
|
| 86 |
return fig1, fig2
|
| 87 |
|
| 88 |
|
| 89 |
+
# ---------- PROCESS ----------
|
| 90 |
def process(file):
|
| 91 |
+
df = clean_data(file)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 92 |
|
| 93 |
+
if df.empty:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 94 |
return (
|
| 95 |
+
pd.DataFrame({"Message": ["No valid data found. Check your CSV structure."]}),
|
| 96 |
pd.DataFrame(),
|
| 97 |
pd.DataFrame(),
|
| 98 |
pd.DataFrame(),
|
|
|
|
| 100 |
None
|
| 101 |
)
|
| 102 |
|
| 103 |
+
top_courses = df.sort_values(by="Health Score", ascending=False).head(3)
|
| 104 |
+
worst_courses = df.sort_values(by="Health Score").head(3)
|
| 105 |
+
attention = df[df["Needs Attention"] == True]
|
| 106 |
+
|
| 107 |
+
fig1, fig2 = charts(df)
|
| 108 |
+
|
| 109 |
+
return df, top_courses, worst_courses, attention, fig1, fig2
|
| 110 |
+
|
| 111 |
+
|
| 112 |
+
# ---------- DEFAULT LOAD ----------
|
| 113 |
+
def load_default():
|
| 114 |
+
return process(None)
|
| 115 |
+
|
| 116 |
|
| 117 |
# ---------- UI ----------
|
| 118 |
with gr.Blocks() as app:
|
| 119 |
gr.Markdown("# 📊 Course Quality Tracker")
|
| 120 |
|
| 121 |
+
gr.Markdown("Upload a CSV or view sample dataset.")
|
| 122 |
|
| 123 |
file_input = gr.File(label="Upload CSV")
|
| 124 |
|
|
|
|
| 144 |
outputs=[table, top_table, worst_table, attention_table, chart1, chart2]
|
| 145 |
)
|
| 146 |
|
|
|
|
| 147 |
app.load(
|
| 148 |
+
fn=load_default,
|
| 149 |
+
inputs=[],
|
| 150 |
outputs=[table, top_table, worst_table, attention_table, chart1, chart2]
|
| 151 |
)
|
| 152 |
|