jays009 commited on
Commit
bf2b357
·
verified ·
1 Parent(s): 4dd171e

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +15 -36
app.py CHANGED
@@ -39,44 +39,23 @@ transform = transforms.Compose([
39
  ])
40
 
41
  # Prediction function for an uploaded image
42
- def predict_from_image(image):
43
  try:
44
- # Ensure the input is a valid PIL image
45
- if not isinstance(image, Image.Image):
46
- raise ValueError("Invalid image format received. Please provide a valid image.")
47
-
48
- # Log the input for debugging
49
- logging.info("Received image for prediction")
50
-
51
- # Apply transformations
52
- image_tensor = transform(image).unsqueeze(0)
53
-
54
- # Predict
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
  except Exception as e:
67
- logging.error(f"Error during prediction: {str(e)}")
68
- return {"error": f"Failed to process the image: {str(e)}"}
69
-
70
- # Gradio interface restricted to image input
71
- iface = gr.Interface(
72
- fn=predict_from_image, # Only handle image input
73
- inputs=gr.Image(type="pil", label="Upload an Image"), # Restrict input to image upload
74
- outputs=gr.JSON(label="Prediction Result"),
75
- live=True,
76
- title="Maize Anomaly Detection",
77
- description="Upload an image to detect anomalies in maize crops.",
78
  )
79
 
80
- # Launch the interface locally
81
  if __name__ == "__main__":
82
- iface.launch()
 
39
  ])
40
 
41
  # Prediction function for an uploaded image
42
+ def predict_from_image(image_url):
43
  try:
44
+ # Download the image from the provided URL
45
+ response = requests.get(image_url)
46
+ image = Image.open(BytesIO(response.content))
47
+ # Process the image...
48
+ return {"result": "Image processed successfully"}
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
49
  except Exception as e:
50
+ return {"error": str(e)}
51
+
52
+ demo = gr.Interface(
53
+ fn=predict_from_image,
54
+ inputs="text",
55
+ outputs="json",
56
+ title="Image Processing",
57
+ description="Enter a URL to an image",
 
 
 
58
  )
59
 
 
60
  if __name__ == "__main__":
61
+ demo.launch()