Satyam0077 commited on
Commit
3a6b383
·
verified ·
1 Parent(s): 18762b0

Upload inference.py

Browse files
Files changed (1) hide show
  1. inference.py +42 -0
inference.py ADDED
@@ -0,0 +1,42 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import os
2
+ from src.preprocessing import clean_text # updated imports with src.
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
+
9
+ # Define base path to notebooks folder relative to this file
10
+ BASE_PATH = os.path.join(os.path.dirname(__file__), '..', 'notebooks')
11
+
12
+ # Load saved models and vectorizer once from notebooks folder
13
+ model_issue = load_model(os.path.join(BASE_PATH, 'model_issue_type.pkl'))
14
+ model_urgency = load_model(os.path.join(BASE_PATH, 'model_urgency_level.pkl'))
15
+ tfidf = joblib.load(os.path.join(BASE_PATH, 'tfidf.pkl'))
16
+
17
+ def predict_ticket(ticket_text):
18
+ clean = clean_text(ticket_text)
19
+ # Create features
20
+ X_tfidf = tfidf.transform([clean])
21
+ ticket_length = len(clean.split())
22
+
23
+ from textblob import TextBlob
24
+ sentiment = TextBlob(clean).sentiment.polarity
25
+
26
+ # Combine features (make sure shape matches training)
27
+ import scipy.sparse
28
+ X_features = scipy.sparse.hstack([
29
+ X_tfidf,
30
+ np.array([[ticket_length]]),
31
+ np.array([[sentiment]])
32
+ ])
33
+
34
+ issue_pred = model_issue.predict(X_features)[0]
35
+ urgency_pred = model_urgency.predict(X_features)[0]
36
+ entities = extract_entities(ticket_text)
37
+
38
+ return {
39
+ 'issue_type': issue_pred,
40
+ 'urgency_level': urgency_pred,
41
+ 'entities': entities
42
+ }