Update src/inference.py
Browse files- src/inference.py +37 -55
src/inference.py
CHANGED
|
@@ -3,67 +3,49 @@ 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.model import load_model
|
| 9 |
from src.entity_extraction import extract_entities
|
| 10 |
|
| 11 |
-
# Define path to models
|
| 12 |
BASE_PATH = os.path.abspath(os.path.join(os.path.dirname(__file__), "..", "models"))
|
| 13 |
|
| 14 |
-
# Load models and vectorizer
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
tfidf = joblib.load(os.path.join(BASE_PATH, "tfidf.pkl"))
|
| 19 |
-
except Exception as e:
|
| 20 |
-
print(f"Error loading models or vectorizer: {e}")
|
| 21 |
-
model_issue = None
|
| 22 |
-
model_urgency = None
|
| 23 |
-
tfidf = None
|
| 24 |
|
| 25 |
def predict_ticket(ticket_text):
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
# Extract entities from original text
|
| 55 |
-
entities = extract_entities(ticket_text)
|
| 56 |
-
|
| 57 |
-
return {
|
| 58 |
-
"issue_type": issue_pred,
|
| 59 |
-
"urgency_level": urgency_pred,
|
| 60 |
-
"entities": entities
|
| 61 |
-
}
|
| 62 |
-
|
| 63 |
-
except Exception as e:
|
| 64 |
-
# Catch any runtime errors and report them for debugging
|
| 65 |
-
return {
|
| 66 |
-
"issue_type": f"Prediction error: {str(e)}",
|
| 67 |
-
"urgency_level": f"Prediction error: {str(e)}",
|
| 68 |
-
"entities": {}
|
| 69 |
-
}
|
|
|
|
| 3 |
import joblib
|
| 4 |
import scipy.sparse
|
| 5 |
from textblob import TextBlob
|
| 6 |
+
import nltk
|
| 7 |
+
|
| 8 |
+
# Download NLTK punkt tokenizer to avoid runtime errors
|
| 9 |
+
nltk.download('punkt')
|
| 10 |
|
| 11 |
from src.preprocessing import clean_text
|
| 12 |
+
from src.features import create_features
|
| 13 |
from src.model import load_model
|
| 14 |
from src.entity_extraction import extract_entities
|
| 15 |
|
| 16 |
+
# Define the path to the models directory
|
| 17 |
BASE_PATH = os.path.abspath(os.path.join(os.path.dirname(__file__), "..", "models"))
|
| 18 |
|
| 19 |
+
# Load models and vectorizer
|
| 20 |
+
model_issue = load_model(os.path.join(BASE_PATH, "model_issue_type.pkl"))
|
| 21 |
+
model_urgency = load_model(os.path.join(BASE_PATH, "model_urgency_level.pkl"))
|
| 22 |
+
tfidf = joblib.load(os.path.join(BASE_PATH, "tfidf.pkl"))
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 23 |
|
| 24 |
def predict_ticket(ticket_text):
|
| 25 |
+
# Preprocess the input ticket text
|
| 26 |
+
clean = clean_text(ticket_text)
|
| 27 |
+
|
| 28 |
+
# TF-IDF transformation
|
| 29 |
+
X_tfidf = tfidf.transform([clean])
|
| 30 |
+
|
| 31 |
+
# Additional features
|
| 32 |
+
ticket_length = len(clean.split())
|
| 33 |
+
sentiment = TextBlob(clean).sentiment.polarity
|
| 34 |
+
|
| 35 |
+
# Combine sparse TF-IDF with dense features
|
| 36 |
+
X_features = scipy.sparse.hstack([
|
| 37 |
+
X_tfidf,
|
| 38 |
+
np.array([[ticket_length]]),
|
| 39 |
+
np.array([[sentiment]])
|
| 40 |
+
])
|
| 41 |
+
|
| 42 |
+
# Make predictions
|
| 43 |
+
issue_pred = model_issue.predict(X_features)[0]
|
| 44 |
+
urgency_pred = model_urgency.predict(X_features)[0]
|
| 45 |
+
entities = extract_entities(ticket_text)
|
| 46 |
+
|
| 47 |
+
return {
|
| 48 |
+
"issue_type": issue_pred,
|
| 49 |
+
"urgency_level": urgency_pred,
|
| 50 |
+
"entities": entities
|
| 51 |
+
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|