From 3d5fadf08cb922941fce64afd6414f62a34a1bcb Mon Sep 17 00:00:00 2001 From: xieincz <39424496+xieincz@users.noreply.github.com> Date: Sat, 9 Dec 2023 00:02:21 +0800 Subject: [PATCH] =?UTF-8?q?=E5=BD=93=20huggingface=20=E7=9A=84=E9=A1=B5?= =?UTF-8?q?=E9=9D=A2=E5=87=BA=E7=8E=B0=20load=20more=20=E7=9A=84=E6=97=B6?= =?UTF-8?q?=E5=80=99=EF=BC=8C=E5=8A=A0=E8=BD=BD=E5=AE=8C=E6=95=B4=E7=9A=84?= =?UTF-8?q?=E6=96=87=E4=BB=B6=E5=88=97=E8=A1=A8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/huggingface-go/main.go | 37 +++++++++++++++++++++++++++++++++++-- 1 file changed, 35 insertions(+), 2 deletions(-) diff --git a/src/huggingface-go/main.go b/src/huggingface-go/main.go index 3540280..c6dc271 100644 --- a/src/huggingface-go/main.go +++ b/src/huggingface-go/main.go @@ -119,7 +119,7 @@ func fetchDirectoryEntriesRecursively(proxyURLHead, baseURL, path string) ([]map return nil, fmt.Errorf("data-props attribute not found") } - entries, err := extractEntries(dataProps) + entries, err := extractEntries(dataProps, proxyURLHead) if err != nil { return nil, err } @@ -183,12 +183,45 @@ func encode(s string) string { return strings.ReplaceAll(u.String(), "%25", "%") } -func extractEntries(dataProps string) ([]map[string]interface{}, error) { +func extractEntries(dataProps, proxyURLHead string) ([]map[string]interface{}, error) { var props map[string]interface{} err := json.Unmarshal([]byte(dataProps), &props) if err != nil { return nil, err } + + nextURL := props["nextURL"] + fmt.Println("nextURL:", nextURL) + if nextURL != nil { + proxyURL := proxyURLHead + urlEncode("https://huggingface.co"+nextURL.(string)) + response, err := http.Get(proxyURL) + if err != nil { + fmt.Println("Error:", err) + return nil, err + } + defer response.Body.Close() + body, err := io.ReadAll(response.Body) + if err != nil { + fmt.Println("Error reading response body:", err) + return nil, err + } + // 解析JSON响应 + var data []interface{} + if err := json.Unmarshal(body, &data); err != nil { + fmt.Println("Error decoding JSON:", err) + return nil, err + } + dataMaps := make([]map[string]interface{}, len(data)) + for i, v := range data { + dataMap, ok := v.(map[string]interface{}) + if !ok { + return nil, fmt.Errorf("v is not a valid object") + } + dataMaps[i] = dataMap + } + return dataMaps, nil + } + entriesValue, exists := props["entries"] if !exists { return nil, fmt.Errorf("entries not found in data-props")