Spaces:
Runtime error
Runtime error
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,28 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
# prompt: gradio image 鍒嗙被
|
2 |
+
import fastai
|
3 |
+
from fastai.vision import *
|
4 |
+
from fastai.vision.all import load_learner,PILImage
|
5 |
+
import gradio as gr
|
6 |
+
from transformers import AutoModelForSequenceClassification
|
7 |
+
# Load the model
|
8 |
+
|
9 |
+
model = AutoModelForImageClassification.from_pretrained("Falconsai/nsfw_image_detection")
|
10 |
+
model = load_learner(model)
|
11 |
+
|
12 |
+
# Define an image classification function
|
13 |
+
def classify_image(image):
|
14 |
+
img = PILImage.create(image)
|
15 |
+
|
16 |
+
# Make a prediction
|
17 |
+
pred_class, pred_idx, probs = model.predict(img)
|
18 |
+
|
19 |
+
# Return the prediction as a dictionary
|
20 |
+
return {model.dls.vocab[i]: float(probs[i]) for i in range(len(probs))}
|
21 |
+
|
22 |
+
# Create the Gradio interface
|
23 |
+
image_input = gr.Image()
|
24 |
+
label_output = gr.Label(num_top_classes=2)
|
25 |
+
interface = gr.Interface(fn=classify_image, inputs=image_input, outputs=label_output)
|
26 |
+
|
27 |
+
# Launch the interface
|
28 |
+
interface.launch()
|