-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathStreamlit_app.py
More file actions
130 lines (101 loc) · 4.72 KB
/
Copy pathStreamlit_app.py
File metadata and controls
130 lines (101 loc) · 4.72 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
import streamlit as st
import pandas as pd
from sklearn.feature_extraction.text import TfidfVectorizer
from sklearn.ensemble import RandomForestClassifier
from sklearn.metrics import classification_report
import numpy as np
import re
import nltk
nltk.download('punkt')
nltk.download('stopwords')
nltk.download('wordnet')
from nltk.corpus import stopwords
from nltk.tokenize import word_tokenize
from nltk.stem import WordNetLemmatizer
def clean_text(text):
# Convert to lowercase
text = text.lower()
# Remove special characters and digits
text = re.sub(r'[^a-zA-Z\s]', '', text)
# Tokenize the text
tokens = word_tokenize(text)
# Remove stopwords
stop_words = set(stopwords.words('english'))
tokens = [token for token in tokens if token not in stop_words]
# Lemmatize the tokens
lemmatizer = WordNetLemmatizer()
tokens = [lemmatizer.lemmatize(token) for token in tokens]
# Join the tokens back into a single string
cleaned_text = ' '.join(tokens)
return cleaned_text
# Load the training dataset
train_df = pd.read_excel('Clean_Data1.xlsx') # Update with your file path
# Extract keywords from the manual classification column and create a new column
keywords = ['Collaboration', 'Complaint', 'Compliment', 'Feedback', 'Suggestion',
'Query', 'Junk', 'Non Relevant', 'Universal', 'Follow Up']
train_df['new_classification'] = train_df['Ticket subject'].apply(
lambda x: next((kw for kw in keywords if kw.lower() in x.lower()), 'Other')
)
# Drop rows where the new classification is None (no keyword found)
train_df = train_df.dropna(subset=['new_classification'])
# Clean and preprocess the training data messages
train_features = train_df['Brief Description of Feedback'].fillna('').apply(clean_text)
train_labels = train_df['new_classification']
# Convert the text messages into numerical feature vectors using TF-IDF
vectorizer = TfidfVectorizer()
train_features = vectorizer.fit_transform(train_features)
# Train a Random Forest Classifier with hyperparameter tuning
num_trees = 25
classifier = RandomForestClassifier(n_estimators=num_trees)
# Train the classifier with the entire training dataset
classifier.fit(train_features, train_labels)
# Define a function to classify new messages
def classify_message(message):
cleaned_message = clean_text(message)
message_features = vectorizer.transform([cleaned_message])
prediction = classifier.predict(message_features)[0]
return prediction
# Create the Streamlit app
def main():
st.title("Ticket Classification App")
# Upload a file for classification
uploaded_file = st.file_uploader("Upload a file", type="xlsx")
if uploaded_file is not None:
test_df = pd.read_excel(uploaded_file)
# Check if the message column exists
if "Message" in test_df.columns:
test_messages = test_df["Message"]
elif "Brief Description of Feedback" in test_df.columns:
test_messages = test_df["Brief Description of Feedback"]
else:
st.error("Error: Message column not found in the uploaded file.")
return
# Clean and preprocess the test data messages
test_features = test_messages.fillna('').apply(clean_text)
# Convert the test data messages into numerical feature vectors using TF-IDF
test_features = vectorizer.transform(test_features)
# Predict the classification on the test dataset
predictions = classifier.predict(test_features)
# Check if the message column exists
if "Junk" in test_df.columns:
test_df['Classified Class'] = test_df['Junk'].apply(
lambda x: next((kw for kw in keywords if kw.lower() in x.lower()), 'Other')
)
elif "Ticket Subject" in test_df.columns:
test_df['Classified Class'] = test_df['Ticket Subject'].apply(
lambda x: next((kw for kw in keywords if kw.lower() in x.lower()), 'Other')
)
else:
st.error("Error: Junk or Ticket subject column not found in the uploaded file.")
return
# Map "no comment" to "To Check" in the predicted classifications
predictions = np.where(test_messages.str.lower() == '(no comment)', 'To Check', predictions)
# Add the predictions to the test dataframe
test_df['predicted_classification'] = predictions
# Print the classification report for evaluation
st.text("Classification Report:")
st.text(classification_report(test_df['Classified Class'], predictions))
# Display the classified data
st.dataframe(test_df)
if __name__ == '__main__':
main()