mirror of
https://github.com/wassname/Open-Assistant.git
synced 2026-07-13 00:50:06 +08:00
ee50b573e1
* [NEW] utils: Endpoint Toxic Roberta * [NEW] Constants API URL * [NEW] Git ignore venv * [NEW] Lint * [NEW] Backend default args * [NEW] HUGGINGFACE_API_ERROR * [NEW] Requests package * [NEW] Get Toxicity Endpoint * [NEW] Schema: ToxicityClassification [NEW] Constants module [FIX] Module * [FIX] Test Key HF * [NEW] settings: HUGGING_FACE_API_KEY * [NEW] Remove requests * [NEW] HuggingFace client * [NEW] Cleaning code
38 lines
1.2 KiB
Python
38 lines
1.2 KiB
Python
from enum import Enum
|
|
from typing import List
|
|
|
|
from fastapi import APIRouter, Depends
|
|
from oasst_backend.api import deps
|
|
from oasst_backend.models import ApiClient
|
|
from oasst_backend.schemas.hugging_face import ToxicityClassification
|
|
from oasst_backend.utils.hugging_face import HuggingFaceAPI
|
|
|
|
router = APIRouter()
|
|
|
|
|
|
class HF_url(str, Enum):
|
|
HUGGINGFACE_TOXIC_ROBERTA = "https://api-inference.huggingface.co/models/unitary/multilingual-toxic-xlm-roberta"
|
|
|
|
|
|
@router.get("/text_toxicity")
|
|
async def get_text_toxicity(
|
|
msg: str,
|
|
api_client: ApiClient = Depends(deps.get_trusted_api_client),
|
|
) -> List[List[ToxicityClassification]]:
|
|
"""Get the Message Toxicity from HuggingFace Roberta model.
|
|
|
|
Args:
|
|
msg (str): the message that we want to analyze.
|
|
api_client (ApiClient, optional): authentification of the user of the request.
|
|
Defaults to Depends(deps.get_trusted_api_client).
|
|
|
|
Returns:
|
|
ToxicityClassification: the score of toxicity of the message.
|
|
"""
|
|
|
|
api_url: str = HF_url.HUGGINGFACE_TOXIC_ROBERTA.value
|
|
hugging_face_api = HuggingFaceAPI(api_url)
|
|
response = await hugging_face_api.post(msg)
|
|
|
|
return response
|