pranav3108 commited on
Commit
cebaf48
Β·
verified Β·
1 Parent(s): 004d001

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +55 -35
app.py CHANGED
@@ -1,63 +1,76 @@
 
 
 
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
- # LOAD MODEL
12
- # ======================
 
 
 
 
 
 
 
13
  model = tf.keras.models.load_model(
14
- "fusion_ticket_model_final.keras",
15
  compile=False
16
  )
17
-
18
  print("βœ… Fusion model loaded")
19
 
20
- # ======================
21
  # LOAD TOKENIZER
22
- # ======================
23
  with open("tokenizer.pkl", "rb") as f:
24
  tokenizer = pickle.load(f)
25
 
26
  print("βœ… Tokenizer loaded")
27
 
28
- # ======================
29
- # CONSTANTS
30
- # ======================
31
- IMG_SIZE = (128, 128)
32
- MAX_LEN = 50
33
- LABELS = ["Critical", "High", "Medium", "Low"]
34
-
35
- # ======================
36
- # PREPROCESS IMAGE
37
- # ======================
38
  def preprocess_image(image: Image.Image):
39
  image = image.convert("RGB")
40
  image = image.resize(IMG_SIZE)
41
  img = np.array(image, dtype=np.float32) / 255.0
42
- return np.expand_dims(img, axis=0)
 
43
 
44
- # ======================
45
- # PREPROCESS TEXT
46
- # ======================
47
- def preprocess_text(text):
48
  if text is None:
49
  text = ""
50
  seq = tokenizer.texts_to_sequences([text])
51
- return tf.keras.preprocessing.sequence.pad_sequences(
52
  seq, maxlen=MAX_LEN
53
  )
 
54
 
55
- # ======================
56
- # PREDICTION
57
- # ======================
58
  def predict_ticket(image, text):
59
  if image is None:
60
- return {"Critical": 0, "High": 0, "Medium": 0, "Low": 0}
 
 
 
 
 
61
 
62
  img = preprocess_image(image)
63
  txt = preprocess_text(text)
@@ -71,18 +84,25 @@ def predict_ticket(image, text):
71
  "Low": float(probs[3]),
72
  }
73
 
74
- # ======================
75
  # GRADIO UI
76
- # ======================
77
  interface = gr.Interface(
78
  fn=predict_ticket,
79
  inputs=[
80
  gr.Image(type="pil", label="πŸ“€ Upload Ticket Screenshot"),
81
- gr.Textbox(lines=4, label="✍️ Ticket Description")
 
 
 
 
82
  ],
83
- outputs=gr.Label(num_top_classes=4, label="🚨 Severity"),
84
- title="🎫 Multimodal Ticket Severity Classifier",
85
- description="CNN + LSTM Fusion Model"
 
 
 
86
  )
87
 
88
- interface.launch()
 
1
+ # ================================
2
+ # ENVIRONMENT (MUST BE FIRST)
3
+ # ================================
4
  import os
5
+ os.environ["KERAS_BACKEND"] = "tensorflow"
6
+ os.environ["TF_CPP_MIN_LOG_LEVEL"] = "2"
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
+ LABELS = ["Critical", "High", "Medium", "Low"]
23
+
24
+ # ================================
25
+ # LOAD MODEL (KERAS 3 SAFE)
26
+ # ================================
27
  model = tf.keras.models.load_model(
28
+ "fusion_model_keras3.keras",
29
  compile=False
30
  )
 
31
  print("βœ… Fusion model loaded")
32
 
33
+ # ================================
34
  # LOAD TOKENIZER
35
+ # ================================
36
  with open("tokenizer.pkl", "rb") as f:
37
  tokenizer = pickle.load(f)
38
 
39
  print("βœ… Tokenizer loaded")
40
 
41
+ # ================================
42
+ # IMAGE PREPROCESS
43
+ # ================================
 
 
 
 
 
 
 
44
  def preprocess_image(image: Image.Image):
45
  image = image.convert("RGB")
46
  image = image.resize(IMG_SIZE)
47
  img = np.array(image, dtype=np.float32) / 255.0
48
+ img = np.expand_dims(img, axis=0)
49
+ return img
50
 
51
+ # ================================
52
+ # TEXT PREPROCESS
53
+ # ================================
54
+ def preprocess_text(text: str):
55
  if text is None:
56
  text = ""
57
  seq = tokenizer.texts_to_sequences([text])
58
+ padded = tf.keras.preprocessing.sequence.pad_sequences(
59
  seq, maxlen=MAX_LEN
60
  )
61
+ return padded
62
 
63
+ # ================================
64
+ # PREDICTION FUNCTION
65
+ # ================================
66
  def predict_ticket(image, text):
67
  if image is None:
68
+ return {
69
+ "Critical": 0.0,
70
+ "High": 0.0,
71
+ "Medium": 0.0,
72
+ "Low": 0.0,
73
+ }
74
 
75
  img = preprocess_image(image)
76
  txt = preprocess_text(text)
 
84
  "Low": float(probs[3]),
85
  }
86
 
87
+ # ================================
88
  # GRADIO UI
89
+ # ================================
90
  interface = gr.Interface(
91
  fn=predict_ticket,
92
  inputs=[
93
  gr.Image(type="pil", label="πŸ“€ Upload Ticket Screenshot"),
94
+ gr.Textbox(
95
+ lines=4,
96
+ placeholder="Describe the issue (recommended)",
97
+ label="✍️ Ticket Description"
98
+ )
99
  ],
100
+ outputs=gr.Label(num_top_classes=4, label="🚨 Predicted Severity"),
101
+ title="🎫 Ticket Severity Classification",
102
+ description=(
103
+ "CNN + NLP **Fusion Model** for ticket urgency detection.\n\n"
104
+ "**Classes:** Critical | High | Medium | Low"
105
+ )
106
  )
107
 
108
+ interface.launch()