Spaces:
Build error
Build error
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,36 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
import numpy as np
|
| 3 |
+
import os
|
| 4 |
+
|
| 5 |
+
from PIL import Image
|
| 6 |
+
from transformers import pipeline
|
| 7 |
+
|
| 8 |
+
def predict_image(image):
|
| 9 |
+
pipe = pipeline("image-classification", model="itsTomLie/genders_microsoft_resnet50")
|
| 10 |
+
|
| 11 |
+
if isinstance(image, np.ndarray):
|
| 12 |
+
image = Image.fromarray(image.astype('uint8'))
|
| 13 |
+
elif isinstance(image, str):
|
| 14 |
+
image = Image.open(image)
|
| 15 |
+
|
| 16 |
+
result = pipe(image)
|
| 17 |
+
|
| 18 |
+
label = result[0]['label']
|
| 19 |
+
confidence = result[0]['score']
|
| 20 |
+
|
| 21 |
+
print(f"Prediction: {label}, Confidence: {confidence}")
|
| 22 |
+
|
| 23 |
+
return label, confidence
|
| 24 |
+
|
| 25 |
+
example_images = [
|
| 26 |
+
os.path.join("examples", img_name) for img_name in sorted(os.listdir("examples"))
|
| 27 |
+
]
|
| 28 |
+
|
| 29 |
+
interface = gr.Interface(
|
| 30 |
+
fn=predict_image,
|
| 31 |
+
inputs=gr.Image(type="numpy", label="Upload an Image"),
|
| 32 |
+
outputs=[gr.Textbox(label="Prediction"), gr.Textbox(label="Confidence")],
|
| 33 |
+
examples=example_images
|
| 34 |
+
)
|
| 35 |
+
|
| 36 |
+
interface.launch()
|