File size: 2,914 Bytes
5d70ecb 42e8f2d ecf779d 5d70ecb ecf779d 90fdda0 5d70ecb ecf779d c07f0c7 5d70ecb 42e8f2d 5d70ecb 42e8f2d 5d70ecb 90fdda0 5d70ecb 90fdda0 42e8f2d 5d70ecb 42e8f2d 5d70ecb 42e8f2d 5d70ecb 42e8f2d 5d70ecb 42e8f2d 5d70ecb 42e8f2d 90fdda0 |
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 |
import torch
import joblib
import gradio as gr
import matplotlib.pyplot as plt
from sklearn.multioutput import MultiOutputClassifier
# Allow loading MultiOutputClassifier
torch.serialization.add_safe_globals([MultiOutputClassifier])
# Load the Model and Dependencies
tfidf_vectorizer = joblib.load("tfidf_vectorizer.pkl")
category_encoder = joblib.load("category_encoder.pkl")
team_encoder = joblib.load("team_encoder.pkl")
# Load the model with full object support
multi_label_classifier = torch.load("multi_label_classifier.pth", weights_only=False)
# multi_label_classifier.eval()
# Dummy function to get keywords
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"])
# Function to predict and visualize results
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)
# Visualization: Pie chart for category distribution
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")
# Styled output
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
# Gradio Interface with Better UI
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"
)
# Launch App
interface.launch()
|