File size: 1,245 Bytes
6633877 e5e31f2 6633877 9a6d3bb e5e31f2 6633877 e5e31f2 a03498c e5e31f2 9a6d3bb a03498c 9a6d3bb e5e31f2 9a6d3bb a03498c 6633877 e5e31f2 a03498c 6633877 e5e31f2 a03498c 9a6d3bb 6633877 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 |
from transformers import pipeline
import gradio as gr
import re
# Load models once for speed
spam_pipe = pipeline("text-classification", model="Titeiiko/OTIS-Official-Spam-Model")
zero_shot = pipeline("zero-shot-classification", model="facebook/bart-large-mnli")
def is_gibberish(text: str) -> bool:
letters = len(re.findall(r"[a-zA-Z]", text))
return len(text) == 0 or (letters / len(text) < 0.6)
def detect(text: str) -> dict:
# 1️⃣ Ad/spam check
spam_flag = spam_pipe(text)[0]["label"] != "LABEL_0"
# 2️⃣ Relevance check (anything not a complaint counts as spam)
zero_result = zero_shot(text, candidate_labels=["complaint", "not complaint"])
not_complaint_flag = zero_result["labels"][0] == "not complaint"
# 3️⃣ Gibberish check
gibberish_flag = is_gibberish(text)
# ✅ Final decision
spam = spam_flag or not_complaint_flag or gibberish_flag
return {
"input": text,
"spam": spam
}
demo = gr.Interface(
fn=detect,
inputs=gr.Textbox(label="Enter complaint text"),
outputs=gr.JSON(label="Result"),
title="Spam Detector",
description="Returns only the input and spam status (True/False)."
)
if __name__ == "__main__":
demo.launch()
|