rmdhirr commited on
Commit
8b45928
1 Parent(s): 37db18f

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +20 -29
app.py CHANGED
@@ -25,32 +25,18 @@ nltk.download('wordnet')
25
  STOPWORDS = set(stopwords.words('english'))
26
  lemmatizer = WordNetLemmatizer()
27
 
28
- def normalize_length(url, target_length=50):
29
- if len(url) < target_length:
30
- url = url + " " * (target_length - len(url))
31
- else:
32
- url = url[:target_length]
33
- return url
34
 
35
- def preprocess_url(url):
36
- url = url.lower()
37
- url = re.sub(r'https?://', '', url)
38
- url = re.sub(r'www\.', '', url)
39
- url = re.sub(r'[^a-zA-Z0-9]', ' ', url)
40
- url = re.sub(r'\s+', ' ', url).strip()
41
- url = normalize_length(url)
42
- tokens = word_tokenize(url)
43
- tokens = [word for word in tokens if word not in STOPWORDS]
44
- tokens = [lemmatizer.lemmatize(word) for word in tokens]
45
- return ' '.join(tokens)
46
-
47
- def preprocess_html(html):
48
- html = re.sub(r'<[^>]+>', ' ', html)
49
- html = html.lower()
50
- html = re.sub(r'https?://', '', html)
51
- html = re.sub(r'[^a-zA-Z0-9]', ' ', html)
52
- html = re.sub(r'\s+', ' ', html).strip()
53
- tokens = word_tokenize(html)
54
  tokens = [word for word in tokens if word not in STOPWORDS]
55
  tokens = [lemmatizer.lemmatize(word) for word in tokens]
56
  return ' '.join(tokens)
@@ -73,20 +59,25 @@ def preprocess_input(input_text, tokenizer, max_length):
73
  def get_prediction(input_text, input_type):
74
  is_url = input_type == "URL"
75
  if is_url:
76
- cleaned_text = preprocess_url(input_text)
77
  input_data = preprocess_input(cleaned_text, url_tokenizer, max_url_length)
78
  input_data = [input_data, np.zeros((1, max_html_length))] # dummy HTML input
79
  else:
80
- cleaned_text = preprocess_html(input_text)
81
  input_data = preprocess_input(cleaned_text, html_tokenizer, max_html_length)
82
  input_data = [np.zeros((1, max_url_length)), input_data] # dummy URL input
83
 
84
  prediction = model.predict(input_data)[0][0]
85
  return prediction
86
 
 
 
 
 
 
87
  def phishing_detection(input_text, input_type):
88
- prediction = get_prediction(input_text, input_type)
89
- threshold = 0.5 # Adjusted threshold
90
  if prediction > threshold:
91
  return f"Warning: This site is likely a phishing site! ({prediction:.2f})"
92
  else:
 
25
  STOPWORDS = set(stopwords.words('english'))
26
  lemmatizer = WordNetLemmatizer()
27
 
28
+ def normalize_length(text, target_length):
29
+ text = text[:target_length].ljust(target_length)
30
+ return text
 
 
 
31
 
32
+ def preprocess_text(text, is_url=True):
33
+ text = text.lower()
34
+ if is_url:
35
+ text = re.sub(r'https?://', '', text)
36
+ text = re.sub(r'www\.', '', text)
37
+ text = re.sub(r'[^a-zA-Z0-9]', ' ', text)
38
+ text = re.sub(r'\s+', ' ', text).strip()
39
+ tokens = word_tokenize(text)
 
 
 
 
 
 
 
 
 
 
 
40
  tokens = [word for word in tokens if word not in STOPWORDS]
41
  tokens = [lemmatizer.lemmatize(word) for word in tokens]
42
  return ' '.join(tokens)
 
59
  def get_prediction(input_text, input_type):
60
  is_url = input_type == "URL"
61
  if is_url:
62
+ cleaned_text = preprocess_text(input_text, is_url=True)
63
  input_data = preprocess_input(cleaned_text, url_tokenizer, max_url_length)
64
  input_data = [input_data, np.zeros((1, max_html_length))] # dummy HTML input
65
  else:
66
+ cleaned_text = preprocess_text(input_text, is_url=False)
67
  input_data = preprocess_input(cleaned_text, html_tokenizer, max_html_length)
68
  input_data = [np.zeros((1, max_url_length)), input_data] # dummy URL input
69
 
70
  prediction = model.predict(input_data)[0][0]
71
  return prediction
72
 
73
+ def ensemble_prediction(input_text, input_type, n_ensemble=5):
74
+ predictions = [get_prediction(input_text, input_type) for _ in range(n_ensemble)]
75
+ avg_prediction = np.mean(predictions)
76
+ return avg_prediction
77
+
78
  def phishing_detection(input_text, input_type):
79
+ prediction = ensemble_prediction(input_text, input_type)
80
+ threshold = 0.5 # Keep the threshold unchanged
81
  if prediction > threshold:
82
  return f"Warning: This site is likely a phishing site! ({prediction:.2f})"
83
  else: