ethio-langid / app.py
xlxuxs
add keyword extraction service
8a0a240
Raw
History Blame Contribute Delete
1.62 kB
import fasttext
from fastapi import FastAPI, HTTPException, Depends
from fastapi.security import APIKeyHeader
from pydantic import BaseModel
from huggingface_hub import hf_hub_download
import os
app = FastAPI()
API_KEY = os.getenv("INTERNAL_API_KEY")
if not API_KEY:
raise RuntimeError("INTERNAL_API_KEY environment variable not set")
API_KEY_NAME = "X-Internal-API-Key"
api_key_header = APIKeyHeader(name=API_KEY_NAME, auto_error=False)
async def verify_api_key(api_key: str = Depends(api_key_header)):
if not api_key or api_key != API_KEY:
raise HTTPException(status_code=403, detail="Invalid API Key")
return api_key
MODEL_PATH = hf_hub_download(
repo_id="facebook/fasttext-language-identification",
filename="model.bin"
)
model = fasttext.load_model(MODEL_PATH)
class TextInput(BaseModel):
text: str
class DetectionResult(BaseModel):
language: str
confidence: float
raw_label: str
@app.post("/detect", response_model=DetectionResult)
async def detect_language(input: TextInput, _ = Depends(verify_api_key)):
if not input.text or not input.text.strip():
raise HTTPException(status_code=400, detail="Empty text")
predictions = model.predict(input.text, k=1)
raw_label = predictions[0][0].replace('__label__', '')
confidence = float(predictions[1][0])
lang_code = raw_label.split('_')[0] if '_' in raw_label else raw_label
return {
"language": lang_code,
"confidence": confidence,
"raw_label": raw_label
}
@app.get("/health")
async def health(_ = Depends(verify_api_key)):
return {"status": "ok"}