Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,114 +1,122 @@
|
|
|
|
|
|
|
|
|
|
|
| 1 |
import os
|
|
|
|
| 2 |
os.environ["TF_ENABLE_ONEDNN_OPTS"] = "0"
|
| 3 |
|
|
|
|
|
|
|
|
|
|
| 4 |
import gradio as gr
|
| 5 |
import tensorflow as tf
|
| 6 |
import numpy as np
|
| 7 |
import pickle
|
| 8 |
from PIL import Image
|
| 9 |
|
| 10 |
-
#
|
| 11 |
-
# CONSTANTS
|
| 12 |
-
#
|
| 13 |
IMG_SIZE = (128, 128)
|
| 14 |
MAX_LEN = 50
|
| 15 |
-
|
| 16 |
LABELS = ["Critical", "High", "Medium", "Low"]
|
| 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 |
-
image_encoder.output,
|
| 55 |
-
text_encoder.output
|
| 56 |
-
])
|
| 57 |
-
|
| 58 |
-
x = tf.keras.layers.Dense(128, activation="relu")(combined)
|
| 59 |
-
x = tf.keras.layers.Dense(64, activation="relu")(x)
|
| 60 |
-
output = tf.keras.layers.Dense(NUM_CLASSES, activation="softmax")(x)
|
| 61 |
|
| 62 |
fusion_model = tf.keras.Model(
|
| 63 |
-
inputs=[
|
| 64 |
outputs=output
|
| 65 |
)
|
| 66 |
|
| 67 |
-
#
|
| 68 |
-
# LOAD WEIGHTS (
|
| 69 |
-
#
|
| 70 |
fusion_model.load_weights("fusion_weights.weights.h5")
|
| 71 |
print("✅ Fusion weights loaded")
|
| 72 |
|
| 73 |
-
#
|
| 74 |
-
#
|
| 75 |
-
#
|
| 76 |
-
|
| 77 |
-
|
| 78 |
-
|
| 79 |
-
|
| 80 |
-
|
| 81 |
-
|
| 82 |
-
def
|
| 83 |
-
|
| 84 |
-
|
| 85 |
-
|
| 86 |
-
|
| 87 |
-
|
| 88 |
-
|
| 89 |
-
|
| 90 |
-
|
| 91 |
-
#
|
| 92 |
-
#
|
| 93 |
-
# ===============================
|
| 94 |
def predict_ticket(image, text):
|
| 95 |
if image is None:
|
| 96 |
-
return {}
|
| 97 |
|
| 98 |
img = preprocess_image(image)
|
| 99 |
txt = preprocess_text(text)
|
| 100 |
|
| 101 |
probs = fusion_model.predict([img, txt], verbose=0)[0]
|
| 102 |
-
return dict(zip(LABELS, map(float, probs)))
|
| 103 |
|
| 104 |
-
|
| 105 |
-
|
| 106 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 107 |
interface = gr.Interface(
|
| 108 |
fn=predict_ticket,
|
| 109 |
-
inputs=[
|
| 110 |
-
|
| 111 |
-
|
|
|
|
|
|
|
|
|
|
| 112 |
)
|
| 113 |
|
| 114 |
-
interface.launch()
|
|
|
|
| 1 |
+
# ================================
|
| 2 |
+
# ENVIRONMENT FIXES (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 |
+
# ================================
|
| 11 |
import gradio as gr
|
| 12 |
import tensorflow as tf
|
| 13 |
import numpy as np
|
| 14 |
import pickle
|
| 15 |
from PIL import Image
|
| 16 |
|
| 17 |
+
# ================================
|
| 18 |
+
# CONSTANTS (MUST MATCH TRAINING)
|
| 19 |
+
# ================================
|
| 20 |
IMG_SIZE = (128, 128)
|
| 21 |
MAX_LEN = 50
|
| 22 |
+
MAX_WORDS = 20000
|
| 23 |
LABELS = ["Critical", "High", "Medium", "Low"]
|
| 24 |
|
| 25 |
+
# ================================
|
| 26 |
+
# LOAD TOKENIZER
|
| 27 |
+
# ================================
|
| 28 |
+
with open("tokenizer.pkl", "rb") as f:
|
| 29 |
+
tokenizer = pickle.load(f)
|
| 30 |
+
|
| 31 |
+
print("✅ Tokenizer loaded")
|
| 32 |
+
|
| 33 |
+
# ================================
|
| 34 |
+
# BUILD FUSION MODEL ARCHITECTURE
|
| 35 |
+
# ================================
|
| 36 |
+
|
| 37 |
+
# ---- Image encoder ----
|
| 38 |
+
image_input = tf.keras.Input(shape=(128, 128, 3), name="image_input")
|
| 39 |
+
|
| 40 |
+
x = tf.keras.layers.Conv2D(32, (3, 3), activation="relu")(image_input)
|
| 41 |
+
x = tf.keras.layers.MaxPooling2D((2, 2))(x)
|
| 42 |
+
x = tf.keras.layers.Conv2D(64, (3, 3), activation="relu")(x)
|
| 43 |
+
x = tf.keras.layers.MaxPooling2D((2, 2))(x)
|
| 44 |
+
x = tf.keras.layers.Conv2D(128, (3, 3), activation="relu")(x)
|
| 45 |
+
x = tf.keras.layers.MaxPooling2D((2, 2))(x)
|
| 46 |
+
x = tf.keras.layers.Flatten()(x)
|
| 47 |
+
image_features = tf.keras.layers.Dense(128, activation="relu")(x)
|
| 48 |
+
|
| 49 |
+
# ---- Text encoder ----
|
| 50 |
+
text_input = tf.keras.Input(shape=(MAX_LEN,), name="text_input")
|
| 51 |
+
|
| 52 |
+
y = tf.keras.layers.Embedding(MAX_WORDS, 128)(text_input)
|
| 53 |
+
y = tf.keras.layers.LSTM(64)(y)
|
| 54 |
+
text_features = tf.keras.layers.Dense(64, activation="relu")(y)
|
| 55 |
+
|
| 56 |
+
# ---- Fusion head ----
|
| 57 |
+
combined = tf.keras.layers.Concatenate()([image_features, text_features])
|
| 58 |
+
z = tf.keras.layers.Dense(128, activation="relu")(combined)
|
| 59 |
+
z = tf.keras.layers.Dense(64, activation="relu")(z)
|
| 60 |
+
output = tf.keras.layers.Dense(4, activation="softmax")(z)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 61 |
|
| 62 |
fusion_model = tf.keras.Model(
|
| 63 |
+
inputs=[image_input, text_input],
|
| 64 |
outputs=output
|
| 65 |
)
|
| 66 |
|
| 67 |
+
# ================================
|
| 68 |
+
# LOAD WEIGHTS (THIS IS THE KEY)
|
| 69 |
+
# ================================
|
| 70 |
fusion_model.load_weights("fusion_weights.weights.h5")
|
| 71 |
print("✅ Fusion weights loaded")
|
| 72 |
|
| 73 |
+
# ================================
|
| 74 |
+
# PREPROCESSING
|
| 75 |
+
# ================================
|
| 76 |
+
def preprocess_image(image: Image.Image):
|
| 77 |
+
image = image.convert("RGB")
|
| 78 |
+
image = image.resize(IMG_SIZE)
|
| 79 |
+
img = np.array(image, dtype=np.float32) / 255.0
|
| 80 |
+
return np.expand_dims(img, axis=0)
|
| 81 |
+
|
| 82 |
+
def preprocess_text(text: str):
|
| 83 |
+
if text is None:
|
| 84 |
+
text = ""
|
| 85 |
+
seq = tokenizer.texts_to_sequences([text])
|
| 86 |
+
return tf.keras.preprocessing.sequence.pad_sequences(
|
| 87 |
+
seq, maxlen=MAX_LEN
|
| 88 |
+
)
|
| 89 |
+
|
| 90 |
+
# ================================
|
| 91 |
+
# PREDICTION FUNCTION
|
| 92 |
+
# ================================
|
|
|
|
| 93 |
def predict_ticket(image, text):
|
| 94 |
if image is None:
|
| 95 |
+
return {"Critical": 0, "High": 0, "Medium": 0, "Low": 0}
|
| 96 |
|
| 97 |
img = preprocess_image(image)
|
| 98 |
txt = preprocess_text(text)
|
| 99 |
|
| 100 |
probs = fusion_model.predict([img, txt], verbose=0)[0]
|
|
|
|
| 101 |
|
| 102 |
+
return {
|
| 103 |
+
"Critical": float(probs[0]),
|
| 104 |
+
"High": float(probs[1]),
|
| 105 |
+
"Medium": float(probs[2]),
|
| 106 |
+
"Low": float(probs[3]),
|
| 107 |
+
}
|
| 108 |
+
|
| 109 |
+
# ================================
|
| 110 |
+
# GRADIO UI
|
| 111 |
+
# ================================
|
| 112 |
interface = gr.Interface(
|
| 113 |
fn=predict_ticket,
|
| 114 |
+
inputs=[
|
| 115 |
+
gr.Image(type="pil", label="📤 Upload Ticket Screenshot"),
|
| 116 |
+
gr.Textbox(lines=4, label="✍️ Ticket Description")
|
| 117 |
+
],
|
| 118 |
+
outputs=gr.Label(num_top_classes=4, label="🚨 Predicted Severity"),
|
| 119 |
+
title="🎫 Ticket Severity Classification",
|
| 120 |
)
|
| 121 |
|
| 122 |
+
interface.launch(server_name="0.0.0.0", server_port=7860)
|