File size: 3,424 Bytes
c2bed9a
 
 
 
 
 
 
9774217
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
c2bed9a
 
 
 
 
 
 
 
 
 
9774217
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
c2bed9a
 
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
The error you're encountering is due to the `HfApi` class not having a `get_user_from_username` method, as this method does not exist in the Hugging Face Hub's Python API.

Instead, you can retrieve user information by searching for models or datasets associated with a specific user and then extracting relevant information, like the Twitter handle, from the metadata. However, direct user information retrieval might not be supported via the Hugging Face Hub API.

Here’s an alternative approach using the available methods:

```python
import streamlit as st
from huggingface_hub import HfApi
import pandas as pd

# Default list of Hugging Face usernames
default_users = {
    "users": [
        "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"
    ]
}

def get_twitter_link(username):
    api = HfApi()
    try:
        # Fetch the models associated with the user
        models = api.list_models(author=username)
        if models:
            for model in models:
                # Try to get the Twitter handle from the first model found
                twitter = model.cardData.get('social_media', {}).get('twitter')
                if twitter:
                    return f"https://twitter.com/{twitter}"
        else:
            st.warning(f"No models found for {username}")
    except Exception as e:
        st.error(f"Error fetching info for {username}: {str(e)}")
    return None

st.title("Hugging Face to Twitter Link Generator")

# Convert the default users list to a string
default_users_str = "\n".join(default_users["users"])

# Text area with default list of usernames
usernames = st.text_area("Enter Hugging Face usernames (one per line):", value=default_users_str, height=300)

if st.button("Generate Twitter Links"):
    if usernames:
        username_list = [username.strip() for username in usernames.split('\n') if username.strip()]
        results = []

        progress_bar = st.progress(0)
        for i, username in enumerate(username_list):
            twitter_link = get_twitter_link(username)
            results.append({"Hugging Face": username, "Twitter Link": twitter_link})
            progress_bar.progress((i + 1) / len(username_list))

        df = pd.DataFrame(results)
        st.dataframe(df)

        # Generate markdown with hyperlinks
        markdown_links = ""
        for _, row in df.iterrows():
            if row['Twitter Link']:
                markdown_links += f"- [{row['Hugging Face']}]({row['Twitter Link']})\n"
            else:
                markdown_links += f"- {row['Hugging Face']} (No Twitter link found)\n"

        st.markdown("### Twitter Profile Links")
        st.markdown(markdown_links)
    else:
        st.warning("Please enter at least one username.")

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 'Generate Twitter Links'.
3. View the results in the table and as clickable links.
4. The progress bar shows the status of link generation.
""")
```