Spaces:
Sleeping
Sleeping
# genesis/funding.py | |
import os | |
import requests | |
from typing import List, Dict | |
# Optional: If you have Crunchbase API key | |
CRUNCHBASE_API_KEY = os.getenv("CRUNCHBASE_API_KEY") | |
CRUNCHBASE_API_URL = "https://api.crunchbase.com/api/v4/entities/organizations" | |
# Fallback public dataset if no API key | |
FALLBACK_DATA_URL = "https://raw.githubusercontent.com/owid/biotech-funding-dataset/main/funding.csv" | |
def search_biotech_funding(query: str, max_results: int = 5) -> List[Dict]: | |
""" | |
Search for biotech funding data via Crunchbase API or fallback CSV. | |
Returns a list of dictionaries with: | |
- name | |
- funding_usd | |
- last_round | |
- investors | |
- website | |
""" | |
if CRUNCHBASE_API_KEY: | |
try: | |
params = { | |
"user_key": CRUNCHBASE_API_KEY, | |
"query": query, | |
"limit": max_results | |
} | |
r = requests.get(CRUNCHBASE_API_URL, params=params, timeout=15) | |
r.raise_for_status() | |
data = r.json() | |
results = [] | |
for entity in data.get("entities", []): | |
props = entity.get("properties", {}) | |
results.append({ | |
"name": props.get("name", "N/A"), | |
"funding_usd": props.get("total_funding_usd", "N/A"), | |
"last_round": props.get("last_funding_type", "N/A"), | |
"investors": ", ".join(props.get("investors", [])) if props.get("investors") else "N/A", | |
"website": props.get("homepage_url", "") | |
}) | |
return results | |
except Exception as e: | |
print(f"[Funding] Crunchbase API failed: {e}") | |
# Fallback: Use CSV dataset | |
try: | |
r = requests.get(FALLBACK_DATA_URL, timeout=10) | |
r.raise_for_status() | |
lines = r.text.strip().split("\n") | |
header = lines[0].split(",") | |
results = [] | |
for row in lines[1:]: | |
cols = row.split(",") | |
if query.lower() in row.lower(): | |
results.append({ | |
"name": cols[0], | |
"funding_usd": cols[1], | |
"last_round": cols[2], | |
"investors": cols[3], | |
"website": cols[4] | |
}) | |
if len(results) >= max_results: | |
break | |
return results | |
except Exception as e: | |
print(f"[Funding] Fallback CSV failed: {e}") | |
return [] | |
def format_funding_markdown(companies: List[Dict]) -> str: | |
"""Convert funding list into markdown table.""" | |
if not companies: | |
return "No funding data found." | |
md = "| Company | Funding (USD) | Last Round | Investors |\n|---------|----------------|------------|-----------|\n" | |
for c in companies: | |
name_link = f"[{c['name']}]({c['website']})" if c["website"] else c["name"] | |
md += f"| {name_link} | {c['funding_usd']} | {c['last_round']} | {c['investors']} |\n" | |
return md | |