File size: 6,122 Bytes
2fbe94f
 
 
adc042f
 
 
 
 
2fbe94f
adc042f
 
 
 
 
 
 
 
 
2fbe94f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
adc042f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2fbe94f
 
 
 
adc042f
 
 
 
 
2fbe94f
 
 
 
 
adc042f
 
 
 
 
2fbe94f
adc042f
2fbe94f
 
adc042f
 
 
 
 
2fbe94f
adc042f
 
 
 
 
2fbe94f
 
adc042f
 
 
 
 
 
 
 
2fbe94f
 
 
 
adc042f
2fbe94f
adc042f
 
 
2fbe94f
adc042f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2fbe94f
 
 
 
 
adc042f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
# app.py
from fastapi import FastAPI, HTTPException, Request, Depends, Security
from fastapi.security import APIKeyHeader
from fastapi.responses import JSONResponse
from pydantic import BaseModel, Field
from typing import Optional
import uvicorn
import time
import os
from moderator import get_moderator

# Initialize FastAPI app
app = FastAPI(
    title="Text Moderation API",
    description="API for moderating user-generated text content",
    version="1.0.0"
)

# ============================================================================
# API KEY AUTHENTICATION
# ============================================================================

# Get API key from environment variable (set in Hugging Face Space settings)
API_KEY = os.getenv("MODERATION_API_KEY", "your-default-secret-key-here")
API_KEY_NAME = "X-API-Key"

# API Key header dependency
api_key_header = APIKeyHeader(name=API_KEY_NAME, auto_error=True)

async def verify_api_key(api_key: str = Security(api_key_header)):
    """Verify the API key from request header"""
    if api_key != API_KEY:
        raise HTTPException(
            status_code=403,
            detail="Invalid API Key. Please provide a valid X-API-Key header."
        )
    return api_key

# ============================================================================
# REQUEST/RESPONSE MODELS
# ============================================================================

class ModerationRequest(BaseModel):
    user_id: str = Field(..., description="User identifier")
    text: str = Field(..., description="Text content to moderate")
    
    class Config:
        json_schema_extra = {
            "example": {
                "user_id": "user123",
                "text": "I love this community!"
            }
        }

class ModerationResponse(BaseModel):
    user_id: str
    action: str = Field(..., description="Action: allow, flag, or delete")
    reason: Optional[str] = Field(None, description="Reason for the action")
    toxic_score: Optional[float] = Field(None, description="Toxicity score (0-1)")
    hate_score: Optional[float] = Field(None, description="Hate speech score (0-1)")
    processing_time_ms: Optional[float] = Field(None, description="Processing time in milliseconds")

# ============================================================================
# STARTUP
# ============================================================================

@app.on_event("startup")
async def startup_event():
    print("Initializing moderator...")
    get_moderator()
    print("Moderator ready!")
    print(f"API Key authentication enabled (key length: {len(API_KEY)} characters)")

# ============================================================================
# ENDPOINTS
# ============================================================================

@app.get("/")
async def root():
    return {
        "message": "Text Moderation API",
        "authentication": "Required - X-API-Key header",
        "endpoints": {
            "/moderate": "POST - Moderate text content (Protected)",
            "/health": "GET - Health check (Public)"
        }
    }

@app.get("/health")
async def health_check():
    """Public health check endpoint - No API key required"""
    try:
        moderator = get_moderator()
        return {
            "status": "healthy",
            "models_loaded": moderator.toxic_model is not None and moderator.hate_model is not None,
            "device": moderator.device,
            "auth_enabled": True
        }
    except Exception as e:
        return JSONResponse(
            status_code=503,
            content={"status": "unhealthy", "error": str(e)}
        )

@app.post("/moderate", response_model=ModerationResponse)
async def moderate_text(
    request: ModerationRequest,
    api_key: str = Depends(verify_api_key)  # Protected endpoint
):
    """
    Moderate text content and return action recommendation (Protected - Requires API Key)
    
    - **user_id**: Identifier for the user who posted the content
    - **text**: The text content to moderate
    - **X-API-Key**: API key in request header
    """
    if not request.text or not request.text.strip():
        raise HTTPException(status_code=400, detail="Text cannot be empty")
    
    if len(request.text) > 2000:
        raise HTTPException(status_code=400, detail="Text too long (max 2000 characters)")
    
    try:
        start_time = time.time()
        
        moderator = get_moderator()
        result = moderator.moderate(request.text)
        
        processing_time = (time.time() - start_time) * 1000
        
        return ModerationResponse(
            user_id=request.user_id,
            action=result["action"],
            reason=result.get("reason"),
            toxic_score=result.get("toxic_score"),
            hate_score=result.get("hate_score"),
            processing_time_ms=round(processing_time, 2)
        )
        
    except Exception as e:
        raise HTTPException(status_code=500, detail=f"Moderation error: {str(e)}")

@app.post("/moderate/batch")
async def moderate_batch(
    requests: list[ModerationRequest],
    api_key: str = Depends(verify_api_key)  # Protected endpoint
):
    """Moderate multiple texts in batch (Protected - Requires API Key)"""
    if len(requests) > 100:
        raise HTTPException(status_code=400, detail="Batch size too large (max 100)")
    
    moderator = get_moderator()
    responses = []
    
    for req in requests:
        try:
            result = moderator.moderate(req.text)
            responses.append({
                "user_id": req.user_id,
                "action": result["action"],
                "reason": result.get("reason"),
                "toxic_score": result.get("toxic_score"),
                "hate_score": result.get("hate_score")
            })
        except Exception as e:
            responses.append({
                "user_id": req.user_id,
                "action": "error",
                "reason": str(e)
            })
    
    return {"results": responses}

if __name__ == "__main__":
    uvicorn.run(app, host="0.0.0.0", port=7860)