Spaces:
Sleeping
Sleeping
ZeroCommand
commited on
Commit
·
346fe42
1
Parent(s):
8a71b00
add hf token validation
Browse files- app_text_classification.py +13 -1
- text_classification.py +2 -3
- text_classification_ui_helpers.py +4 -2
app_text_classification.py
CHANGED
@@ -11,7 +11,14 @@ from text_classification_ui_helpers import (
|
|
11 |
try_submit,
|
12 |
write_column_mapping_to_config,
|
13 |
)
|
14 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
15 |
|
16 |
MAX_LABELS = 40
|
17 |
MAX_FEATURES = 20
|
@@ -225,6 +232,11 @@ def get_demo():
|
|
225 |
return gr.update(interactive=False)
|
226 |
if not column_mapping_accordion.visible:
|
227 |
return gr.update(interactive=False)
|
|
|
|
|
|
|
|
|
|
|
228 |
return gr.update(interactive=True)
|
229 |
|
230 |
gr.on(
|
|
|
11 |
try_submit,
|
12 |
write_column_mapping_to_config,
|
13 |
)
|
14 |
+
|
15 |
+
from text_classification import get_example_prediction, HuggingFaceInferenceAPIResponse
|
16 |
+
from wordings import (
|
17 |
+
CONFIRM_MAPPING_DETAILS_MD,
|
18 |
+
INTRODUCTION_MD,
|
19 |
+
USE_INFERENCE_API_TIP,
|
20 |
+
CHECK_LOG_SECTION_RAW
|
21 |
+
)
|
22 |
|
23 |
MAX_LABELS = 40
|
24 |
MAX_FEATURES = 20
|
|
|
232 |
return gr.update(interactive=False)
|
233 |
if not column_mapping_accordion.visible:
|
234 |
return gr.update(interactive=False)
|
235 |
+
_, prediction_response = get_example_prediction(
|
236 |
+
model_id, dataset_id, dataset_config, dataset_split, inference_token
|
237 |
+
)
|
238 |
+
if not isinstance(prediction_response, HuggingFaceInferenceAPIResponse):
|
239 |
+
return gr.update(interactive=False)
|
240 |
return gr.update(interactive=True)
|
241 |
|
242 |
gr.on(
|
text_classification.py
CHANGED
@@ -272,7 +272,7 @@ def select_the_first_string_column(ds):
|
|
272 |
return None
|
273 |
|
274 |
|
275 |
-
def get_example_prediction(model_id, dataset_id, dataset_config, dataset_split):
|
276 |
# get a sample prediction from the model on the dataset
|
277 |
prediction_input = None
|
278 |
prediction_result = None
|
@@ -284,8 +284,7 @@ def get_example_prediction(model_id, dataset_id, dataset_config, dataset_split):
|
|
284 |
prediction_input = ds[0][select_the_first_string_column(ds)]
|
285 |
else:
|
286 |
prediction_input = ds[0]["text"]
|
287 |
-
|
288 |
-
hf_token = os.environ.get(HF_WRITE_TOKEN, default="")
|
289 |
payload = {"inputs": prediction_input, "options": {"use_cache": True}}
|
290 |
results = hf_inference_api(model_id, hf_token, payload)
|
291 |
|
|
|
272 |
return None
|
273 |
|
274 |
|
275 |
+
def get_example_prediction(model_id, dataset_id, dataset_config, dataset_split, hf_token):
|
276 |
# get a sample prediction from the model on the dataset
|
277 |
prediction_input = None
|
278 |
prediction_result = None
|
|
|
284 |
prediction_input = ds[0][select_the_first_string_column(ds)]
|
285 |
else:
|
286 |
prediction_input = ds[0]["text"]
|
287 |
+
|
|
|
288 |
payload = {"inputs": prediction_input, "options": {"use_cache": True}}
|
289 |
results = hf_inference_api(model_id, hf_token, payload)
|
290 |
|
text_classification_ui_helpers.py
CHANGED
@@ -27,6 +27,7 @@ from wordings import (
|
|
27 |
CHECK_LOG_SECTION_RAW,
|
28 |
get_styled_input,
|
29 |
)
|
|
|
30 |
|
31 |
MAX_LABELS = 40
|
32 |
MAX_FEATURES = 20
|
@@ -214,9 +215,11 @@ def align_columns_and_show_prediction(
|
|
214 |
dropdown_placement = [
|
215 |
gr.Dropdown(visible=False) for _ in range(MAX_LABELS + MAX_FEATURES)
|
216 |
]
|
|
|
|
|
217 |
|
218 |
prediction_input, prediction_response = get_example_prediction(
|
219 |
-
model_id, dataset_id, dataset_config, dataset_split
|
220 |
)
|
221 |
|
222 |
if prediction_input is None or prediction_response is None:
|
@@ -325,7 +328,6 @@ def construct_label_and_feature_mapping(all_mappings, ds_labels, ds_features):
|
|
325 |
feature_mapping = all_mappings["features"]
|
326 |
return label_mapping, feature_mapping
|
327 |
|
328 |
-
|
329 |
def try_submit(m_id, d_id, config, split, inference, inference_token, uid):
|
330 |
all_mappings = read_column_mapping(uid)
|
331 |
check_column_mapping_keys_validity(all_mappings)
|
|
|
27 |
CHECK_LOG_SECTION_RAW,
|
28 |
get_styled_input,
|
29 |
)
|
30 |
+
import os
|
31 |
|
32 |
MAX_LABELS = 40
|
33 |
MAX_FEATURES = 20
|
|
|
215 |
dropdown_placement = [
|
216 |
gr.Dropdown(visible=False) for _ in range(MAX_LABELS + MAX_FEATURES)
|
217 |
]
|
218 |
+
|
219 |
+
hf_token = os.environ.get("HF_WRITE_TOKEN", default="")
|
220 |
|
221 |
prediction_input, prediction_response = get_example_prediction(
|
222 |
+
model_id, dataset_id, dataset_config, dataset_split, hf_token
|
223 |
)
|
224 |
|
225 |
if prediction_input is None or prediction_response is None:
|
|
|
328 |
feature_mapping = all_mappings["features"]
|
329 |
return label_mapping, feature_mapping
|
330 |
|
|
|
331 |
def try_submit(m_id, d_id, config, split, inference, inference_token, uid):
|
332 |
all_mappings = read_column_mapping(uid)
|
333 |
check_column_mapping_keys_validity(all_mappings)
|