Delete handler.py
Browse files- handler.py +0 -83
handler.py
DELETED
|
@@ -1,83 +0,0 @@
|
|
| 1 |
-
from huggingface_hub import hf_hub_download
|
| 2 |
-
import joblib
|
| 3 |
-
import json
|
| 4 |
-
|
| 5 |
-
class EndpointHandler:
|
| 6 |
-
def __init__(self, path=""):
|
| 7 |
-
"""
|
| 8 |
-
Initialize the handler, similar to your CuisineClassifier __init__
|
| 9 |
-
"""
|
| 10 |
-
print("Initializing CuisineClassifier Handler...")
|
| 11 |
-
|
| 12 |
-
components = ["cuisine_pipeline", "label_encoder"]
|
| 13 |
-
paths = {}
|
| 14 |
-
|
| 15 |
-
print("Loading files from local path...")
|
| 16 |
-
for name in components:
|
| 17 |
-
print(f"Loading {name}.joblib...")
|
| 18 |
-
try:
|
| 19 |
-
# In Inference Endpoints, files are already local in path
|
| 20 |
-
full_path = f"{path}/{name}.joblib" if path else f"{name}.joblib"
|
| 21 |
-
paths[name] = full_path
|
| 22 |
-
print(f"{name} path set to {full_path}")
|
| 23 |
-
except Exception as e:
|
| 24 |
-
print(f"Failed to set path for {name}: {e}")
|
| 25 |
-
raise
|
| 26 |
-
|
| 27 |
-
print("Loading model components with joblib...")
|
| 28 |
-
try:
|
| 29 |
-
self.model = joblib.load(paths["cuisine_pipeline"])
|
| 30 |
-
print("Model loaded.")
|
| 31 |
-
self.label_encoder = joblib.load(paths["label_encoder"])
|
| 32 |
-
print("Label encoder loaded.")
|
| 33 |
-
except Exception as e:
|
| 34 |
-
print(f"Failed to load components: {e}")
|
| 35 |
-
raise
|
| 36 |
-
|
| 37 |
-
print("Handler initialized successfully.")
|
| 38 |
-
|
| 39 |
-
def __call__(self, data):
|
| 40 |
-
"""
|
| 41 |
-
Handle inference requests
|
| 42 |
-
Expected input format:
|
| 43 |
-
{
|
| 44 |
-
"inputs": ["salt", "flour", "sugar", "eggs"]
|
| 45 |
-
}
|
| 46 |
-
OR
|
| 47 |
-
{
|
| 48 |
-
"inputs": "salt flour sugar eggs"
|
| 49 |
-
}
|
| 50 |
-
"""
|
| 51 |
-
try:
|
| 52 |
-
# Extract ingredients from request
|
| 53 |
-
inputs = data.get("inputs", [])
|
| 54 |
-
|
| 55 |
-
# Handle both list and string inputs
|
| 56 |
-
if isinstance(inputs, list):
|
| 57 |
-
ingredients_text = " ".join(inputs)
|
| 58 |
-
else:
|
| 59 |
-
ingredients_text = str(inputs)
|
| 60 |
-
|
| 61 |
-
print(f"Processing ingredients: {ingredients_text}")
|
| 62 |
-
|
| 63 |
-
# Use your existing classify logic
|
| 64 |
-
predicted_class = self.model.predict([ingredients_text])
|
| 65 |
-
predicted_label = self.label_encoder.inverse_transform(predicted_class)
|
| 66 |
-
|
| 67 |
-
# Get prediction probabilities for confidence scores
|
| 68 |
-
prediction_proba = self.model.predict_proba([ingredients_text])
|
| 69 |
-
max_confidence = float(max(prediction_proba[0]))
|
| 70 |
-
|
| 71 |
-
# Return structured response
|
| 72 |
-
return {
|
| 73 |
-
"cuisine": predicted_label[0],
|
| 74 |
-
"confidence": max_confidence,
|
| 75 |
-
"ingredients_processed": ingredients_text
|
| 76 |
-
}
|
| 77 |
-
|
| 78 |
-
except Exception as e:
|
| 79 |
-
return {
|
| 80 |
-
"error": str(e),
|
| 81 |
-
"cuisine": None,
|
| 82 |
-
"confidence": 0.0
|
| 83 |
-
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|