Spaces:
Sleeping
Sleeping
inoki-giskard
commited on
Commit
•
45c5476
1
Parent(s):
4641c89
Quick fix for inference API not ready
Browse files- text_classification.py +20 -9
- text_classification_ui_helpers.py +1 -0
text_classification.py
CHANGED
@@ -7,9 +7,13 @@ import pandas as pd
|
|
7 |
from transformers import pipeline
|
8 |
import requests
|
9 |
import os
|
|
|
10 |
|
11 |
HF_WRITE_TOKEN = "HF_WRITE_TOKEN"
|
12 |
|
|
|
|
|
|
|
13 |
def get_labels_and_features_from_dataset(ds):
|
14 |
try:
|
15 |
dataset_features = ds.features
|
@@ -72,13 +76,19 @@ def hf_inference_api(model_id, hf_token, payload):
|
|
72 |
)
|
73 |
url = f"{hf_inference_api_endpoint}/models/{model_id}"
|
74 |
headers = {"Authorization": f"Bearer {hf_token}"}
|
75 |
-
|
76 |
-
|
77 |
-
|
78 |
-
|
79 |
-
|
80 |
-
|
81 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
82 |
|
83 |
def check_model_pipeline(model_id):
|
84 |
try:
|
@@ -262,7 +272,7 @@ def get_example_prediction(model_id, dataset_id, dataset_config, dataset_split):
|
|
262 |
ds = datasets.load_dataset(dataset_id, dataset_config)[dataset_split]
|
263 |
if "text" not in ds.features.keys():
|
264 |
# Dataset does not have text column
|
265 |
-
prediction_input = ds[0][ds.features.keys()[0]]
|
266 |
else:
|
267 |
prediction_input = ds[0]["text"]
|
268 |
|
@@ -276,8 +286,9 @@ def get_example_prediction(model_id, dataset_id, dataset_config, dataset_split):
|
|
276 |
prediction_result = {
|
277 |
f'{result["label"]}': result["score"] for result in results
|
278 |
}
|
279 |
-
except Exception:
|
280 |
# Pipeline prediction failed, need to provide labels
|
|
|
281 |
return prediction_input, None
|
282 |
|
283 |
return prediction_input, prediction_result
|
|
|
7 |
from transformers import pipeline
|
8 |
import requests
|
9 |
import os
|
10 |
+
import time
|
11 |
|
12 |
HF_WRITE_TOKEN = "HF_WRITE_TOKEN"
|
13 |
|
14 |
+
logger = logging.getLogger(__file__)
|
15 |
+
|
16 |
+
|
17 |
def get_labels_and_features_from_dataset(ds):
|
18 |
try:
|
19 |
dataset_features = ds.features
|
|
|
76 |
)
|
77 |
url = f"{hf_inference_api_endpoint}/models/{model_id}"
|
78 |
headers = {"Authorization": f"Bearer {hf_token}"}
|
79 |
+
output = {"error": "First attemp"}
|
80 |
+
attempt = 30
|
81 |
+
while "error" in output and attempt > 0:
|
82 |
+
response = requests.post(url, headers=headers, json=payload)
|
83 |
+
if response.status_code != 200:
|
84 |
+
logging.error(f"Request to inference API returns {response.status_code}")
|
85 |
+
try:
|
86 |
+
return response.json()
|
87 |
+
except Exception:
|
88 |
+
logging.error(f"{response.content}")
|
89 |
+
output = {"error": response.content}
|
90 |
+
attempt -= 1
|
91 |
+
time.sleep(2)
|
92 |
|
93 |
def check_model_pipeline(model_id):
|
94 |
try:
|
|
|
272 |
ds = datasets.load_dataset(dataset_id, dataset_config)[dataset_split]
|
273 |
if "text" not in ds.features.keys():
|
274 |
# Dataset does not have text column
|
275 |
+
prediction_input = ds[0][list(ds.features.keys())[0]]
|
276 |
else:
|
277 |
prediction_input = ds[0]["text"]
|
278 |
|
|
|
286 |
prediction_result = {
|
287 |
f'{result["label"]}': result["score"] for result in results
|
288 |
}
|
289 |
+
except Exception as e:
|
290 |
# Pipeline prediction failed, need to provide labels
|
291 |
+
logger.warn(f"Pipeline prediction failed due to {e}")
|
292 |
return prediction_input, None
|
293 |
|
294 |
return prediction_input, prediction_result
|
text_classification_ui_helpers.py
CHANGED
@@ -209,6 +209,7 @@ def align_columns_and_show_prediction(
|
|
209 |
gr.Dropdown(visible=False) for _ in range(MAX_LABELS + MAX_FEATURES)
|
210 |
]
|
211 |
|
|
|
212 |
prediction_input, prediction_output = get_example_prediction(
|
213 |
model_id, dataset_id, dataset_config, dataset_split
|
214 |
)
|
|
|
209 |
gr.Dropdown(visible=False) for _ in range(MAX_LABELS + MAX_FEATURES)
|
210 |
]
|
211 |
|
212 |
+
# FIXME: prefiction_output could be None
|
213 |
prediction_input, prediction_output = get_example_prediction(
|
214 |
model_id, dataset_id, dataset_config, dataset_split
|
215 |
)
|