JahnaviCh25 commited on
Commit
b13cf23
·
1 Parent(s): a5272c9

Add ToxiScan project with folder structure

Browse files
Files changed (2) hide show
  1. huggingface/app.py +39 -0
  2. huggingface/requirements.txt +7 -0
huggingface/app.py ADDED
@@ -0,0 +1,39 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import streamlit as st
2
+ import torch
3
+ from model import ToxicImageClassifier
4
+ from utils import load_image, predict_toxicity, get_label
5
+ from PIL import Image
6
+
7
+ def main():
8
+ st.title("Toxic Image Classifier")
9
+ st.write("Upload an image to check if it contains toxic content")
10
+
11
+ # Load model
12
+ device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
13
+ model = ToxicImageClassifier()
14
+ model.load_state_dict(torch.load("toxic_classifier.pth", map_location=device))
15
+ model.to(device)
16
+
17
+ # File uploader
18
+ uploaded_file = st.file_uploader("Choose an image...", type=["jpg", "png", "jpeg"])
19
+
20
+ if uploaded_file is not None:
21
+ # Display image
22
+ image = Image.open(uploaded_file)
23
+ st.image(image, caption="Uploaded Image", use_column_width=True)
24
+
25
+ # Process and predict
26
+ with st.spinner("Analyzing..."):
27
+ image_tensor = load_image(uploaded_file)
28
+ prediction, probabilities = predict_toxicity(model, image_tensor, device)
29
+ label = get_label(prediction)
30
+
31
+ # Display results
32
+ st.write(f"Prediction: **{label}**")
33
+ st.write(f"Confidence: Toxic: {probabilities[1]:.2%}, Non-Toxic: {probabilities[0]:.2%}")
34
+
35
+ # Probability bar chart
36
+ st.bar_chart({"Toxic": probabilities[1], "Non-Toxic": probabilities[0]})
37
+
38
+ if __name__ == "__main__":
39
+ main()
huggingface/requirements.txt ADDED
@@ -0,0 +1,7 @@
 
 
 
 
 
 
 
 
1
+ torch>=2.0.0
2
+ torchvision>=0.15.0
3
+ transformers>=4.35.0
4
+ streamlit>=1.28.0
5
+ pillow>=9.0.0
6
+ numpy>=1.24.0
7
+ gradio>=4.0.0