-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathstatistics.go
48 lines (40 loc) · 1.39 KB
/
statistics.go
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
package apiv2
import (
"encoding/json"
"fmt"
"io/ioutil"
"net/http"
)
// Statistics represents the statistics for a dataset.
type Statistics struct {
NumExamples int `json:"num_examples"`
Statistics []map[string]any `json:"statistics"`
Partial bool `json:"partial"`
}
// GetDatasetStatistics fetches the statistics for a specified dataset and split.
func (client *HuggingFaceClient) GetDatasetStatistics(repoName, split string) (*Statistics, error) {
// Construct the API URL for the specified dataset and split.
url := fmt.Sprintf("https://datasets-server.huggingface.co/statistics?dataset=%s&config=cola&split=%s", repoName, split)
// Create a new HTTP GET request.
req, err := http.NewRequest("GET", url, nil)
if err != nil {
return nil, fmt.Errorf("failed to create request: %w", err)
}
// Execute the request using the client's configured doRequest method.
res, err := client.doRequest(req)
if err != nil {
return nil, fmt.Errorf("request failed: %w", err)
}
defer res.Body.Close()
// Read the response body.
data, err := ioutil.ReadAll(res.Body)
if err != nil {
return nil, fmt.Errorf("failed to read response body: %w", err)
}
// Unmarshal the JSON data into the Statistics struct.
var stat Statistics
if err := json.Unmarshal(data, &stat); err != nil {
return nil, fmt.Errorf("failed to unmarshal response: %w", err)
}
return &stat, nil
}