JanviMl commited on
Commit
a5272c9
·
verified ·
1 Parent(s): 8b2f9fc

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +39 -0
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()