Created
October 25, 2024 11:17
-
-
Save jakobhuss/54c27c28bb860fb7e1069bde85122ad2 to your computer and use it in GitHub Desktop.
Python script to list all ollama models from their library site
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
#!/usr/bin/env python | |
from bs4 import BeautifulSoup | |
import requests | |
import json | |
def main(): | |
soup = BeautifulSoup(requests.get("https://ollama.com/library?sort=popular").text) | |
popular_models = [h2.text.strip() for h2 in soup.find_all("h2")] | |
popular_models = [m for m in popular_models if m] | |
model_tags = [] | |
for i, model_name in enumerate(popular_models): | |
response = requests.get(f"https://ollama.com/library/{model_name}/tags") | |
response.raise_for_status() | |
soup = BeautifulSoup( | |
response.text, | |
) | |
for a in soup.find_all("a"): | |
if not a["href"].startswith(f"/library/{model_name}:"): | |
continue | |
metadata_spans = a.parent.parent.find_all("span") | |
metadata_str = list(metadata_spans[0].children)[-1].text.strip() | |
_, size, uploaded = metadata_str.split("•") | |
result = { | |
"model": model_name, | |
"tag": a.text.strip(), | |
"popularity": i, | |
"hash": metadata_spans[-1].text.strip(), | |
"size": size.strip(), | |
"uploaded": uploaded.strip(), | |
} | |
model_tags.append(result) | |
with open("ollama_models.json", "w") as f: | |
json.dump(model_tags, f) | |
if __name__ == "__main__": | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment