File size: 7,876 Bytes
3d5dd87
 
f389421
00551a6
00ca949
f389421
281bb73
f389421
3d5dd87
00551a6
 
 
 
 
281bb73
3d5dd87
 
 
 
 
 
 
 
 
 
 
281bb73
f389421
3d5dd87
281bb73
34210b1
 
3d5dd87
 
 
 
281bb73
3d5dd87
 
281bb73
3d5dd87
 
281bb73
f389421
 
 
 
00551a6
281bb73
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
00551a6
 
 
 
 
 
 
 
cf0448c
bbd8a08
cf0448c
 
 
 
 
bbd8a08
 
 
cf0448c
bbd8a08
281bb73
 
 
 
3d5dd87
 
281bb73
3d5dd87
 
281bb73
3d5dd87
 
 
 
00ca949
 
 
3d5dd87
bbd8a08
 
 
 
 
 
cf0448c
 
 
bbd8a08
cf0448c
00551a6
bbd8a08
00ca949
 
cf0448c
 
00ca949
 
cf0448c
 
 
00ca949
 
 
 
 
 
cf0448c
 
 
00ca949
 
 
 
 
bbd8a08
3d5dd87
00ca949
 
 
 
 
 
 
 
 
 
 
 
3d5dd87
281bb73
3d5dd87
281bb73
3d5dd87
 
 
 
281bb73
 
00ca949
3d5dd87
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
import streamlit as st
from huggingface_hub import HfApi
import asyncio
import os
import plotly.express as px

# Initialize the Hugging Face API
api = HfApi()

# Directory to save the generated HTML files
HTML_DIR = "generated_html_pages"
if not os.path.exists(HTML_DIR):
    os.makedirs(HTML_DIR)

# Default list of Hugging Face usernames - where all the magic begins! πŸͺ„
default_users = {
    "users": [
        "awacke1", "rogerxavier", "jonatasgrosman", "kenshinn", "Csplk", "DavidVivancos",
        "cdminix", "Jaward", "TuringsSolutions", "Severian", "Wauplin",
        "phosseini", "Malikeh1375", "gokaygokay", "MoritzLaurer", "mrm8488",
        "TheBloke", "lhoestq", "xw-eric", "Paul", "Muennighoff",
        "ccdv", "haonan-li", "chansung", "lukaemon", "hails",
        "pharmapsychotic", "KingNish", "merve", "ameerazam08", "ashleykleynhans"
    ]
}

# Asynchronous function to fetch user content - because why wait when you can multitask? πŸš€
async def fetch_user_content(username):
    try:
        # Fetch models and datasets - the stars of our show! 🌟
        models = list(await asyncio.to_thread(api.list_models, author=username))  # Convert generator to list
        datasets = list(await asyncio.to_thread(api.list_datasets, author=username))  # Convert generator to list
        
        return {
            "username": username,
            "models": models,
            "datasets": datasets
        }
    except Exception as e:
        # Oops! Something went wrong - blame it on the gremlins! 😈
        return {"username": username, "error": str(e)}

# Fetch all users concurrently - more hands (or threads) make light work! πŸ’ͺ
async def fetch_all_users(usernames):
    tasks = [fetch_user_content(username) for username in usernames]
    return await asyncio.gather(*tasks)

# Generate HTML content for a user and save it to a file - because who doesn't love a good download link? πŸ’Ύ
def generate_html_page(username, models, datasets):
    html_content = f"""
    <html>
    <head>
        <title>{username}'s Hugging Face Content</title>
    </head>
    <body>
        <h1>{username}'s Hugging Face Profile</h1>
        <p><a href="https://huggingface.co/{username}">πŸ”— Profile Link</a></p>
        <h2>🧠 Models</h2>
        <ul>
    """
    for model in models:
        model_name = model.modelId.split("/")[-1]
        html_content += f'<li><a href="https://huggingface.co/{model.modelId}">{model_name}</a></li>'
    
    html_content += """
        </ul>
        <h2>πŸ“š Datasets</h2>
        <ul>
    """
    for dataset in datasets:
        dataset_name = dataset.id.split("/")[-1]
        html_content += f'<li><a href="https://huggingface.co/datasets/{dataset.id}">{dataset_name}</a></li>'
    
    html_content += """
        </ul>
    </body>
    </html>
    """

    # Save the HTML content to a file
    html_file_path = os.path.join(HTML_DIR, f"{username}.html")
    with open(html_file_path, "w") as html_file:
        html_file.write(html_content)

    return html_file_path

# Cache the HTML file path using Streamlit's caching decorator
@st.cache_data(show_spinner=False)
def get_cached_html_file(username):
    return generate_html_page(username, *get_user_content(username))

# Fetch user content from the API (without caching)
def get_user_content(username):
    user_data = asyncio.run(fetch_user_content(username))
    if "error" in user_data:
        return None, user_data["error"]
    return user_data["models"], user_data["datasets"]

# Streamlit app setup - the nerve center of our operation! πŸŽ›οΈ
st.title("Hugging Face User Content Display - Let's Automate Some Fun! πŸŽ‰")

# Convert the default users list to a string - because nobody likes typing out long lists! πŸ“
default_users_str = "\n".join(default_users["users"])

# Text area with default list of usernames - feel free to add your friends! πŸ‘₯
usernames = st.text_area("Enter Hugging Face usernames (one per line):", value=default_users_str, height=300)

# Show User Content button - the big red button! (But actually it's blue) πŸ–±οΈ
if st.button("Show User Content"):
    if usernames:
        username_list = [username.strip() for username in usernames.split('\n') if username.strip()]

        # Collect statistics for Plotly graphs
        stats = {"username": [], "models_count": [], "datasets_count": []}

        st.markdown("### User Content Overview")
        for username in username_list:
            with st.container():
                # Profile link - because everyone deserves their 15 seconds of fame! 🎀
                st.markdown(f"**{username}** [πŸ”— Profile](https://huggingface.co/{username})")

                # Generate HTML page and provide download link - because who wouldn't want a custom webpage? 🌐
                models, datasets = get_user_content(username)
                if models is None:
                    st.warning(f"{username}: {datasets} - Looks like the AI needs a coffee break β˜•")
                else:
                    html_file_path = get_cached_html_file(username)
                    st.markdown(f"[πŸ“„ Download {username}'s HTML Page]({html_file_path})")

                    # Add to statistics for Plotly graphs
                    stats["username"].append(username)
                    stats["models_count"].append(len(models))
                    stats["datasets_count"].append(len(datasets))

                    # Models section with expander - 🧠 because AI models are brainy! 🧠
                    with st.expander(f"🧠 Models ({len(models)})", expanded=False):
                        if models:
                            for model in models:
                                model_name = model.modelId.split("/")[-1]
                                st.markdown(f"- [{model_name}](https://huggingface.co/{model.modelId})")
                        else:
                            st.markdown("No models found. Did you check under the rug? πŸ•΅οΈβ€β™‚οΈ")

                    # Datasets section with expander - πŸ“š because data is the foundation of AI! πŸ“š
                    with st.expander(f"πŸ“š Datasets ({len(datasets)})", expanded=False):
                        if datasets:
                            for dataset in datasets:
                                dataset_name = dataset.id.split("/")[-1]
                                st.markdown(f"- [{dataset_name}](https://huggingface.co/datasets/{dataset.id})")
                        else:
                            st.markdown("No datasets found. Maybe they’re still baking in the oven? πŸͺ")

                st.markdown("---")

        # Plotly graphs to visualize the number of models and datasets each user has
        if stats["username"]:
            st.markdown("### User Content Statistics")

            # Plotting the number of models per user
            fig_models = px.bar(x=stats["username"], y=stats["models_count"], labels={'x':'Username', 'y':'Number of Models'}, title="Number of Models per User")
            st.plotly_chart(fig_models)

            # Plotting the number of datasets per user
            fig_datasets = px.bar(x=stats["username"], y=stats["datasets_count"], labels={'x':'Username', 'y':'Number of Datasets'}, title="Number of Datasets per User")
            st.plotly_chart(fig_datasets)

    else:
        st.warning("Please enter at least one username. Don't be shy! πŸ˜…")

# Sidebar instructions - just in case you get lost! πŸ—ΊοΈ
st.sidebar.markdown("""
## How to use:
1. The text area is pre-filled with a list of Hugging Face usernames. You can edit this list or add more usernames.
2. Click 'Show User Content'.
3. View the user's models and datasets along with a link to their Hugging Face profile.
4. Download an HTML page for each user to use the absolute links offline!
5. Check out the statistics visualizations at the end!
""")