Spaces:
Runtime error
Runtime error
| import os | |
| from fastapi import APIRouter, Header, HTTPException | |
| from pydantic import BaseModel | |
| from typing import Optional | |
| from pipeline.explainer import FallacyExplainer | |
| FH_TOKEN = os.environ["FH_TOKEN"] | |
| router = APIRouter() | |
| _explainer = None | |
| def get_explainer(): | |
| global _explainer | |
| if _explainer is None: | |
| _explainer = FallacyExplainer() | |
| return _explainer | |
| class ExplainRequest(BaseModel): | |
| text: str | |
| label: str | |
| class ExplainResponse(BaseModel): | |
| explanation: Optional[str] = None | |
| highlighted_phrase: Optional[str] = None | |
| def explain_fallacy(req: ExplainRequest, x_fh_token: str = Header(None)): | |
| if x_fh_token != FH_TOKEN: | |
| raise HTTPException(status_code=403, detail="Forbidden") | |
| explainer = get_explainer() | |
| definition = explainer.fallacy_definitions.get(req.label.lower(), "") | |
| result = explainer.explain(req.text, req.label, {"explanation": definition}) | |
| return ExplainResponse( | |
| explanation=result["explanation"], | |
| highlighted_phrase=result["highlighted_phrase"], | |
| ) | |