Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,3 +1,37 @@
|
|
| 1 |
import gradio as gr
|
|
|
|
|
|
|
| 2 |
|
| 3 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
+
from transformers import AutoModelForImageClassification, AutoProcessor
|
| 3 |
+
import torch
|
| 4 |
|
| 5 |
+
# Load the model and processor from Hugging Face
|
| 6 |
+
model_name = "dima806/facial_age_image_detection"
|
| 7 |
+
model = AutoModelForImageClassification.from_pretrained(model_name)
|
| 8 |
+
processor = AutoProcessor.from_pretrained(model_name)
|
| 9 |
+
|
| 10 |
+
# Define the prediction function
|
| 11 |
+
def predict(image):
|
| 12 |
+
# Process the input image
|
| 13 |
+
inputs = processor(images=image, return_tensors="pt")
|
| 14 |
+
# Perform the prediction
|
| 15 |
+
with torch.no_grad():
|
| 16 |
+
outputs = model(**inputs)
|
| 17 |
+
|
| 18 |
+
# Get the model's original outputs (e.g., logits or probabilities)
|
| 19 |
+
predictions = outputs.logits
|
| 20 |
+
|
| 21 |
+
# Convert predictions to a list and round to 2 decimal places if necessary
|
| 22 |
+
predictions_list = predictions.tolist()
|
| 23 |
+
rounded_predictions = [[round(pred, 2) for pred in prediction] for prediction in predictions_list]
|
| 24 |
+
|
| 25 |
+
return rounded_predictions
|
| 26 |
+
|
| 27 |
+
# Create Gradio interface
|
| 28 |
+
iface = gr.Interface(
|
| 29 |
+
fn=predict,
|
| 30 |
+
inputs="image",
|
| 31 |
+
outputs="label", # Use the model's original output type
|
| 32 |
+
title="Facial Age Prediction",
|
| 33 |
+
description="This application predicts your age from a facial image."
|
| 34 |
+
)
|
| 35 |
+
|
| 36 |
+
# Launch the Gradio application
|
| 37 |
+
iface.launch(share=True)
|