IreNkweke commited on
Commit
6243c16
·
1 Parent(s): 7a11da0

Spam classifier

Browse files
Files changed (2) hide show
  1. app.py +54 -0
  2. requirements.txt +2 -0
app.py ADDED
@@ -0,0 +1,54 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ from transformers import pipeline
3
+
4
+ # Load the model
5
+ classifier = pipeline("text-classification", model="IreNkweke/HamOrSpamModel")
6
+
7
+ # Define the function that will be used in the interface
8
+ def classify_text(text):
9
+ result = classifier(text)
10
+ label = result[0]['label']
11
+ score = result[0]['score']
12
+
13
+ # Assuming the model outputs scores for both classes
14
+ if label == 'LABEL_1':
15
+ spam_percentage = round(score * 100, 2)
16
+ not_spam_percentage = round((1 - score) * 100, 2)
17
+ else:
18
+ spam_percentage = round((1 - score) * 100, 2)
19
+ not_spam_percentage = round(score * 100, 2)
20
+
21
+ label_mapping = {
22
+ 'LABEL_0': 'Ham', # Non-spam
23
+ 'LABEL_1': 'Spam' # Spam
24
+ }
25
+
26
+ return label_mapping[label], spam_percentage, not_spam_percentage
27
+
28
+ # Create the Gradio interface
29
+ iface = gr.Interface(
30
+ fn=classify_text,
31
+ inputs=gr.Textbox(
32
+ label="Input Text",
33
+ placeholder="Enter your message here...",
34
+ lines=4
35
+ ),
36
+ outputs=[
37
+ gr.Textbox(label="Class", placeholder="Classification result", lines=1),
38
+ gr.Number(label="Spam (%)"),
39
+ gr.Number(label="Not Spam (%)")
40
+ ],
41
+ title="Spam Classifier",
42
+ description="Classify messages as Spam or Ham with percentage confidence. Enter a message below to see the classification and confidence percentages.",
43
+ examples=[
44
+ ["Congratulations! You've won a free gift card!"],
45
+ ["Hi! I wanted to check in and see how you’ve been doing."],
46
+ ["Urgent: Your account has been compromised. Please provide your login details to verify your identity."],
47
+ ["The meeting is scheduled for 10 AM tomorrow. Please let me know if you need any changes."],
48
+ ["Limited Time Offer: Buy one, get one free on all items! Shop now and save big!"]
49
+ ],
50
+ theme="default"
51
+ )
52
+
53
+ # Launch the interface
54
+ iface.launch()
requirements.txt ADDED
@@ -0,0 +1,2 @@
 
 
 
1
+ gradio
2
+ transformers