pranav3108 commited on
Commit
f63b67c
·
verified ·
1 Parent(s): 67a3064

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +89 -81
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
- NUM_CLASSES = 4
16
  LABELS = ["Critical", "High", "Medium", "Low"]
17
 
18
- # ===============================
19
- # IMAGE ENCODER (MATCH TRAINING)
20
- # ===============================
21
- def build_image_encoder():
22
- inp = tf.keras.Input(shape=(128,128,3), name="image_input")
23
-
24
- x = tf.keras.layers.Conv2D(32, 3, activation="relu")(inp)
25
- x = tf.keras.layers.MaxPooling2D()(x)
26
- x = tf.keras.layers.Conv2D(64, 3, activation="relu")(x)
27
- x = tf.keras.layers.MaxPooling2D()(x)
28
- x = tf.keras.layers.Conv2D(128, 3, activation="relu")(x)
29
- x = tf.keras.layers.MaxPooling2D()(x)
30
- x = tf.keras.layers.Flatten()(x)
31
- x = tf.keras.layers.Dense(128, activation="relu", name="image_embedding")(x)
32
-
33
- return tf.keras.Model(inp, x)
34
-
35
- # ===============================
36
- # TEXT ENCODER (MATCH TRAINING)
37
- # ===============================
38
- def build_text_encoder(vocab_size=20000):
39
- inp = tf.keras.Input(shape=(MAX_LEN,), name="text_input")
40
-
41
- x = tf.keras.layers.Embedding(vocab_size, 128)(inp)
42
- x = tf.keras.layers.LSTM(64)(x)
43
- x = tf.keras.layers.Dense(64, activation="relu", name="text_embedding")(x)
44
-
45
- return tf.keras.Model(inp, x)
46
-
47
- # ===============================
48
- # FUSION MODEL
49
- # ===============================
50
- image_encoder = build_image_encoder()
51
- text_encoder = build_text_encoder()
52
-
53
- combined = tf.keras.layers.Concatenate()([
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=[image_encoder.input, text_encoder.input],
64
  outputs=output
65
  )
66
 
67
- # ===============================
68
- # LOAD WEIGHTS (SAFE)
69
- # ===============================
70
  fusion_model.load_weights("fusion_weights.weights.h5")
71
  print("✅ Fusion weights loaded")
72
 
73
- # ===============================
74
- # LOAD TOKENIZER
75
- # ===============================
76
- with open("tokenizer.pkl", "rb") as f:
77
- tokenizer = pickle.load(f)
78
-
79
- # ===============================
80
- # PREPROCESS
81
- # ===============================
82
- def preprocess_image(image):
83
- image = image.convert("RGB").resize(IMG_SIZE)
84
- img = np.array(image) / 255.0
85
- return np.expand_dims(img, 0)
86
-
87
- def preprocess_text(text):
88
- seq = tokenizer.texts_to_sequences([text or ""])
89
- return tf.keras.preprocessing.sequence.pad_sequences(seq, maxlen=MAX_LEN)
90
-
91
- # ===============================
92
- # PREDICT
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
- # UI
106
- # ===============================
 
 
 
 
 
 
 
107
  interface = gr.Interface(
108
  fn=predict_ticket,
109
- inputs=[gr.Image(type="pil"), gr.Textbox(lines=3)],
110
- outputs=gr.Label(num_top_classes=4),
111
- title="Ticket Severity Classifier"
 
 
 
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)