pranav3108 commited on
Commit
45a038c
Β·
verified Β·
1 Parent(s): 55dcc13

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +52 -52
app.py CHANGED
@@ -11,92 +11,76 @@ os.environ["TF_ENABLE_ONEDNN_OPTS"] = "0"
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 {
@@ -113,10 +97,26 @@ 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)
 
11
  import gradio as gr
12
  import tensorflow as tf
13
  import numpy as np
 
14
  from PIL import Image
15
+ from tensorflow.keras.preprocessing.text import tokenizer_from_json
16
 
17
+ print("βœ… Imports loaded")
 
 
 
 
 
 
18
 
19
  # ================================
20
+ # LOAD FUSION MODEL (KERAS 3 SAFE)
21
  # ================================
22
+ MODEL_PATH = "fusion_ticket_model_final.keras"
 
23
 
24
+ fusion_model = tf.keras.models.load_model(
25
+ MODEL_PATH,
26
+ compile=False
27
+ )
28
+
29
+ print("βœ… Fusion model loaded")
30
 
31
  # ================================
32
+ # LOAD TOKENIZER (JSON, NOT PICKLE)
33
  # ================================
34
+ with open("tokenizer.json", "r") as f:
35
+ tokenizer = tokenizer_from_json(f.read())
36
 
37
+ print("βœ… Tokenizer loaded")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
38
 
39
  # ================================
40
+ # CONSTANTS (MUST MATCH TRAINING)
41
  # ================================
42
+ IMG_SIZE = (128, 128)
43
+ MAX_LEN = 50
44
+ LABELS = ["Critical", "High", "Medium", "Low"]
45
 
46
  # ================================
47
+ # IMAGE PREPROCESSING
48
  # ================================
49
  def preprocess_image(image: Image.Image):
50
  image = image.convert("RGB")
51
  image = image.resize(IMG_SIZE)
52
  img = np.array(image, dtype=np.float32) / 255.0
53
+ img = np.expand_dims(img, axis=0)
54
+ return img
55
 
56
+ # ================================
57
+ # TEXT PREPROCESSING
58
+ # ================================
59
  def preprocess_text(text: str):
60
  if text is None:
61
  text = ""
62
  seq = tokenizer.texts_to_sequences([text])
63
+ padded = tf.keras.preprocessing.sequence.pad_sequences(
64
  seq, maxlen=MAX_LEN
65
  )
66
+ return padded
67
 
68
  # ================================
69
  # PREDICTION FUNCTION
70
  # ================================
71
  def predict_ticket(image, text):
72
  if image is None:
73
+ return {
74
+ "Critical": 0.0,
75
+ "High": 0.0,
76
+ "Medium": 0.0,
77
+ "Low": 0.0,
78
+ }
79
 
80
  img = preprocess_image(image)
81
  txt = preprocess_text(text)
82
 
83
+ # IMPORTANT: input order MUST match training
84
  probs = fusion_model.predict([img, txt], verbose=0)[0]
85
 
86
  return {
 
97
  fn=predict_ticket,
98
  inputs=[
99
  gr.Image(type="pil", label="πŸ“€ Upload Ticket Screenshot"),
100
+ gr.Textbox(
101
+ lines=4,
102
+ placeholder="Describe the issue (optional but recommended)",
103
+ label="✍️ Ticket Description"
104
+ )
105
  ],
106
+ outputs=gr.Label(
107
+ num_top_classes=4,
108
+ label="🚨 Predicted Ticket Severity"
109
+ ),
110
+ title="🎫 Ticket Severity Classification System",
111
+ description=(
112
+ "This system uses a **CNN + NLP Fusion Model** trained on real ticket screenshots "
113
+ "and descriptions to predict ticket urgency.\n\n"
114
+ "**Severity Levels:** Critical | High | Medium | Low"
115
+ ),
116
+ allow_flagging="never"
117
  )
118
 
119
+ # ================================
120
+ # LAUNCH (HF SPACES SAFE)
121
+ # ================================
122
  interface.launch(server_name="0.0.0.0", server_port=7860)