Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -8,10 +8,6 @@ from PIL import Image
|
|
8 |
import requests
|
9 |
import os
|
10 |
from io import BytesIO
|
11 |
-
import logging
|
12 |
-
|
13 |
-
# Set up basic logging
|
14 |
-
logging.basicConfig(level=logging.INFO)
|
15 |
|
16 |
# Define the number of classes
|
17 |
num_classes = 2
|
@@ -41,9 +37,6 @@ transform = transforms.Compose([
|
|
41 |
transforms.Normalize([0.485, 0.456, 0.406], [0.229, 0.224, 0.225]),
|
42 |
])
|
43 |
|
44 |
-
# Global variable to store the file path
|
45 |
-
file_path = None
|
46 |
-
|
47 |
# Function to predict from image content
|
48 |
def predict_from_image(image):
|
49 |
# Ensure the image is a PIL Image
|
@@ -66,46 +59,16 @@ def predict_from_image(image):
|
|
66 |
else:
|
67 |
return {"error": "Unexpected class prediction."}
|
68 |
|
69 |
-
# Function to handle the file path sent via POST request
|
70 |
-
def process_file_path(file_path_input):
|
71 |
-
global file_path
|
72 |
-
file_path = file_path_input # Store the file path
|
73 |
-
|
74 |
-
logging.info(f"Received file path: {file_path}")
|
75 |
-
|
76 |
-
# Ensure the path is correctly formatted for Windows
|
77 |
-
file_path = file_path.replace("\\", "/") # Replace backslashes with forward slashes
|
78 |
-
logging.info(f"Corrected file path: {file_path}")
|
79 |
-
|
80 |
-
if not os.path.exists(file_path):
|
81 |
-
logging.error(f"File not found at {file_path}")
|
82 |
-
return {"error": f"File not found at {file_path}"}
|
83 |
-
|
84 |
-
image = Image.open(file_path)
|
85 |
-
logging.info(f"Processing image from path: {file_path}")
|
86 |
-
return predict_from_image(image)
|
87 |
-
|
88 |
-
|
89 |
-
# Function to fetch the result (for the GET request)
|
90 |
-
def fetch_result():
|
91 |
-
if file_path:
|
92 |
-
image = Image.open(file_path)
|
93 |
-
logging.info(f"Making prediction for image at path: {file_path}")
|
94 |
-
return predict_from_image(image)
|
95 |
-
else:
|
96 |
-
logging.warning("No file path available. Please send a POST request with a file path first.")
|
97 |
-
return {"error": "No file path available. Please send a POST request with a file path first."}
|
98 |
-
|
99 |
# Gradio interface
|
100 |
iface = gr.Interface(
|
101 |
-
fn=
|
102 |
inputs=[
|
103 |
-
gr.
|
104 |
],
|
105 |
outputs=gr.JSON(label="Prediction Result"),
|
106 |
-
live=
|
107 |
title="Maize Anomaly Detection",
|
108 |
-
description="
|
109 |
)
|
110 |
|
111 |
# Launch the interface
|
|
|
8 |
import requests
|
9 |
import os
|
10 |
from io import BytesIO
|
|
|
|
|
|
|
|
|
11 |
|
12 |
# Define the number of classes
|
13 |
num_classes = 2
|
|
|
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 |
else:
|
60 |
return {"error": "Unexpected class prediction."}
|
61 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
62 |
# Gradio interface
|
63 |
iface = gr.Interface(
|
64 |
+
fn=lambda image: predict_from_image(image)
|
65 |
inputs=[
|
66 |
+
gr.Image(type="pil", label="Upload an Image"),
|
67 |
],
|
68 |
outputs=gr.JSON(label="Prediction Result"),
|
69 |
+
live=True,
|
70 |
title="Maize Anomaly Detection",
|
71 |
+
description="Upload an image or provide a URL to detect anomalies in maize crops.",
|
72 |
)
|
73 |
|
74 |
# Launch the interface
|