Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
@@ -82,7 +82,7 @@ investor_company_mapping = build_investor_company_mapping(data)
|
|
82 |
logger.info("Investor to company mapping created.")
|
83 |
|
84 |
# Function to filter investors based on selected country and industry
|
85 |
-
def filter_investors_by_country_and_industry(selected_country, selected_industry):
|
86 |
filtered_data = data.copy()
|
87 |
logger.info(f"Filtering data for Country: {selected_country}, Industry: {selected_industry}")
|
88 |
|
@@ -99,138 +99,56 @@ def filter_investors_by_country_and_industry(selected_country, selected_industry
|
|
99 |
investor_valuations = {}
|
100 |
for investor, companies in investor_company_mapping_filtered.items():
|
101 |
total_valuation = filtered_data[filtered_data["Company"].isin(companies)]["Valuation_Billions"].sum()
|
102 |
-
if total_valuation >=
|
103 |
investor_valuations[investor] = total_valuation
|
104 |
|
105 |
-
logger.info(f"Filtered investors with total valuation >=
|
106 |
|
107 |
return list(investor_valuations.keys()), filtered_data
|
108 |
|
109 |
-
#
|
110 |
-
def
|
111 |
-
|
112 |
-
logger.warning("No investors selected. Returning empty figure.")
|
113 |
-
return go.Figure()
|
114 |
|
115 |
-
|
116 |
-
|
117 |
-
|
118 |
-
|
119 |
-
|
120 |
-
# Build the graph
|
121 |
-
G = nx.Graph()
|
122 |
-
for investor, companies in filtered_mapping.items():
|
123 |
-
for company in companies:
|
124 |
-
G.add_edge(investor, company)
|
125 |
-
|
126 |
-
# Generate positions using spring layout
|
127 |
-
pos = nx.spring_layout(G, k=0.2, seed=42)
|
128 |
-
|
129 |
-
# Prepare Plotly traces
|
130 |
-
edge_x = []
|
131 |
-
edge_y = []
|
132 |
-
for edge in G.edges():
|
133 |
-
x0, y0 = pos[edge[0]]
|
134 |
-
x1, y1 = pos[edge[1]]
|
135 |
-
edge_x += [x0, x1, None]
|
136 |
-
edge_y += [y0, y1, None]
|
137 |
-
|
138 |
-
edge_trace = go.Scatter(
|
139 |
-
x=edge_x, y=edge_y,
|
140 |
-
line=dict(width=0.5, color='#888'),
|
141 |
-
hoverinfo='none',
|
142 |
-
mode='lines'
|
143 |
-
)
|
144 |
-
|
145 |
-
node_x = []
|
146 |
-
node_y = []
|
147 |
-
node_text = []
|
148 |
-
node_size = []
|
149 |
-
node_color = []
|
150 |
-
customdata = []
|
151 |
-
for node in G.nodes():
|
152 |
-
x, y = pos[node]
|
153 |
-
node_x.append(x)
|
154 |
-
node_y.append(y)
|
155 |
-
if node in filtered_mapping:
|
156 |
-
node_text.append(f"Investor: {node}")
|
157 |
-
node_size.append(20) # Investors have larger size
|
158 |
-
node_color.append('orange')
|
159 |
-
customdata.append(None) # Investors do not have a single valuation
|
160 |
-
else:
|
161 |
-
valuation = filtered_data.loc[filtered_data["Company"] == node, "Valuation_Billions"].sum()
|
162 |
-
node_text.append(f"Company: {node}<br>Valuation: ${valuation}B")
|
163 |
-
node_size.append(10 + (valuation / filtered_data["Valuation_Billions"].max()) * 30 if filtered_data["Valuation_Billions"].max() else 10)
|
164 |
-
node_color.append('green')
|
165 |
-
customdata.append(f"${valuation}B")
|
166 |
-
|
167 |
-
node_trace = go.Scatter(
|
168 |
-
x=node_x, y=node_y,
|
169 |
-
mode='markers',
|
170 |
-
hoverinfo='text',
|
171 |
-
text=node_text,
|
172 |
-
customdata=customdata,
|
173 |
-
marker=dict(
|
174 |
-
showscale=False,
|
175 |
-
colorscale='YlGnBu',
|
176 |
-
color=node_color,
|
177 |
-
size=node_size,
|
178 |
-
line_width=2
|
179 |
)
|
180 |
-
)
|
181 |
|
182 |
-
|
183 |
-
layout=go.Layout(
|
184 |
-
title='Venture Network Visualization',
|
185 |
-
titlefont_size=16,
|
186 |
-
showlegend=False,
|
187 |
-
hovermode='closest',
|
188 |
-
margin=dict(b=20,l=5,r=5,t=40),
|
189 |
-
annotations=[ dict(
|
190 |
-
text="",
|
191 |
-
showarrow=False,
|
192 |
-
xref="paper", yref="paper") ],
|
193 |
-
xaxis=dict(showgrid=False, zeroline=False, showticklabels=False),
|
194 |
-
yaxis=dict(showgrid=False, zeroline=False, showticklabels=False))
|
195 |
-
)
|
196 |
-
|
197 |
-
fig.update_traces(marker=dict(line=dict(width=0.5, color='white')), selector=dict(mode='markers'))
|
198 |
-
|
199 |
-
logger.info("Plotly graph generated successfully.")
|
200 |
-
|
201 |
-
return fig
|
202 |
|
203 |
# Gradio Interface
|
204 |
def main():
|
205 |
country_list = ["All"] + sorted(data["Country"].dropna().unique())
|
206 |
industry_list = ["All"] + sorted(data["Industry"].dropna().unique())
|
207 |
|
208 |
-
default_country = "United States" if "United States" in country_list else "All"
|
209 |
-
default_industry = "Enterprise Tech" if "Enterprise Tech" in industry_list else "All"
|
210 |
-
|
211 |
logger.info(f"Available countries: {country_list}")
|
212 |
logger.info(f"Available industries: {industry_list}")
|
213 |
|
214 |
with gr.Blocks() as demo:
|
215 |
with gr.Row():
|
216 |
-
country_filter = gr.Dropdown(choices=country_list, label="Filter by Country", value=
|
217 |
-
industry_filter = gr.Dropdown(choices=industry_list, label="Filter by Industry", value=
|
218 |
-
|
219 |
-
|
220 |
graph_output = gr.Plot(label="Venture Network Graph")
|
221 |
-
|
222 |
-
|
223 |
-
filtered_data_holder = gr.State()
|
224 |
|
225 |
country_filter.change(
|
226 |
-
|
227 |
-
inputs=[country_filter, industry_filter],
|
228 |
-
outputs=[
|
|
|
|
|
|
|
|
|
|
|
229 |
)
|
230 |
-
|
231 |
-
|
232 |
-
inputs=[
|
233 |
-
outputs=graph_output
|
234 |
)
|
235 |
|
236 |
demo.launch()
|
|
|
82 |
logger.info("Investor to company mapping created.")
|
83 |
|
84 |
# Function to filter investors based on selected country and industry
|
85 |
+
def filter_investors_by_country_and_industry(selected_country, selected_industry, valuation_threshold):
|
86 |
filtered_data = data.copy()
|
87 |
logger.info(f"Filtering data for Country: {selected_country}, Industry: {selected_industry}")
|
88 |
|
|
|
99 |
investor_valuations = {}
|
100 |
for investor, companies in investor_company_mapping_filtered.items():
|
101 |
total_valuation = filtered_data[filtered_data["Company"].isin(companies)]["Valuation_Billions"].sum()
|
102 |
+
if total_valuation >= valuation_threshold:
|
103 |
investor_valuations[investor] = total_valuation
|
104 |
|
105 |
+
logger.info(f"Filtered investors with total valuation >= {valuation_threshold}B: {len(investor_valuations)}")
|
106 |
|
107 |
return list(investor_valuations.keys()), filtered_data
|
108 |
|
109 |
+
# Gradio app function
|
110 |
+
def app(selected_country, selected_industry, valuation_threshold):
|
111 |
+
investor_list, filtered_data = filter_investors_by_country_and_industry(selected_country, selected_industry, valuation_threshold)
|
|
|
|
|
112 |
|
113 |
+
if not investor_list:
|
114 |
+
return (
|
115 |
+
"No investors meet the selected criteria. Try reducing the valuation threshold or selecting different filters.",
|
116 |
+
None
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
117 |
)
|
|
|
118 |
|
119 |
+
return investor_list, generate_graph(investor_list, filtered_data)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
120 |
|
121 |
# Gradio Interface
|
122 |
def main():
|
123 |
country_list = ["All"] + sorted(data["Country"].dropna().unique())
|
124 |
industry_list = ["All"] + sorted(data["Industry"].dropna().unique())
|
125 |
|
|
|
|
|
|
|
126 |
logger.info(f"Available countries: {country_list}")
|
127 |
logger.info(f"Available industries: {industry_list}")
|
128 |
|
129 |
with gr.Blocks() as demo:
|
130 |
with gr.Row():
|
131 |
+
country_filter = gr.Dropdown(choices=country_list, label="Filter by Country", value="All")
|
132 |
+
industry_filter = gr.Dropdown(choices=industry_list, label="Filter by Industry", value="All")
|
133 |
+
valuation_threshold = gr.Slider(minimum=0, maximum=50, step=1, value=20, label="Valuation Threshold (in B)")
|
134 |
+
|
135 |
graph_output = gr.Plot(label="Venture Network Graph")
|
136 |
+
investor_output = gr.Text(label="Investor Results")
|
|
|
|
|
137 |
|
138 |
country_filter.change(
|
139 |
+
app,
|
140 |
+
inputs=[country_filter, industry_filter, valuation_threshold],
|
141 |
+
outputs=[investor_output, graph_output]
|
142 |
+
)
|
143 |
+
industry_filter.change(
|
144 |
+
app,
|
145 |
+
inputs=[country_filter, industry_filter, valuation_threshold],
|
146 |
+
outputs=[investor_output, graph_output]
|
147 |
)
|
148 |
+
valuation_threshold.change(
|
149 |
+
app,
|
150 |
+
inputs=[country_filter, industry_filter, valuation_threshold],
|
151 |
+
outputs=[investor_output, graph_output]
|
152 |
)
|
153 |
|
154 |
demo.launch()
|