Commit ·
4ff9aed
1
Parent(s): 89d63b5
fix: Construct RequestParameters with all 6 required args (url, task, model, json, data, headers)
Browse files- src/hf_inference.py +28 -54
src/hf_inference.py
CHANGED
|
@@ -9,14 +9,13 @@ import os
|
|
| 9 |
import json
|
| 10 |
import logging
|
| 11 |
import time
|
| 12 |
-
import inspect
|
| 13 |
|
| 14 |
logger = logging.getLogger(__name__)
|
| 15 |
|
| 16 |
HF_API_TOKEN = os.environ.get("HF_API_TOKEN", "")
|
|
|
|
| 17 |
|
| 18 |
_client = None
|
| 19 |
-
_RequestParameters = None
|
| 20 |
|
| 21 |
|
| 22 |
def _get_client():
|
|
@@ -27,14 +26,6 @@ def _get_client():
|
|
| 27 |
return _client
|
| 28 |
|
| 29 |
|
| 30 |
-
def _get_request_params_class():
|
| 31 |
-
global _RequestParameters
|
| 32 |
-
if _RequestParameters is None:
|
| 33 |
-
from huggingface_hub.inference._common import RequestParameters
|
| 34 |
-
_RequestParameters = RequestParameters
|
| 35 |
-
return _RequestParameters
|
| 36 |
-
|
| 37 |
-
|
| 38 |
# Repository IDs
|
| 39 |
SUMMARIZATION_REPO = os.environ.get("SUMMARIZATION_REPO_ID", "bayan10/summarization-model")
|
| 40 |
SPELLING_REPO = os.environ.get("SPELLING_REPO_ID", "bayan10/AraSpell-Model")
|
|
@@ -42,45 +33,32 @@ PUNCTUATION_REPO = os.environ.get("PUNCTUATION_REPO_ID", "bayan10/PuncAra-v1")
|
|
| 42 |
AUTOCOMPLETE_REPO = os.environ.get("AUTOCOMPLETE_REPO_ID", "bayan10/AutoComplete")
|
| 43 |
|
| 44 |
|
| 45 |
-
def _call_model(repo_id, payload, task=
|
| 46 |
"""
|
| 47 |
-
Call HF model
|
|
|
|
|
|
|
| 48 |
"""
|
|
|
|
|
|
|
| 49 |
client = _get_client()
|
| 50 |
-
RP = _get_request_params_class()
|
| 51 |
|
| 52 |
if "options" not in payload:
|
| 53 |
payload["options"] = {"wait_for_model": True}
|
| 54 |
|
| 55 |
-
|
| 56 |
-
logger.info("Calling HF model: %s (task=%s)", repo_id, task)
|
| 57 |
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
|
| 62 |
-
|
| 63 |
-
|
| 64 |
-
|
| 65 |
-
|
| 66 |
-
rp_kwargs = {}
|
| 67 |
-
if "model" in params:
|
| 68 |
-
rp_kwargs["model"] = repo_id
|
| 69 |
-
if "task" in params and task:
|
| 70 |
-
rp_kwargs["task"] = task
|
| 71 |
-
if "data" in params:
|
| 72 |
-
rp_kwargs["data"] = data_bytes
|
| 73 |
-
if "json" in params:
|
| 74 |
-
rp_kwargs["json"] = payload
|
| 75 |
-
|
| 76 |
-
rp = RP(**rp_kwargs)
|
| 77 |
-
response = client._inner_post(rp)
|
| 78 |
|
| 79 |
-
|
| 80 |
-
logger.warning("RequestParameters construction failed: %s", e)
|
| 81 |
-
# Last resort: try creating with just positional arg
|
| 82 |
-
rp = RP(data_bytes)
|
| 83 |
-
response = client._inner_post(rp)
|
| 84 |
|
| 85 |
# Parse response
|
| 86 |
if isinstance(response, bytes):
|
|
@@ -92,6 +70,11 @@ def _call_model(repo_id, payload, task=None):
|
|
| 92 |
|
| 93 |
logger.info("HF result for %s: type=%s preview=%s",
|
| 94 |
repo_id, type(result).__name__, str(result)[:200])
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 95 |
return result
|
| 96 |
|
| 97 |
|
|
@@ -106,8 +89,6 @@ def _extract_text(result, fallback=""):
|
|
| 106 |
or fallback)
|
| 107 |
return str(item) if str(item).strip() else fallback
|
| 108 |
if isinstance(result, dict):
|
| 109 |
-
if "error" in result:
|
| 110 |
-
raise RuntimeError("HF API error: " + str(result["error"]))
|
| 111 |
return (result.get("summary_text")
|
| 112 |
or result.get("generated_text")
|
| 113 |
or result.get("translation_text")
|
|
@@ -128,12 +109,12 @@ def hf_summarize(text, max_length=128, min_length=30):
|
|
| 128 |
|
| 129 |
|
| 130 |
def hf_correct_spelling(text):
|
| 131 |
-
result = _call_model(SPELLING_REPO, {"inputs": text})
|
| 132 |
return _extract_text(result, text)
|
| 133 |
|
| 134 |
|
| 135 |
def hf_add_punctuation(text):
|
| 136 |
-
result = _call_model(PUNCTUATION_REPO, {"inputs": text})
|
| 137 |
return _extract_text(result, text)
|
| 138 |
|
| 139 |
|
|
@@ -166,23 +147,16 @@ def check_hf_api_available():
|
|
| 166 |
|
| 167 |
|
| 168 |
def debug_test_all_models():
|
| 169 |
-
"""Test all HF models
|
| 170 |
results = {}
|
| 171 |
test_text = "هذا نص تجريبي للاختبار"
|
| 172 |
long_text = (test_text + " ") * 5
|
| 173 |
|
| 174 |
try:
|
| 175 |
import huggingface_hub
|
| 176 |
-
|
| 177 |
-
rp_sig = str(inspect.signature(RP))
|
| 178 |
-
rp_fields = list(inspect.signature(RP).parameters.keys())
|
| 179 |
-
results["_info"] = {
|
| 180 |
-
"hf_hub_version": huggingface_hub.__version__,
|
| 181 |
-
"RequestParameters_signature": rp_sig[:300],
|
| 182 |
-
"RequestParameters_fields": rp_fields,
|
| 183 |
-
}
|
| 184 |
except Exception as e:
|
| 185 |
-
results["_info"] = {"error": repr(e)[:
|
| 186 |
|
| 187 |
for name, fn, args in [
|
| 188 |
("summarization", hf_summarize, (long_text, 30, 10)),
|
|
|
|
| 9 |
import json
|
| 10 |
import logging
|
| 11 |
import time
|
|
|
|
| 12 |
|
| 13 |
logger = logging.getLogger(__name__)
|
| 14 |
|
| 15 |
HF_API_TOKEN = os.environ.get("HF_API_TOKEN", "")
|
| 16 |
+
HF_API_BASE = "https://api-inference.huggingface.co/models/"
|
| 17 |
|
| 18 |
_client = None
|
|
|
|
| 19 |
|
| 20 |
|
| 21 |
def _get_client():
|
|
|
|
| 26 |
return _client
|
| 27 |
|
| 28 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 29 |
# Repository IDs
|
| 30 |
SUMMARIZATION_REPO = os.environ.get("SUMMARIZATION_REPO_ID", "bayan10/summarization-model")
|
| 31 |
SPELLING_REPO = os.environ.get("SPELLING_REPO_ID", "bayan10/AraSpell-Model")
|
|
|
|
| 33 |
AUTOCOMPLETE_REPO = os.environ.get("AUTOCOMPLETE_REPO_ID", "bayan10/AutoComplete")
|
| 34 |
|
| 35 |
|
| 36 |
+
def _call_model(repo_id, payload, task=""):
|
| 37 |
"""
|
| 38 |
+
Call HF model via InferenceClient._inner_post with RequestParameters.
|
| 39 |
+
|
| 40 |
+
RequestParameters(url, task, model, json, data, headers)
|
| 41 |
"""
|
| 42 |
+
from huggingface_hub.inference._common import RequestParameters
|
| 43 |
+
|
| 44 |
client = _get_client()
|
|
|
|
| 45 |
|
| 46 |
if "options" not in payload:
|
| 47 |
payload["options"] = {"wait_for_model": True}
|
| 48 |
|
| 49 |
+
url = HF_API_BASE + repo_id
|
| 50 |
+
logger.info("Calling HF model: %s (url=%s, task=%s)", repo_id, url, task)
|
| 51 |
|
| 52 |
+
rp = RequestParameters(
|
| 53 |
+
url=url,
|
| 54 |
+
task=task,
|
| 55 |
+
model=repo_id,
|
| 56 |
+
json=payload,
|
| 57 |
+
data=None,
|
| 58 |
+
headers={},
|
| 59 |
+
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 60 |
|
| 61 |
+
response = client._inner_post(rp)
|
|
|
|
|
|
|
|
|
|
|
|
|
| 62 |
|
| 63 |
# Parse response
|
| 64 |
if isinstance(response, bytes):
|
|
|
|
| 70 |
|
| 71 |
logger.info("HF result for %s: type=%s preview=%s",
|
| 72 |
repo_id, type(result).__name__, str(result)[:200])
|
| 73 |
+
|
| 74 |
+
# Check for API errors in response
|
| 75 |
+
if isinstance(result, dict) and "error" in result:
|
| 76 |
+
raise RuntimeError("HF API error: " + str(result["error"]))
|
| 77 |
+
|
| 78 |
return result
|
| 79 |
|
| 80 |
|
|
|
|
| 89 |
or fallback)
|
| 90 |
return str(item) if str(item).strip() else fallback
|
| 91 |
if isinstance(result, dict):
|
|
|
|
|
|
|
| 92 |
return (result.get("summary_text")
|
| 93 |
or result.get("generated_text")
|
| 94 |
or result.get("translation_text")
|
|
|
|
| 109 |
|
| 110 |
|
| 111 |
def hf_correct_spelling(text):
|
| 112 |
+
result = _call_model(SPELLING_REPO, {"inputs": text}, task="text2text-generation")
|
| 113 |
return _extract_text(result, text)
|
| 114 |
|
| 115 |
|
| 116 |
def hf_add_punctuation(text):
|
| 117 |
+
result = _call_model(PUNCTUATION_REPO, {"inputs": text}, task="text2text-generation")
|
| 118 |
return _extract_text(result, text)
|
| 119 |
|
| 120 |
|
|
|
|
| 147 |
|
| 148 |
|
| 149 |
def debug_test_all_models():
|
| 150 |
+
"""Test all HF models."""
|
| 151 |
results = {}
|
| 152 |
test_text = "هذا نص تجريبي للاختبار"
|
| 153 |
long_text = (test_text + " ") * 5
|
| 154 |
|
| 155 |
try:
|
| 156 |
import huggingface_hub
|
| 157 |
+
results["_info"] = {"hf_hub_version": huggingface_hub.__version__}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 158 |
except Exception as e:
|
| 159 |
+
results["_info"] = {"error": repr(e)[:200]}
|
| 160 |
|
| 161 |
for name, fn, args in [
|
| 162 |
("summarization", hf_summarize, (long_text, 30, 10)),
|