Sabari231024 commited on
Commit
2afe0ff
·
verified ·
1 Parent(s): 40d56ab

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +42 -0
app.py ADDED
@@ -0,0 +1,42 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ import torch
3
+ from transformers import AutoTokenizer, AutoModelForSequenceClassification
4
+
5
+ # Load the fine-tuned model and tokenizer
6
+ MODEL_PATH = "D:/Phishing/model"
7
+ tokenizer = AutoTokenizer.from_pretrained(MODEL_PATH)
8
+ model = AutoModelForSequenceClassification.from_pretrained(MODEL_PATH)
9
+
10
+ # Set device
11
+ device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
12
+ model.to(device)
13
+
14
+ # Function to predict spam
15
+ def predict_spam(text):
16
+ # Tokenize input text
17
+ inputs = tokenizer(text, return_tensors="pt", truncation=True, padding=True, max_length=512)
18
+ inputs = {key: value.to(device) for key, value in inputs.items()}
19
+
20
+ # Get model predictions
21
+ with torch.no_grad():
22
+ outputs = model(**inputs)
23
+
24
+ # Convert logits to label
25
+ logits = outputs.logits
26
+ predicted_class = torch.argmax(logits, dim=1).item()
27
+
28
+ # Interpret the prediction
29
+ return "Spam" if predicted_class == 1 else "Not Spam"
30
+
31
+ # Gradio UI
32
+ iface = gr.Interface(
33
+ fn=predict_spam,
34
+ inputs=gr.Textbox(label="Enter Email Content"),
35
+ outputs=gr.Label(label="Spam Detection Result"),
36
+ title="📧 Spam Email Detector",
37
+ description="Paste an email body and get a spam classification prediction using a Transformer model."
38
+ )
39
+
40
+ # Launch the app
41
+ if __name__ == "__main__":
42
+ iface.launch()