Spaces:
Sleeping
Sleeping
mischeiwiller
commited on
Commit
•
a96ede8
1
Parent(s):
6388a95
Update app.py
Browse files
app.py
CHANGED
@@ -1,15 +1,32 @@
|
|
1 |
import gradio as gr
|
2 |
from transformers import pipeline
|
|
|
3 |
|
4 |
-
|
|
|
5 |
|
6 |
def predict(image):
|
7 |
-
|
8 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
9 |
|
10 |
-
|
11 |
-
|
12 |
-
|
13 |
-
|
|
|
14 |
title="Hot Dog? Or Not?",
|
15 |
-
|
|
|
|
|
|
|
|
|
|
1 |
import gradio as gr
|
2 |
from transformers import pipeline
|
3 |
+
import numpy as np
|
4 |
|
5 |
+
# Initialize the pipeline
|
6 |
+
classifier = pipeline(task="image-classification", model="julien-c/hotdog-not-hotdog")
|
7 |
|
8 |
def predict(image):
|
9 |
+
# Check if the input is a file path or numpy array
|
10 |
+
if isinstance(image, str):
|
11 |
+
# If it's a file path, pass it directly to the pipeline
|
12 |
+
predictions = classifier(image)
|
13 |
+
else:
|
14 |
+
# If it's a numpy array, we need to convert it to PIL Image
|
15 |
+
from PIL import Image
|
16 |
+
image = Image.fromarray(image.astype('uint8'), 'RGB')
|
17 |
+
predictions = classifier(image)
|
18 |
+
|
19 |
+
# Convert predictions to the format expected by Gradio
|
20 |
+
return {p["label"]: float(p["score"]) for p in predictions}
|
21 |
|
22 |
+
# Create the Gradio interface
|
23 |
+
iface = gr.Interface(
|
24 |
+
fn=predict,
|
25 |
+
inputs=gr.Image(label="Upload hot dog candidate"),
|
26 |
+
outputs=gr.Label(num_top_classes=2),
|
27 |
title="Hot Dog? Or Not?",
|
28 |
+
description="Upload an image to see if it's a hot dog or not!",
|
29 |
+
)
|
30 |
+
|
31 |
+
if __name__ == "__main__":
|
32 |
+
iface.launch()
|