sales / backend /app /hf_utils.py
Corin1998's picture
Create hf_utils.py
fe7a9c2 verified
import httpx
from .config import settings
HF_API_URL = "https://api-inference.huggingface.co/models"
async def hf_text_classification(model: str, text: str):
headers = {"Authorization": f"Bearer {settings.hf_api_key}"}
async with httpx.AsyncClient(timeout=60) as client:
r = await client.post(f"{HF_API_URL}/{model}", headers=headers, json={"inputs": text})
r.raise_for_status()
return r.json()
async def toxicity_score(text: str) -> float:
try:
results = await hf_text_classification(settings.hf_toxic_model, text)
# モデルによりスキーマ差異あり。unitary/toxic-bertは label "toxic" のscore
if isinstance(results, list) and results and isinstance(results[0], list):
# pipeline="text-classification" (top_k>1) 形式
labels = {d["label"].lower(): d["score"] for d in results[0]}
return float(labels.get("toxic", 0.0))
elif isinstance(results, list) and results and "label" in results[0]:
return float(results[0]["score"])
except Exception:
pass
return 0.0
async def sentiment_polarity(text: str) -> float:
try:
results = await hf_text_classification(settings.hf_sentiment_model, text)
# ラベル: NEGATIVE/NEUTRAL/POSITIVE を [+1,0,+1] に近似
if isinstance(results, list) and results and isinstance(results[0], list):
labels = {d["label"].upper(): d["score"] for d in results[0]}
return float(labels.get("POSITIVE", 0.0) - labels.get("NEGATIVE", 0.0))
elif isinstance(results, list) and results and "label" in results[0]:
label = results[0]["label"].upper()
score = results[0]["score"]
return score if label == "POSITIVE" else (-score if label == "NEGATIVE" else 0.0)
except Exception:
pass
return 0.0