pranav3108 commited on
Commit
fb84e9f
Β·
verified Β·
1 Parent(s): a685a70

Update app.py

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