Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
@@ -1,3 +1,33 @@
|
|
1 |
import gradio as gr
|
|
|
|
|
|
|
2 |
|
3 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
import gradio as gr
|
2 |
+
import torch
|
3 |
+
from PIL import Image
|
4 |
+
from transformers import AutoModelForImageClassification, ViTImageProcessor
|
5 |
|
6 |
+
# Load model
|
7 |
+
model = AutoModelForImageClassification.from_pretrained("Falconsai/nsfw_image_detection")
|
8 |
+
processor = ViTImageProcessor.from_pretrained('Falconsai/nsfw_image_detection')
|
9 |
+
|
10 |
+
# Function to make predictions
|
11 |
+
def classify_image(img):
|
12 |
+
with torch.no_grad():
|
13 |
+
inputs = processor(images=img, return_tensors="pt")
|
14 |
+
outputs = model(**inputs)
|
15 |
+
logits = outputs.logits
|
16 |
+
|
17 |
+
predicted_label = logits.argmax(-1).item()
|
18 |
+
return model.config.id2label[predicted_label]
|
19 |
+
|
20 |
+
# Gradio Interface
|
21 |
+
iface = gr.Interface(
|
22 |
+
fn=classify_image,
|
23 |
+
inputs="image",
|
24 |
+
outputs="text",
|
25 |
+
live=True,
|
26 |
+
interpretation="default"
|
27 |
+
)
|
28 |
+
|
29 |
+
# Launch the Gradio Interface
|
30 |
+
iface.launch()
|
31 |
+
|
32 |
+
|
33 |
+
#gr.load("models/Falconsai/nsfw_image_detection").launch()
|