Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
@@ -37,6 +37,9 @@ transform = transforms.Compose([
|
|
37 |
transforms.Normalize([0.485, 0.456, 0.406], [0.229, 0.224, 0.225]),
|
38 |
])
|
39 |
|
|
|
|
|
|
|
40 |
# Function to predict from image content
|
41 |
def predict_from_image(image):
|
42 |
# Ensure the image is a PIL Image
|
@@ -59,38 +62,33 @@ def predict_from_image(image):
|
|
59 |
else:
|
60 |
return {"error": "Unexpected class prediction."}
|
61 |
|
62 |
-
# Function to handle
|
63 |
-
def
|
64 |
-
|
65 |
-
|
66 |
-
|
67 |
-
|
68 |
-
|
69 |
-
|
70 |
-
|
71 |
-
|
72 |
-
|
73 |
-
|
74 |
-
|
75 |
-
|
76 |
-
|
77 |
-
|
78 |
-
else:
|
79 |
-
return {"error": "No valid input provided."}
|
80 |
-
except Exception as e:
|
81 |
-
return {"error": f"Failed to process the input: {str(e)}"}
|
82 |
|
83 |
# Gradio interface
|
84 |
iface = gr.Interface(
|
85 |
-
fn=
|
86 |
inputs=[
|
87 |
-
gr.Textbox(label="Enter Image
|
88 |
-
gr.Textbox(label="Or Enter Local Image Path", placeholder="Provide the local image path (optional)", optional=True),
|
89 |
],
|
90 |
outputs=gr.JSON(label="Prediction Result"),
|
91 |
-
live=
|
92 |
title="Maize Anomaly Detection",
|
93 |
-
description="Provide
|
94 |
)
|
95 |
|
96 |
# Launch the interface
|
|
|
37 |
transforms.Normalize([0.485, 0.456, 0.406], [0.229, 0.224, 0.225]),
|
38 |
])
|
39 |
|
40 |
+
# Global variable to store the file path
|
41 |
+
file_path = None
|
42 |
+
|
43 |
# Function to predict from image content
|
44 |
def predict_from_image(image):
|
45 |
# Ensure the image is a PIL Image
|
|
|
62 |
else:
|
63 |
return {"error": "Unexpected class prediction."}
|
64 |
|
65 |
+
# Function to handle the file path sent via POST request
|
66 |
+
def process_file_path(file_path_input):
|
67 |
+
global file_path
|
68 |
+
file_path = file_path_input # Store the file path
|
69 |
+
if not os.path.exists(file_path):
|
70 |
+
return {"error": f"File not found at {file_path}"}
|
71 |
+
image = Image.open(file_path)
|
72 |
+
return predict_from_image(image)
|
73 |
+
|
74 |
+
# Function to fetch the result (for the GET request)
|
75 |
+
def fetch_result():
|
76 |
+
if file_path:
|
77 |
+
image = Image.open(file_path)
|
78 |
+
return predict_from_image(image)
|
79 |
+
else:
|
80 |
+
return {"error": "No file path available. Please send a POST request with a file path first."}
|
|
|
|
|
|
|
|
|
81 |
|
82 |
# Gradio interface
|
83 |
iface = gr.Interface(
|
84 |
+
fn=process_file_path,
|
85 |
inputs=[
|
86 |
+
gr.Textbox(label="Enter Local Image Path", placeholder="Provide the local image path"),
|
|
|
87 |
],
|
88 |
outputs=gr.JSON(label="Prediction Result"),
|
89 |
+
live=False,
|
90 |
title="Maize Anomaly Detection",
|
91 |
+
description="Provide a local file path via POST request to process an image.",
|
92 |
)
|
93 |
|
94 |
# Launch the interface
|