minggo-commits commited on
Commit
b6c0da1
·
verified ·
1 Parent(s): 707ac7d

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +30 -7
app.py CHANGED
@@ -1,5 +1,8 @@
 
1
  import gradio as gr
2
  import joblib
 
 
3
  from src.preprocessing import clean_text
4
 
5
  model = joblib.load("model/saved_model.joblib")
@@ -9,14 +12,34 @@ def predict(text):
9
  clean = clean_text(text)
10
  vec = vectorizer.transform([clean])
11
  pred = model.predict(vec)[0]
12
- return "🚫 Judi Online" if pred == 1 else "✅ Aman"
 
 
 
13
 
14
- demo = gr.Interface(
15
- fn=predict,
16
- inputs=gr.Textbox(lines=3, placeholder="Masukkan komentar..."),
17
- outputs="text",
18
- title="Deteksi Komentar Judi Online"
19
- )
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
20
 
21
  if __name__ == "__main__":
22
  demo.launch()
 
1
+ # app.py
2
  import gradio as gr
3
  import joblib
4
+ import json
5
+ import os
6
  from src.preprocessing import clean_text
7
 
8
  model = joblib.load("model/saved_model.joblib")
 
12
  clean = clean_text(text)
13
  vec = vectorizer.transform([clean])
14
  pred = model.predict(vec)[0]
15
+ proba = None
16
+ if hasattr(model, "predict_proba"):
17
+ proba = model.predict_proba(vec)[0]
18
+ proba = { "Aman": round(proba[0]*100, 2), "Judi": round(proba[1]*100, 2) }
19
 
20
+ label = "🚫 Judi Online" if pred == "judi" or pred == 1 else "✅ Aman"
21
+ return {"Prediksi": label, "Probabilitas (%)": proba}
22
+
23
+ def load_metrics():
24
+ path = "model/metrics_summary.json"
25
+ if os.path.exists(path):
26
+ with open(path) as f:
27
+ return json.load(f)
28
+ return {}
29
+
30
+ with gr.Blocks() as demo:
31
+ with gr.Tab("Inference"):
32
+ inp = gr.Textbox(lines=3, placeholder="Masukkan komentar...")
33
+ out = gr.JSON()
34
+ gr.Button("Prediksi").click(fn=predict, inputs=inp, outputs=out)
35
+
36
+ with gr.Tab("Monitoring"):
37
+ metrics = load_metrics()
38
+ if metrics:
39
+ gr.Markdown("### 📊 Model Performance")
40
+ gr.JSON(metrics)
41
+ if os.path.exists("model/confusion_matrix.png"):
42
+ gr.Image("model/confusion_matrix.png", label="Confusion Matrix")
43
 
44
  if __name__ == "__main__":
45
  demo.launch()