Setting the file. One moment.
Hf Model Papers Auth · Huggingface Tool Builder · huggingface/skills · Skills Docs
ContentsBack to the top of the page references/hf_model_papers_auth.sh
references/ hf_model_papers_auth.sh
Shell · 171 lines · 5 KB
Usage:
17 $0 [OPTIONS]
18
19 Options:
20 MODEL_ID Specific model to analyze (e.g., microsoft/DialoGPT-medium)
21 --trending [N] Show papers for top N trending models (default: 5)
22 --help Show this help message
23
24 Environment Variables:
25 HF_TOKEN Hugging Face API token (optional, for private models)
26
27 Examples:
28 # Get papers for a specific model
29 $0 microsoft/DialoGPT-medium
30
31 # Get papers with authentication
32 HF_TOKEN=your_token_here $0 your-private-model
33
34 # Get papers for top 3 trending models
35 $0 --trending 3
36
37 EOF
38 }
39
40 # Function to make authenticated API calls
41 hf_api_call () {
42 local url = " $1 "
43 local headers = ()
44
45 # Add authentication header if HF_TOKEN is set
46 if [[ -n "${ HF_TOKEN :- }" ]]; then
47 headers += ( -H "Authorization: Bearer $HF_TOKEN " )
48 fi
49
50 curl -s "${ headers [ @ ]}" " $url " 2> /dev/null || echo '{"error": "Network error"}'
51 }
52
53 # Function to extract papers from text
54 extract_papers () {
55 local text = " $1 "
56 local title = " $2 "
57
58 echo " $title "
59
60 # Find ArXiv URLs
61 local arxiv_urls = $( echo " $text " | grep -oE 'https?://arxiv\.org/[^[:space:]\])]+' | head -5 )
62 if [[ -n " $arxiv_urls " ]]; then
63 echo "ArXiv Papers:"
64 echo " $arxiv_urls " | sed 's/^/ • /'
65 fi
66
67 # Find DOI URLs
68 local doi_urls = $( echo " $text " | grep -oE 'https?://doi\.org/[^[:space:]\])]+' | head -3 )
69 if [[ -n " $doi_urls " ]]; then
70 echo "DOI Papers:"
71 echo " $doi_urls " | sed 's/^/ • /'
72 fi
73
74 # Find arxiv IDs in format YYYY.NNNNN
75 local arxiv_ids = $( echo " $text " | grep -oE 'arXiv:[0-9]{4}\.[0-9]{4,5}' | head -5 )
76 if [[ -n " $arxiv_ids " ]]; then
77 echo "ArXiv IDs:"
78 echo " $arxiv_ids " | sed 's/^/ • /'
79 fi
80
81 # Check for paper mentions
82 if echo " $text " | grep -qi "paper\|publication\|citation" ; then
83 local paper_mentions = $( echo " $text " | grep -i -A1 -B1 "paper\|publication" | head -6 )
84 if [[ -n " $paper_mentions " ]]; then
85 echo "Paper mentions:"
86 echo " $paper_mentions " | sed 's/^/ /'
87 fi
88 fi
89
90 if [[ -z " $arxiv_urls " && -z " $doi_urls " && -z " $arxiv_ids " ]]; then
91 echo "No papers found in model card"
92 fi
93 }
94
95 # Function to get model papers
96 get_model_papers () {
97 local model_id = " $1 "
98
99 echo "=== $model_id ==="
100
101 # Get model info from API with authentication
102 local api_url = "https://huggingface.co/api/models/ $model_id "
103 local response = $( hf_api_call " $api_url " )
104
105 if echo " $response " | grep -q '"error"' ; then
106 echo "Error: Could not fetch model ' $model_id '"
107 if [[ -z "${ HF_TOKEN :- }" ]]; then
108 echo "Note: This might be a private model. Try setting HF_TOKEN environment variable."
109 fi
110 return 1
111 fi
112
113 # Parse basic info
114 local downloads = $( echo " $response " | jq -r '.downloads // 0' )
115 local likes = $( echo " $response " | jq -r '.likes // 0' )
116 echo "Downloads: $downloads | Likes: $likes "
117
118 # Get model card
119 local card_url = "https://huggingface.co/ $model_id /raw/main/README.md"
120 local card_content = $( curl -s " $card_url " 2> /dev/null || echo "" )
121
122 if [[ -n " $card_content " ]]; then
123 extract_papers " $card_content " "Papers from model card:"
124 else
125 echo "Could not fetch model card"
126 fi
127
128 # Check tags for arxiv references
129 local arxiv_tag = $( echo " $response " | jq -r '.tags[]' 2> /dev/null | grep arxiv || true )
130 if [[ -n " $arxiv_tag " ]]; then
131 echo "ArXiv from tags: $arxiv_tag "
132 fi
133
134 echo
135 }
136
137 # Function to get trending models
138 get_trending_models () {
139 local limit = " ${1 :- 5} "
140
141 echo "Fetching top $limit trending models..."
142
143 local trending_url = "https://huggingface.co/api/trending?type=model&limit= $limit "
144 local response = $( hf_api_call " $trending_url " )
145
146 echo " $response " | jq -r '.recentlyTrending[] | .repoData.id' | head - " $limit " | while read -r model_id ; do
147 if [[ -n " $model_id " ]]; then
148 get_model_papers " $model_id "
149 fi
150 done
151 }
152
153 # Main
154 if [[ $# -eq 0 ]]; then
155 echo "Error: No arguments provided"
156 show_help
157 exit 1
158 fi
159
160 if [[ " $1 " == "--help" ]]; then
161 show_help
162 exit 0
163 elif [[ " $1 " == "--trending" ]]; then
164 if [[ -n " ${2 :- } " ]] && [[ " $2 " =~ ^[0-9]+$ ]]; then
165 get_trending_models " $2 "
166 else
167 get_trending_models 5
168 fi
169 else
170 get_model_papers " $1 "
171 fi