LeonceNsh commited on
Commit
2a73beb
·
verified ·
1 Parent(s): 3ad5f4f

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +23 -45
app.py CHANGED
@@ -66,8 +66,8 @@ def build_investor_company_mapping(df):
66
  investor_company_mapping = build_investor_company_mapping(data)
67
  logger.info("Investor to company mapping created.")
68
 
69
- # Filter investors by country, industry, and investor selection
70
- def filter_investors(selected_country, selected_industry, selected_investors):
71
  filtered_data = data.copy()
72
  if selected_country != "All":
73
  filtered_data = filtered_data[filtered_data["Country"] == selected_country]
@@ -76,7 +76,9 @@ def filter_investors(selected_country, selected_industry, selected_investors):
76
  if selected_investors:
77
  pattern = '|'.join([re.escape(inv) for inv in selected_investors])
78
  filtered_data = filtered_data[filtered_data["Select_Investors"].str.contains(pattern, na=False)]
79
-
 
 
80
  investor_company_mapping_filtered = build_investor_company_mapping(filtered_data)
81
  filtered_investors = list(investor_company_mapping_filtered.keys())
82
  return filtered_investors, filtered_data
@@ -101,7 +103,6 @@ def generate_graph(investors, filtered_data):
101
  "#f781bf", # Pink
102
  "#999999", # Grey
103
  ]
104
- # Extend color_palette if necessary
105
  while num_colors > len(color_palette):
106
  color_palette.extend(color_palette)
107
 
@@ -145,24 +146,22 @@ def generate_graph(investors, filtered_data):
145
  node_x.append(x)
146
  node_y.append(y)
147
  if node in investors:
148
- # Investor node
149
- node_text.append(node) # Label investors
150
- node_color.append(investor_color_map[node]) # Color assigned to investor
151
- node_size.append(30) # Fixed size for investors
152
  node_hovertext.append(f"Investor: {node}")
153
  else:
154
- # Company node
155
  valuation = filtered_data.loc[filtered_data["Company"] == node, "Valuation_Billions"].values
156
  industry = filtered_data.loc[filtered_data["Company"] == node, "Industry"].values
157
  if len(valuation) > 0 and not pd.isnull(valuation[0]):
158
- size = valuation[0] * 5 # Scale size as needed
159
  if size < 10:
160
- size = 10 # Minimum size
161
  else:
162
- size = 15 # Default size
163
  node_size.append(size)
164
- node_text.append("") # Hide company labels by default
165
- node_color.append("#a6d854") # Light green color for companies
166
  hovertext = f"Company: {node}"
167
  if len(industry) > 0 and not pd.isnull(industry[0]):
168
  hovertext += f"<br>Industry: {industry[0]}"
@@ -187,7 +186,6 @@ def generate_graph(investors, filtered_data):
187
  textfont=dict(size=12, color="#000000")
188
  )
189
 
190
- # Add legend manually
191
  legend_items = []
192
  for investor in unique_investors:
193
  legend_items.append(
@@ -215,7 +213,6 @@ def generate_graph(investors, filtered_data):
215
  height=800
216
  )
217
 
218
- # Improve layout responsiveness
219
  fig.update_layout(
220
  autosize=True,
221
  xaxis={'showgrid': False, 'zeroline': False, 'visible': False},
@@ -225,8 +222,8 @@ def generate_graph(investors, filtered_data):
225
  return fig
226
 
227
  # Gradio app
228
- def app(selected_country, selected_industry, selected_investors):
229
- investors, filtered_data = filter_investors(selected_country, selected_industry, selected_investors)
230
  if not investors:
231
  return "No investors found with the selected filters.", go.Figure()
232
  graph = generate_graph(investors, filtered_data)
@@ -234,9 +231,9 @@ def app(selected_country, selected_industry, selected_investors):
234
 
235
  # Main function
236
  def main():
237
- import re # Added import for regex
238
  country_list = ["All"] + sorted(data["Country"].dropna().unique())
239
  industry_list = ["All"] + sorted(data["Industry"].dropna().unique())
 
240
  investor_list = sorted(investor_company_mapping.keys())
241
 
242
  with gr.Blocks(title="Venture Networks Visualization") as demo:
@@ -245,43 +242,24 @@ def main():
245
  Explore the connections between investors and companies in the venture capital ecosystem. Use the filters below to customize the network graph.
246
  """)
247
  with gr.Row():
248
- country_filter = gr.Dropdown(
249
- choices=country_list,
250
- label="Country",
251
- value="All",
252
- info="Filter companies by country."
253
- )
254
- industry_filter = gr.Dropdown(
255
- choices=industry_list,
256
- label="Industry",
257
- value="All",
258
- info="Filter companies by industry."
259
- )
260
- investor_filter = gr.Dropdown(
261
- choices=investor_list,
262
- label="Select Investors",
263
- value=[],
264
- multiselect=True,
265
- info="Select one or more investors to visualize."
266
- )
267
  with gr.Row():
268
  investor_output = gr.Textbox(label="Filtered Investors", interactive=False)
269
  graph_output = gr.Plot(label="Network Graph")
270
 
271
- inputs = [country_filter, industry_filter, investor_filter]
272
  outputs = [investor_output, graph_output]
273
 
274
- # Update the graph when any filter changes
275
  country_filter.change(app, inputs, outputs)
276
  industry_filter.change(app, inputs, outputs)
 
277
  investor_filter.change(app, inputs, outputs)
278
 
279
  gr.Markdown("""
280
- **Instructions:**
281
- - **Country**: Select a country to filter companies based on their location.
282
- - **Industry**: Choose an industry to focus on companies within that sector.
283
- - **Select Investors**: Pick one or more investors to visualize their network.
284
-
285
  """)
286
 
287
  demo.launch()
 
66
  investor_company_mapping = build_investor_company_mapping(data)
67
  logger.info("Investor to company mapping created.")
68
 
69
+ # Filter investors by country, industry, investor selection, and company selection
70
+ def filter_investors(selected_country, selected_industry, selected_investors, selected_company):
71
  filtered_data = data.copy()
72
  if selected_country != "All":
73
  filtered_data = filtered_data[filtered_data["Country"] == selected_country]
 
76
  if selected_investors:
77
  pattern = '|'.join([re.escape(inv) for inv in selected_investors])
78
  filtered_data = filtered_data[filtered_data["Select_Investors"].str.contains(pattern, na=False)]
79
+ if selected_company != "All":
80
+ filtered_data = filtered_data[filtered_data["Company"] == selected_company]
81
+
82
  investor_company_mapping_filtered = build_investor_company_mapping(filtered_data)
83
  filtered_investors = list(investor_company_mapping_filtered.keys())
84
  return filtered_investors, filtered_data
 
103
  "#f781bf", # Pink
104
  "#999999", # Grey
105
  ]
 
106
  while num_colors > len(color_palette):
107
  color_palette.extend(color_palette)
108
 
 
146
  node_x.append(x)
147
  node_y.append(y)
148
  if node in investors:
149
+ node_text.append(node)
150
+ node_color.append(investor_color_map[node])
151
+ node_size.append(30)
 
152
  node_hovertext.append(f"Investor: {node}")
153
  else:
 
154
  valuation = filtered_data.loc[filtered_data["Company"] == node, "Valuation_Billions"].values
155
  industry = filtered_data.loc[filtered_data["Company"] == node, "Industry"].values
156
  if len(valuation) > 0 and not pd.isnull(valuation[0]):
157
+ size = valuation[0] * 5
158
  if size < 10:
159
+ size = 10
160
  else:
161
+ size = 15
162
  node_size.append(size)
163
+ node_text.append("")
164
+ node_color.append("#a6d854")
165
  hovertext = f"Company: {node}"
166
  if len(industry) > 0 and not pd.isnull(industry[0]):
167
  hovertext += f"<br>Industry: {industry[0]}"
 
186
  textfont=dict(size=12, color="#000000")
187
  )
188
 
 
189
  legend_items = []
190
  for investor in unique_investors:
191
  legend_items.append(
 
213
  height=800
214
  )
215
 
 
216
  fig.update_layout(
217
  autosize=True,
218
  xaxis={'showgrid': False, 'zeroline': False, 'visible': False},
 
222
  return fig
223
 
224
  # Gradio app
225
+ def app(selected_country, selected_industry, selected_company, selected_investors):
226
+ investors, filtered_data = filter_investors(selected_country, selected_industry, selected_investors, selected_company)
227
  if not investors:
228
  return "No investors found with the selected filters.", go.Figure()
229
  graph = generate_graph(investors, filtered_data)
 
231
 
232
  # Main function
233
  def main():
 
234
  country_list = ["All"] + sorted(data["Country"].dropna().unique())
235
  industry_list = ["All"] + sorted(data["Industry"].dropna().unique())
236
+ company_list = ["All"] + sorted(data["Company"].dropna().unique())
237
  investor_list = sorted(investor_company_mapping.keys())
238
 
239
  with gr.Blocks(title="Venture Networks Visualization") as demo:
 
242
  Explore the connections between investors and companies in the venture capital ecosystem. Use the filters below to customize the network graph.
243
  """)
244
  with gr.Row():
245
+ country_filter = gr.Dropdown(choices=country_list, label="Country", value="All")
246
+ industry_filter = gr.Dropdown(choices=industry_list, label="Industry", value="All")
247
+ company_filter = gr.Dropdown(choices=company_list, label="Company", value="All")
248
+ investor_filter = gr.Dropdown(choices=investor_list, label="Select Investors", value=[], multiselect=True)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
249
  with gr.Row():
250
  investor_output = gr.Textbox(label="Filtered Investors", interactive=False)
251
  graph_output = gr.Plot(label="Network Graph")
252
 
253
+ inputs = [country_filter, industry_filter, company_filter, investor_filter]
254
  outputs = [investor_output, graph_output]
255
 
 
256
  country_filter.change(app, inputs, outputs)
257
  industry_filter.change(app, inputs, outputs)
258
+ company_filter.change(app, inputs, outputs)
259
  investor_filter.change(app, inputs, outputs)
260
 
261
  gr.Markdown("""
262
+ **Instructions:** Use the dropdowns to filter the network graph.
 
 
 
 
263
  """)
264
 
265
  demo.launch()