Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -4,23 +4,26 @@ import gradio as gr
|
|
| 4 |
# Load the model
|
| 5 |
learn = load_learner('model.pkl')
|
| 6 |
|
| 7 |
-
# Fetch the labels dynamically from the model's vocabulary
|
| 8 |
-
labels = learn.dls.vocab
|
| 9 |
-
|
| 10 |
# Define the prediction function
|
| 11 |
def predict(img):
|
| 12 |
try:
|
| 13 |
# Create the image object
|
| 14 |
img = PILImage.create(img)
|
|
|
|
| 15 |
# Get predictions from the model
|
| 16 |
pred, pred_idx, probs = learn.predict(img)
|
|
|
|
|
|
|
|
|
|
|
|
|
| 17 |
# Convert probabilities to percentages and construct the output dictionary dynamically
|
| 18 |
-
return {labels[i]: f"{
|
| 19 |
except Exception as e:
|
| 20 |
# Log the exception and return it as an error message
|
| 21 |
print(f"An error occurred: {e}")
|
| 22 |
return {"error": str(e)}
|
| 23 |
|
|
|
|
| 24 |
# Define the Gradio interface
|
| 25 |
title = "Interior Design Classifier"
|
| 26 |
description = "Upload an image of an interior design and get a prediction of the design style."
|
|
|
|
| 4 |
# Load the model
|
| 5 |
learn = load_learner('model.pkl')
|
| 6 |
|
|
|
|
|
|
|
|
|
|
| 7 |
# Define the prediction function
|
| 8 |
def predict(img):
|
| 9 |
try:
|
| 10 |
# Create the image object
|
| 11 |
img = PILImage.create(img)
|
| 12 |
+
|
| 13 |
# Get predictions from the model
|
| 14 |
pred, pred_idx, probs = learn.predict(img)
|
| 15 |
+
|
| 16 |
+
# Fetch the labels dynamically from the model's vocabulary
|
| 17 |
+
labels = learn.dls.vocab
|
| 18 |
+
|
| 19 |
# Convert probabilities to percentages and construct the output dictionary dynamically
|
| 20 |
+
return {labels[i]: f"{probs[i]*100:.2f}%" for i in range(len(labels))}
|
| 21 |
except Exception as e:
|
| 22 |
# Log the exception and return it as an error message
|
| 23 |
print(f"An error occurred: {e}")
|
| 24 |
return {"error": str(e)}
|
| 25 |
|
| 26 |
+
|
| 27 |
# Define the Gradio interface
|
| 28 |
title = "Interior Design Classifier"
|
| 29 |
description = "Upload an image of an interior design and get a prediction of the design style."
|