File size: 1,245 Bytes
40a34f8
1e9c865
 
40a34f8
1e9c865
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import gradio as gr
from transformers import AutoModelForImageClassification, AutoProcessor
import torch

# Load the model and processor from Hugging Face
model_name = "dima806/facial_age_image_detection"
model = AutoModelForImageClassification.from_pretrained(model_name)
processor = AutoProcessor.from_pretrained(model_name)

# Define the prediction function
def predict(image):
    # Process the input image
    inputs = processor(images=image, return_tensors="pt")
    # Perform the prediction
    with torch.no_grad():
        outputs = model(**inputs)
    
    # Get the model's original outputs (e.g., logits or probabilities)
    predictions = outputs.logits

    # Convert predictions to a list and round to 2 decimal places if necessary
    predictions_list = predictions.tolist()
    rounded_predictions = [[round(pred, 2) for pred in prediction] for prediction in predictions_list]
    
    return rounded_predictions

# Create Gradio interface
iface = gr.Interface(
    fn=predict,
    inputs="image",
    outputs="label",  # Use the model's original output type
    title="Facial Age Prediction",
    description="This application predicts your age from a facial image."
)

# Launch the Gradio application
iface.launch(share=True)