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

Update app.py

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