osanseviero commited on
Commit
7d6d8a0
·
verified ·
1 Parent(s): 2393eb8

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +82 -0
app.py ADDED
@@ -0,0 +1,82 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import requests
2
+ import gradio as gr
3
+ from huggingface_hub import list_models, list_datasets, list_spaces, login
4
+
5
+ # Helper function to get the total storage for models, datasets, or spaces
6
+ def get_total_storage(author, resource_type, oauth_token: gr.OAuthToken | None):
7
+ if resource_type == "model":
8
+ resources = list_models(author=author, token=oauth_token.token)
9
+ url_base = "https://huggingface.co/api/models"
10
+ elif resource_type == "dataset":
11
+ resources = list_datasets(author=author, token=oauth_token.token)
12
+ url_base = "https://huggingface.co/api/datasets"
13
+ elif resource_type == "space":
14
+ resources = list_spaces(author=author, token=oauth_token.token)
15
+ url_base = "https://huggingface.co/api/spaces"
16
+ else:
17
+ raise ValueError("Invalid resource type. Choose 'model', 'dataset', or 'space'.")
18
+
19
+ total_storage = 0
20
+ for resource in resources:
21
+ resource_id = resource.id
22
+ url = f"{url_base}/{resource_id}/treesize/main"
23
+ response = requests.get(url)
24
+ if response.status_code == 200:
25
+ size_info = response.json()
26
+ total_storage += size_info.get("size", 0)
27
+
28
+ return total_storage, len(resources)
29
+
30
+ def get_report(oauth_token: gr.OAuthToken | None):
31
+ # Log in to Hugging Face using the provided API token
32
+ login(api_token)
33
+
34
+ # Replace with the actual author namespace
35
+ author = "your-namespace" # Example: 'osanseviero'
36
+
37
+ # Fetch storage and counts for models, datasets, and spaces
38
+ model_storage, n_models = get_total_storage(author, "model")
39
+ dataset_storage, n_datasets = get_total_storage(author, "dataset")
40
+ space_storage, n_spaces = get_total_storage(author, "space")
41
+
42
+ # Total storage
43
+ total_storage = model_storage + dataset_storage + space_storage
44
+ total_storage_gb = total_storage / (1024 ** 3) # Convert from bytes to GB
45
+ total_storage_tb = total_storage_gb / 1024 # Convert from GB to TB
46
+
47
+ # Cost calculation (1 TB = 20 USD)
48
+ estimated_cost = total_storage_tb * 20
49
+
50
+ # Generate a report
51
+ report = f"""
52
+ ## Hugging Face Storage Report for {author}
53
+
54
+ - **Number of Models**: {n_models}
55
+ - **Number of Datasets**: {n_datasets}
56
+ - **Number of Spaces**: {n_spaces}
57
+
58
+ - **Total Storage**: {total_storage_gb:.2f} GB ({total_storage_tb:.2f} TB)
59
+ - **Estimated Cost**: ${estimated_cost:.2f} USD (at 1 TB = 1 USD)
60
+ """
61
+
62
+ return report
63
+
64
+ # Gradio interface
65
+ def gradio_interface(api_token):
66
+ report = get_report(api_token)
67
+ return report
68
+
69
+ # Create Gradio UI
70
+ with gr.Blocks() as demo:
71
+ gr.Markdown("# Hugging Face Storage Report")
72
+
73
+ gr.LoginButton
74
+
75
+ output = gr.Markdown()
76
+
77
+ # Button to trigger the report generation
78
+ report_button = gr.Button("Generate Report")
79
+ report_button.click(fn=gradio_interface, inputs=None, outputs=output)
80
+
81
+ # Launch the Gradio app
82
+ demo.launch()