File size: 2,029 Bytes
1067825 | 1 2 3 4 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 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 | from backend.state import session_store
from backend.services.profileService import run_profile
from backend.services.correlationService import get_correlation
from backend.services.clusterService import get_clusters
from backend.services.aiService import call_ai
from backend.services.promptService import build_prompt
def run_explain(session_id, target):
if session_id not in session_store:
raise Exception("session not found")
df = session_store[session_id]["df"]
if target and target in df.columns:
mode = "ml"
else:
mode = "eda"
target = None
profile = run_profile(df)
if mode == "ml":
correlation = get_correlation(df, target)
top_features = correlation["feature_importance"]
pearson = correlation.get("pearson", {}) # Get pearson if it exists, otherwise use an empty dictionary instead of crashing
spearman = correlation.get("spearman", {}) #{} is just a safe backup value when "pearson" or "spearman" is missing.
else:
correlation = get_correlation(df, None)
top_features = {}
pearson = correlation.get("pearson", {})
spearman = correlation.get("spearman", {})
try:
clusters = get_clusters(df)
except Exception:
clusters = {
"status": "skipped",
"reason": "Clustering failed"
}
summary = {
"rows": profile["rows"],
"columns": profile["columns"],
"numeric_columns": profile["numeric_column_count"],
"mode": mode,
"target": target,
"top_features": top_features,
"clusters": clusters,
"pearson": pearson,
"spearman": spearman
}
prompt = build_prompt(summary, profile)
ai_text = call_ai(prompt)
session_store[session_id]["summary"] = summary
session_store[session_id]["explanation"] = ai_text
return {
"summary": summary,
"explanation": ai_text
} |