Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -1,4 +1,5 @@
|
|
1 |
import gradio as gr
|
|
|
2 |
import torch
|
3 |
from torch import nn
|
4 |
from torchvision import models, transforms
|
@@ -48,7 +49,7 @@ transform = transforms.Compose([
|
|
48 |
def predict(image):
|
49 |
try:
|
50 |
print(f"Received image input: {image}")
|
51 |
-
|
52 |
# Check if the input is a PIL Image type
|
53 |
if isinstance(image, Image.Image):
|
54 |
print(f"Image is already loaded as PIL Image: {image}")
|
@@ -61,7 +62,7 @@ def predict(image):
|
|
61 |
print(f"Decoded base64 image: {image}")
|
62 |
except Exception as e:
|
63 |
print(f"Error decoding base64 image: {e}")
|
64 |
-
return f"Error decoding base64 image: {e}"
|
65 |
|
66 |
# Try to fetch the image from a URL
|
67 |
elif isinstance(image, str) and image.startswith("http"):
|
@@ -71,7 +72,7 @@ def predict(image):
|
|
71 |
print(f"Fetched image from URL: {image}")
|
72 |
except Exception as e:
|
73 |
print(f"Error fetching image from URL: {e}")
|
74 |
-
return f"Error fetching image from URL: {e}"
|
75 |
|
76 |
# Try to load the image from a local file path
|
77 |
elif isinstance(image, str) and os.path.isfile(image):
|
@@ -80,12 +81,12 @@ def predict(image):
|
|
80 |
print(f"Loaded image from local path: {image}")
|
81 |
except Exception as e:
|
82 |
print(f"Error loading image from local path: {e}")
|
83 |
-
return f"Error loading image from local path: {e}"
|
84 |
|
85 |
# Validate that the image is correctly loaded
|
86 |
if not isinstance(image, Image.Image):
|
87 |
print("Invalid image format received.")
|
88 |
-
return "Invalid image format received."
|
89 |
|
90 |
# Apply transformations
|
91 |
image = transform(image).unsqueeze(0)
|
@@ -99,19 +100,19 @@ def predict(image):
|
|
99 |
print(f"Prediction output: {outputs}, Predicted class: {predicted_class}")
|
100 |
|
101 |
if predicted_class == 0:
|
102 |
-
return "The photo you've sent is of fall army worm with problem ID 126."
|
103 |
elif predicted_class == 1:
|
104 |
-
return "The photo you've sent is of a healthy maize image."
|
105 |
else:
|
106 |
-
return "Unexpected class prediction."
|
107 |
except Exception as e:
|
108 |
print(f"Error processing image: {e}")
|
109 |
-
return f"Error processing image: {e}"
|
110 |
|
111 |
# Create the Gradio interface
|
112 |
iface = gr.Interface(
|
113 |
fn=predict,
|
114 |
-
inputs=gr.Image(type="pil", label="Upload an image or provide a URL or local path"),
|
115 |
outputs=gr.Textbox(label="Prediction Result"),
|
116 |
live=True,
|
117 |
title="Maize Anomaly Detection",
|
|
|
1 |
import gradio as gr
|
2 |
+
import json
|
3 |
import torch
|
4 |
from torch import nn
|
5 |
from torchvision import models, transforms
|
|
|
49 |
def predict(image):
|
50 |
try:
|
51 |
print(f"Received image input: {image}")
|
52 |
+
|
53 |
# Check if the input is a PIL Image type
|
54 |
if isinstance(image, Image.Image):
|
55 |
print(f"Image is already loaded as PIL Image: {image}")
|
|
|
62 |
print(f"Decoded base64 image: {image}")
|
63 |
except Exception as e:
|
64 |
print(f"Error decoding base64 image: {e}")
|
65 |
+
return json.dumps({"error": f"Error decoding base64 image: {e}"})
|
66 |
|
67 |
# Try to fetch the image from a URL
|
68 |
elif isinstance(image, str) and image.startswith("http"):
|
|
|
72 |
print(f"Fetched image from URL: {image}")
|
73 |
except Exception as e:
|
74 |
print(f"Error fetching image from URL: {e}")
|
75 |
+
return json.dumps({"error": f"Error fetching image from URL: {e}"})
|
76 |
|
77 |
# Try to load the image from a local file path
|
78 |
elif isinstance(image, str) and os.path.isfile(image):
|
|
|
81 |
print(f"Loaded image from local path: {image}")
|
82 |
except Exception as e:
|
83 |
print(f"Error loading image from local path: {e}")
|
84 |
+
return json.dumps({"error": f"Error loading image from local path: {e}"})
|
85 |
|
86 |
# Validate that the image is correctly loaded
|
87 |
if not isinstance(image, Image.Image):
|
88 |
print("Invalid image format received.")
|
89 |
+
return json.dumps({"error": "Invalid image format received."})
|
90 |
|
91 |
# Apply transformations
|
92 |
image = transform(image).unsqueeze(0)
|
|
|
100 |
print(f"Prediction output: {outputs}, Predicted class: {predicted_class}")
|
101 |
|
102 |
if predicted_class == 0:
|
103 |
+
return json.dumps({"result": "The photo you've sent is of fall army worm with problem ID 126."})
|
104 |
elif predicted_class == 1:
|
105 |
+
return json.dumps({"result": "The photo you've sent is of a healthy maize image."})
|
106 |
else:
|
107 |
+
return json.dumps({"error": "Unexpected class prediction."})
|
108 |
except Exception as e:
|
109 |
print(f"Error processing image: {e}")
|
110 |
+
return json.dumps({"error": f"Error processing image: {e}"})
|
111 |
|
112 |
# Create the Gradio interface
|
113 |
iface = gr.Interface(
|
114 |
fn=predict,
|
115 |
+
inputs=gr.Image(type="pil", label="Upload an image or provide a URL or local path"),
|
116 |
outputs=gr.Textbox(label="Prediction Result"),
|
117 |
live=True,
|
118 |
title="Maize Anomaly Detection",
|