Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,10 +1,3 @@
|
|
| 1 |
-
# ================================
|
| 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 |
# ================================
|
| 9 |
# IMPORTS
|
| 10 |
# ================================
|
|
@@ -12,14 +5,14 @@ import gradio as gr
|
|
| 12 |
import tensorflow as tf
|
| 13 |
import numpy as np
|
| 14 |
import json
|
| 15 |
-
from PIL import Image
|
| 16 |
from tensorflow.keras.preprocessing.text import tokenizer_from_json
|
|
|
|
| 17 |
|
| 18 |
# ================================
|
| 19 |
-
# LOAD MODEL (.
|
| 20 |
# ================================
|
| 21 |
-
|
| 22 |
-
"
|
| 23 |
compile=False
|
| 24 |
)
|
| 25 |
|
|
@@ -29,12 +22,12 @@ print("β
Fusion model loaded")
|
|
| 29 |
# LOAD TOKENIZER (JSON β SAFE)
|
| 30 |
# ================================
|
| 31 |
with open("tokenizer.json", "r") as f:
|
| 32 |
-
tokenizer = tokenizer_from_json(
|
| 33 |
|
| 34 |
print("β
Tokenizer loaded")
|
| 35 |
|
| 36 |
# ================================
|
| 37 |
-
# CONSTANTS (
|
| 38 |
# ================================
|
| 39 |
IMG_SIZE = (128, 128)
|
| 40 |
MAX_LEN = 50
|
|
@@ -47,7 +40,8 @@ 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 |
|
| 52 |
# ================================
|
| 53 |
# TEXT PREPROCESSING
|
|
@@ -56,9 +50,10 @@ def preprocess_text(text):
|
|
| 56 |
if text is None:
|
| 57 |
text = ""
|
| 58 |
seq = tokenizer.texts_to_sequences([text])
|
| 59 |
-
|
| 60 |
seq, maxlen=MAX_LEN
|
| 61 |
)
|
|
|
|
| 62 |
|
| 63 |
# ================================
|
| 64 |
# PREDICTION FUNCTION
|
|
@@ -75,7 +70,7 @@ def predict_ticket(image, text):
|
|
| 75 |
img = preprocess_image(image)
|
| 76 |
txt = preprocess_text(text)
|
| 77 |
|
| 78 |
-
probs =
|
| 79 |
|
| 80 |
return {
|
| 81 |
"Critical": float(probs[0]),
|
|
@@ -93,15 +88,16 @@ interface = gr.Interface(
|
|
| 93 |
gr.Image(type="pil", label="π€ Upload Ticket Screenshot"),
|
| 94 |
gr.Textbox(
|
| 95 |
lines=4,
|
| 96 |
-
placeholder="Describe the issue (
|
| 97 |
label="βοΈ Ticket Description"
|
| 98 |
)
|
| 99 |
],
|
| 100 |
outputs=gr.Label(num_top_classes=4, label="π¨ Predicted Severity"),
|
| 101 |
title="π« Ticket Severity Classification System",
|
| 102 |
description=(
|
| 103 |
-
"CNN + NLP
|
| 104 |
-
"
|
|
|
|
| 105 |
)
|
| 106 |
)
|
| 107 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
# ================================
|
| 2 |
# IMPORTS
|
| 3 |
# ================================
|
|
|
|
| 5 |
import tensorflow as tf
|
| 6 |
import numpy as np
|
| 7 |
import json
|
|
|
|
| 8 |
from tensorflow.keras.preprocessing.text import tokenizer_from_json
|
| 9 |
+
from PIL import Image
|
| 10 |
|
| 11 |
# ================================
|
| 12 |
+
# LOAD MODEL (TF 2.15 SAFE)
|
| 13 |
# ================================
|
| 14 |
+
model = tf.keras.models.load_model(
|
| 15 |
+
"fusion_ticket_model_tf215.h5",
|
| 16 |
compile=False
|
| 17 |
)
|
| 18 |
|
|
|
|
| 22 |
# LOAD TOKENIZER (JSON β SAFE)
|
| 23 |
# ================================
|
| 24 |
with open("tokenizer.json", "r") as f:
|
| 25 |
+
tokenizer = tokenizer_from_json(json.load(f))
|
| 26 |
|
| 27 |
print("β
Tokenizer loaded")
|
| 28 |
|
| 29 |
# ================================
|
| 30 |
+
# CONSTANTS (MATCH TRAINING)
|
| 31 |
# ================================
|
| 32 |
IMG_SIZE = (128, 128)
|
| 33 |
MAX_LEN = 50
|
|
|
|
| 40 |
image = image.convert("RGB")
|
| 41 |
image = image.resize(IMG_SIZE)
|
| 42 |
img = np.array(image, dtype=np.float32) / 255.0
|
| 43 |
+
img = np.expand_dims(img, axis=0)
|
| 44 |
+
return img
|
| 45 |
|
| 46 |
# ================================
|
| 47 |
# TEXT PREPROCESSING
|
|
|
|
| 50 |
if text is None:
|
| 51 |
text = ""
|
| 52 |
seq = tokenizer.texts_to_sequences([text])
|
| 53 |
+
padded = tf.keras.preprocessing.sequence.pad_sequences(
|
| 54 |
seq, maxlen=MAX_LEN
|
| 55 |
)
|
| 56 |
+
return padded
|
| 57 |
|
| 58 |
# ================================
|
| 59 |
# PREDICTION FUNCTION
|
|
|
|
| 70 |
img = preprocess_image(image)
|
| 71 |
txt = preprocess_text(text)
|
| 72 |
|
| 73 |
+
probs = model.predict([img, txt], verbose=0)[0]
|
| 74 |
|
| 75 |
return {
|
| 76 |
"Critical": float(probs[0]),
|
|
|
|
| 88 |
gr.Image(type="pil", label="π€ Upload Ticket Screenshot"),
|
| 89 |
gr.Textbox(
|
| 90 |
lines=4,
|
| 91 |
+
placeholder="Describe the issue (optional but improves accuracy)",
|
| 92 |
label="βοΈ Ticket Description"
|
| 93 |
)
|
| 94 |
],
|
| 95 |
outputs=gr.Label(num_top_classes=4, label="π¨ Predicted Severity"),
|
| 96 |
title="π« Ticket Severity Classification System",
|
| 97 |
description=(
|
| 98 |
+
"This system uses a **CNN + NLP Fusion Model** trained on "
|
| 99 |
+
"ticket screenshots and descriptions.\n\n"
|
| 100 |
+
"**Severity Levels:** Critical | High | Medium | Low"
|
| 101 |
)
|
| 102 |
)
|
| 103 |
|