Last active
November 17, 2024 07:57
-
-
Save NTT123/076fc94fc5a404bd9ad904277f8fae99 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
""" | |
This script fetches download statistics for major LLM provider packages (OpenAI, Anthropic, Claude) from PyPI Stats API | |
and generates an HTML visualization showing the relative market share across different operating systems. | |
The visualization consists of three pie charts displaying the percentage of downloads for each package on: | |
- Windows | |
- MacOS (Darwin) | |
- Linux | |
Each chart shows: | |
- Package name | |
- Percentage of total downloads for that OS | |
- Absolute number of downloads | |
""" | |
import requests | |
import pandas as pd | |
import altair as alt | |
URL = "https://pypistats.org/api/packages/{PACKAGE}/system" | |
PACKAGES = ["openai", "anthropic", "google-generativeai"] | |
CATEGORIES = ["Windows", "Darwin", "Linux"] | |
def fetch_package_data(package): | |
url = URL.format(PACKAGE=package) | |
response = requests.get(url) | |
json_data = response.json() | |
return [dict(item, package=package) for item in json_data['data']] | |
def create_category_chart(base, category, title): | |
return base.transform_filter( | |
alt.datum.category == category | |
).properties(title=f'{title}') | |
all_data = [] | |
for package in PACKAGES: | |
all_data.extend(fetch_package_data(package)) | |
df = pd.DataFrame(all_data) | |
df['date'] = pd.to_datetime(df['date']) | |
df = df[df['category'].isin(CATEGORIES)] | |
df_totals = df.groupby(['category', 'package'])['downloads'].sum().reset_index() | |
df_totals['percentage'] = df_totals.groupby('category')['downloads'].transform( | |
lambda x: x / x.sum() * 100 | |
) | |
base = alt.Chart(df_totals).mark_arc().encode( | |
theta='percentage:Q', | |
color='package:N', | |
tooltip=['package:N', 'percentage:Q', 'downloads:Q'] | |
).properties( | |
width=300, | |
height=300 | |
) | |
charts = [ | |
create_category_chart(base, 'Windows', 'Windows'), | |
create_category_chart(base, 'Darwin', 'MacOS'), | |
create_category_chart(base, 'Linux', 'Linux') | |
] | |
alt.hconcat(*charts).save('llm_provider_pipstats.html') |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment