| |
| import requests |
| import json |
| from collections import Counter |
|
|
| def get_model_counts(): |
| |
| url = "https://civitai.com/api/v1/models" |
| |
| |
| params = { |
| 'limit': 100, |
| 'page': 1 |
| } |
|
|
| |
| total_models = 0 |
|
|
| |
| allow_commercial_use = [] |
| creator = [] |
|
|
| |
| nsfw_count = [] |
| allow_derivatives = [] |
| allow_no_credit = [] |
| download_count = [] |
| favorite_count = [] |
| comment_count = [] |
| rating_count = [] |
| tipped_amount_count = [] |
| ratings = [] |
| failures = 0 |
| total_pages = 1000 |
|
|
| while True: |
| |
| try: |
| response = requests.get(url, params=params) |
| response.raise_for_status() |
| data = response.json() |
|
|
| |
| models = data["items"] |
| total_models += len(models) |
|
|
| for model in models: |
| allow_commercial_use.append(model["allowCommercialUse"]) |
| creator.append(model["creator"]["username"]) |
| nsfw_count.append(model["nsfw"]) |
| allow_derivatives.append(model["allowDerivatives"]) |
| allow_no_credit.append(model["allowNoCredit"]) |
| download_count.append(model["stats"]["downloadCount"]) |
| favorite_count.append(model["stats"]["favoriteCount"]) |
| comment_count.append(model["stats"]["commentCount"]) |
| rating_count.append(model["stats"]["ratingCount"]) |
| tipped_amount_count.append(model["stats"]["tippedAmountCount"]) |
| ratings.append(model["stats"]["rating"]) |
|
|
| |
| total_pages = int(data.get('metadata', {}).get('totalPages', 0)) |
| |
| if params['page'] >= 2: |
| break |
|
|
| |
| params['page'] += 1 |
|
|
| print(f"{params['page']} / {total_pages}") |
| except: |
| failures += 1 |
| params["page"] += 1 |
|
|
| return { |
| 'total_models': total_models, |
| 'allow_commercial_use': allow_commercial_use, |
| 'creator': creator, |
| 'nsfw_count': nsfw_count, |
| 'allow_derivatives': allow_derivatives, |
| 'allow_no_credit': allow_no_credit, |
| 'download_count': download_count, |
| 'favorite_count': favorite_count, |
| 'comment_count': comment_count, |
| 'rating_count': rating_count, |
| 'tipped_amount_count': tipped_amount_count, |
| 'ratings': ratings, |
| 'failures': failures, |
| } |
|
|
| outputs = get_model_counts() |
|
|
| def map_fn(k, v): |
| if k in ["total_models", 'failures']: |
| return v |
| elif k in ["creator", "allow_commercial_use"]: |
| return Counter(v) |
| elif k in ["ratings"]: |
| return sum(v) / len(v) |
| else: |
| return { |
| "sum": sum(v), |
| "avg": round(sum(v) / len(v), 3) |
| } |
|
|
| stats = {k: map_fn(k, v) for k,v in outputs.items()} |
| stats["num_creators"] = len(stats.pop("creator")) |
|
|
| for k in ["total_models", "download_count", "comment_count", "rating_count", "tipped_amount_count", "favorite_count"]: |
| if isinstance(stats[k], dict): |
| stats[f"{k}_per_creator"] = round(stats[k]["sum"] / stats["num_creators"], 2) |
| else: |
| stats[f"{k}_per_creator"] = round(stats[k] / stats["num_creators"], 2) |
|
|
| filename = "civitai_stats.json" |
| with open(filename, 'w') as file: |
| json.dump(stats, file, indent=4) |
|
|