Download app.py from Ramosh99/codemix: direct link, hf CLI and curl.
- Browser
- Download file 1.21 kB
-
https://huggingface.co/spaces/Ramosh99/codemix/resolve/main/app.py
- Command line
-
hf download hf://spaces/Ramosh99/codemix/app.py
-
curl -L -o app.py https://huggingface.co/spaces/Ramosh99/codemix/resolve/main/app.py
1.21 kB
| """ | |
| Sinhala-English Codemix PII Detector — HuggingFace Space API | |
| Model: Ramosh99/mixcode_pii_detector (XLM-RoBERTa token-classification) | |
| Also used as fallback for pure Sinhala inputs. | |
| """ | |
| import os | |
| from transformers import pipeline | |
| from fastapi import FastAPI | |
| from pydantic import BaseModel | |
| app = FastAPI(title="Codemix PII Detector", version="1.0.0") | |
| # Load model at startup — stays in memory | |
| ner = pipeline( | |
| "ner", | |
| model="Ramosh99/mixcode_pii_detector", | |
| aggregation_strategy="average", | |
| token=os.getenv("HF_TOKEN"), | |
| ) | |
| class PredictRequest(BaseModel): | |
| text: str | |
| def health(): | |
| return {"status": "ok", "model": "Ramosh99/mixcode_pii_detector"} | |
| def predict(req: PredictRequest): | |
| raw = ner(req.text) | |
| entities = [] | |
| for item in raw: | |
| label = item.get("entity_group", "") | |
| if label.startswith(("B-", "I-")): | |
| label = label[2:] | |
| entities.append({ | |
| "span": item["word"].strip(), | |
| "start": item["start"], | |
| "end": item["end"], | |
| "entity_group": label, | |
| "confidence": round(float(item["score"]), 4), | |
| }) | |
| return {"entities": entities} | |