Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
@@ -45,12 +45,29 @@ def predict_from_image(image_url):
|
|
45 |
try:
|
46 |
# Download the image from the provided URL
|
47 |
response = requests.get(image_url)
|
|
|
48 |
image = Image.open(BytesIO(response.content))
|
49 |
-
|
50 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
51 |
except Exception as e:
|
52 |
return {"error": str(e)}
|
53 |
|
|
|
54 |
demo = gr.Interface(
|
55 |
fn=predict_from_image,
|
56 |
inputs="text",
|
|
|
45 |
try:
|
46 |
# Download the image from the provided URL
|
47 |
response = requests.get(image_url)
|
48 |
+
response.raise_for_status() # Check if the request was successful
|
49 |
image = Image.open(BytesIO(response.content))
|
50 |
+
|
51 |
+
# Apply transformations
|
52 |
+
image_tensor = transform(image).unsqueeze(0) # Add batch dimension
|
53 |
+
|
54 |
+
# Perform prediction
|
55 |
+
with torch.no_grad():
|
56 |
+
outputs = model(image_tensor)
|
57 |
+
predicted_class = torch.argmax(outputs, dim=1).item()
|
58 |
+
|
59 |
+
# Interpret the result
|
60 |
+
if predicted_class == 0:
|
61 |
+
return {"result": "The photo is of fall army worm with problem ID 126."}
|
62 |
+
elif predicted_class == 1:
|
63 |
+
return {"result": "The photo is of a healthy maize image."}
|
64 |
+
else:
|
65 |
+
return {"error": "Unexpected class prediction."}
|
66 |
+
|
67 |
except Exception as e:
|
68 |
return {"error": str(e)}
|
69 |
|
70 |
+
|
71 |
demo = gr.Interface(
|
72 |
fn=predict_from_image,
|
73 |
inputs="text",
|