Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
@@ -58,36 +58,59 @@ transform = transforms.Compose([
|
|
58 |
])
|
59 |
|
60 |
# Prediction function for an uploaded image
|
61 |
-
|
62 |
def predict_from_image_url(image_url):
|
63 |
try:
|
64 |
# Download the image from the provided URL
|
65 |
response = requests.get(image_url)
|
66 |
response.raise_for_status()
|
67 |
-
image = Image.open(BytesIO(response.content))
|
68 |
|
69 |
# Apply transformations
|
70 |
-
image_tensor = transform(image).unsqueeze(0)
|
|
|
71 |
|
72 |
# Perform prediction
|
73 |
with torch.no_grad():
|
74 |
-
outputs = model(image_tensor)
|
|
|
|
|
75 |
predicted_class = torch.argmax(outputs, dim=1).item()
|
76 |
|
77 |
# Interpret the result
|
78 |
if predicted_class == 0:
|
79 |
-
return {
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
80 |
elif predicted_class == 1:
|
81 |
-
return {
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
82 |
elif predicted_class == 2:
|
83 |
-
return {
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
84 |
else:
|
85 |
return {"error": "Unexpected class prediction."}
|
86 |
|
87 |
except Exception as e:
|
88 |
return {"error": str(e)}
|
89 |
|
90 |
-
|
91 |
demo = gr.Interface(
|
92 |
fn=predict_from_image_url,
|
93 |
inputs="text",
|
|
|
58 |
])
|
59 |
|
60 |
# Prediction function for an uploaded image
|
|
|
61 |
def predict_from_image_url(image_url):
|
62 |
try:
|
63 |
# Download the image from the provided URL
|
64 |
response = requests.get(image_url)
|
65 |
response.raise_for_status()
|
66 |
+
image = Image.open(BytesIO(response.content)).convert("RGB") # Convert to RGB (3 channels)
|
67 |
|
68 |
# Apply transformations
|
69 |
+
image_tensor = transform(image).unsqueeze(0) # Shape: [1, 3, 224, 224]
|
70 |
+
print(f"Input image tensor shape: {image_tensor.shape}") # Debug: Should be [1, 3, 224, 224]
|
71 |
|
72 |
# Perform prediction
|
73 |
with torch.no_grad():
|
74 |
+
outputs = model(image_tensor) # Shape: [1, 3]
|
75 |
+
print(f"Model output shape: {outputs.shape}") # Debug: Should be [1, 3]
|
76 |
+
probabilities = torch.softmax(outputs, dim=1)[0] # Convert to probabilities
|
77 |
predicted_class = torch.argmax(outputs, dim=1).item()
|
78 |
|
79 |
# Interpret the result
|
80 |
if predicted_class == 0:
|
81 |
+
return {
|
82 |
+
"result": "The photo is of Fall Army Worm with problem ID 126.",
|
83 |
+
"probabilities": {
|
84 |
+
"Fall Army Worm": f"{probabilities[0]*100:.2f}%",
|
85 |
+
"Phosphorus Deficiency": f"{probabilities[1]*100:.2f}%",
|
86 |
+
"Bacterial Leaf Blight": f"{probabilities[2]*100:.2f}%"
|
87 |
+
}
|
88 |
+
}
|
89 |
elif predicted_class == 1:
|
90 |
+
return {
|
91 |
+
"result": "The photo shows symptoms of Phosphorus Deficiency with Problem ID 142.",
|
92 |
+
"probabilities": {
|
93 |
+
"Fall Army Worm": f"{probabilities[0]*100:.2f}%",
|
94 |
+
"Phosphorus Deficiency": f"{probabilities[1]*100:.2f}%",
|
95 |
+
"Bacterial Leaf Blight": f"{probabilities[2]*100:.2f}%"
|
96 |
+
}
|
97 |
+
}
|
98 |
elif predicted_class == 2:
|
99 |
+
return {
|
100 |
+
"result": "The photo shows symptoms of Bacterial Leaf Blight with Problem ID 203.",
|
101 |
+
"probabilities": {
|
102 |
+
"Fall Army Worm": f"{probabilities[0]*100:.2f}%",
|
103 |
+
"Phosphorus Deficiency": f"{probabilities[1]*100:.2f}%",
|
104 |
+
"Bacterial Leaf Blight": f"{probabilities[2]*100:.2f}%"
|
105 |
+
}
|
106 |
+
}
|
107 |
else:
|
108 |
return {"error": "Unexpected class prediction."}
|
109 |
|
110 |
except Exception as e:
|
111 |
return {"error": str(e)}
|
112 |
|
113 |
+
# Gradio interface
|
114 |
demo = gr.Interface(
|
115 |
fn=predict_from_image_url,
|
116 |
inputs="text",
|