Download features/image_classifier/inferencer.py from Pujan-Dev/AI_API: direct link, hf CLI and curl.
- Browser
- Download file 1.15 kB
-
https://huggingface.co/spaces/Pujan-Dev/AI_API/resolve/main/features/image_classifier/inferencer.py
- Command line
-
hf download hf://spaces/Pujan-Dev/AI_API/features/image_classifier/inferencer.py
-
curl -L -o inferencer.py https://huggingface.co/spaces/Pujan-Dev/AI_API/resolve/main/features/image_classifier/inferencer.py
1.15 kB
| import numpy as np | |
| from .model_loader import get_model | |
| # Thresholds | |
| AI_THRESHOLD = 0.55 | |
| HUMAN_THRESHOLD = 0.45 | |
| def classify_image(image_array: np.ndarray) -> dict: | |
| try: | |
| model = get_model() | |
| predictions = model.predict(image_array) | |
| if predictions.ndim != 2 or predictions.shape[1] != 1: | |
| raise ValueError( | |
| "Model output shape is invalid. Expected shape: (batch, 1)" | |
| ) | |
| ai_conf = float(np.clip(predictions[0][0], 0.0, 1.0)) | |
| human_conf = 1.0 - ai_conf | |
| # Classification logic | |
| if ai_conf > AI_THRESHOLD: | |
| label = "AI Generated" | |
| elif ai_conf < HUMAN_THRESHOLD: | |
| label = "Human Generated" | |
| else: | |
| label = "Uncertain (Maybe AI)" | |
| return { | |
| "label": label, | |
| "ai_confidence": round(ai_conf * 100, 2), | |
| "human_confidence": round(human_conf * 100, 2), | |
| } | |
| except Exception as e: | |
| return { | |
| "error": str(e), | |
| "label": "Classification Failed", | |
| "ai_confidence": None, | |
| "human_confidence": None, | |
| } | |