TuShar2309 commited on
Commit
2a0b720
Β·
verified Β·
1 Parent(s): 855f3da

Program Files are Added.

Browse files
Files changed (3) hide show
  1. README.md +25 -12
  2. app.py +256 -0
  3. requirements.txt +4 -0
README.md CHANGED
@@ -1,12 +1,25 @@
1
- ---
2
- title: Ticket Classifier
3
- emoji: πŸ“‰
4
- colorFrom: indigo
5
- colorTo: blue
6
- sdk: gradio
7
- sdk_version: 6.2.0
8
- app_file: app.py
9
- pinned: false
10
- ---
11
-
12
- Check out the configuration reference at https://huggingface.co/docs/hub/spaces-config-reference
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ ---
2
+ title: IT Ticket Classifier
3
+ emoji: 🎫
4
+ colorFrom: green
5
+ colorTo: blue
6
+ sdk: gradio
7
+ sdk_version: 4.44.0
8
+ app_file: app.py
9
+ pinned: false
10
+ license: mit
11
+ ---
12
+
13
+ # IT Service Desk Ticket Classifier
14
+
15
+ Classify IT support tickets into 12 categories using DistilBERT.
16
+
17
+ ## Categories
18
+ - Access Management, Backup, Database, Email
19
+ - General Inquiry, Hardware, Network, Other
20
+ - Printing, Security, Software, Storage
21
+
22
+ ## Model
23
+ - **Base**: DistilBERT (distilbert-base-uncased)
24
+ - **Training**: 5,760 IT support tickets
25
+ - **Architecture**: DistilBERT + Classifier head
app.py ADDED
@@ -0,0 +1,256 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ """
2
+ IT Ticket Classifier - HuggingFace Spaces App
3
+ Gradio interface for classifying IT support tickets
4
+ """
5
+
6
+ import gradio as gr
7
+ import torch
8
+ import torch.nn as nn
9
+ from transformers import DistilBertModel, AutoTokenizer
10
+ from huggingface_hub import hf_hub_download
11
+ import re
12
+ import os
13
+
14
+ # Configuration
15
+ HF_REPO_ID = "TuShar2309/ticket-classifier"
16
+ MODEL_FILENAME = "ticket_classifier.pt"
17
+
18
+ CLASS_NAMES = [
19
+ "Access Management", "Backup", "Database", "Email",
20
+ "General Inquiry", "Hardware", "Network", "Other",
21
+ "Printing", "Security", "Software", "Storage"
22
+ ]
23
+
24
+ # Category descriptions for display
25
+ CATEGORY_INFO = {
26
+ "Access Management": "πŸ” Login, permissions, MFA, account issues",
27
+ "Backup": "πŸ’Ύ Backup and restore operations",
28
+ "Database": "πŸ—„οΈ SQL, database connectivity, queries",
29
+ "Email": "πŸ“§ Outlook, calendar, mailbox issues",
30
+ "General Inquiry": "❓ How-to questions, policies",
31
+ "Hardware": "πŸ’» Laptop, monitor, keyboard, mouse",
32
+ "Network": "🌐 WiFi, VPN, internet connectivity",
33
+ "Other": "πŸ“‹ Miscellaneous requests",
34
+ "Printing": "πŸ–¨οΈ Printers, scanning, print queue",
35
+ "Security": "πŸ”’ Threats, malware, security incidents",
36
+ "Software": "πŸ“¦ Application issues, installations",
37
+ "Storage": "πŸ“ OneDrive, SharePoint, file storage"
38
+ }
39
+
40
+
41
+ class TicketPreprocessor:
42
+ def __init__(self):
43
+ self._email = re.compile(r'\b[\w.-]+@[\w.-]+\.\w+\b')
44
+
45
+ def clean(self, text):
46
+ return ' '.join(self._email.sub('[EMAIL]', str(text or '')).lower().split())
47
+
48
+ def combine(self, subject, description):
49
+ return f"[SUBJECT] {self.clean(subject)} [SEP] [DESCRIPTION] {self.clean(description)}"
50
+
51
+
52
+ class TicketClassifier(nn.Module):
53
+ def __init__(self, num_classes, model_name="distilbert-base-uncased", dropout=0.3):
54
+ super().__init__()
55
+ self.bert = DistilBertModel.from_pretrained(model_name)
56
+ self.classifier = nn.Sequential(
57
+ nn.Dropout(dropout),
58
+ nn.Linear(768, 256),
59
+ nn.GELU(),
60
+ nn.Dropout(dropout),
61
+ nn.Linear(256, num_classes)
62
+ )
63
+
64
+ def forward(self, input_ids, attention_mask):
65
+ outputs = self.bert(input_ids=input_ids, attention_mask=attention_mask)
66
+ return self.classifier(outputs.last_hidden_state[:, 0, :])
67
+
68
+ def predict_proba(self, input_ids, attention_mask):
69
+ logits = self.forward(input_ids, attention_mask)
70
+ return torch.softmax(logits, dim=-1)
71
+
72
+
73
+ # Load model
74
+ print("Loading model...")
75
+ device = "cuda" if torch.cuda.is_available() else "cpu"
76
+ print(f"Device: {device}")
77
+
78
+ try:
79
+ model_path = hf_hub_download(repo_id=HF_REPO_ID, filename=MODEL_FILENAME)
80
+ print(f"Model downloaded: {model_path}")
81
+
82
+ tokenizer = AutoTokenizer.from_pretrained("distilbert-base-uncased")
83
+ model = TicketClassifier(num_classes=len(CLASS_NAMES))
84
+
85
+ checkpoint = torch.load(model_path, map_location=device)
86
+ if 'model_state_dict' in checkpoint:
87
+ model.load_state_dict(checkpoint['model_state_dict'])
88
+ else:
89
+ model.load_state_dict(checkpoint)
90
+
91
+ model.to(device)
92
+ model.eval()
93
+ MODEL_LOADED = True
94
+ print("Model loaded successfully!")
95
+ except Exception as e:
96
+ print(f"Error loading model: {e}")
97
+ MODEL_LOADED = False
98
+
99
+ preprocessor = TicketPreprocessor()
100
+
101
+
102
+ def classify_ticket(subject, description):
103
+ """Classify a ticket and return results."""
104
+ if not subject and not description:
105
+ return "⚠️ Please enter a subject or description", "", ""
106
+
107
+ if not MODEL_LOADED:
108
+ return "❌ Model not loaded", "", ""
109
+
110
+ try:
111
+ # Preprocess and tokenize
112
+ combined = preprocessor.combine(subject, description)
113
+ inputs = tokenizer(
114
+ combined,
115
+ return_tensors="pt",
116
+ truncation=True,
117
+ max_length=256,
118
+ padding='max_length'
119
+ ).to(device)
120
+
121
+ # Predict
122
+ with torch.no_grad():
123
+ probs = model.predict_proba(inputs['input_ids'], inputs['attention_mask'])[0]
124
+
125
+ probs_np = probs.cpu().numpy()
126
+ top_indices = probs_np.argsort()[::-1]
127
+
128
+ # Primary prediction
129
+ primary_idx = top_indices[0]
130
+ primary_cat = CLASS_NAMES[primary_idx]
131
+ primary_conf = probs_np[primary_idx] * 100
132
+
133
+ # Status
134
+ if primary_conf >= 80:
135
+ status = "βœ… **High Confidence** - Auto-route recommended"
136
+ elif primary_conf >= 60:
137
+ status = "⚠️ **Medium Confidence** - Review suggested"
138
+ else:
139
+ status = "πŸ” **Low Confidence** - Human review required"
140
+
141
+ # Format primary result
142
+ primary_result = f"""
143
+ ## {CATEGORY_INFO.get(primary_cat, primary_cat)}
144
+
145
+ ### Predicted Category: **{primary_cat}**
146
+ ### Confidence: **{primary_conf:.1f}%**
147
+
148
+ {status}
149
+ """
150
+
151
+ # Format alternatives
152
+ alternatives = "### Other Possibilities:\n\n"
153
+ for i in range(1, min(4, len(top_indices))):
154
+ idx = top_indices[i]
155
+ cat = CLASS_NAMES[idx]
156
+ conf = probs_np[idx] * 100
157
+ alternatives += f"- **{cat}**: {conf:.1f}%\n"
158
+
159
+ # Confidence bar
160
+ conf_display = f"{'β–ˆ' * int(primary_conf / 5)}{'β–‘' * (20 - int(primary_conf / 5))} {primary_conf:.1f}%"
161
+
162
+ return primary_result, alternatives, conf_display
163
+
164
+ except Exception as e:
165
+ return f"❌ Error: {str(e)}", "", ""
166
+
167
+
168
+ # Example tickets
169
+ examples = [
170
+ ["VPN not connecting", "Cannot connect to corporate VPN from home, getting timeout error"],
171
+ ["Suspicious email received", "Got an email asking for my password, looks like phishing"],
172
+ ["Need SharePoint access", "Just joined the marketing team, need access to the team SharePoint"],
173
+ ["Laptop screen flickering", "My laptop screen has been flickering intermittently since yesterday"],
174
+ ["Outlook not receiving emails", "Haven't received any emails in Outlook for the past 3 hours"],
175
+ ["How to reset password", "What is the process to reset my Active Directory password?"],
176
+ ["Printer not working", "Print jobs stuck in queue and won't print"],
177
+ ["SQL query slow", "Database query that used to take 2 seconds now takes 10 minutes"],
178
+ ]
179
+
180
+
181
+ # Create Gradio interface
182
+ with gr.Blocks(
183
+ title="IT Ticket Classifier",
184
+ theme=gr.themes.Soft(primary_hue="green", secondary_hue="blue"),
185
+ css="""
186
+ .gradio-container { max-width: 900px !important; }
187
+ .primary-result { font-size: 1.2em; }
188
+ """
189
+ ) as demo:
190
+ gr.Markdown("""
191
+ # 🎫 IT Service Desk Ticket Classifier
192
+
193
+ **Powered by DistilBERT** | Classifies tickets into 12 IT support categories
194
+
195
+ Enter a ticket subject and description below to get the predicted category.
196
+ """)
197
+
198
+ with gr.Row():
199
+ with gr.Column(scale=1):
200
+ subject_input = gr.Textbox(
201
+ label="πŸ“‹ Ticket Subject",
202
+ placeholder="e.g., VPN not connecting",
203
+ lines=1
204
+ )
205
+ description_input = gr.Textbox(
206
+ label="πŸ“ Ticket Description",
207
+ placeholder="e.g., Cannot connect to corporate VPN from home, getting timeout error after 30 seconds...",
208
+ lines=4
209
+ )
210
+ classify_btn = gr.Button("πŸ” Classify Ticket", variant="primary", size="lg")
211
+
212
+ with gr.Column(scale=1):
213
+ primary_output = gr.Markdown(label="Primary Prediction")
214
+ confidence_output = gr.Textbox(label="Confidence", interactive=False)
215
+ alternatives_output = gr.Markdown(label="Alternatives")
216
+
217
+ classify_btn.click(
218
+ fn=classify_ticket,
219
+ inputs=[subject_input, description_input],
220
+ outputs=[primary_output, alternatives_output, confidence_output]
221
+ )
222
+
223
+ gr.Examples(
224
+ examples=examples,
225
+ inputs=[subject_input, description_input],
226
+ outputs=[primary_output, alternatives_output, confidence_output],
227
+ fn=classify_ticket,
228
+ cache_examples=False
229
+ )
230
+
231
+ gr.Markdown("""
232
+ ---
233
+ ### πŸ“Š Supported Categories
234
+
235
+ | Category | Description |
236
+ |----------|-------------|
237
+ | Access Management | Login, permissions, MFA |
238
+ | Backup | Backup and restore |
239
+ | Database | SQL, queries, DB issues |
240
+ | Email | Outlook, calendar |
241
+ | General Inquiry | How-to questions |
242
+ | Hardware | Devices, laptops |
243
+ | Network | WiFi, VPN, internet |
244
+ | Other | Miscellaneous |
245
+ | Printing | Printers, scanning |
246
+ | Security | Threats, incidents |
247
+ | Software | Applications |
248
+ | Storage | OneDrive, SharePoint |
249
+
250
+ ---
251
+ **Model**: DistilBERT fine-tuned on 5,760 IT support tickets
252
+ """)
253
+
254
+
255
+ if __name__ == "__main__":
256
+ demo.launch()
requirements.txt ADDED
@@ -0,0 +1,4 @@
 
 
 
 
 
1
+ gradio==4.44.0
2
+ torch==2.1.0
3
+ transformers==4.36.0
4
+ huggingface_hub==0.20.0