Update app.py
Browse files
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 |
-
|
|
|
|
|
|
|
|
|
|
| 13 |
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 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()
|