Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,118 +1,272 @@
|
|
| 1 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 2 |
import streamlit as st
|
| 3 |
-
import tempfile
|
| 4 |
from pymongo import MongoClient
|
| 5 |
-
|
| 6 |
-
|
| 7 |
-
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|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
-
|
| 109 |
-
|
| 110 |
-
|
| 111 |
-
|
| 112 |
-
|
| 113 |
-
|
| 114 |
-
|
| 115 |
-
|
| 116 |
-
|
| 117 |
-
|
| 118 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# app.py
|
| 2 |
+
import json
|
| 3 |
+
import math
|
| 4 |
+
from datetime import datetime
|
| 5 |
+
from typing import Dict, List
|
| 6 |
+
|
| 7 |
+
import numpy as np
|
| 8 |
+
import pandas as pd
|
| 9 |
+
import plotly.graph_objects as go
|
| 10 |
import streamlit as st
|
|
|
|
| 11 |
from pymongo import MongoClient
|
| 12 |
+
|
| 13 |
+
st.set_page_config(page_title="Student Skill Radar", layout="wide")
|
| 14 |
+
|
| 15 |
+
# ------------------- Constants -------------------
|
| 16 |
+
SKILLS = [
|
| 17 |
+
"Problem-Solving",
|
| 18 |
+
"Critical Thinking",
|
| 19 |
+
"Analytical Reasoning",
|
| 20 |
+
"Adaptability",
|
| 21 |
+
"Continuous Learning",
|
| 22 |
+
"Creativity",
|
| 23 |
+
"Communication",
|
| 24 |
+
"Collaboration",
|
| 25 |
+
"Community Engagement",
|
| 26 |
+
"Emotional Intelligence",
|
| 27 |
+
"Ethical Decision-Making",
|
| 28 |
+
"Time Management",
|
| 29 |
+
"Tech Aptitude",
|
| 30 |
+
]
|
| 31 |
+
|
| 32 |
+
SKILL_GROUPS = {
|
| 33 |
+
"Problem-Solving, Critical Thinking, Analytical Reasoning": [
|
| 34 |
+
"Problem-Solving",
|
| 35 |
+
"Critical Thinking",
|
| 36 |
+
"Analytical Reasoning",
|
| 37 |
+
],
|
| 38 |
+
"Adaptability, Continuous Learning, Creativity": [
|
| 39 |
+
"Adaptability",
|
| 40 |
+
"Continuous Learning",
|
| 41 |
+
"Creativity",
|
| 42 |
+
],
|
| 43 |
+
"Time Management": ["Time Management"],
|
| 44 |
+
"Communication, Teamwork, Collaboration, Community Engagement": [
|
| 45 |
+
"Communication",
|
| 46 |
+
"Collaboration",
|
| 47 |
+
"Community Engagement",
|
| 48 |
+
],
|
| 49 |
+
"Emotional Intelligence, Ethical Decision Making": [
|
| 50 |
+
"Emotional Intelligence",
|
| 51 |
+
"Ethical Decision-Making",
|
| 52 |
+
],
|
| 53 |
+
"Tech Aptitude": ["Tech Aptitude"],
|
| 54 |
+
}
|
| 55 |
+
|
| 56 |
+
# ------------------- Helpers -------------------
|
| 57 |
+
def safe_mean(vals):
|
| 58 |
+
vals = [v for v in vals if v is not None]
|
| 59 |
+
return float(np.mean(vals)) if vals else 0.0
|
| 60 |
+
|
| 61 |
+
|
| 62 |
+
def to_frame(records: List[dict]) -> pd.DataFrame:
|
| 63 |
+
if not records:
|
| 64 |
+
return pd.DataFrame()
|
| 65 |
+
df = pd.DataFrame(records)
|
| 66 |
+
# Expand skills into columns
|
| 67 |
+
skill_df = pd.json_normalize(df["skills"]).reindex(columns=SKILLS)
|
| 68 |
+
for k in SKILLS:
|
| 69 |
+
if k not in skill_df:
|
| 70 |
+
skill_df[k] = 0.0
|
| 71 |
+
df = pd.concat([df.drop(columns=["skills"]), skill_df], axis=1)
|
| 72 |
+
return df
|
| 73 |
+
|
| 74 |
+
|
| 75 |
+
def summarize_records(records: List[dict], level: str = "student") -> pd.DataFrame:
|
| 76 |
+
df = to_frame(records)
|
| 77 |
+
if df.empty:
|
| 78 |
+
return df
|
| 79 |
+
if level == "student+source":
|
| 80 |
+
df["label"] = df["student"].astype(str) + " — " + df["source"].astype(str)
|
| 81 |
+
else:
|
| 82 |
+
df["label"] = df["student"].astype(str)
|
| 83 |
+
return df.groupby("label")[SKILLS].mean().reset_index()
|
| 84 |
+
|
| 85 |
+
|
| 86 |
+
def aggregate_groups(row: pd.Series) -> Dict[str, float]:
|
| 87 |
+
out = {}
|
| 88 |
+
for group, members in SKILL_GROUPS.items():
|
| 89 |
+
out[group] = safe_mean([float(row.get(m, 0.0)) for m in members])
|
| 90 |
+
return out
|
| 91 |
+
|
| 92 |
+
|
| 93 |
+
def polar_radar(df: pd.DataFrame, grouped: bool, title: str):
|
| 94 |
+
if df.empty:
|
| 95 |
+
return go.Figure()
|
| 96 |
+
|
| 97 |
+
if grouped:
|
| 98 |
+
labels = list(SKILL_GROUPS.keys())
|
| 99 |
+
traces = []
|
| 100 |
+
for _, r in df.iterrows():
|
| 101 |
+
grp = aggregate_groups(r)
|
| 102 |
+
values = [grp[k] for k in labels]
|
| 103 |
+
traces.append(
|
| 104 |
+
go.Scatterpolar(r=values + [values[0]], theta=labels + [labels[0]], name=r["label"], fill="toself")
|
| 105 |
+
)
|
| 106 |
+
else:
|
| 107 |
+
labels = SKILLS
|
| 108 |
+
traces = []
|
| 109 |
+
for _, r in df.iterrows():
|
| 110 |
+
values = [float(r.get(k, 0.0)) for k in SKILLS]
|
| 111 |
+
traces.append(
|
| 112 |
+
go.Scatterpolar(r=values + [values[0]], theta=labels + [labels[0]], name=r["label"], fill="toself")
|
| 113 |
+
)
|
| 114 |
+
|
| 115 |
+
fig = go.Figure(traces)
|
| 116 |
+
fig.update_layout(
|
| 117 |
+
title=title or "Skill Radar",
|
| 118 |
+
showlegend=True,
|
| 119 |
+
polar=dict(radialaxis=dict(range=[0, 1.0], tickvals=[0.2, 0.4, 0.6, 0.8])),
|
| 120 |
+
margin=dict(l=30, r=30, t=60, b=30),
|
| 121 |
+
)
|
| 122 |
+
return fig
|
| 123 |
+
|
| 124 |
+
|
| 125 |
+
# ------------------- Data Loaders -------------------
|
| 126 |
+
@st.cache_data(show_spinner=False)
|
| 127 |
+
def parse_summary_files(files) -> pd.DataFrame:
|
| 128 |
+
"""Uploads: list of per-student summary JSON files"""
|
| 129 |
+
rows = []
|
| 130 |
+
for f in files or []:
|
| 131 |
try:
|
| 132 |
+
data = json.loads(f.read().decode("utf-8"))
|
| 133 |
except Exception:
|
| 134 |
+
f.seek(0)
|
| 135 |
+
data = json.load(f)
|
| 136 |
+
name = data.get("Name") or data.get("Student") or "Unknown"
|
| 137 |
+
scores = data.get("Average Skill Scores") or {}
|
| 138 |
+
row = {"label": name}
|
| 139 |
+
for k in SKILLS:
|
| 140 |
+
row[k] = float(scores.get(k, 0.0))
|
| 141 |
+
rows.append(row)
|
| 142 |
+
return pd.DataFrame(rows)
|
| 143 |
+
|
| 144 |
+
|
| 145 |
+
@st.cache_data(show_spinner=False)
|
| 146 |
+
def mongo_records(uri: str, db_name: str, coll_name: str, student: str | None, source: str | None, start: str | None, end: str | None) -> List[dict]:
|
| 147 |
+
if not (uri and db_name and coll_name):
|
| 148 |
+
return []
|
| 149 |
+
client = MongoClient(uri, serverSelectionTimeoutMS=6000)
|
| 150 |
+
coll = client[db_name][coll_name]
|
| 151 |
+
|
| 152 |
+
q = {}
|
| 153 |
+
if student and student != "(All)":
|
| 154 |
+
q["student"] = student
|
| 155 |
+
if source and source != "(All)":
|
| 156 |
+
q["source"] = source
|
| 157 |
+
if start or end:
|
| 158 |
+
q["date"] = {}
|
| 159 |
+
if start:
|
| 160 |
+
q["date"]["$gte"] = start
|
| 161 |
+
if end:
|
| 162 |
+
q["date"]["$lte"] = end
|
| 163 |
+
|
| 164 |
+
cur = coll.find(q, {"_id": 0, "student": 1, "source": 1, "date": 1, "skills": 1})
|
| 165 |
+
recs = []
|
| 166 |
+
for r in cur:
|
| 167 |
+
r.setdefault("skills", {})
|
| 168 |
+
r["skills"] = {k: float(r["skills"].get(k, 0.0)) for k in SKILLS}
|
| 169 |
+
recs.append(r)
|
| 170 |
+
return recs
|
| 171 |
+
|
| 172 |
+
|
| 173 |
+
@st.cache_data(show_spinner=False)
|
| 174 |
+
def mongo_distinct(uri: str, db_name: str, coll_name: str, field: str) -> List[str]:
|
| 175 |
+
if not (uri and db_name and coll_name):
|
| 176 |
+
return []
|
| 177 |
+
try:
|
| 178 |
+
client = MongoClient(uri, serverSelectionTimeoutMS=6000)
|
| 179 |
+
coll = client[db_name][coll_name]
|
| 180 |
+
vals = coll.distinct(field)
|
| 181 |
+
return sorted([v for v in vals if isinstance(v, str) and v.strip()])
|
| 182 |
+
except Exception:
|
| 183 |
+
return []
|
| 184 |
+
|
| 185 |
+
|
| 186 |
+
# ------------------- UI -------------------
|
| 187 |
+
st.title("Student Skill Radar — Streamlit")
|
| 188 |
+
|
| 189 |
+
with st.sidebar:
|
| 190 |
+
st.subheader("Data Source")
|
| 191 |
+
data_source = st.radio("Select source", ["Upload JSON summaries", "MongoDB"], index=0)
|
| 192 |
+
use_groups = st.toggle("Grouped skills (skill clusters)", value=False)
|
| 193 |
+
agg_level = st.selectbox("Aggregation level", ["student", "student+source"], index=0, help="How to average records before plotting")
|
| 194 |
+
chart_title = st.text_input("Chart title", value="")
|
| 195 |
+
|
| 196 |
+
if data_source == "Upload JSON summaries":
|
| 197 |
+
files = st.file_uploader("Upload 1+ summary JSON files", type=["json"], accept_multiple_files=True)
|
| 198 |
+
df = parse_summary_files(files)
|
| 199 |
+
|
| 200 |
+
# Student dropdown based on uploaded files
|
| 201 |
+
labels = ["(All)"] + (sorted(df["label"].unique().tolist()) if not df.empty else [])
|
| 202 |
+
selected = st.sidebar.selectbox("Select student", labels)
|
| 203 |
+
|
| 204 |
+
if selected != "(All)" and not df.empty:
|
| 205 |
+
df = df[df["label"] == selected]
|
| 206 |
+
|
| 207 |
+
else:
|
| 208 |
+
st.sidebar.subheader("MongoDB Settings")
|
| 209 |
+
default_uri = st.secrets.get("MONGO_URI", "")
|
| 210 |
+
mongo_uri = st.sidebar.text_input("MongoDB URI", value=default_uri, type="password")
|
| 211 |
+
db_name = st.sidebar.text_input("Database name", value="grant_docs")
|
| 212 |
+
coll_name = st.sidebar.text_input("Collection name", value="doc_chunks")
|
| 213 |
+
|
| 214 |
+
# Dynamic dropdowns from MongoDB
|
| 215 |
+
students = ["(All)"] + mongo_distinct(mongo_uri, db_name, coll_name, "student")
|
| 216 |
+
sources = ["(All)"] + mongo_distinct(mongo_uri, db_name, coll_name, "source")
|
| 217 |
+
|
| 218 |
+
student_choice = st.sidebar.selectbox("Select student", students)
|
| 219 |
+
source_choice = st.sidebar.selectbox("Select source/week", sources)
|
| 220 |
+
|
| 221 |
+
c1, c2 = st.sidebar.columns(2)
|
| 222 |
+
start_date = c1.text_input("Start date (YYYY-MM-DD)", value="")
|
| 223 |
+
end_date = c2.text_input("End date (YYYY-MM-DD)", value="")
|
| 224 |
+
|
| 225 |
+
recs = mongo_records(mongo_uri, db_name, coll_name, student_choice, source_choice, start_date or None, end_date or None)
|
| 226 |
+
df_raw = to_frame(recs)
|
| 227 |
+
if not df_raw.empty:
|
| 228 |
+
if agg_level == "student+source":
|
| 229 |
+
df_raw["label"] = df_raw["student"].astype(str) + " — " + df_raw["source"].astype(str)
|
| 230 |
else:
|
| 231 |
+
df_raw["label"] = df_raw["student"].astype(str)
|
| 232 |
+
df = df_raw.groupby("label")[SKILLS].mean().reset_index()
|
| 233 |
+
else:
|
| 234 |
+
df = pd.DataFrame()
|
| 235 |
+
|
| 236 |
+
# ------------------- Output -------------------
|
| 237 |
+
left, right = st.columns([2, 1])
|
| 238 |
+
|
| 239 |
+
with left:
|
| 240 |
+
fig = polar_radar(df if not df.empty else pd.DataFrame(), use_groups, chart_title)
|
| 241 |
+
st.plotly_chart(fig, use_container_width=True)
|
| 242 |
+
|
| 243 |
+
with right:
|
| 244 |
+
st.subheader("Averaged Scores")
|
| 245 |
+
if df.empty:
|
| 246 |
+
st.info("No data yet. Upload summaries or configure MongoDB, then select a student.")
|
| 247 |
+
else:
|
| 248 |
+
st.dataframe(df, use_container_width=True, height=450)
|
| 249 |
+
# CSV download
|
| 250 |
+
csv = df.to_csv(index=False).encode("utf-8")
|
| 251 |
+
st.download_button("Download CSV", data=csv, file_name="skill_scores.csv", mime="text/csv")
|
| 252 |
+
|
| 253 |
+
# --------------- README (for reference in Space) ---------------
|
| 254 |
+
"""
|
| 255 |
+
To deploy on Hugging Face Spaces:
|
| 256 |
+
1) Create a new Space → SDK: Streamlit → Python.
|
| 257 |
+
2) Add `app.py` and `requirements.txt` below.
|
| 258 |
+
3) (Optional) Add a Secret named `MONGO_URI` for your Mongo connection.
|
| 259 |
+
|
| 260 |
+
Accepted Schemas
|
| 261 |
+
- Summary JSON (per student):
|
| 262 |
+
{
|
| 263 |
+
"Name": "Student Name",
|
| 264 |
+
"Average Skill Scores": {"Problem-Solving": 0.6, ...}
|
| 265 |
+
}
|
| 266 |
+
- MongoDB record (per response):
|
| 267 |
+
{
|
| 268 |
+
"uid": "...", "student": "...", "source": "week_2", "date": "YYYY-MM-DD",
|
| 269 |
+
"prompt": "...", "answer": "...",
|
| 270 |
+
"skills": { "Problem-Solving": 0.6, "Collaboration": 0.7, ... }
|
| 271 |
+
}
|
| 272 |
+
"""
|