Skip to content

Commit 55b410c

Browse files
authored
catch proper HF exception to make example less falkey (#1664)
1 parent f363dbf commit 55b410c

File tree

1 file changed

+12
-9
lines changed

1 file changed

+12
-9
lines changed

examples/llm_and_nlp/hf-dataset-llm-eval.py

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
import sys
33

44
from huggingface_hub import InferenceClient
5-
from requests import HTTPError
5+
from huggingface_hub.errors import HfHubHTTPError
66

77
import datachain as dc
88

@@ -45,7 +45,7 @@ def eval_dialog(
4545
"json_schema": {"schema": DialogEval.model_json_schema()},
4646
},
4747
)
48-
except HTTPError as e:
48+
except HfHubHTTPError as e:
4949
return DialogEval(result="Error", reason=str(e))
5050

5151
message = completion.choices[0].message
@@ -69,12 +69,15 @@ def eval_dialog(
6969
.to_parquet("hf://datasets/dvcorg/test-datachain-llm-eval/data.parquet")
7070
)
7171

72+
result = dc.read_parquet(
73+
"hf://datasets/dvcorg/test-datachain-llm-eval/data.parquet", source=False
74+
)
75+
errors = result.filter(dc.C("response.result") == "Error")
76+
77+
if result.count() == errors.count():
78+
errors.show(3)
79+
raise RuntimeError("All Hugging Face inference requests failed.")
80+
7281
# Read it back to filter and show.
7382
# It restores the Pydantic model from Parquet under the hood.
74-
(
75-
dc.read_parquet(
76-
"hf://datasets/dvcorg/test-datachain-llm-eval/data.parquet", source=False
77-
)
78-
.filter(dc.C("response.result") == "Failure")
79-
.show(3)
80-
)
83+
result.filter(dc.C("response.result") == "Failure").show(3)

0 commit comments

Comments
 (0)