# Preprocess data
# Split data into input and output variables
X = df[['tenant_id', 'ticket_desc', 'ticket_summary']]
y = df[['ticket_call_type', 'ticket_type', 'ticket_category', 'ticket_type', 'ticket_item', 'ticket_severity']]
# Convert text data to numeric features using CountVectorizer
X['text'] = X['ticket_desc'] + ' ' + X['ticket_summary']
X_text = X['text']
cv = CountVectorizer(stop_words='english',max_features=1000)
X_cv = cv.fit_transform(X_text)
X_cv_df = pd.DataFrame(X_cv.toarray(), columns=cv.get_feature_names_out())
X_cv_df = X_cv_df.values.reshape(X.shape[0], -1)
# Split data into training and test sets
X_train, X_test, y_train, y_test = train_test_split(X_cv_df, y, test_size=0.2, random_state=42)
# Train random forest classifier to predict output variables
rf = RandomForestClassifier(n_estimators=100, random_state=42)
rf.fit(X_train, y_train)
from chatterbot.trainers import ChatterBotCorpusTrainer
from chatterbot import ChatBot
import nltk
import ssl
try:
_create_unverified_https_context = ssl._create_unverified_context
except AttributeError:
pass
else:
ssl._create_default_https_context = _create_unverified_https_context
# Define chatbot
chatbot = ChatBot('TicketBot')
trainer = ChatterBotCorpusTrainer(chatbot)
# Train chatbot using corpus
trainer.train('chatterbot.corpus.english')
# Define function to extract tenant name from user input
def extract_tenant(input_text):
tenant_names = ['INfra', 'Tool_Team', 'Sodexo_Core', 'Platform_Team']
input_text = input_text.lower()
for name in tenant_names:
if name.lower() in input_text:
return name
return None
# Define function to generate incident/ticket based on user input
def generate_ticket(input_text):
# Extract tenant name from user input
tenant = extract_tenant(input_text)
if not tenant:
return 'Please provide a valid tenant name'
# Get description and summary from user input
input_text = input_text.lower()
description = input_text.split('description:')[1].strip() if 'description:' in input_text else ''
summary = input_text.split('summary:')[1].strip() if 'summary:' in input_text else ''
# Use machine learning model to predict output variables
input_vector = cv.transform(np.array([tenant, description, summary]))
input_vector = input_vector.toarray().reshape(1, -1)
prediction = rf.predict(input_vector)
# Create incident/ticket
ticket = {
'Tenant': tenant,
'Description': description,
'Summary': summary,
'Call_type': prediction[0][0],
'Ticket_Type': prediction[0][1],
'Requestor': prediction[0][2],
'Category': prediction[0][3],
'Type': prediction[0][4],
'Item': prediction[0][5],
'Severity': prediction[0][6]
}
return ticket
# Define function to handle user input and generate response
def handle_input(input_text):
# Generate incident/ticket
ticket = generate_ticket(input_text)
# Auto-populate ticket fields
ticket_type = ticket['ticket_type']
requestor = ticket['requestor']
category = ticket['category']
ticket_item = ticket['ticket_item']
severity = ticket['severity']
description = ticket['description']
tenant = ticket['tenant']
call_type = ticket['call_type']
summary = ticket['summary']