|
|
|
|
|
import torch |
|
import gradio as gr |
|
|
|
from typing import Dict |
|
from transformers import pipeline |
|
|
|
|
|
def spam_not_spam_classifier(text: str) -> Dict[str, float]: |
|
""" |
|
Takes an input string of text and classifies it into spam/not_spam in the form of a dictionary. |
|
""" |
|
|
|
|
|
spam_not_spam_classifier = pipeline(task="text-classification", |
|
model="drvpokhilko/huggingface_spam_not_spam_classifier-distilbert-base-uncased", |
|
batch_size=32, |
|
device="cuda" if torch.cuda.is_available() else "cpu", |
|
top_k=None) |
|
|
|
|
|
outputs = spam_not_spam_classifier(text)[0] |
|
|
|
|
|
output_dict = {} |
|
|
|
for item in outputs: |
|
output_dict[item["label"]] = item["score"] |
|
|
|
return output_dict |
|
|
|
|
|
description = """ |
|
A text classifier to determine if an email text is spam or not spam. |
|
|
|
Fine-tuned from [DistilBERT](https://huggingface.co/distilbert/distilbert-base-uncased) on a relatively small [dataset (~11k samples) of spam or not spam emails](https://huggingface.co/datasets/Deysi/spam-detection-dataset). |
|
""" |
|
|
|
demo = gr.Interface(fn=spam_not_spam_classifier, |
|
inputs="text", |
|
outputs=gr.Label(num_top_classes=2), |
|
title="📧⌨️👩💻Spam or Not Spam Email Classifier", |
|
description=description, |
|
examples=[["Hi John, here's the project report you requested. Let me know if you need any changes."], |
|
["Get access to unlimited movies and TV shows for free. Sign up today!"]]) |
|
|
|
|
|
if __name__ == "__main__": |
|
demo.launch() |
|
|