Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
@@ -54,37 +54,25 @@ def predict(image):
|
|
54 |
if isinstance(image, Image.Image):
|
55 |
print(f"Image is already loaded as PIL Image: {image}")
|
56 |
else:
|
57 |
-
#
|
58 |
if isinstance(image, dict) and image.get("data"):
|
59 |
-
|
60 |
-
|
|
|
|
|
|
|
|
|
|
|
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 |
-
# Try to fetch the image from a URL
|
68 |
elif isinstance(image, str) and image.startswith("http"):
|
69 |
-
|
70 |
-
|
71 |
-
|
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):
|
79 |
-
|
80 |
-
|
81 |
-
|
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 |
|
@@ -112,7 +100,7 @@ def predict(image):
|
|
112 |
# Create the Gradio interface
|
113 |
iface = gr.Interface(
|
114 |
fn=predict,
|
115 |
-
inputs=gr.
|
116 |
outputs=gr.Textbox(label="Prediction Result"),
|
117 |
live=True,
|
118 |
title="Maize Anomaly Detection",
|
@@ -121,3 +109,4 @@ iface = gr.Interface(
|
|
121 |
|
122 |
# Launch the Gradio interface
|
123 |
iface.launch(share=True, show_error=True)
|
|
|
|
54 |
if isinstance(image, Image.Image):
|
55 |
print(f"Image is already loaded as PIL Image: {image}")
|
56 |
else:
|
57 |
+
# Check if the input contains a base64-encoded string or URL
|
58 |
if isinstance(image, dict) and image.get("data"):
|
59 |
+
image_data = image["data"]
|
60 |
+
if image_data.startswith("http"): # URL case
|
61 |
+
response = requests.get(image_data)
|
62 |
+
image = Image.open(BytesIO(response.content))
|
63 |
+
print(f"Fetched image from URL: {image}")
|
64 |
+
else: # Base64-encoded image case
|
65 |
+
image_data = base64.b64decode(image_data)
|
66 |
image = Image.open(BytesIO(image_data))
|
67 |
print(f"Decoded base64 image: {image}")
|
|
|
|
|
|
|
|
|
|
|
68 |
elif isinstance(image, str) and image.startswith("http"):
|
69 |
+
response = requests.get(image)
|
70 |
+
image = Image.open(BytesIO(response.content))
|
71 |
+
print(f"Fetched image from URL: {image}")
|
|
|
|
|
|
|
|
|
|
|
|
|
72 |
elif isinstance(image, str) and os.path.isfile(image):
|
73 |
+
image = Image.open(image)
|
74 |
+
print(f"Loaded image from local path: {image}")
|
75 |
+
else:
|
|
|
|
|
|
|
|
|
|
|
|
|
76 |
print("Invalid image format received.")
|
77 |
return json.dumps({"error": "Invalid image format received."})
|
78 |
|
|
|
100 |
# Create the Gradio interface
|
101 |
iface = gr.Interface(
|
102 |
fn=predict,
|
103 |
+
inputs=gr.JSON(label="Input JSON"),
|
104 |
outputs=gr.Textbox(label="Prediction Result"),
|
105 |
live=True,
|
106 |
title="Maize Anomaly Detection",
|
|
|
109 |
|
110 |
# Launch the Gradio interface
|
111 |
iface.launch(share=True, show_error=True)
|
112 |
+
|