farouk04 commited on
Commit
ca898cf
Β·
verified Β·
1 Parent(s): bb7985c

Upload folder using huggingface_hub

Browse files
Files changed (1) hide show
  1. app.py +29 -37
app.py CHANGED
@@ -126,45 +126,41 @@ print("All models ready.")
126
  # ---------------------------------------------------------------------------
127
  # Prediction helper
128
  # ---------------------------------------------------------------------------
129
- LABEL_MAP = {0: "βœ… Ham (not spam)", 1: "🚨 SPAM"}
130
- COLOUR_MAP = {0: "green", 1: "red"}
 
 
131
 
132
  def predict_sms(text: str):
133
  if not text or not text.strip():
134
- return (
135
- "β€”", "β€”", "β€”", "β€”",
136
- "Please enter a message above.",
137
- )
138
-
139
- results = []
140
- verdict_html_parts = []
141
 
 
142
  for model_name, model in MODELS.items():
143
- pred = int(model.predict([text])[0])
144
- label = LABEL_MAP[pred]
145
- colour = COLOUR_MAP[pred]
 
 
146
  contrib = CONTRIBUTORS[model_name]
147
 
148
- verdict_html_parts.append(
149
- f"<div style='border:1px solid #ddd; border-radius:8px; padding:12px; margin:6px 0;'>"
150
- f"<b>{contrib['emoji']} {model_name}</b> &nbsp;"
151
- f"<span style='color:{colour}; font-weight:bold;'>{label}</span><br>"
152
- f"<small style='color:#888;'>by {contrib['name']} ({contrib['id']})</small>"
 
 
 
153
  f"</div>"
154
  )
155
 
156
- results.append(label)
157
-
158
- summary_html = (
159
- "<h3 style='margin-bottom:8px;'>Predictions from all 4 models</h3>"
160
- + "".join(verdict_html_parts)
161
- )
162
-
163
- svm_result, lr_result, nb_result, xgb_result = (
164
- results[0], results[2], results[1], results[3]
165
  )
166
-
167
- return svm_result, xgb_result, lr_result, nb_result, summary_html
168
 
169
 
170
  # ---------------------------------------------------------------------------
@@ -276,13 +272,9 @@ with gr.Blocks(title="SMS Spam Detector") as demo:
276
 
277
  classify_btn = gr.Button("Classify β–Ά", variant="primary", size="lg")
278
 
279
- with gr.Row():
280
- out_svm = gr.Label(label="⚑ Linear SVM\n(Sanjivan TP070073)")
281
- out_xgb = gr.Label(label="🌲 XGBoost\n(Mohamud Farah TP076875)")
282
- out_lr = gr.Label(label="πŸ“Š Logistic Regression\n(Farouk TP075438)")
283
- out_nb = gr.Label(label="πŸ”’ Multinomial NB\n(Devara TP073570)")
284
-
285
- summary_out = gr.HTML()
286
 
287
  with gr.Accordion("🚨 Spam Examples β€” click any to load", open=False):
288
  gr.Examples(
@@ -303,13 +295,13 @@ with gr.Blocks(title="SMS Spam Detector") as demo:
303
  classify_btn.click(
304
  fn=predict_sms,
305
  inputs=sms_input,
306
- outputs=[out_svm, out_xgb, out_lr, out_nb, summary_out],
307
  )
308
 
309
  sms_input.submit(
310
  fn=predict_sms,
311
  inputs=sms_input,
312
- outputs=[out_svm, out_xgb, out_lr, out_nb, summary_out],
313
  )
314
 
315
  # ── About tab ─────────────────────────────────────────────────────
 
126
  # ---------------------------------------------------------------------------
127
  # Prediction helper
128
  # ---------------------------------------------------------------------------
129
+ LABEL_MAP = {0: "βœ… Ham (not spam)", 1: "🚨 SPAM"}
130
+ BG_MAP = {0: "#1a3a1a", 1: "#3a1a1a"}
131
+ BORDER_MAP = {0: "#2ecc71", 1: "#e74c3c"}
132
+ COLOR_MAP = {0: "#2ecc71", 1: "#e74c3c"}
133
 
134
  def predict_sms(text: str):
135
  if not text or not text.strip():
136
+ return "<p style='color:#888; padding:12px;'>⬆️ Enter a message above and click <b>Classify</b>.</p>"
 
 
 
 
 
 
137
 
138
+ cards = []
139
  for model_name, model in MODELS.items():
140
+ pred = int(model.predict([text])[0])
141
+ label = LABEL_MAP[pred]
142
+ bg = BG_MAP[pred]
143
+ border = BORDER_MAP[pred]
144
+ colour = COLOR_MAP[pred]
145
  contrib = CONTRIBUTORS[model_name]
146
 
147
+ cards.append(
148
+ f"<div style='"
149
+ f"background:{bg}; border:2px solid {border}; border-radius:12px;"
150
+ f"padding:16px 20px; flex:1; min-width:180px;'>"
151
+ f"<div style='font-size:1.6em; margin-bottom:4px;'>{contrib['emoji']}</div>"
152
+ f"<div style='font-weight:700; font-size:1.05em; color:#fff;'>{model_name}</div>"
153
+ f"<div style='color:{colour}; font-size:1.25em; font-weight:800; margin:8px 0;'>{label}</div>"
154
+ f"<div style='color:#aaa; font-size:0.8em;'>{contrib['name']}<br>{contrib['id']}</div>"
155
  f"</div>"
156
  )
157
 
158
+ grid = (
159
+ "<div style='display:flex; gap:12px; flex-wrap:wrap; margin-top:8px;'>"
160
+ + "".join(cards)
161
+ + "</div>"
 
 
 
 
 
162
  )
163
+ return grid
 
164
 
165
 
166
  # ---------------------------------------------------------------------------
 
272
 
273
  classify_btn = gr.Button("Classify β–Ά", variant="primary", size="lg")
274
 
275
+ results_out = gr.HTML(
276
+ value="<p style='color:#888; padding:12px;'>⬆️ Enter a message above and click <b>Classify</b>.</p>"
277
+ )
 
 
 
 
278
 
279
  with gr.Accordion("🚨 Spam Examples β€” click any to load", open=False):
280
  gr.Examples(
 
295
  classify_btn.click(
296
  fn=predict_sms,
297
  inputs=sms_input,
298
+ outputs=results_out,
299
  )
300
 
301
  sms_input.submit(
302
  fn=predict_sms,
303
  inputs=sms_input,
304
+ outputs=results_out,
305
  )
306
 
307
  # ── About tab ─────────────────────────────────────────────────────