LeonceNsh commited on
Commit
a165958
·
verified ·
1 Parent(s): 99eb020

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +58 -88
app.py CHANGED
@@ -1,102 +1,69 @@
 
1
  import networkx as nx
2
  import matplotlib.pyplot as plt
3
- import gradio as gr
4
  from io import BytesIO
5
  from PIL import Image
 
6
 
7
- # Define investors and their companies
8
- investors = {
9
- "Accel": ["Meta", "Dropbox", "Spotify", "Adroll", "PackLink", "Zoom", "Slack"],
10
- "Andreessen Horowitz": [
11
- "Airbnb", "Lyft", "Pinterest", "Coinbase", "Robinhood", "Slack"
12
- ],
13
- "Google Ventures": ["Uber", "LendingClub"],
14
- "Greylock": ["Workday", "Palo Alto Networks"],
15
- "Lightspeed Venture Partners": ["Snap", "Grubhub", "AppDynamics"],
16
- "Benchmark": ["Snap", "Uber", "WeWork"],
17
- "Norwest Venture Partners": ["LendingClub", "Opendoor"],
18
- "Emergence Capital Partners": ["Zoom", "Box", "Salesforce"],
19
- "Trinity Ventures": ["New Relic", "Care.com", "TubeMogul"],
20
- "Citi Ventures": ["Square", "Nutanix"],
21
- "Sequoia": ["Alphabet (Google)", "NVIDIA", "Dropbox", "Airbnb", "Meta"],
22
- "Y Combinator": ["Dropbox", "Airbnb", "Coinbase", "DoorDash", "Reddit"]
23
- }
24
-
25
- # Example market capitalization values (in billions USD)
26
- market_cap = {
27
- "Meta": 900,
28
- "Dropbox": 10,
29
- "Spotify": 30,
30
- "Zoom": 20,
31
- "Slack": 27,
32
- "Airbnb": 100,
33
- "Lyft": 4,
34
- "Pinterest": 14,
35
- "Coinbase": 70,
36
- "Robinhood": 10,
37
- "Uber": 60,
38
- "LendingClub": 1,
39
- "Snap": 18,
40
- "Grubhub": 6,
41
- "AppDynamics": 1,
42
- "WeWork": 0.9,
43
- "Opendoor": 3,
44
- "Box": 4,
45
- "Salesforce": 200,
46
- "Square": 90,
47
- "Nutanix": 10,
48
- "Alphabet (Google)": 1500,
49
- "NVIDIA": 1200
50
- }
51
-
52
- # Assign default size for missing companies
53
- default_size = 5
54
-
55
- # Define a color map for the investors
56
- investor_colors = {
57
- "Accel": "#1f77b4",
58
- "Andreessen Horowitz": "#ff7f0e",
59
- "Google Ventures": "#2ca02c",
60
- "Greylock": "#d62728",
61
- "Lightspeed Venture Partners": "#9467bd",
62
- "Benchmark": "#8c564b",
63
- "Norwest Venture Partners": "#e377c2",
64
- "Emergence Capital Partners": "#7f7f7f",
65
- "Trinity Ventures": "#bcbd22",
66
- "Citi Ventures": "#17becf",
67
- "Sequoia": "#1b9e77",
68
- "Y Combinator": "#d95f02"
69
- }
70
-
71
  def generate_graph(selected_investors):
72
  if not selected_investors:
73
- selected_investors = list(investors.keys())
74
-
75
  G = nx.Graph()
76
 
77
  # Add edges and nodes based on selected investors
78
  for investor in selected_investors:
79
- companies = investors[investor]
80
  for company in companies:
81
- G.add_edge(investor, company, color=investor_colors[investor])
82
 
83
- # Get edge colors
84
- edge_colors = [G[u][v]['color'] for u, v in G.edges]
 
 
 
 
 
 
85
 
86
- # Set node colors and sizes
87
  node_colors = []
88
- node_sizes = []
89
  for node in G.nodes:
90
- if node in investor_colors:
91
- node_colors.append(investor_colors[node])
92
- node_sizes.append(2000) # Fixed size for investors
93
  else:
94
- node_colors.append("#F0E68C") # Khaki for companies
95
- node_sizes.append(market_cap.get(node, default_size) * 100) # Scale up sizes
96
 
97
- # Create plot
98
  plt.figure(figsize=(18, 18))
99
- pos = nx.spring_layout(G, k=0.2, seed=42) # Fixed seed for consistency
100
  nx.draw(
101
  G, pos,
102
  with_labels=True,
@@ -104,13 +71,13 @@ def generate_graph(selected_investors):
104
  node_color=node_colors,
105
  font_size=10,
106
  font_weight="bold",
107
- edge_color=edge_colors,
108
- width=2
109
  )
110
- plt.title("Venture Funded Companies as a Densely Connected Subgraph", fontsize=20)
111
  plt.axis('off')
112
 
113
- # Save plot to a BytesIO object
114
  buf = BytesIO()
115
  plt.savefig(buf, format="png", bbox_inches="tight")
116
  plt.close()
@@ -120,10 +87,9 @@ def generate_graph(selected_investors):
120
  image = Image.open(buf)
121
  return image
122
 
123
- # Define Gradio interface
124
  def main():
125
- # Create a sorted list of investors for better UX
126
- investor_list = sorted(investors.keys())
127
 
128
  iface = gr.Interface(
129
  fn=generate_graph,
@@ -134,7 +100,11 @@ def main():
134
  ),
135
  outputs=gr.Image(type="pil", label="Venture Network Graph"),
136
  title="Venture Networks Visualization",
137
- description="Select investors to visualize their investments in various companies. The graph shows connections between investors and the companies they've invested in. Node sizes represent market capitalization.",
 
 
 
 
138
  flagging_mode="never"
139
  )
140
 
 
1
+ import pandas as pd
2
  import networkx as nx
3
  import matplotlib.pyplot as plt
 
4
  from io import BytesIO
5
  from PIL import Image
6
+ import gradio as gr
7
 
8
+ # Load and preprocess the dataset
9
+ file_path = "cbinsights_data.csv" # Replace with your file path
10
+ data = pd.read_csv(file_path)
11
+
12
+ # Rename columns based on the first row and drop the header row
13
+ data.columns = data.iloc[0]
14
+ data = data[1:]
15
+ data.columns = ["Company", "Valuation_Billions", "Date_Joined", "Country", "City", "Industry", "Select_Investors"]
16
+
17
+ # Clean and prepare data
18
+ data["Valuation_Billions"] = data["Valuation_Billions"].str.replace('$', '').str.split('.').str[0]
19
+ data["Valuation_Billions"] = pd.to_numeric(data["Valuation_Billions"], errors='coerce')
20
+ data = data.applymap(lambda x: x.strip() if isinstance(x, str) else x)
21
+
22
+ # Parse the "Select_Investors" column to map investors to companies
23
+ investor_company_mapping = {}
24
+ for _, row in data.iterrows():
25
+ company = row["Company"]
26
+ investors = row["Select_Investors"]
27
+ if pd.notnull(investors):
28
+ for investor in investors.split(","):
29
+ investor = investor.strip()
30
+ if investor not in investor_company_mapping:
31
+ investor_company_mapping[investor] = []
32
+ investor_company_mapping[investor].append(company)
33
+
34
+ # Gradio app function
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
35
  def generate_graph(selected_investors):
36
  if not selected_investors:
37
+ selected_investors = list(investor_company_mapping.keys())
38
+
39
  G = nx.Graph()
40
 
41
  # Add edges and nodes based on selected investors
42
  for investor in selected_investors:
43
+ companies = investor_company_mapping.get(investor, [])
44
  for company in companies:
45
+ G.add_edge(investor, company)
46
 
47
+ # Node sizes based on valuation
48
+ node_sizes = []
49
+ for node in G.nodes:
50
+ if node in investor_company_mapping: # Fixed size for investors
51
+ node_sizes.append(2000)
52
+ else: # Company size based on valuation
53
+ valuation = data.loc[data["Company"] == node, "Valuation_Billions"].values
54
+ node_sizes.append(valuation[0] * 100 if len(valuation) > 0 else 100)
55
 
56
+ # Node colors
57
  node_colors = []
 
58
  for node in G.nodes:
59
+ if node in investor_company_mapping:
60
+ node_colors.append("#FF5733") # Distinct color for investors
 
61
  else:
62
+ node_colors.append("#33FF57") # Distinct color for companies
 
63
 
64
+ # Create the graph plot
65
  plt.figure(figsize=(18, 18))
66
+ pos = nx.spring_layout(G, k=0.2, seed=42) # Fixed seed for consistent layout
67
  nx.draw(
68
  G, pos,
69
  with_labels=True,
 
71
  node_color=node_colors,
72
  font_size=10,
73
  font_weight="bold",
74
+ edge_color="gray",
75
+ width=1.5
76
  )
77
+ plt.title("Venture Funded Companies Visualization", fontsize=20)
78
  plt.axis('off')
79
 
80
+ # Save plot to BytesIO object
81
  buf = BytesIO()
82
  plt.savefig(buf, format="png", bbox_inches="tight")
83
  plt.close()
 
87
  image = Image.open(buf)
88
  return image
89
 
90
+ # Gradio Interface
91
  def main():
92
+ investor_list = sorted(investor_company_mapping.keys())
 
93
 
94
  iface = gr.Interface(
95
  fn=generate_graph,
 
100
  ),
101
  outputs=gr.Image(type="pil", label="Venture Network Graph"),
102
  title="Venture Networks Visualization",
103
+ description=(
104
+ "Select investors to visualize their investments in various companies. "
105
+ "The graph shows connections between investors and the companies they've invested in. "
106
+ "Node sizes represent company valuations."
107
+ ),
108
  flagging_mode="never"
109
  )
110