Ajay1311 commited on
Commit
fdfed2c
·
verified ·
1 Parent(s): 2f1cede

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +53 -0
app.py ADDED
@@ -0,0 +1,53 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ from transformers import AutoModelForSequenceClassification, AutoTokenizer, pipeline
3
+ import torch
4
+ import os
5
+
6
+ # Path to your repository with renamed files
7
+ model_path = "Ajay1311/bert-finetuned-phishing"
8
+
9
+ # Define paths to renamed files
10
+ weights_file = os.path.join(model_path, "phish_model.bin")
11
+ config_file = os.path.join(model_path, "phish_config.json")
12
+ tokenizer_path = model_path # Directory containing tokenizer files
13
+
14
+ # Load the tokenizer with renamed files
15
+ tokenizer = AutoTokenizer.from_pretrained(
16
+ tokenizer_path,
17
+ config_file="phish_tokenizer_config.json",
18
+ vocab_file="phish_vocab.txt",
19
+ tokenizer_file="phih_tokenizer.json",
20
+ special_tokens_map_file="phish_special_tokens_map.json",
21
+ local_files_only=True
22
+ )
23
+
24
+ # Load the model with renamed files
25
+ model = AutoModelForSequenceClassification.from_pretrained(
26
+ model_path,
27
+ config_file=config_file,
28
+ local_files_only=True
29
+ )
30
+
31
+ # Load the weights manually
32
+ model.load_state_dict(torch.load(weights_file))
33
+
34
+ # Create the pipeline for text classification
35
+ classifier = pipeline("text-classification", model=model, tokenizer=tokenizer)
36
+
37
+ # Define the prediction function
38
+ def predict_phishing(text):
39
+ result = classifier(text)[0]
40
+ label = "Phishing" if result["label"] == "LABEL_1" else "Benign"
41
+ return f"Prediction: {label}\nConfidence: {result['score']:.4f}"
42
+
43
+ # Create the Gradio interface
44
+ iface = gr.Interface(
45
+ fn=predict_phishing,
46
+ inputs=gr.Textbox(lines=2, placeholder="Enter URL, SMS, or email text here..."),
47
+ outputs="text",
48
+ title="Phishing Detection with BERT",
49
+ description="Enter a URL, SMS, or email text to check if it's phishing or benign."
50
+ )
51
+
52
+ # Launch the interface
53
+ iface.launch()