Max Chis
commited on
Commit
·
6766ca8
1
Parent(s):
3257124
Create handler.py
Browse files- handler.py +37 -0
handler.py
ADDED
|
@@ -0,0 +1,37 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from typing import Dict, Any
|
| 2 |
+
from sklearn.model_selection import train_test_split
|
| 3 |
+
|
| 4 |
+
from sklearn.datasets import make_classification
|
| 5 |
+
from sklearn.linear_model import LogisticRegression
|
| 6 |
+
from sklearn.model_selection import train_test_split
|
| 7 |
+
from sklearn.metrics import classification_report
|
| 8 |
+
|
| 9 |
+
|
| 10 |
+
|
| 11 |
+
class EndpointHandler:
|
| 12 |
+
def __init__(self, path: str):
|
| 13 |
+
# model_dir = os.getenv("HF_MODEL_DIR", ".")
|
| 14 |
+
#
|
| 15 |
+
# with open(os.path.join(model_dir, "model.pkl"), "rb") as f:
|
| 16 |
+
# self.model = pickle.load(f)
|
| 17 |
+
#
|
| 18 |
+
# # optional: you could also load a vocabulary or vectorizer
|
| 19 |
+
# with open(os.path.join(model_dir, "tokenizer.pkl"), "rb") as f:
|
| 20 |
+
# self.vectorizer = pickle.load(f)
|
| 21 |
+
|
| 22 |
+
# 1. Generate synthetic binary classification data
|
| 23 |
+
X, y = make_classification(n_samples=100, n_features=4, n_classes=2, random_state=42)
|
| 24 |
+
|
| 25 |
+
# 2. Split into train/test sets
|
| 26 |
+
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)
|
| 27 |
+
|
| 28 |
+
# 3. Create and train the Logistic Regression model
|
| 29 |
+
self.model = LogisticRegression()
|
| 30 |
+
self.model.fit(X_train, y_train)
|
| 31 |
+
|
| 32 |
+
|
| 33 |
+
|
| 34 |
+
def __call__(self, inputs: Dict[str, Any]) -> Dict[str, str]:
|
| 35 |
+
# Expecting input like: {"inputs": "<html>...</html>"}
|
| 36 |
+
html = inputs["inputs"]
|
| 37 |
+
return {"label": str(1)}
|