|
import torch |
|
import joblib |
|
import gradio as gr |
|
import matplotlib.pyplot as plt |
|
from sklearn.multioutput import MultiOutputClassifier |
|
|
|
|
|
torch.serialization.add_safe_globals([MultiOutputClassifier]) |
|
|
|
|
|
tfidf_vectorizer = joblib.load("tfidf_vectorizer.pkl") |
|
category_encoder = joblib.load("category_encoder.pkl") |
|
team_encoder = joblib.load("team_encoder.pkl") |
|
|
|
|
|
multi_label_classifier = torch.load("multi_label_classifier.pth", weights_only=False) |
|
|
|
|
|
|
|
def get_top_keywords_per_category(category, n=5): |
|
keywords_dict = { |
|
"UX Issue": ["mobile", "responsive", "alignment", "css", "layout"], |
|
"Backend Failure": ["API", "server", "timeout", "database", "error"], |
|
"UI Bug": ["button", "color", "CSS", "overlap", "scroll"], |
|
} |
|
return keywords_dict.get(category, ["No keywords found"]) |
|
|
|
|
|
def predict_with_visuals(phrase): |
|
text_features = tfidf_vectorizer.transform([phrase]) |
|
predicted_labels = multi_label_classifier.predict(text_features) |
|
|
|
predicted_category = category_encoder.inverse_transform([predicted_labels[0][0]])[0] |
|
predicted_team = team_encoder.inverse_transform([predicted_labels[0][1]])[0] |
|
team_email = f"support@{predicted_team.replace(' ', '').lower()}.com" |
|
keywords = get_top_keywords_per_category(predicted_category, n=5) |
|
|
|
|
|
categories = ["UX Issue", "Backend Failure", "UI Bug"] |
|
category_counts = [1 if cat == predicted_category else 0 for cat in categories] |
|
|
|
fig, ax = plt.subplots() |
|
ax.pie(category_counts, labels=categories, autopct="%1.1f%%", colors=["#ff9999","#66b3ff","#99ff99"]) |
|
ax.set_title("Predicted Category Distribution") |
|
|
|
|
|
result = f""" |
|
<div style='font-size: 18px; font-family: Arial;'> |
|
<strong>π Predicted Category:</strong> <span style='color:blue;'>{predicted_category}</span><br> |
|
<strong>π¨βπ» Assigned Team:</strong> <span style='color:green;'>{predicted_team}</span><br> |
|
<strong>π§ Team Email:</strong> <span style='color:red;'>{team_email}</span><br> |
|
<strong>π Top Keywords:</strong> <span style='color:purple;'>{', '.join(keywords)}</span> |
|
</div> |
|
""" |
|
|
|
return result, fig |
|
|
|
|
|
interface = gr.Interface( |
|
fn=predict_with_visuals, |
|
inputs=gr.Textbox(lines=2, placeholder="Enter defect description..."), |
|
outputs=["html", "plot"], |
|
title="π AI Defect Ticket Classifier", |
|
description="Enter a defect description to predict its **Category, Assigned Team, and relevant Keywords**. Get a **visual breakdown** of the classification!", |
|
theme="compact" |
|
) |
|
|
|
|
|
interface.launch() |
|
|