Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,282 +1,294 @@
|
|
| 1 |
-
# app.py
|
| 2 |
-
# Mutual Fund Churn Explorer — Custom Modal (no Gradio.Modal required)
|
| 3 |
-
# Works on any Gradio version, including Hugging Face default
|
| 4 |
-
|
| 5 |
import gradio as gr
|
| 6 |
import pandas as pd
|
| 7 |
import networkx as nx
|
| 8 |
import plotly.graph_objects as go
|
| 9 |
import numpy as np
|
| 10 |
from collections import defaultdict
|
| 11 |
-
import io
|
| 12 |
-
|
| 13 |
-
########################################
|
| 14 |
-
# DATA + LOGIC (unchanged from before)
|
| 15 |
-
########################################
|
| 16 |
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
|
|
|
|
|
|
|
|
|
| 20 |
]
|
| 21 |
|
| 22 |
-
|
| 23 |
-
"HDFC Bank","ICICI Bank","Bajaj Finance","Bajaj Finserv","Adani Ports",
|
| 24 |
-
"Tata Motors","Shriram Finance","HAL","TCS","AU Small Finance Bank",
|
| 25 |
-
"Pearl Global","Hindalco","Tata Elxsi","Cummins India","Vedanta"
|
| 26 |
]
|
| 27 |
|
| 28 |
-
|
| 29 |
-
"SBI MF":["Bajaj Finance","AU Small Finance Bank"],
|
| 30 |
-
"ICICI Pru MF":["HDFC Bank"],
|
| 31 |
-
"HDFC MF":["Tata Elxsi","TCS"],
|
| 32 |
-
"Nippon India MF":["Hindalco"],
|
| 33 |
-
"Kotak MF":["Bajaj Finance"],
|
| 34 |
-
"UTI MF":["Adani Ports","Shriram Finance"],
|
| 35 |
-
"Axis MF":["Tata Motors","Shriram Finance"],
|
| 36 |
-
"Aditya Birla SL MF":["AU Small Finance Bank"],
|
| 37 |
-
"Mirae MF":["Bajaj Finance","HAL"],
|
| 38 |
-
"DSP MF":["Tata Motors","Bajaj Finserv"]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 39 |
}
|
| 40 |
|
| 41 |
-
|
| 42 |
-
"
|
| 43 |
-
"ICICI Pru MF":["Bajaj Finance","Adani Ports"],
|
| 44 |
-
"HDFC MF":["HDFC Bank"],
|
| 45 |
-
"Nippon India MF":["Hindalco"],
|
| 46 |
-
"Kotak MF":["AU Small Finance Bank"],
|
| 47 |
-
"UTI MF":["Hindalco","TCS"],
|
| 48 |
-
"Axis MF":["TCS"],
|
| 49 |
-
"Aditya Birla SL MF":["Adani Ports"],
|
| 50 |
-
"Mirae MF":["TCS"],
|
| 51 |
-
"DSP MF":["HAL","Shriram Finance"]
|
| 52 |
}
|
| 53 |
|
| 54 |
-
|
| 55 |
-
|
|
|
|
|
|
|
|
|
|
| 56 |
|
| 57 |
-
def sanitize_map(m
|
| 58 |
out = {}
|
| 59 |
-
for k,
|
| 60 |
-
out[k] = [
|
| 61 |
return out
|
| 62 |
|
| 63 |
-
|
| 64 |
-
|
| 65 |
-
|
| 66 |
-
|
| 67 |
-
|
| 68 |
-
|
| 69 |
-
|
| 70 |
-
|
| 71 |
-
|
| 72 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 73 |
transfers = defaultdict(int)
|
| 74 |
-
|
| 75 |
-
|
| 76 |
-
|
| 77 |
-
|
| 78 |
-
|
| 79 |
-
for
|
| 80 |
-
for c in comps:
|
| 81 |
-
|
| 82 |
-
for c in set(list(
|
| 83 |
-
|
| 84 |
-
|
|
|
|
|
|
|
| 85 |
transfers[(s,b)] += 1
|
|
|
|
|
|
|
|
|
|
|
|
|
| 86 |
|
| 87 |
-
|
| 88 |
-
for (s,b),w in transfers.items():
|
| 89 |
-
edges.append((s,b,{"action":"transfer","weight":w}))
|
| 90 |
-
return edges
|
| 91 |
|
| 92 |
-
def build_graph(
|
| 93 |
G = nx.DiGraph()
|
| 94 |
-
for a in AMCS:
|
| 95 |
-
|
| 96 |
-
|
| 97 |
-
|
| 98 |
-
|
|
|
|
|
|
|
| 99 |
if G.has_edge(a,c):
|
| 100 |
-
G[a][c]["weight"] += weight
|
| 101 |
-
G[a][c]["actions"].append(action)
|
| 102 |
else:
|
| 103 |
-
G.add_edge(a,c,weight=weight,actions=[action])
|
| 104 |
-
|
| 105 |
-
for a,cs in BUY.items(): [add(a,c,"buy",1) for c in cs]
|
| 106 |
-
for a,cs in SELL.items(): [add(a,c,"sell",1) for c in cs]
|
| 107 |
-
for a,cs in CEXIT.items():[add(a,c,"complete_exit",3) for c in cs]
|
| 108 |
-
for a,cs in FBUY.items(): [add(a,c,"fresh_buy",3) for c in cs]
|
| 109 |
-
|
| 110 |
if include_transfers:
|
| 111 |
-
|
| 112 |
-
|
|
|
|
| 113 |
if G.has_edge(s,b):
|
| 114 |
-
G[s][b]["weight"] +=
|
| 115 |
G[s][b]["actions"].append("transfer")
|
| 116 |
else:
|
| 117 |
-
G.add_edge(s,b,weight=
|
| 118 |
return G
|
| 119 |
|
|
|
|
|
|
|
|
|
|
| 120 |
def graph_to_plotly(G,
|
| 121 |
-
|
| 122 |
-
|
| 123 |
-
|
| 124 |
-
|
| 125 |
-
|
| 126 |
-
|
| 127 |
-
|
| 128 |
-
|
| 129 |
-
|
| 130 |
-
|
| 131 |
-
|
| 132 |
-
|
| 133 |
-
|
| 134 |
-
|
| 135 |
-
|
|
|
|
| 136 |
else:
|
| 137 |
-
|
| 138 |
|
| 139 |
-
|
| 140 |
-
x=
|
| 141 |
-
|
| 142 |
-
|
|
|
|
| 143 |
)
|
| 144 |
|
| 145 |
-
edge_traces=[]
|
| 146 |
-
for u,v,
|
| 147 |
-
|
| 148 |
-
|
| 149 |
-
|
| 150 |
-
|
| 151 |
-
|
| 152 |
-
|
| 153 |
-
|
| 154 |
-
|
| 155 |
-
|
| 156 |
-
|
|
|
|
| 157 |
else:
|
| 158 |
-
color=edge_color_buy; dash="solid";
|
| 159 |
-
|
| 160 |
-
|
| 161 |
-
|
| 162 |
-
|
| 163 |
-
|
| 164 |
-
|
| 165 |
-
|
| 166 |
-
|
| 167 |
-
|
| 168 |
-
|
| 169 |
-
width=1400,height=800,
|
| 170 |
-
showlegend=False,
|
| 171 |
-
xaxis=dict(visible=False),
|
| 172 |
-
yaxis=dict(visible=False),
|
| 173 |
-
margin=dict(t=50,l=10,r=10,b=10)
|
| 174 |
-
)
|
| 175 |
-
)
|
| 176 |
return fig
|
| 177 |
|
| 178 |
-
|
| 179 |
-
#
|
| 180 |
-
|
| 181 |
-
|
| 182 |
-
|
| 183 |
-
|
| 184 |
-
|
| 185 |
-
|
| 186 |
-
|
| 187 |
-
|
| 188 |
-
|
| 189 |
-
|
| 190 |
-
|
| 191 |
-
|
| 192 |
-
|
| 193 |
-
|
| 194 |
-
|
| 195 |
-
|
| 196 |
-
|
| 197 |
-
|
| 198 |
-
|
| 199 |
-
|
| 200 |
-
|
| 201 |
-
|
| 202 |
-
|
| 203 |
-
|
| 204 |
-
|
| 205 |
-
|
| 206 |
-
|
| 207 |
-
|
| 208 |
-
|
| 209 |
-
|
| 210 |
-
|
| 211 |
-
|
| 212 |
-
|
| 213 |
-
#
|
| 214 |
-
|
| 215 |
-
|
| 216 |
-
|
| 217 |
-
|
| 218 |
-
|
| 219 |
-
|
|
|
|
|
|
|
| 220 |
with gr.Row():
|
| 221 |
-
with gr.Column(scale=
|
| 222 |
-
|
| 223 |
-
|
| 224 |
-
|
| 225 |
-
|
| 226 |
-
|
| 227 |
-
|
| 228 |
-
|
| 229 |
-
|
| 230 |
-
|
| 231 |
-
|
| 232 |
-
|
| 233 |
-
|
| 234 |
-
|
| 235 |
-
|
| 236 |
-
|
| 237 |
-
|
| 238 |
-
|
| 239 |
-
|
| 240 |
-
|
| 241 |
-
|
| 242 |
-
|
| 243 |
-
(()
|
| 244 |
-
|
| 245 |
-
|
| 246 |
-
|
| 247 |
-
|
| 248 |
-
|
| 249 |
-
btn.onclick = () => {
|
| 250 |
-
mod.innerHTML = src.innerHTML; // copy settings UI into modal
|
| 251 |
-
bg.style.display = 'block';
|
| 252 |
-
mod.style.display = 'block';
|
| 253 |
-
// close when clicking outside
|
| 254 |
-
bg.onclick = () => {
|
| 255 |
-
mod.style.display = 'none';
|
| 256 |
-
bg.style.display = 'none';
|
| 257 |
-
};
|
| 258 |
-
};
|
| 259 |
-
})();
|
| 260 |
-
""")
|
| 261 |
-
|
| 262 |
-
# When user presses Update Graph inside modal
|
| 263 |
-
def update_graph(csvfile, colA, colC, buyC, sellC, transC, use_trans):
|
| 264 |
-
AM,CP,BY,SL,CE,FB = load_default_dataset()
|
| 265 |
-
G = build_graph(AM,CP,BY,SL,CE,FB,use_trans)
|
| 266 |
fig = graph_to_plotly(G,
|
| 267 |
-
|
| 268 |
-
|
| 269 |
-
|
| 270 |
-
|
| 271 |
-
|
| 272 |
-
|
|
|
|
|
|
|
|
|
|
| 273 |
return fig
|
| 274 |
|
| 275 |
-
|
| 276 |
-
|
| 277 |
-
|
| 278 |
-
|
| 279 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 280 |
|
| 281 |
if __name__ == "__main__":
|
| 282 |
-
demo.
|
|
|
|
| 1 |
+
# app.py (fixed)
|
|
|
|
|
|
|
|
|
|
| 2 |
import gradio as gr
|
| 3 |
import pandas as pd
|
| 4 |
import networkx as nx
|
| 5 |
import plotly.graph_objects as go
|
| 6 |
import numpy as np
|
| 7 |
from collections import defaultdict
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 8 |
|
| 9 |
+
# ---------------------------
|
| 10 |
+
# Data (same as before)
|
| 11 |
+
# ---------------------------
|
| 12 |
+
AMCS = [
|
| 13 |
+
"SBI MF", "ICICI Pru MF", "HDFC MF", "Nippon India MF", "Kotak MF",
|
| 14 |
+
"UTI MF", "Axis MF", "Aditya Birla SL MF", "Mirae MF", "DSP MF"
|
| 15 |
]
|
| 16 |
|
| 17 |
+
COMPANIES = [
|
| 18 |
+
"HDFC Bank", "ICICI Bank", "Bajaj Finance", "Bajaj Finserv", "Adani Ports",
|
| 19 |
+
"Tata Motors", "Shriram Finance", "HAL", "TCS", "AU Small Finance Bank",
|
| 20 |
+
"Pearl Global", "Hindalco", "Tata Elxsi", "Cummins India", "Vedanta"
|
| 21 |
]
|
| 22 |
|
| 23 |
+
BUY_MAP = {
|
| 24 |
+
"SBI MF": ["Bajaj Finance", "AU Small Finance Bank"],
|
| 25 |
+
"ICICI Pru MF": ["HDFC Bank"],
|
| 26 |
+
"HDFC MF": ["Tata Elxsi", "TCS"],
|
| 27 |
+
"Nippon India MF": ["Hindalco"],
|
| 28 |
+
"Kotak MF": ["Bajaj Finance"],
|
| 29 |
+
"UTI MF": ["Adani Ports", "Shriram Finance"],
|
| 30 |
+
"Axis MF": ["Tata Motors", "Shriram Finance"],
|
| 31 |
+
"Aditya Birla SL MF": ["AU Small Finance Bank"],
|
| 32 |
+
"Mirae MF": ["Bajaj Finance", "HAL"],
|
| 33 |
+
"DSP MF": ["Tata Motors", "Bajaj Finserv"]
|
| 34 |
+
}
|
| 35 |
+
|
| 36 |
+
SELL_MAP = {
|
| 37 |
+
"SBI MF": ["Tata Motors"],
|
| 38 |
+
"ICICI Pru MF": ["Bajaj Finance", "Adani Ports"],
|
| 39 |
+
"HDFC MF": ["HDFC Bank"],
|
| 40 |
+
"Nippon India MF": ["Hindalco"],
|
| 41 |
+
"Kotak MF": ["AU Small Finance Bank"],
|
| 42 |
+
"UTI MF": ["Hindalco", "TCS"],
|
| 43 |
+
"Axis MF": ["TCS"],
|
| 44 |
+
"Aditya Birla SL MF": ["Adani Ports"],
|
| 45 |
+
"Mirae MF": ["TCS"],
|
| 46 |
+
"DSP MF": ["HAL", "Shriram Finance"]
|
| 47 |
}
|
| 48 |
|
| 49 |
+
COMPLETE_EXIT = {
|
| 50 |
+
"DSP MF": ["Shriram Finance"],
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 51 |
}
|
| 52 |
|
| 53 |
+
FRESH_BUY = {
|
| 54 |
+
"HDFC MF": ["Tata Elxsi"],
|
| 55 |
+
"UTI MF": ["Adani Ports"],
|
| 56 |
+
"Mirae MF": ["HAL"]
|
| 57 |
+
}
|
| 58 |
|
| 59 |
+
def sanitize_map(m):
|
| 60 |
out = {}
|
| 61 |
+
for k, vals in m.items():
|
| 62 |
+
out[k] = [v for v in vals if v in COMPANIES]
|
| 63 |
return out
|
| 64 |
|
| 65 |
+
BUY_MAP = sanitize_map(BUY_MAP)
|
| 66 |
+
SELL_MAP = sanitize_map(SELL_MAP)
|
| 67 |
+
COMPLETE_EXIT = sanitize_map(COMPLETE_EXIT)
|
| 68 |
+
FRESH_BUY = sanitize_map(FRESH_BUY)
|
| 69 |
+
|
| 70 |
+
company_edges = []
|
| 71 |
+
for amc, comps in BUY_MAP.items():
|
| 72 |
+
for c in comps:
|
| 73 |
+
company_edges.append((amc, c, {"action": "buy", "weight": 1}))
|
| 74 |
+
for amc, comps in SELL_MAP.items():
|
| 75 |
+
for c in comps:
|
| 76 |
+
company_edges.append((amc, c, {"action": "sell", "weight": 1}))
|
| 77 |
+
for amc, comps in COMPLETE_EXIT.items():
|
| 78 |
+
for c in comps:
|
| 79 |
+
company_edges.append((amc, c, {"action": "complete_exit", "weight": 3}))
|
| 80 |
+
for amc, comps in FRESH_BUY.items():
|
| 81 |
+
for c in comps:
|
| 82 |
+
company_edges.append((amc, c, {"action": "fresh_buy", "weight": 3}))
|
| 83 |
+
|
| 84 |
+
def infer_amc_transfers(buy_map, sell_map):
|
| 85 |
transfers = defaultdict(int)
|
| 86 |
+
company_to_sellers = defaultdict(list)
|
| 87 |
+
company_to_buyers = defaultdict(list)
|
| 88 |
+
for amc, comps in sell_map.items():
|
| 89 |
+
for c in comps:
|
| 90 |
+
company_to_sellers[c].append(amc)
|
| 91 |
+
for amc, comps in buy_map.items():
|
| 92 |
+
for c in comps:
|
| 93 |
+
company_to_buyers[c].append(amc)
|
| 94 |
+
for c in set(list(company_to_sellers.keys()) + list(company_to_buyers.keys())):
|
| 95 |
+
sellers = company_to_sellers.get(c, [])
|
| 96 |
+
buyers = company_to_buyers.get(c, [])
|
| 97 |
+
for s in sellers:
|
| 98 |
+
for b in buyers:
|
| 99 |
transfers[(s,b)] += 1
|
| 100 |
+
edge_list = []
|
| 101 |
+
for (s,b), w in transfers.items():
|
| 102 |
+
edge_list.append((s,b, {"action": "transfer", "weight": w, "company_count": w}))
|
| 103 |
+
return edge_list
|
| 104 |
|
| 105 |
+
transfer_edges = infer_amc_transfers(BUY_MAP, SELL_MAP)
|
|
|
|
|
|
|
|
|
|
| 106 |
|
| 107 |
+
def build_graph(include_transfers=True):
|
| 108 |
G = nx.DiGraph()
|
| 109 |
+
for a in AMCS:
|
| 110 |
+
G.add_node(a, type="amc", label=a)
|
| 111 |
+
for c in COMPANIES:
|
| 112 |
+
G.add_node(c, type="company", label=c)
|
| 113 |
+
for a, c, attrs in company_edges:
|
| 114 |
+
if not G.has_node(a) or not G.has_node(c):
|
| 115 |
+
continue
|
| 116 |
if G.has_edge(a,c):
|
| 117 |
+
G[a][c]["weight"] += attrs.get("weight",1)
|
| 118 |
+
G[a][c]["actions"].append(attrs["action"])
|
| 119 |
else:
|
| 120 |
+
G.add_edge(a, c, weight=attrs.get("weight",1), actions=[attrs["action"]])
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 121 |
if include_transfers:
|
| 122 |
+
for s,b,attrs in transfer_edges:
|
| 123 |
+
if not G.has_node(s) or not G.has_node(b):
|
| 124 |
+
continue
|
| 125 |
if G.has_edge(s,b):
|
| 126 |
+
G[s][b]["weight"] += attrs.get("weight",1)
|
| 127 |
G[s][b]["actions"].append("transfer")
|
| 128 |
else:
|
| 129 |
+
G.add_edge(s, b, weight=attrs.get("weight",1), actions=["transfer"])
|
| 130 |
return G
|
| 131 |
|
| 132 |
+
# ---------------------------
|
| 133 |
+
# Visualization
|
| 134 |
+
# ---------------------------
|
| 135 |
def graph_to_plotly(G,
|
| 136 |
+
node_color_amc="#9EC5FF",
|
| 137 |
+
node_color_company="#FFCF9E",
|
| 138 |
+
node_shape_amc="circle",
|
| 139 |
+
node_shape_company="circle",
|
| 140 |
+
edge_color_buy="#2ca02c",
|
| 141 |
+
edge_color_sell="#d62728",
|
| 142 |
+
edge_color_transfer="#888888",
|
| 143 |
+
edge_thickness_base=1.2,
|
| 144 |
+
show_labels=True):
|
| 145 |
+
pos = nx.spring_layout(G, seed=42, k=1.2)
|
| 146 |
+
node_x, node_y, node_text, node_color, node_size = [], [], [], [], []
|
| 147 |
+
for n, d in G.nodes(data=True):
|
| 148 |
+
x, y = pos[n]
|
| 149 |
+
node_x.append(x); node_y.append(y); node_text.append(n)
|
| 150 |
+
if d["type"] == "amc":
|
| 151 |
+
node_color.append(node_color_amc); node_size.append(40)
|
| 152 |
else:
|
| 153 |
+
node_color.append(node_color_company); node_size.append(60)
|
| 154 |
|
| 155 |
+
node_trace = go.Scatter(
|
| 156 |
+
x=node_x, y=node_y,
|
| 157 |
+
mode='markers+text' if show_labels else 'markers',
|
| 158 |
+
marker=dict(color=node_color, size=node_size, line=dict(width=2, color="#222")),
|
| 159 |
+
text=node_text if show_labels else None, textposition="top center", hoverinfo='text'
|
| 160 |
)
|
| 161 |
|
| 162 |
+
edge_traces = []
|
| 163 |
+
for u, v, attrs in G.edges(data=True):
|
| 164 |
+
actions = attrs.get("actions",[])
|
| 165 |
+
weight = attrs.get("weight",1)
|
| 166 |
+
x0, y0 = pos[u]; x1, y1 = pos[v]
|
| 167 |
+
if "complete_exit" in actions:
|
| 168 |
+
color = edge_color_sell; dash = "solid"; width = max(edge_thickness_base * 3, 3)
|
| 169 |
+
elif "fresh_buy" in actions:
|
| 170 |
+
color = edge_color_buy; dash = "solid"; width = max(edge_thickness_base * 3, 3)
|
| 171 |
+
elif "transfer" in actions:
|
| 172 |
+
color = edge_color_transfer; dash = "dash"; width = max(edge_thickness_base * (1 + np.log1p(weight)), 1.5)
|
| 173 |
+
elif "sell" in actions:
|
| 174 |
+
color = edge_color_sell; dash = "dot"; width = max(edge_thickness_base * (1 + np.log1p(weight)), 1)
|
| 175 |
else:
|
| 176 |
+
color = edge_color_buy; dash = "solid"; width = max(edge_thickness_base * (1 + np.log1p(weight)), 1)
|
| 177 |
+
trace = go.Scatter(x=[x0, x1, None], y=[y0, y1, None],
|
| 178 |
+
line=dict(width=width, color=color, dash=dash),
|
| 179 |
+
hoverinfo='none', mode='lines')
|
| 180 |
+
edge_traces.append(trace)
|
| 181 |
+
|
| 182 |
+
fig = go.Figure(data=edge_traces + [node_trace],
|
| 183 |
+
layout=go.Layout(showlegend=False, margin=dict(b=20,l=5,r=5,t=40),
|
| 184 |
+
xaxis=dict(showgrid=False, zeroline=False, showticklabels=False),
|
| 185 |
+
yaxis=dict(showgrid=False, zeroline=False, showticklabels=False),
|
| 186 |
+
height=900, width=1400))
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 187 |
return fig
|
| 188 |
|
| 189 |
+
# ---------------------------
|
| 190 |
+
# Analysis helpers
|
| 191 |
+
# ---------------------------
|
| 192 |
+
def company_trade_summary(company_name):
|
| 193 |
+
buyers = [amc for amc, comps in BUY_MAP.items() if company_name in comps]
|
| 194 |
+
sellers = [amc for amc, comps in SELL_MAP.items() if company_name in comps]
|
| 195 |
+
fresh = [amc for amc, comps in FRESH_BUY.items() if company_name in comps]
|
| 196 |
+
exits = [amc for amc, comps in COMPLETE_EXIT.items() if company_name in comps]
|
| 197 |
+
df = pd.DataFrame({
|
| 198 |
+
"Role": ["Buyer"]*len(buyers) + ["Seller"]*len(sellers) + ["Fresh buy"]*len(fresh) + ["Complete exit"]*len(exits),
|
| 199 |
+
"AMC": buyers + sellers + fresh + exits
|
| 200 |
+
})
|
| 201 |
+
if df.empty:
|
| 202 |
+
return None, pd.DataFrame([], columns=["Role","AMC"])
|
| 203 |
+
counts = df.groupby("Role").size().reset_index(name="Count")
|
| 204 |
+
fig = go.Figure(go.Bar(x=counts["Role"], y=counts["Count"], marker_color=["green","red","orange","black"][:len(counts)]))
|
| 205 |
+
fig.update_layout(title_text=f"Trade summary for {company_name}", height=300, width=600)
|
| 206 |
+
return fig, df
|
| 207 |
+
|
| 208 |
+
def amc_transfer_summary(amc_name):
|
| 209 |
+
sold = SELL_MAP.get(amc_name, [])
|
| 210 |
+
transfers = []
|
| 211 |
+
for s in sold:
|
| 212 |
+
buyers = [amc for amc, comps in BUY_MAP.items() if s in comps]
|
| 213 |
+
for b in buyers:
|
| 214 |
+
transfers.append({"security": s, "buyer_amc": b})
|
| 215 |
+
df = pd.DataFrame(transfers)
|
| 216 |
+
if df.empty:
|
| 217 |
+
return None, pd.DataFrame([], columns=["security","buyer_amc"])
|
| 218 |
+
counts = df['buyer_amc'].value_counts().reset_index()
|
| 219 |
+
counts.columns = ['Buyer AMC', 'Count']
|
| 220 |
+
fig = go.Figure(go.Bar(x=counts['Buyer AMC'], y=counts['Count'], marker_color='lightslategray'))
|
| 221 |
+
fig.update_layout(title_text=f"Inferred transfers from {amc_name}", height=300, width=600)
|
| 222 |
+
return fig, df
|
| 223 |
+
|
| 224 |
+
# Build an initial figure (no transfers by default)
|
| 225 |
+
initial_G = build_graph(include_transfers=True)
|
| 226 |
+
initial_fig = graph_to_plotly(initial_G)
|
| 227 |
+
|
| 228 |
+
# ---------------------------
|
| 229 |
+
# Gradio UI (Blocks)
|
| 230 |
+
# ---------------------------
|
| 231 |
+
with gr.Blocks() as demo:
|
| 232 |
+
gr.Markdown("## Mutual Fund Churn explorer — interactive network + transfer analysis")
|
| 233 |
with gr.Row():
|
| 234 |
+
with gr.Column(scale=3):
|
| 235 |
+
gr.Markdown("### Network customization")
|
| 236 |
+
node_color_company = gr.ColorPicker(value="#FFCF9E", label="Company node color")
|
| 237 |
+
node_color_amc = gr.ColorPicker(value="#9EC5FF", label="AMC node color")
|
| 238 |
+
node_shape_company = gr.Dropdown(choices=["circle","square","diamond"], value="circle", label="Company node shape")
|
| 239 |
+
node_shape_amc = gr.Dropdown(choices=["circle","square","diamond"], value="circle", label="AMC node shape")
|
| 240 |
+
edge_color_buy = gr.ColorPicker(value="#2ca02c", label="BUY edge color")
|
| 241 |
+
edge_color_sell = gr.ColorPicker(value="#d62728", label="SELL edge color")
|
| 242 |
+
edge_color_transfer = gr.ColorPicker(value="#888888", label="Transfer edge color")
|
| 243 |
+
edge_thickness = gr.Slider(minimum=0.5, maximum=6.0, value=1.4, step=0.1, label="Edge thickness base")
|
| 244 |
+
include_transfers = gr.Checkbox(value=True, label="Infer AMC → AMC transfers (show direct loops)")
|
| 245 |
+
update_button = gr.Button("Update network")
|
| 246 |
+
|
| 247 |
+
gr.Markdown("### Inspect specific node")
|
| 248 |
+
select_company = gr.Dropdown(choices=COMPANIES, label="Select company (show buyers/sellers)")
|
| 249 |
+
select_amc = gr.Dropdown(choices=AMCS, label="Select AMC (show inferred transfers)")
|
| 250 |
+
with gr.Column(scale=7):
|
| 251 |
+
network_plot = gr.Plot(value=initial_fig, label="Network graph (drag to zoom)")
|
| 252 |
+
|
| 253 |
+
company_out_plot = gr.Plot(label="Company trade summary")
|
| 254 |
+
company_out_table = gr.DataFrame(label="Trades (company)")
|
| 255 |
+
amc_out_plot = gr.Plot(label="AMC inferred transfers")
|
| 256 |
+
amc_out_table = gr.DataFrame(label="Inferred transfers (AMC)")
|
| 257 |
+
|
| 258 |
+
def update_network(node_color_company_val, node_color_amc_val, node_shape_company_val, node_shape_amc_val,
|
| 259 |
+
edge_color_buy_val, edge_color_sell_val, edge_color_transfer_val, edge_thickness_val,
|
| 260 |
+
include_transfers_val):
|
| 261 |
+
G = build_graph(include_transfers=include_transfers_val)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 262 |
fig = graph_to_plotly(G,
|
| 263 |
+
node_color_amc=node_color_amc_val,
|
| 264 |
+
node_color_company=node_color_company_val,
|
| 265 |
+
node_shape_amc=node_shape_amc_val,
|
| 266 |
+
node_shape_company=node_shape_company_val,
|
| 267 |
+
edge_color_buy=edge_color_buy_val,
|
| 268 |
+
edge_color_sell=edge_color_sell_val,
|
| 269 |
+
edge_color_transfer=edge_color_transfer_val,
|
| 270 |
+
edge_thickness_base=edge_thickness_val,
|
| 271 |
+
show_labels=True)
|
| 272 |
return fig
|
| 273 |
|
| 274 |
+
def on_company_select(cname):
|
| 275 |
+
fig, df = company_trade_summary(cname)
|
| 276 |
+
if fig is None:
|
| 277 |
+
return None, pd.DataFrame([], columns=["Role","AMC"])
|
| 278 |
+
return fig, df
|
| 279 |
+
|
| 280 |
+
def on_amc_select(aname):
|
| 281 |
+
fig, df = amc_transfer_summary(aname)
|
| 282 |
+
if fig is None:
|
| 283 |
+
return None, pd.DataFrame([], columns=["security","buyer_amc"])
|
| 284 |
+
return fig, df
|
| 285 |
+
|
| 286 |
+
update_button.click(fn=update_network,
|
| 287 |
+
inputs=[node_color_company, node_color_amc, node_shape_company, node_shape_amc,
|
| 288 |
+
edge_color_buy, edge_color_sell, edge_color_transfer, edge_thickness, include_transfers],
|
| 289 |
+
outputs=[network_plot])
|
| 290 |
+
select_company.change(fn=on_company_select, inputs=[select_company], outputs=[company_out_plot, company_out_table])
|
| 291 |
+
select_amc.change(fn=on_amc_select, inputs=[select_amc], outputs=[amc_out_plot, amc_out_table])
|
| 292 |
|
| 293 |
if __name__ == "__main__":
|
| 294 |
+
demo.launch(server_name="0.0.0.0", server_port=7860)
|