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()