Spaces:
Running
Running
NN model
Browse files
app.py
CHANGED
|
@@ -4,10 +4,19 @@ import json
|
|
| 4 |
import tensorflow as tf
|
| 5 |
import numpy as np
|
| 6 |
|
|
|
|
|
|
|
|
|
|
| 7 |
model_probs_path = hf_hub_download(repo_id="tbitai/bayes-enron1-spam", filename="probs.json")
|
| 8 |
with open(model_probs_path) as f:
|
| 9 |
model_probs = json.load(f)
|
| 10 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 11 |
UNK = '[UNK]'
|
| 12 |
|
| 13 |
def tokenize(text):
|
|
@@ -27,9 +36,14 @@ def get_interesting_probs(probs, intr_threshold):
|
|
| 27 |
key=lambda p: abs(p - 0.5),
|
| 28 |
reverse=True)[:intr_threshold]
|
| 29 |
|
|
|
|
|
|
|
| 30 |
def unbias(p):
|
| 31 |
return (2 * p) / (p + 1)
|
| 32 |
|
|
|
|
|
|
|
|
|
|
| 33 |
def predict_bayes(text, intr_threshold, unbiased=False):
|
| 34 |
words = tokenize(text)
|
| 35 |
probs = []
|
|
@@ -44,29 +58,39 @@ def predict_bayes(text, intr_threshold, unbiased=False):
|
|
| 44 |
interesting_probs = get_interesting_probs(probs, intr_threshold)
|
| 45 |
return combine(interesting_probs)
|
| 46 |
|
| 47 |
-
|
|
|
|
| 48 |
|
| 49 |
MODELS = [
|
| 50 |
BAYES := "Bayes Enron1 spam",
|
|
|
|
| 51 |
]
|
| 52 |
|
| 53 |
def predict(model, unbiased, intr_threshold, input_txt):
|
| 54 |
if model == BAYES:
|
| 55 |
return predict_bayes(input_txt, unbiased=unbiased, intr_threshold=intr_threshold)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 56 |
|
| 57 |
demo = gr.Interface(
|
| 58 |
fn=predict,
|
| 59 |
inputs=[
|
| 60 |
gr.Dropdown(choices=MODELS, value=BAYES, label="Model"),
|
|
|
|
|
|
|
|
|
|
|
|
|
| 61 |
gr.Checkbox(label="Unbias", info="Correct Graham's bias?"),
|
| 62 |
gr.Slider(minimum=1, maximum=DEFAULT_INTR_THRESHOLD + 5, step=1, value=DEFAULT_INTR_THRESHOLD,
|
| 63 |
label="Interestingness threshold",
|
| 64 |
info=f"How many of the most interesting words to select in the probability calculation? ({DEFAULT_INTR_THRESHOLD} for Graham)"),
|
| 65 |
-
gr.TextArea(label="Email"),
|
| 66 |
],
|
| 67 |
outputs=[gr.Number(label="Spam probability")],
|
| 68 |
title="Bayes or Spam?",
|
| 69 |
-
description="Choose
|
| 70 |
examples=[
|
| 71 |
[BAYES, False, DEFAULT_INTR_THRESHOLD, "Enron actuals for June 26, 2000"],
|
| 72 |
[BAYES, False, DEFAULT_INTR_THRESHOLD, nerissa_email := "Stop the aging clock\nNerissa"],
|
|
|
|
| 4 |
import tensorflow as tf
|
| 5 |
import numpy as np
|
| 6 |
|
| 7 |
+
|
| 8 |
+
# Load models
|
| 9 |
+
|
| 10 |
model_probs_path = hf_hub_download(repo_id="tbitai/bayes-enron1-spam", filename="probs.json")
|
| 11 |
with open(model_probs_path) as f:
|
| 12 |
model_probs = json.load(f)
|
| 13 |
|
| 14 |
+
nn_model_path = hf_hub_download(repo_id="tbitai/nn-enron1-spam", filename="nn-enron1-spam.keras")
|
| 15 |
+
nn_model = tf.keras.models.load_model(nn_model_path)
|
| 16 |
+
|
| 17 |
+
|
| 18 |
+
# Utils for Bayes
|
| 19 |
+
|
| 20 |
UNK = '[UNK]'
|
| 21 |
|
| 22 |
def tokenize(text):
|
|
|
|
| 36 |
key=lambda p: abs(p - 0.5),
|
| 37 |
reverse=True)[:intr_threshold]
|
| 38 |
|
| 39 |
+
DEFAULT_INTR_THRESHOLD = 15
|
| 40 |
+
|
| 41 |
def unbias(p):
|
| 42 |
return (2 * p) / (p + 1)
|
| 43 |
|
| 44 |
+
|
| 45 |
+
# Predict functions
|
| 46 |
+
|
| 47 |
def predict_bayes(text, intr_threshold, unbiased=False):
|
| 48 |
words = tokenize(text)
|
| 49 |
probs = []
|
|
|
|
| 58 |
interesting_probs = get_interesting_probs(probs, intr_threshold)
|
| 59 |
return combine(interesting_probs)
|
| 60 |
|
| 61 |
+
def predict_nn(text):
|
| 62 |
+
return nn_model(np.array([text]))[0][0].numpy()
|
| 63 |
|
| 64 |
MODELS = [
|
| 65 |
BAYES := "Bayes Enron1 spam",
|
| 66 |
+
NN := "NN Enron1 spam",
|
| 67 |
]
|
| 68 |
|
| 69 |
def predict(model, unbiased, intr_threshold, input_txt):
|
| 70 |
if model == BAYES:
|
| 71 |
return predict_bayes(input_txt, unbiased=unbiased, intr_threshold=intr_threshold)
|
| 72 |
+
elif model == NN:
|
| 73 |
+
return predict_nn(input_txt)
|
| 74 |
+
|
| 75 |
+
|
| 76 |
+
# UI
|
| 77 |
|
| 78 |
demo = gr.Interface(
|
| 79 |
fn=predict,
|
| 80 |
inputs=[
|
| 81 |
gr.Dropdown(choices=MODELS, value=BAYES, label="Model"),
|
| 82 |
+
gr.TextArea(label="Email"),
|
| 83 |
+
],
|
| 84 |
+
additional_inputs_accordion="Additional configuration for Bayes",
|
| 85 |
+
additional_inputs=[
|
| 86 |
gr.Checkbox(label="Unbias", info="Correct Graham's bias?"),
|
| 87 |
gr.Slider(minimum=1, maximum=DEFAULT_INTR_THRESHOLD + 5, step=1, value=DEFAULT_INTR_THRESHOLD,
|
| 88 |
label="Interestingness threshold",
|
| 89 |
info=f"How many of the most interesting words to select in the probability calculation? ({DEFAULT_INTR_THRESHOLD} for Graham)"),
|
|
|
|
| 90 |
],
|
| 91 |
outputs=[gr.Number(label="Spam probability")],
|
| 92 |
title="Bayes or Spam?",
|
| 93 |
+
description="Choose your model, and predict if your email is a spam! 📨<br>COMING SOON: LLM models.",
|
| 94 |
examples=[
|
| 95 |
[BAYES, False, DEFAULT_INTR_THRESHOLD, "Enron actuals for June 26, 2000"],
|
| 96 |
[BAYES, False, DEFAULT_INTR_THRESHOLD, nerissa_email := "Stop the aging clock\nNerissa"],
|