Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -46,27 +46,21 @@ transform = transforms.Compose([
|
|
46 |
transforms.Normalize([0.485, 0.456, 0.406], [0.229, 0.224, 0.225]),
|
47 |
])
|
48 |
|
49 |
-
def predict(
|
50 |
try:
|
51 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
52 |
|
53 |
-
|
54 |
-
if isinstance(image, Image.Image):
|
55 |
-
print(f"Image is already loaded as PIL Image: {image}")
|
56 |
-
else:
|
57 |
-
# Try to handle base64-encoded image
|
58 |
-
if isinstance(image, dict) and image.get("data"):
|
59 |
-
try:
|
60 |
-
image_data = base64.b64decode(image["data"])
|
61 |
-
image = Image.open(BytesIO(image_data))
|
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 |
# Check if the input is a URL
|
68 |
if isinstance(image_input, str):
|
69 |
-
|
70 |
try:
|
71 |
response = requests.get(image_input)
|
72 |
response.raise_for_status() # Check for HTTP errors
|
@@ -75,22 +69,8 @@ def predict(image):
|
|
75 |
except Exception as e:
|
76 |
print(f"Error fetching image from URL: {e}")
|
77 |
return json.dumps({"error": f"Error fetching image from URL: {e}"})
|
78 |
-
|
79 |
-
|
80 |
-
|
81 |
-
# Try to load the image from a local file path
|
82 |
-
elif isinstance(image, str) and os.path.isfile(image):
|
83 |
-
try:
|
84 |
-
image = Image.open(image)
|
85 |
-
print(f"Loaded image from local path: {image}")
|
86 |
-
except Exception as e:
|
87 |
-
print(f"Error loading image from local path: {e}")
|
88 |
-
return json.dumps({"error": f"Error loading image from local path: {e}"})
|
89 |
-
|
90 |
-
# Validate that the image is correctly loaded
|
91 |
-
if not isinstance(image, Image.Image):
|
92 |
-
print("Invalid image format received.")
|
93 |
-
return json.dumps({"error": "Invalid image format received."})
|
94 |
|
95 |
# Apply transformations
|
96 |
image = transform(image).unsqueeze(0)
|
@@ -116,7 +96,7 @@ def predict(image):
|
|
116 |
# Create the Gradio interface
|
117 |
iface = gr.Interface(
|
118 |
fn=predict,
|
119 |
-
inputs=gr.
|
120 |
outputs=gr.Textbox(label="Prediction Result"),
|
121 |
live=True,
|
122 |
title="Maize Anomaly Detection",
|
|
|
46 |
transforms.Normalize([0.485, 0.456, 0.406], [0.229, 0.224, 0.225]),
|
47 |
])
|
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 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 |
# Check if the input is a URL
|
62 |
if isinstance(image_input, str):
|
63 |
+
if 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
|
|
|
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 |
+
else:
|
73 |
+
return json.dumps({"error": "Invalid URL format. Please provide a valid URL starting with 'http://' or 'https://'."})
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
74 |
|
75 |
# Apply transformations
|
76 |
image = transform(image).unsqueeze(0)
|
|
|
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",
|