Spaces:
Sleeping
Sleeping
Create funding.py
Browse files- genesis/funding.py +80 -0
genesis/funding.py
ADDED
@@ -0,0 +1,80 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
# genesis/funding.py
|
2 |
+
import os
|
3 |
+
import requests
|
4 |
+
from typing import List, Dict
|
5 |
+
|
6 |
+
# Optional: If you have Crunchbase API key
|
7 |
+
CRUNCHBASE_API_KEY = os.getenv("CRUNCHBASE_API_KEY")
|
8 |
+
CRUNCHBASE_API_URL = "https://api.crunchbase.com/api/v4/entities/organizations"
|
9 |
+
|
10 |
+
# Fallback public dataset if no API key
|
11 |
+
FALLBACK_DATA_URL = "https://raw.githubusercontent.com/owid/biotech-funding-dataset/main/funding.csv"
|
12 |
+
|
13 |
+
def search_biotech_funding(query: str, max_results: int = 5) -> List[Dict]:
|
14 |
+
"""
|
15 |
+
Search for biotech funding data via Crunchbase API or fallback CSV.
|
16 |
+
Returns a list of dictionaries with:
|
17 |
+
- name
|
18 |
+
- funding_usd
|
19 |
+
- last_round
|
20 |
+
- investors
|
21 |
+
- website
|
22 |
+
"""
|
23 |
+
if CRUNCHBASE_API_KEY:
|
24 |
+
try:
|
25 |
+
params = {
|
26 |
+
"user_key": CRUNCHBASE_API_KEY,
|
27 |
+
"query": query,
|
28 |
+
"limit": max_results
|
29 |
+
}
|
30 |
+
r = requests.get(CRUNCHBASE_API_URL, params=params, timeout=15)
|
31 |
+
r.raise_for_status()
|
32 |
+
data = r.json()
|
33 |
+
|
34 |
+
results = []
|
35 |
+
for entity in data.get("entities", []):
|
36 |
+
props = entity.get("properties", {})
|
37 |
+
results.append({
|
38 |
+
"name": props.get("name", "N/A"),
|
39 |
+
"funding_usd": props.get("total_funding_usd", "N/A"),
|
40 |
+
"last_round": props.get("last_funding_type", "N/A"),
|
41 |
+
"investors": ", ".join(props.get("investors", [])) if props.get("investors") else "N/A",
|
42 |
+
"website": props.get("homepage_url", "")
|
43 |
+
})
|
44 |
+
return results
|
45 |
+
except Exception as e:
|
46 |
+
print(f"[Funding] Crunchbase API failed: {e}")
|
47 |
+
|
48 |
+
# Fallback: Use CSV dataset
|
49 |
+
try:
|
50 |
+
r = requests.get(FALLBACK_DATA_URL, timeout=10)
|
51 |
+
r.raise_for_status()
|
52 |
+
lines = r.text.strip().split("\n")
|
53 |
+
header = lines[0].split(",")
|
54 |
+
results = []
|
55 |
+
for row in lines[1:]:
|
56 |
+
cols = row.split(",")
|
57 |
+
if query.lower() in row.lower():
|
58 |
+
results.append({
|
59 |
+
"name": cols[0],
|
60 |
+
"funding_usd": cols[1],
|
61 |
+
"last_round": cols[2],
|
62 |
+
"investors": cols[3],
|
63 |
+
"website": cols[4]
|
64 |
+
})
|
65 |
+
if len(results) >= max_results:
|
66 |
+
break
|
67 |
+
return results
|
68 |
+
except Exception as e:
|
69 |
+
print(f"[Funding] Fallback CSV failed: {e}")
|
70 |
+
return []
|
71 |
+
|
72 |
+
def format_funding_markdown(companies: List[Dict]) -> str:
|
73 |
+
"""Convert funding list into markdown table."""
|
74 |
+
if not companies:
|
75 |
+
return "No funding data found."
|
76 |
+
md = "| Company | Funding (USD) | Last Round | Investors |\n|---------|----------------|------------|-----------|\n"
|
77 |
+
for c in companies:
|
78 |
+
name_link = f"[{c['name']}]({c['website']})" if c["website"] else c["name"]
|
79 |
+
md += f"| {name_link} | {c['funding_usd']} | {c['last_round']} | {c['investors']} |\n"
|
80 |
+
return md
|