Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -48,13 +48,12 @@ transform = transforms.Compose([
|
|
48 |
|
49 |
def predict(data):
|
50 |
try:
|
51 |
-
# Expecting data to be a list
|
52 |
if not isinstance(data, list) or len(data) == 0:
|
53 |
-
return
|
54 |
|
55 |
image_input = data[0].get('image', None)
|
56 |
if not image_input:
|
57 |
-
return
|
58 |
|
59 |
print(f"Received image input: {image_input}")
|
60 |
|
@@ -70,7 +69,7 @@ def predict(data):
|
|
70 |
print(f"Fetched image from URL: {image}")
|
71 |
except Exception as e:
|
72 |
print(f"Error fetching image from URL: {e}")
|
73 |
-
return
|
74 |
else: # Assuming it is base64-encoded image data
|
75 |
try:
|
76 |
image_data = base64.b64decode(image_input)
|
@@ -78,7 +77,7 @@ def predict(data):
|
|
78 |
print(f"Decoded base64 image: {image}")
|
79 |
except Exception as e:
|
80 |
print(f"Error decoding base64 image: {e}")
|
81 |
-
return
|
82 |
|
83 |
# Apply transformations
|
84 |
image = transform(image).unsqueeze(0)
|
@@ -92,20 +91,20 @@ def predict(data):
|
|
92 |
print(f"Prediction output: {outputs}, Predicted class: {predicted_class}")
|
93 |
|
94 |
if predicted_class == 0:
|
95 |
-
return
|
96 |
elif predicted_class == 1:
|
97 |
-
return
|
98 |
else:
|
99 |
-
return
|
100 |
except Exception as e:
|
101 |
print(f"Error processing image: {e}")
|
102 |
-
return
|
103 |
|
104 |
# Create the Gradio interface
|
105 |
iface = gr.Interface(
|
106 |
fn=predict,
|
107 |
inputs=gr.JSON(label="Input JSON"),
|
108 |
-
outputs=gr.
|
109 |
live=True,
|
110 |
title="Maize Anomaly Detection",
|
111 |
description="Upload an image of maize to detect anomalies like disease or pest infestation. You can provide local paths, URLs, or base64-encoded images."
|
|
|
48 |
|
49 |
def predict(data):
|
50 |
try:
|
|
|
51 |
if not isinstance(data, list) or len(data) == 0:
|
52 |
+
return {"error": "Input data should be a non-empty list."}
|
53 |
|
54 |
image_input = data[0].get('image', None)
|
55 |
if not image_input:
|
56 |
+
return {"error": "No image provided."}
|
57 |
|
58 |
print(f"Received image input: {image_input}")
|
59 |
|
|
|
69 |
print(f"Fetched image from URL: {image}")
|
70 |
except Exception as e:
|
71 |
print(f"Error fetching image from URL: {e}")
|
72 |
+
return {"error": f"Error fetching image from URL: {e}"}
|
73 |
else: # Assuming it is base64-encoded image data
|
74 |
try:
|
75 |
image_data = base64.b64decode(image_input)
|
|
|
77 |
print(f"Decoded base64 image: {image}")
|
78 |
except Exception as e:
|
79 |
print(f"Error decoding base64 image: {e}")
|
80 |
+
return {"error": f"Error decoding base64 image: {e}"}
|
81 |
|
82 |
# Apply transformations
|
83 |
image = transform(image).unsqueeze(0)
|
|
|
91 |
print(f"Prediction output: {outputs}, Predicted class: {predicted_class}")
|
92 |
|
93 |
if predicted_class == 0:
|
94 |
+
return {"result": "The photo you've sent is of fall army worm with problem ID 126."}
|
95 |
elif predicted_class == 1:
|
96 |
+
return {"result": "The photo you've sent is of a healthy maize image."}
|
97 |
else:
|
98 |
+
return {"error": "Unexpected class prediction."}
|
99 |
except Exception as e:
|
100 |
print(f"Error processing image: {e}")
|
101 |
+
return {"error": f"Error processing image: {e}"}
|
102 |
|
103 |
# Create the Gradio interface
|
104 |
iface = gr.Interface(
|
105 |
fn=predict,
|
106 |
inputs=gr.JSON(label="Input JSON"),
|
107 |
+
outputs=gr.JSON(label="Prediction Result"),
|
108 |
live=True,
|
109 |
title="Maize Anomaly Detection",
|
110 |
description="Upload an image of maize to detect anomalies like disease or pest infestation. You can provide local paths, URLs, or base64-encoded images."
|