Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -48,34 +48,42 @@ transform = transforms.Compose([
|
|
48 |
|
49 |
def predict(data):
|
50 |
try:
|
51 |
-
#
|
52 |
if not isinstance(data, list) or len(data) == 0:
|
53 |
return json.dumps({"error": "Input data should be a non-empty list."})
|
54 |
|
|
|
55 |
image_input = data[0].get('path', None)
|
56 |
if not image_input:
|
57 |
-
return json.dumps({"error": "No image provided."})
|
58 |
|
59 |
print(f"Received image input: {image_input}")
|
60 |
|
61 |
-
#
|
62 |
-
if isinstance(image_input, str):
|
63 |
-
|
64 |
-
|
65 |
-
|
66 |
-
|
67 |
-
|
68 |
-
|
69 |
-
|
70 |
-
|
71 |
-
|
72 |
-
|
73 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
74 |
|
75 |
-
# Apply transformations
|
76 |
image = transform(image).unsqueeze(0)
|
77 |
print(f"Transformed image tensor: {image.shape}")
|
78 |
-
|
79 |
image = image.to(torch.device("cuda" if torch.cuda.is_available() else "cpu"))
|
80 |
|
81 |
with torch.no_grad():
|
@@ -83,25 +91,15 @@ def predict(data):
|
|
83 |
predicted_class = torch.argmax(outputs, dim=1).item()
|
84 |
print(f"Prediction output: {outputs}, Predicted class: {predicted_class}")
|
85 |
|
|
|
86 |
if predicted_class == 0:
|
87 |
return json.dumps({"result": "The photo you've sent is of fall army worm with problem ID 126."})
|
88 |
elif predicted_class == 1:
|
89 |
return json.dumps({"result": "The photo you've sent is of a healthy maize image."})
|
90 |
else:
|
91 |
return json.dumps({"error": "Unexpected class prediction."})
|
|
|
92 |
except Exception as e:
|
93 |
print(f"Error processing image: {e}")
|
94 |
return json.dumps({"error": f"Error processing image: {e}"})
|
95 |
|
96 |
-
# Create the Gradio interface
|
97 |
-
iface = gr.Interface(
|
98 |
-
fn=predict,
|
99 |
-
inputs=gr.JSON(label="Input JSON"),
|
100 |
-
outputs=gr.Textbox(label="Prediction Result"),
|
101 |
-
live=True,
|
102 |
-
title="Maize Anomaly Detection",
|
103 |
-
description="Upload an image of maize to detect anomalies like disease or pest infestation. You can provide local paths, URLs, or base64-encoded images."
|
104 |
-
)
|
105 |
-
|
106 |
-
# Launch the Gradio interface
|
107 |
-
iface.launch(share=True, show_error=True)
|
|
|
48 |
|
49 |
def predict(data):
|
50 |
try:
|
51 |
+
# Check if the data is a list and not empty
|
52 |
if not isinstance(data, list) or len(data) == 0:
|
53 |
return json.dumps({"error": "Input data should be a non-empty list."})
|
54 |
|
55 |
+
# Extract the image path
|
56 |
image_input = data[0].get('path', None)
|
57 |
if not image_input:
|
58 |
+
return json.dumps({"error": "No image path provided."})
|
59 |
|
60 |
print(f"Received image input: {image_input}")
|
61 |
|
62 |
+
# Handle URLs
|
63 |
+
if isinstance(image_input, str) and (image_input.startswith("http://") or image_input.startswith("https://")):
|
64 |
+
try:
|
65 |
+
response = requests.get(image_input)
|
66 |
+
response.raise_for_status() # Check for HTTP errors
|
67 |
+
image = Image.open(BytesIO(response.content))
|
68 |
+
print(f"Fetched image from URL: {image}")
|
69 |
+
except Exception as e:
|
70 |
+
print(f"Error fetching image from URL: {e}")
|
71 |
+
return json.dumps({"error": f"Error fetching image from URL: {e}"})
|
72 |
+
|
73 |
+
# Check if the image path is a valid local path
|
74 |
+
elif isinstance(image_input, str) and os.path.exists(image_input):
|
75 |
+
try:
|
76 |
+
image = Image.open(image_input)
|
77 |
+
print(f"Loaded image from local path: {image}")
|
78 |
+
except Exception as e:
|
79 |
+
return json.dumps({"error": f"Error loading image from local path: {e}"})
|
80 |
+
|
81 |
+
else:
|
82 |
+
return json.dumps({"error": "Invalid image path. Ensure it's a valid URL or local path."})
|
83 |
|
84 |
+
# Apply the transformations and make prediction
|
85 |
image = transform(image).unsqueeze(0)
|
86 |
print(f"Transformed image tensor: {image.shape}")
|
|
|
87 |
image = image.to(torch.device("cuda" if torch.cuda.is_available() else "cpu"))
|
88 |
|
89 |
with torch.no_grad():
|
|
|
91 |
predicted_class = torch.argmax(outputs, dim=1).item()
|
92 |
print(f"Prediction output: {outputs}, Predicted class: {predicted_class}")
|
93 |
|
94 |
+
# Return the result based on the predicted class
|
95 |
if predicted_class == 0:
|
96 |
return json.dumps({"result": "The photo you've sent is of fall army worm with problem ID 126."})
|
97 |
elif predicted_class == 1:
|
98 |
return json.dumps({"result": "The photo you've sent is of a healthy maize image."})
|
99 |
else:
|
100 |
return json.dumps({"error": "Unexpected class prediction."})
|
101 |
+
|
102 |
except Exception as e:
|
103 |
print(f"Error processing image: {e}")
|
104 |
return json.dumps({"error": f"Error processing image: {e}"})
|
105 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|