jays009 commited on
Commit
6ba58ea
·
verified ·
1 Parent(s): 47d3394

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +9 -10
app.py CHANGED
@@ -48,13 +48,12 @@ transform = transforms.Compose([
48
 
49
  def predict(data):
50
  try:
51
- # Expecting data to be a list
52
  if not isinstance(data, list) or len(data) == 0:
53
- return json.dumps({"error": "Input data should be a non-empty list."})
54
 
55
  image_input = data[0].get('image', None)
56
  if not image_input:
57
- return json.dumps({"error": "No image provided."})
58
 
59
  print(f"Received image input: {image_input}")
60
 
@@ -70,7 +69,7 @@ def predict(data):
70
  print(f"Fetched image from URL: {image}")
71
  except Exception as e:
72
  print(f"Error fetching image from URL: {e}")
73
- return json.dumps({"error": f"Error fetching image from URL: {e}"})
74
  else: # Assuming it is base64-encoded image data
75
  try:
76
  image_data = base64.b64decode(image_input)
@@ -78,7 +77,7 @@ def predict(data):
78
  print(f"Decoded base64 image: {image}")
79
  except Exception as e:
80
  print(f"Error decoding base64 image: {e}")
81
- return json.dumps({"error": f"Error decoding base64 image: {e}"})
82
 
83
  # Apply transformations
84
  image = transform(image).unsqueeze(0)
@@ -92,20 +91,20 @@ def predict(data):
92
  print(f"Prediction output: {outputs}, Predicted class: {predicted_class}")
93
 
94
  if predicted_class == 0:
95
- return json.dumps({"result": "The photo you've sent is of fall army worm with problem ID 126."})
96
  elif predicted_class == 1:
97
- return json.dumps({"result": "The photo you've sent is of a healthy maize image."})
98
  else:
99
- return json.dumps({"error": "Unexpected class prediction."})
100
  except Exception as e:
101
  print(f"Error processing image: {e}")
102
- return json.dumps({"error": f"Error processing image: {e}"})
103
 
104
  # Create the Gradio interface
105
  iface = gr.Interface(
106
  fn=predict,
107
  inputs=gr.JSON(label="Input JSON"),
108
- outputs=gr.Textbox(label="Prediction Result"),
109
  live=True,
110
  title="Maize Anomaly Detection",
111
  description="Upload an image of maize to detect anomalies like disease or pest infestation. You can provide local paths, URLs, or base64-encoded images."
 
48
 
49
  def predict(data):
50
  try:
 
51
  if not isinstance(data, list) or len(data) == 0:
52
+ return {"error": "Input data should be a non-empty list."}
53
 
54
  image_input = data[0].get('image', None)
55
  if not image_input:
56
+ return {"error": "No image provided."}
57
 
58
  print(f"Received image input: {image_input}")
59
 
 
69
  print(f"Fetched image from URL: {image}")
70
  except Exception as e:
71
  print(f"Error fetching image from URL: {e}")
72
+ return {"error": f"Error fetching image from URL: {e}"}
73
  else: # Assuming it is base64-encoded image data
74
  try:
75
  image_data = base64.b64decode(image_input)
 
77
  print(f"Decoded base64 image: {image}")
78
  except Exception as e:
79
  print(f"Error decoding base64 image: {e}")
80
+ return {"error": f"Error decoding base64 image: {e}"}
81
 
82
  # Apply transformations
83
  image = transform(image).unsqueeze(0)
 
91
  print(f"Prediction output: {outputs}, Predicted class: {predicted_class}")
92
 
93
  if predicted_class == 0:
94
+ return {"result": "The photo you've sent is of fall army worm with problem ID 126."}
95
  elif predicted_class == 1:
96
+ return {"result": "The photo you've sent is of a healthy maize image."}
97
  else:
98
+ return {"error": "Unexpected class prediction."}
99
  except Exception as e:
100
  print(f"Error processing image: {e}")
101
+ return {"error": f"Error processing image: {e}"}
102
 
103
  # Create the Gradio interface
104
  iface = gr.Interface(
105
  fn=predict,
106
  inputs=gr.JSON(label="Input JSON"),
107
+ outputs=gr.JSON(label="Prediction Result"),
108
  live=True,
109
  title="Maize Anomaly Detection",
110
  description="Upload an image of maize to detect anomalies like disease or pest infestation. You can provide local paths, URLs, or base64-encoded images."