Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -2,6 +2,7 @@
|
|
| 2 |
# ENVIRONMENT (MUST BE FIRST)
|
| 3 |
# ================================
|
| 4 |
import os
|
|
|
|
| 5 |
os.environ["TF_ENABLE_ONEDNN_OPTS"] = "0"
|
| 6 |
|
| 7 |
# ================================
|
|
@@ -12,33 +13,34 @@ import tensorflow as tf
|
|
| 12 |
import numpy as np
|
| 13 |
import json
|
| 14 |
from PIL import Image
|
| 15 |
-
from
|
| 16 |
-
from tensorflow.keras.preprocessing.sequence import pad_sequences
|
| 17 |
|
| 18 |
# ================================
|
| 19 |
-
#
|
| 20 |
# ================================
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 24 |
|
| 25 |
# ================================
|
| 26 |
-
# LOAD TOKENIZER (JSON
|
| 27 |
# ================================
|
| 28 |
-
with open("tokenizer.json", "r") as f:
|
| 29 |
tokenizer = tokenizer_from_json(json.load(f))
|
| 30 |
|
| 31 |
print("β
Tokenizer loaded")
|
| 32 |
|
| 33 |
# ================================
|
| 34 |
-
#
|
| 35 |
# ================================
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
)
|
| 40 |
-
|
| 41 |
-
print("β
Fusion model loaded")
|
| 42 |
|
| 43 |
# ================================
|
| 44 |
# IMAGE PREPROCESSING
|
|
@@ -47,21 +49,21 @@ def preprocess_image(image: Image.Image):
|
|
| 47 |
image = image.convert("RGB")
|
| 48 |
image = image.resize(IMG_SIZE)
|
| 49 |
img = np.array(image, dtype=np.float32) / 255.0
|
| 50 |
-
|
| 51 |
-
return img
|
| 52 |
|
| 53 |
# ================================
|
| 54 |
# TEXT PREPROCESSING
|
| 55 |
# ================================
|
| 56 |
-
def preprocess_text(text
|
| 57 |
-
if text
|
| 58 |
text = ""
|
| 59 |
seq = tokenizer.texts_to_sequences([text])
|
| 60 |
-
|
| 61 |
-
|
|
|
|
| 62 |
|
| 63 |
# ================================
|
| 64 |
-
# PREDICTION FUNCTION
|
| 65 |
# ================================
|
| 66 |
def predict_ticket(image, text):
|
| 67 |
if image is None:
|
|
@@ -69,44 +71,40 @@ def predict_ticket(image, text):
|
|
| 69 |
"Critical": 0.0,
|
| 70 |
"High": 0.0,
|
| 71 |
"Medium": 0.0,
|
| 72 |
-
"Low": 0.0
|
| 73 |
}
|
| 74 |
|
| 75 |
img = preprocess_image(image)
|
| 76 |
txt = preprocess_text(text)
|
| 77 |
|
| 78 |
-
|
| 79 |
|
| 80 |
return {
|
| 81 |
-
"Critical": float(
|
| 82 |
-
"High": float(
|
| 83 |
-
"Medium": float(
|
| 84 |
-
"Low": float(
|
| 85 |
}
|
| 86 |
|
| 87 |
# ================================
|
| 88 |
-
# GRADIO
|
| 89 |
# ================================
|
| 90 |
-
|
| 91 |
fn=predict_ticket,
|
| 92 |
inputs=[
|
| 93 |
gr.Image(type="pil", label="π€ Upload Ticket Screenshot"),
|
| 94 |
gr.Textbox(
|
| 95 |
lines=4,
|
| 96 |
placeholder="Describe the issue (recommended)",
|
| 97 |
-
label="βοΈ Ticket Description"
|
| 98 |
-
)
|
| 99 |
],
|
| 100 |
-
outputs=gr.Label(
|
| 101 |
-
num_top_classes=4,
|
| 102 |
-
label="π¨ Predicted Severity"
|
| 103 |
-
),
|
| 104 |
title="π« Ticket Severity Classification System",
|
| 105 |
description=(
|
| 106 |
-
"
|
| 107 |
-
"ticket screenshots and descriptions.\n\n"
|
| 108 |
"**Classes:** Critical | High | Medium | Low"
|
| 109 |
),
|
| 110 |
)
|
| 111 |
|
| 112 |
-
|
|
|
|
| 2 |
# ENVIRONMENT (MUST BE FIRST)
|
| 3 |
# ================================
|
| 4 |
import os
|
| 5 |
+
os.environ["KERAS_BACKEND"] = "tensorflow"
|
| 6 |
os.environ["TF_ENABLE_ONEDNN_OPTS"] = "0"
|
| 7 |
|
| 8 |
# ================================
|
|
|
|
| 13 |
import numpy as np
|
| 14 |
import json
|
| 15 |
from PIL import Image
|
| 16 |
+
from keras.preprocessing.text import tokenizer_from_json
|
|
|
|
| 17 |
|
| 18 |
# ================================
|
| 19 |
+
# LOAD MODEL (KERAS 3 SAFE)
|
| 20 |
# ================================
|
| 21 |
+
MODEL_PATH = "fusion_ticket_model_final.keras"
|
| 22 |
+
|
| 23 |
+
model = tf.keras.models.load_model(
|
| 24 |
+
MODEL_PATH,
|
| 25 |
+
compile=False
|
| 26 |
+
)
|
| 27 |
+
|
| 28 |
+
print("β
Model loaded")
|
| 29 |
|
| 30 |
# ================================
|
| 31 |
+
# LOAD TOKENIZER (JSON ONLY)
|
| 32 |
# ================================
|
| 33 |
+
with open("tokenizer.json", "r", encoding="utf-8") as f:
|
| 34 |
tokenizer = tokenizer_from_json(json.load(f))
|
| 35 |
|
| 36 |
print("β
Tokenizer loaded")
|
| 37 |
|
| 38 |
# ================================
|
| 39 |
+
# CONSTANTS (MUST MATCH TRAINING)
|
| 40 |
# ================================
|
| 41 |
+
IMG_SIZE = (128, 128)
|
| 42 |
+
MAX_LEN = 50
|
| 43 |
+
LABELS = ["Critical", "High", "Medium", "Low"]
|
|
|
|
|
|
|
|
|
|
| 44 |
|
| 45 |
# ================================
|
| 46 |
# IMAGE PREPROCESSING
|
|
|
|
| 49 |
image = image.convert("RGB")
|
| 50 |
image = image.resize(IMG_SIZE)
|
| 51 |
img = np.array(image, dtype=np.float32) / 255.0
|
| 52 |
+
return np.expand_dims(img, axis=0)
|
|
|
|
| 53 |
|
| 54 |
# ================================
|
| 55 |
# TEXT PREPROCESSING
|
| 56 |
# ================================
|
| 57 |
+
def preprocess_text(text):
|
| 58 |
+
if not text:
|
| 59 |
text = ""
|
| 60 |
seq = tokenizer.texts_to_sequences([text])
|
| 61 |
+
return tf.keras.preprocessing.sequence.pad_sequences(
|
| 62 |
+
seq, maxlen=MAX_LEN
|
| 63 |
+
)
|
| 64 |
|
| 65 |
# ================================
|
| 66 |
+
# PREDICTION FUNCTION
|
| 67 |
# ================================
|
| 68 |
def predict_ticket(image, text):
|
| 69 |
if image is None:
|
|
|
|
| 71 |
"Critical": 0.0,
|
| 72 |
"High": 0.0,
|
| 73 |
"Medium": 0.0,
|
| 74 |
+
"Low": 0.0,
|
| 75 |
}
|
| 76 |
|
| 77 |
img = preprocess_image(image)
|
| 78 |
txt = preprocess_text(text)
|
| 79 |
|
| 80 |
+
preds = model.predict([img, txt], verbose=0)[0]
|
| 81 |
|
| 82 |
return {
|
| 83 |
+
"Critical": float(preds[0]),
|
| 84 |
+
"High": float(preds[1]),
|
| 85 |
+
"Medium": float(preds[2]),
|
| 86 |
+
"Low": float(preds[3]),
|
| 87 |
}
|
| 88 |
|
| 89 |
# ================================
|
| 90 |
+
# GRADIO APP
|
| 91 |
# ================================
|
| 92 |
+
app = gr.Interface(
|
| 93 |
fn=predict_ticket,
|
| 94 |
inputs=[
|
| 95 |
gr.Image(type="pil", label="π€ Upload Ticket Screenshot"),
|
| 96 |
gr.Textbox(
|
| 97 |
lines=4,
|
| 98 |
placeholder="Describe the issue (recommended)",
|
| 99 |
+
label="βοΈ Ticket Description",
|
| 100 |
+
),
|
| 101 |
],
|
| 102 |
+
outputs=gr.Label(num_top_classes=4, label="π¨ Predicted Severity"),
|
|
|
|
|
|
|
|
|
|
| 103 |
title="π« Ticket Severity Classification System",
|
| 104 |
description=(
|
| 105 |
+
"CNN + NLP Fusion Model to predict ticket urgency.\n\n"
|
|
|
|
| 106 |
"**Classes:** Critical | High | Medium | Low"
|
| 107 |
),
|
| 108 |
)
|
| 109 |
|
| 110 |
+
app.launch(server_name="0.0.0.0", server_port=7860)
|