kithangw commited on
Commit
a39dfac
·
verified ·
1 Parent(s): a5d668d

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +25 -14
app.py CHANGED
@@ -1,11 +1,11 @@
1
  import streamlit as st
2
- import torch
3
  from PIL import Image
4
  from transformers import pipeline, AutoModelForSequenceClassification, AutoTokenizer
5
 
6
  # Initialize the image-to-text pipeline and models
7
  @st.cache(allow_output_mutation=True)
8
  def load_models():
 
9
  image_pipeline = pipeline("image-to-text", model="microsoft/trocr-large-printed")
10
  phishing_model = AutoModelForSequenceClassification.from_pretrained("kithangw/phishing_link_detection", num_labels=2)
11
  phishing_tokenizer = AutoTokenizer.from_pretrained("google/bert_uncased_L-2_H-128_A-2")
@@ -31,21 +31,32 @@ def check_phishing(url_for_recognize):
31
 
32
  # Streamlit interface
33
  st.title("Phishing URL Detection from Image")
34
- uploaded_image = st.file_uploader("Upload an image of the URL", type=["png", "jpg", "jpeg"])
 
 
 
 
 
35
 
36
  if uploaded_image is not None:
37
  image = Image.open(uploaded_image)
38
  st.image(image, caption='Uploaded URL Image', use_column_width=True)
39
 
40
- # Convert image to URL text
41
- url_for_recognize = image_pipeline(uploaded_image)[0]['generated_text'].replace(" ", "").lower()
42
- st.write("Recognized URL:")
43
- # Use a text input to let the user verify and possibly edit the recognized URL
44
- verified_url = st.text_input("Verify or edit the recognized URL if necessary:", value=url_for_recognize)
45
-
46
- if st.button('Detect Phishing'):
47
- if verified_url:
48
- result = check_phishing(verified_url)
49
- st.write(result)
50
- else:
51
- st.write("Please enter a URL to check for phishing.")
 
 
 
 
 
 
 
1
  import streamlit as st
 
2
  from PIL import Image
3
  from transformers import pipeline, AutoModelForSequenceClassification, AutoTokenizer
4
 
5
  # Initialize the image-to-text pipeline and models
6
  @st.cache(allow_output_mutation=True)
7
  def load_models():
8
+ # Make sure to use the correct model names and tokenizer
9
  image_pipeline = pipeline("image-to-text", model="microsoft/trocr-large-printed")
10
  phishing_model = AutoModelForSequenceClassification.from_pretrained("kithangw/phishing_link_detection", num_labels=2)
11
  phishing_tokenizer = AutoTokenizer.from_pretrained("google/bert_uncased_L-2_H-128_A-2")
 
31
 
32
  # Streamlit interface
33
  st.title("Phishing URL Detection from Image")
34
+
35
+ # Text box for URL input
36
+ verified_url = st.text_input("Enter or paste a URL to check for phishing:")
37
+
38
+ # File uploader to scan the image
39
+ uploaded_image = st.file_uploader("Alternatively, upload an image of the URL", type=["png", "jpg", "jpeg"])
40
 
41
  if uploaded_image is not None:
42
  image = Image.open(uploaded_image)
43
  st.image(image, caption='Uploaded URL Image', use_column_width=True)
44
 
45
+ try:
46
+ # Process the image with the OCR pipeline
47
+ ocr_result = image_pipeline(image)[0]['generated_text'].replace(" ", "").lower()
48
+ # Update the text input with the OCR result
49
+ st.session_state['verified_url'] = ocr_result
50
+ except Exception as e:
51
+ st.error(f"An error occurred during image processing: {e}")
52
+
53
+ if st.button('Detect Phishing'):
54
+ if verified_url:
55
+ result = check_phishing(verified_url)
56
+ st.write(result)
57
+ else:
58
+ st.write("Please enter or upload a URL to check for phishing.")
59
+
60
+ # Ensure the text box is updated with the OCR result (if any)
61
+ if 'verified_url' in st.session_state and uploaded_image:
62
+ verified_url = st.session_state['verified_url']