|
|
from transformers import pipeline |
|
|
import gradio as gr |
|
|
import re |
|
|
|
|
|
|
|
|
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: |
|
|
|
|
|
spam_flag = spam_pipe(text)[0]["label"] != "LABEL_0" |
|
|
|
|
|
|
|
|
zero_result = zero_shot(text, candidate_labels=["complaint", "not complaint"]) |
|
|
not_complaint_flag = zero_result["labels"][0] == "not complaint" |
|
|
|
|
|
|
|
|
gibberish_flag = is_gibberish(text) |
|
|
|
|
|
|
|
|
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() |
|
|
|