File size: 4,203 Bytes
f483b61
b5c64a1
 
 
4ccdef9
 
 
f483b61
b5c64a1
f483b61
 
 
 
4ccdef9
f483b61
 
 
 
4ccdef9
 
f483b61
4ccdef9
f483b61
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
a515aaf
 
f483b61
a515aaf
4ccdef9
f483b61
 
a515aaf
 
f483b61
 
 
 
 
 
 
 
 
 
 
 
 
 
 
4ccdef9
 
 
 
 
f483b61
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import os
os.environ["TF_KERAS"] = "1"  # โœ… Forces Transformers to use TensorFlow-Keras

import streamlit as st
import tensorflow as tf
from transformers import TFDistilBertForSequenceClassification, DistilBertTokenizer
from huggingface_hub import snapshot_download


# โœ… Student Information
My_info = "Student ID: 6319250G, Name: Aung Hlaing Tun"

# โœ… Define Hugging Face Model Repo
MODEL_REPO_ID = "ZAM-ITI-110/Dist_Bert"

# โœ… Load Model & Tokenizer from Hugging Face
@st.cache_resource
def load_model(repo_id):
    """Download and load the TensorFlow model and tokenizer."""

    # ๐Ÿ“Œ Define cache directory
    cache_dir = "hf_models"

    # ๐Ÿ“Œ Ensure directory exists
    os.makedirs(cache_dir, exist_ok=True)

    # ๐Ÿ“Œ Download model from Hugging Face (if not cached)
    try:
        st.write("๐Ÿ”„ Downloading Model from Hugging Face...")
        download_dir = snapshot_download(repo_id, cache_dir=cache_dir, local_files_only=False)
        st.write(f"โœ… Model downloaded at: {download_dir}")

        # ๐Ÿ“Œ Debugging: Print contents of model directory
        st.write("๐Ÿ“‚ Model Directory Contents:", os.listdir(download_dir))

    except Exception as e:
        st.error(f"โŒ Error downloading model: {str(e)}")
        return None, None
        
    # ๐Ÿ“Œ Load Model and Tokenizer (Remove `from_tf=True`)
    try:
        model = TFDistilBertForSequenceClassification.from_pretrained(download_dir)  # โœ… FIX: Removed `from_tf=True`
        tokenizer = DistilBertTokenizer.from_pretrained(download_dir)
    except Exception as e:
        st.error(f"โŒ Error loading model: {str(e)}")
        model, tokenizer = None, None
        
    return model, tokenizer

# โœ… Load Model
st.write("๐Ÿ”„ Loading Model...")
model, tokenizer = load_model(MODEL_REPO_ID)

if model is None or tokenizer is None:
    st.error("โŒ Model loading failed. Please check logs above.")
    st.stop()

st.success("โœ… Model Loaded Successfully!")

# โœ… Prediction Function
def predict_team_and_email(text):
    """Predict the team and corresponding email for a given ticket description."""
    inputs = tokenizer(text, return_tensors="tf", truncation=True, padding=True)
    
    # Perform inference
    preds = model(inputs["input_ids"])
    predicted_label = tf.argmax(preds.logits, axis=1).numpy()[0]

    # Mapping Labels to Team Names and Emails
    label_mapping = {
        0: "Code Review Team",
        1: "Functional Team",
        2: "Infrastructure Team",
        3: "Performance Team",
        4: "Security Team"
    }
    email_mapping = {
        0: "codereview.team@company.com",
        1: "functional.team@company.com",
        2: "infra.team@company.com",
        3: "performance.team@company.com",
        4: "security.team@company.com"
    }
    
    return label_mapping.get(predicted_label, "Unknown"), email_mapping.get(predicted_label, "Unknown")

# โœ… Title and Student Info
st.title("๐Ÿ“ฉ Development of an AI Ticket Classifier Model Using DistilBERT")
st.markdown(f"*{My_info}*")

st.markdown("""
### ๐Ÿ” About this App  
- This system predicts the appropriate **team assignment** and **contact email** based on the ticket description.  
- Simply enter up to **6 ticket descriptions**, and the AI will classify them accordingly.
""")

# โœ… Layout for Ticket Inputs and Predictions
col1, col2, col3 = st.columns(3)

# โœ… Dictionary to store ticket descriptions
ticket_inputs = {}

for i in range(6):  # Up to 6 tickets
    with [col1, col2, col3][i % 3]:  # Distribute across 3 columns
        ticket_inputs[f"ticket_{i+1}"] = st.text_area(f"๐ŸŽŸ๏ธ Ticket {i+1}", placeholder=f"Enter ticket description {i+1}...")

# โœ… Prediction Button
if st.button("๐Ÿ”ฎ Predict All Tickets"):
    st.subheader("๐Ÿ“Œ Prediction Results:")
    for i, (key, text) in enumerate(ticket_inputs.items()):
        if text.strip():
            team, email = predict_team_and_email(text)
            st.success(f"๐ŸŽŸ๏ธ **Ticket {i+1}:** Predicted Team โ†’ {team}, Contact: {email}")
        else:
            st.warning(f"๐Ÿšจ Ticket {i+1} is empty. Please enter a description.")

st.write("๐Ÿ“Œ Enter your ticket descriptions and click **Predict All Tickets** to classify them.")