jays009 commited on
Commit
a53a305
·
verified ·
1 Parent(s): 5b86dff

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +33 -23
app.py CHANGED
@@ -39,6 +39,9 @@ transform = transforms.Compose([
39
  # Function to predict from image content
40
  def predict_from_image(image):
41
  try:
 
 
 
42
  # Ensure the image is a PIL Image
43
  if not isinstance(image, Image.Image):
44
  raise ValueError("Invalid image format received. Please provide a valid image.")
@@ -53,41 +56,51 @@ def predict_from_image(image):
53
 
54
  # Interpret the result
55
  if predicted_class == 0:
56
- return {"status": "success", "result": "Fall army worm detected (Problem ID: 126)."}
57
  elif predicted_class == 1:
58
- return {"status": "success", "result": "Healthy maize image detected."}
59
  else:
60
- return {"status": "error", "message": "Unexpected class prediction."}
 
61
  except Exception as e:
62
- return {"status": "error", "message": f"Error during prediction: {str(e)}"}
 
63
 
64
  # Function to predict from URL
65
  def predict_from_url(url):
66
  try:
67
- if not url.startswith(("http://", "https://")):
68
- raise ValueError("Invalid URL format. Please provide a valid image URL.")
69
-
70
  response = requests.get(url)
71
  response.raise_for_status() # Ensure the request was successful
72
  image = Image.open(BytesIO(response.content))
 
73
  return predict_from_image(image)
74
  except Exception as e:
75
- return {"status": "error", "message": f"Failed to process the URL: {str(e)}"}
 
76
 
77
- # Combined prediction function for Gradio
78
- def combined_predict(image, url):
79
- if image and url:
80
- return {"status": "error", "message": "Provide either an image or a URL, not both."}
81
- elif image:
82
- return predict_from_image(image)
83
- elif url:
84
- return predict_from_url(url)
85
- else:
86
- return {"status": "error", "message": "No input provided. Please upload an image or provide a URL."}
 
 
 
 
 
 
 
 
87
 
88
- # Gradio interface
89
  iface = gr.Interface(
90
- fn=combined_predict,
91
  inputs=[
92
  gr.Image(type="pil", label="Upload an Image"),
93
  gr.Textbox(label="Or Enter an Image URL", placeholder="Provide a valid image URL"),
@@ -96,9 +109,6 @@ iface = gr.Interface(
96
  live=True,
97
  title="Maize Anomaly Detection",
98
  description="Upload an image or provide a URL to detect anomalies in maize crops.",
99
- examples=[
100
- [None, "https://example.com/sample-image.jpg"], # Replace with a valid example URL
101
- ]
102
  )
103
 
104
  # Launch the interface
 
39
  # Function to predict from image content
40
  def predict_from_image(image):
41
  try:
42
+ # Log the image processing
43
+ print(f"Processing image: {image}")
44
+
45
  # Ensure the image is a PIL Image
46
  if not isinstance(image, Image.Image):
47
  raise ValueError("Invalid image format received. Please provide a valid image.")
 
56
 
57
  # Interpret the result
58
  if predicted_class == 0:
59
+ return {"result": "The photo is of fall army worm with problem ID 126."}
60
  elif predicted_class == 1:
61
+ return {"result": "The photo is of a healthy maize image."}
62
  else:
63
+ return {"error": "Unexpected class prediction."}
64
+
65
  except Exception as e:
66
+ print(f"Error during image processing: {e}")
67
+ return {"error": str(e)}
68
 
69
  # Function to predict from URL
70
  def predict_from_url(url):
71
  try:
72
+ # Fetch the image from the URL
 
 
73
  response = requests.get(url)
74
  response.raise_for_status() # Ensure the request was successful
75
  image = Image.open(BytesIO(response.content))
76
+ print(f"Fetched image from URL: {url}")
77
  return predict_from_image(image)
78
  except Exception as e:
79
+ print(f"Error during URL processing: {e}")
80
+ return {"error": f"Failed to process the URL: {str(e)}"}
81
 
82
+ # Gradio interface with logging
83
+ def predict(image, url):
84
+ try:
85
+ if image:
86
+ result = predict_from_image(image)
87
+ elif url:
88
+ result = predict_from_url(url)
89
+ else:
90
+ result = {"error": "No input provided. Please upload an image or provide a URL."}
91
+
92
+ # Log the result
93
+ event_id = id(result) # Generate a simple event ID for logging purposes
94
+ print(f"Event ID: {event_id}, Result: {result}")
95
+ return result
96
+
97
+ except Exception as e:
98
+ print(f"Error in prediction function: {e}")
99
+ return {"error": str(e)}
100
 
101
+ # Gradio interface setup
102
  iface = gr.Interface(
103
+ fn=predict,
104
  inputs=[
105
  gr.Image(type="pil", label="Upload an Image"),
106
  gr.Textbox(label="Or Enter an Image URL", placeholder="Provide a valid image URL"),
 
109
  live=True,
110
  title="Maize Anomaly Detection",
111
  description="Upload an image or provide a URL to detect anomalies in maize crops.",
 
 
 
112
  )
113
 
114
  # Launch the interface