pranav3108 commited on
Commit
b2c601f
·
verified ·
1 Parent(s): 67c4c8e

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +74 -22
app.py CHANGED
@@ -6,39 +6,91 @@ import tensorflow as tf
6
  import numpy as np
7
  import pickle
8
  from PIL import Image
9
- import json
10
 
11
- # ----------------------------
12
- # Rebuild model safely (Keras 3 compatible)
13
- # ----------------------------
14
- with open("fusion_config.json", "r") as f:
15
- model_json = f.read()
 
 
16
 
17
- fusion_model = tf.keras.models.model_from_json(model_json)
18
- fusion_model.load_weights("fusion_weights.weights.h5")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
19
 
20
- print("✅ Model rebuilt + weights loaded")
 
 
 
21
 
22
- # ----------------------------
23
- # Load tokenizer
24
- # ----------------------------
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
25
  with open("tokenizer.pkl", "rb") as f:
26
  tokenizer = pickle.load(f)
27
 
28
- # ----------------------------
29
- IMG_SIZE = (128, 128)
30
- MAX_LEN = 50
31
- LABELS = ["Critical", "High", "Medium", "Low"]
32
-
33
  def preprocess_image(image):
34
  image = image.convert("RGB").resize(IMG_SIZE)
35
- img = np.array(image, dtype=np.float32) / 255.0
36
  return np.expand_dims(img, 0)
37
 
38
  def preprocess_text(text):
39
  seq = tokenizer.texts_to_sequences([text or ""])
40
  return tf.keras.preprocessing.sequence.pad_sequences(seq, maxlen=MAX_LEN)
41
 
 
 
 
42
  def predict_ticket(image, text):
43
  if image is None:
44
  return {}
@@ -49,12 +101,12 @@ def predict_ticket(image, text):
49
  probs = fusion_model.predict([img, txt], verbose=0)[0]
50
  return dict(zip(LABELS, map(float, probs)))
51
 
 
 
 
52
  interface = gr.Interface(
53
  fn=predict_ticket,
54
- inputs=[
55
- gr.Image(type="pil"),
56
- gr.Textbox(lines=3)
57
- ],
58
  outputs=gr.Label(num_top_classes=4),
59
  title="Ticket Severity Classifier"
60
  )
 
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 {}
 
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
  )