File size: 2,058 Bytes
967a22b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
c15ebd3
85358ed
 
 
c15ebd3
 
 
85358ed
 
967a22b
 
c15ebd3
85358ed
 
c15ebd3
85358ed
 
c15ebd3
85358ed
 
c15ebd3
85358ed
c15ebd3
 
85358ed
c15ebd3
 
85358ed
 
c15ebd3
7242fab
c15ebd3
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
# from transformers import pipeline
# import gradio as gr
# from PIL import Image

# # Initialize the image classification pipeline with the specific model
# pipe = pipeline("image-classification", model="prithivMLmods/Age-Classification-SigLIP2")

# # Prediction function
# def predict(input_img):
#     # Get the predictions from the pipeline
#     predictions = pipe(input_img)
    
#     result = {p["label"]: p["score"] for p in predictions}
    
#     # Return the image and the top predictions as a string
#     top_labels = [f"{label}: {score:.2f}" for label, score in result.items()]
#     return input_img, "\n".join(top_labels)

# # Create the Gradio interface
# gradio_app = gr.Interface(
#     fn=predict,
#     inputs=gr.Image(label="Select Image", sources=['upload', 'webcam'], type="pil"),
#     outputs=[
#         gr.Image(label="Processed Image"),
#         gr.Textbox(label="Result", placeholder="Top predictions here")
#     ],
#     title="Age Classification",
#     description="Upload or capture an image to classify age using the SigLIP2 model."
# )

# # Launch the app
# gradio_app.launch()

from transformers import pipeline
import gradio as gr
from PIL import Image

# Load the pretrained model pipeline
classifier = pipeline("image-classification", model="sherab65/age-classification")

# Prediction function
def predict(input_img):
    predictions = classifier(input_img)

    # Format predictions
    result = {p["label"]: p["score"] for p in predictions}
    top_labels = [f"{label}: {score:.2f}" for label, score in result.items()]

    return input_img, "\n".join(top_labels)

# Create Gradio interface
gradio_app = gr.Interface(
    fn=predict,
    inputs=gr.Image(label="Select Image", sources=["upload", "webcam"], type="pil"),
    outputs=[
        gr.Image(label="Uploaded Image"),
        gr.Textbox(label="Predicted Age Group(s)")
    ],
    title="Age Classification using Hugging Face Model",
    description="Upload or capture an image to classify the person's age group."
)

# Launch the app
gradio_app.launch()