Update app.py
Browse files
app.py
CHANGED
|
@@ -1,5 +1,3 @@
|
|
| 1 |
-
# ticket_classifier.py
|
| 2 |
-
|
| 3 |
import pandas as pd
|
| 4 |
import numpy as np
|
| 5 |
import re
|
|
@@ -20,7 +18,7 @@ nltk.download('stopwords')
|
|
| 20 |
nltk.download('wordnet')
|
| 21 |
nltk.download('omw-1.4')
|
| 22 |
|
| 23 |
-
#
|
| 24 |
def load_data(file_path):
|
| 25 |
df = pd.read_excel(file_path)
|
| 26 |
print(f"Loaded data shape: {df.shape}")
|
|
@@ -45,7 +43,8 @@ def preprocess_data(df):
|
|
| 45 |
df['processed_text'] = df['clean_text'].apply(lambda x: ' '.join(tokenize_lemmatize(x)))
|
| 46 |
return df
|
| 47 |
|
| 48 |
-
#
|
|
|
|
| 49 |
def simple_sentiment(text):
|
| 50 |
pos = ['good', 'great', 'excellent', 'thanks']
|
| 51 |
neg = ['bad', 'broken', 'late', 'error', 'issue', 'problem']
|
|
@@ -58,7 +57,10 @@ def feature_engineering(df):
|
|
| 58 |
df['sentiment'] = df['clean_text'].apply(simple_sentiment)
|
| 59 |
return df
|
| 60 |
|
| 61 |
-
#
|
|
|
|
|
|
|
|
|
|
| 62 |
def train_models(df):
|
| 63 |
X = df[['processed_text', 'ticket_length', 'word_count', 'sentiment']]
|
| 64 |
y_issue = df['issue_type']
|
|
@@ -95,7 +97,7 @@ def train_models(df):
|
|
| 95 |
|
| 96 |
return issue_model, urgency_model
|
| 97 |
|
| 98 |
-
#
|
| 99 |
def predict_ticket(ticket_text, issue_model, urgency_model):
|
| 100 |
cleaned = clean_text(ticket_text)
|
| 101 |
processed = ' '.join(tokenize_lemmatize(cleaned))
|
|
@@ -107,7 +109,7 @@ def predict_ticket(ticket_text, issue_model, urgency_model):
|
|
| 107 |
}])
|
| 108 |
return issue_model.predict(features)[0], urgency_model.predict(features)[0]
|
| 109 |
|
| 110 |
-
#
|
| 111 |
def create_gradio_interface(issue_model, urgency_model):
|
| 112 |
def wrapped(ticket_text):
|
| 113 |
try:
|
|
@@ -135,7 +137,8 @@ def create_gradio_interface(issue_model, urgency_model):
|
|
| 135 |
]
|
| 136 |
)
|
| 137 |
|
| 138 |
-
|
|
|
|
| 139 |
if __name__ == "__main__":
|
| 140 |
df = load_data("ai_dev_assignment_tickets_complex_1000.xls")
|
| 141 |
df = preprocess_data(df)
|
|
|
|
|
|
|
|
|
|
| 1 |
import pandas as pd
|
| 2 |
import numpy as np
|
| 3 |
import re
|
|
|
|
| 18 |
nltk.download('wordnet')
|
| 19 |
nltk.download('omw-1.4')
|
| 20 |
|
| 21 |
+
# DataLoad and Preprocess
|
| 22 |
def load_data(file_path):
|
| 23 |
df = pd.read_excel(file_path)
|
| 24 |
print(f"Loaded data shape: {df.shape}")
|
|
|
|
| 43 |
df['processed_text'] = df['clean_text'].apply(lambda x: ' '.join(tokenize_lemmatize(x)))
|
| 44 |
return df
|
| 45 |
|
| 46 |
+
# Feature Engineering
|
| 47 |
+
# Checking the sentimenet analysis on basees of different word like bad, good, late etc.
|
| 48 |
def simple_sentiment(text):
|
| 49 |
pos = ['good', 'great', 'excellent', 'thanks']
|
| 50 |
neg = ['bad', 'broken', 'late', 'error', 'issue', 'problem']
|
|
|
|
| 57 |
df['sentiment'] = df['clean_text'].apply(simple_sentiment)
|
| 58 |
return df
|
| 59 |
|
| 60 |
+
# Train Models
|
| 61 |
+
# so here we are train the randomforest model.
|
| 62 |
+
# and we need to train the model as per the requirement issue_type and uregency_level
|
| 63 |
+
# also calculating the model performance by Classification Report
|
| 64 |
def train_models(df):
|
| 65 |
X = df[['processed_text', 'ticket_length', 'word_count', 'sentiment']]
|
| 66 |
y_issue = df['issue_type']
|
|
|
|
| 97 |
|
| 98 |
return issue_model, urgency_model
|
| 99 |
|
| 100 |
+
# Predict Single Ticket
|
| 101 |
def predict_ticket(ticket_text, issue_model, urgency_model):
|
| 102 |
cleaned = clean_text(ticket_text)
|
| 103 |
processed = ' '.join(tokenize_lemmatize(cleaned))
|
|
|
|
| 109 |
}])
|
| 110 |
return issue_model.predict(features)[0], urgency_model.predict(features)[0]
|
| 111 |
|
| 112 |
+
# Generating the Gradio Interface as per task
|
| 113 |
def create_gradio_interface(issue_model, urgency_model):
|
| 114 |
def wrapped(ticket_text):
|
| 115 |
try:
|
|
|
|
| 137 |
]
|
| 138 |
)
|
| 139 |
|
| 140 |
+
|
| 141 |
+
|
| 142 |
if __name__ == "__main__":
|
| 143 |
df = load_data("ai_dev_assignment_tickets_complex_1000.xls")
|
| 144 |
df = preprocess_data(df)
|