Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -31,31 +31,35 @@
|
|
| 31 |
# # Launch the app
|
| 32 |
# gradio_app.launch()
|
| 33 |
|
| 34 |
-
|
| 35 |
-
|
| 36 |
import gradio as gr
|
| 37 |
from PIL import Image
|
| 38 |
|
|
|
|
|
|
|
|
|
|
| 39 |
# Prediction function
|
| 40 |
def predict(input_img):
|
| 41 |
-
# Get the predictions from the pipeline
|
| 42 |
predictions = classifier(input_img)
|
| 43 |
|
|
|
|
| 44 |
result = {p["label"]: p["score"] for p in predictions}
|
| 45 |
-
|
| 46 |
-
# Return the image and the top predictions as a string
|
| 47 |
top_labels = [f"{label}: {score:.2f}" for label, score in result.items()]
|
|
|
|
| 48 |
return input_img, "\n".join(top_labels)
|
| 49 |
|
| 50 |
-
# Create
|
| 51 |
gradio_app = gr.Interface(
|
| 52 |
fn=predict,
|
| 53 |
-
inputs=gr.Image(label="Select Image", sources=[
|
| 54 |
outputs=[
|
| 55 |
-
gr.Image(label="
|
| 56 |
-
gr.Textbox(label="
|
| 57 |
],
|
| 58 |
-
title="Age Classification",
|
|
|
|
| 59 |
)
|
| 60 |
|
|
|
|
| 61 |
gradio_app.launch()
|
|
|
|
|
|
| 31 |
# # Launch the app
|
| 32 |
# gradio_app.launch()
|
| 33 |
|
| 34 |
+
from transformers import pipeline
|
|
|
|
| 35 |
import gradio as gr
|
| 36 |
from PIL import Image
|
| 37 |
|
| 38 |
+
# Load the pretrained model pipeline
|
| 39 |
+
classifier = pipeline("image-classification", model="sherab65/age-classification")
|
| 40 |
+
|
| 41 |
# Prediction function
|
| 42 |
def predict(input_img):
|
|
|
|
| 43 |
predictions = classifier(input_img)
|
| 44 |
|
| 45 |
+
# Format predictions
|
| 46 |
result = {p["label"]: p["score"] for p in predictions}
|
|
|
|
|
|
|
| 47 |
top_labels = [f"{label}: {score:.2f}" for label, score in result.items()]
|
| 48 |
+
|
| 49 |
return input_img, "\n".join(top_labels)
|
| 50 |
|
| 51 |
+
# Create Gradio interface
|
| 52 |
gradio_app = gr.Interface(
|
| 53 |
fn=predict,
|
| 54 |
+
inputs=gr.Image(label="Select Image", sources=["upload", "webcam"], type="pil"),
|
| 55 |
outputs=[
|
| 56 |
+
gr.Image(label="Uploaded Image"),
|
| 57 |
+
gr.Textbox(label="Predicted Age Group(s)")
|
| 58 |
],
|
| 59 |
+
title="Age Classification using Hugging Face Model",
|
| 60 |
+
description="Upload or capture an image to classify the person's age group."
|
| 61 |
)
|
| 62 |
|
| 63 |
+
# Launch the app
|
| 64 |
gradio_app.launch()
|
| 65 |
+
|