Update src/inference.py
Browse files- src/inference.py +19 -13
src/inference.py
CHANGED
|
@@ -1,39 +1,45 @@
|
|
| 1 |
import os
|
| 2 |
-
from src.preprocessing import clean_text
|
| 3 |
-
from src.features import create_features
|
| 4 |
-
from src.model import load_model
|
| 5 |
-
from src.entity_extraction import extract_entities
|
| 6 |
import numpy as np
|
| 7 |
import joblib
|
| 8 |
import scipy.sparse
|
| 9 |
from textblob import TextBlob
|
| 10 |
|
| 11 |
-
|
| 12 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 13 |
|
| 14 |
-
#
|
| 15 |
-
model_issue = load_model(os.path.join(BASE_PATH,
|
| 16 |
-
model_urgency = load_model(os.path.join(BASE_PATH,
|
| 17 |
-
tfidf = joblib.load(os.path.join(BASE_PATH,
|
| 18 |
|
| 19 |
def predict_ticket(ticket_text):
|
|
|
|
| 20 |
clean = clean_text(ticket_text)
|
|
|
|
| 21 |
X_tfidf = tfidf.transform([clean])
|
|
|
|
| 22 |
ticket_length = len(clean.split())
|
| 23 |
sentiment = TextBlob(clean).sentiment.polarity
|
| 24 |
|
|
|
|
| 25 |
X_features = scipy.sparse.hstack([
|
| 26 |
X_tfidf,
|
| 27 |
np.array([[ticket_length]]),
|
| 28 |
np.array([[sentiment]])
|
| 29 |
])
|
| 30 |
|
|
|
|
| 31 |
issue_pred = model_issue.predict(X_features)[0]
|
| 32 |
urgency_pred = model_urgency.predict(X_features)[0]
|
| 33 |
entities = extract_entities(ticket_text)
|
| 34 |
|
| 35 |
return {
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
}
|
|
|
|
| 1 |
import os
|
|
|
|
|
|
|
|
|
|
|
|
|
| 2 |
import numpy as np
|
| 3 |
import joblib
|
| 4 |
import scipy.sparse
|
| 5 |
from textblob import TextBlob
|
| 6 |
|
| 7 |
+
from src.preprocessing import clean_text
|
| 8 |
+
from src.features import create_features
|
| 9 |
+
from src.model import load_model
|
| 10 |
+
from src.entity_extraction import extract_entities
|
| 11 |
+
|
| 12 |
+
# βββ Adjusted BASE_PATH to point at ../models ββββββββββββββββββββββββββββββββββ
|
| 13 |
+
BASE_PATH = os.path.abspath(os.path.join(os.path.dirname(__file__), "..", "models"))
|
| 14 |
|
| 15 |
+
# Load models and TF-IDF vectorizer from the models/ folder at repo root
|
| 16 |
+
model_issue = load_model(os.path.join(BASE_PATH, "model_issue_type.pkl"))
|
| 17 |
+
model_urgency = load_model(os.path.join(BASE_PATH, "model_urgency_level.pkl"))
|
| 18 |
+
tfidf = joblib.load(os.path.join(BASE_PATH, "tfidf.pkl"))
|
| 19 |
|
| 20 |
def predict_ticket(ticket_text):
|
| 21 |
+
# Preprocess
|
| 22 |
clean = clean_text(ticket_text)
|
| 23 |
+
# TF-IDF transform
|
| 24 |
X_tfidf = tfidf.transform([clean])
|
| 25 |
+
# Additional features
|
| 26 |
ticket_length = len(clean.split())
|
| 27 |
sentiment = TextBlob(clean).sentiment.polarity
|
| 28 |
|
| 29 |
+
# Combine features (sparse + dense)
|
| 30 |
X_features = scipy.sparse.hstack([
|
| 31 |
X_tfidf,
|
| 32 |
np.array([[ticket_length]]),
|
| 33 |
np.array([[sentiment]])
|
| 34 |
])
|
| 35 |
|
| 36 |
+
# Predictions
|
| 37 |
issue_pred = model_issue.predict(X_features)[0]
|
| 38 |
urgency_pred = model_urgency.predict(X_features)[0]
|
| 39 |
entities = extract_entities(ticket_text)
|
| 40 |
|
| 41 |
return {
|
| 42 |
+
"issue_type": issue_pred,
|
| 43 |
+
"urgency_level": urgency_pred,
|
| 44 |
+
"entities": entities
|
| 45 |
}
|