Text Classification
Keras
English
Azerbaijani
prompt-injection
security
llm-security
document-security
retvec
cnn
tensorflow
fastapi
Eval Results (legacy)
Instructions to use MegrurNiftiyev/MyGuard-Prompt-Injection-Detector with libraries, inference providers, notebooks, and local apps. Follow these links to get started.
- Libraries
- Keras
How to use MegrurNiftiyev/MyGuard-Prompt-Injection-Detector with Keras:
# !pip install -U keras tensorflow huggingface_hub # Keras needs TensorFlow installed to read "hf://" paths, so the tensorflow backend is selected here; # "jax" and "torch" also work for computation once TensorFlow is installed. import os os.environ["KERAS_BACKEND"] = "tensorflow" import keras model = keras.saving.load_model("hf://MegrurNiftiyev/MyGuard-Prompt-Injection-Detector") - Notebooks
- Google Colab
- Kaggle
Download app/api/routes/classify.py from MegrurNiftiyev/MyGuard-Prompt-Injection-Detector: direct link, hf CLI and curl.
- Browser
- Download file 2.68 kB
-
https://huggingface.co/MegrurNiftiyev/MyGuard-Prompt-Injection-Detector/resolve/main/app/api/routes/classify.py
- Command line
-
hf download hf://MegrurNiftiyev/MyGuard-Prompt-Injection-Detector/app/api/routes/classify.py
-
curl -L -o classify.py https://huggingface.co/MegrurNiftiyev/MyGuard-Prompt-Injection-Detector/resolve/main/app/api/routes/classify.py
2.68 kB
| """ | |
| POST /classify β document text classification endpoint. | |
| """ | |
| from fastapi import APIRouter, Depends, Header, HTTPException | |
| from app.api.dependencies import verify_internal_service | |
| from app.models.schemas import ClassifyRequest, ClassifyResponse, ErrorResponse | |
| from app.ml.serving.registry import load_active_model | |
| from app.ml.serving.inference import run_prediction | |
| from app.core.logging import get_logger | |
| logger = get_logger(__name__) | |
| router = APIRouter(prefix="/analyze-injection", tags=["Prompt Injection Analysis"]) | |
| async def classify( | |
| req: ClassifyRequest, | |
| ): | |
| """Run the active RETVec+CNN model on fullText.""" | |
| words = req.fullText.strip().split() if req.fullText else [] | |
| if len(words) < 5: | |
| raise HTTPException( | |
| status_code=503, | |
| detail="insufficient_text" | |
| ) | |
| try: | |
| model = await load_active_model() | |
| except Exception as e: | |
| logger.error("Classification model unavailable: %s", str(e)) | |
| raise HTTPException( | |
| status_code=503, | |
| detail={"error": "Classification model unavailable", "detail": str(e)} | |
| ) | |
| doc_id = req.documentId or "N/A" | |
| try: | |
| label, confidence = run_prediction(model, req.fullText) | |
| except Exception as e: | |
| logger.error("Inference prediction error for document %s: %s", doc_id, str(e), exc_info=True) | |
| raise HTTPException( | |
| status_code=500, | |
| detail=f"Inference failed: {str(e)}" | |
| ) | |
| logger.info( | |
| "Classified document %s (length: %d chars, words: %d) β %s (confidence: %.2f)", | |
| doc_id, | |
| len(req.fullText), | |
| len(words), | |
| label, | |
| confidence, | |
| ) | |
| return ClassifyResponse( | |
| label=label, confidence=confidence | |
| ) | |