jays009 commited on
Commit
29a1140
·
verified ·
1 Parent(s): 342396f

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +23 -25
app.py CHANGED
@@ -37,6 +37,9 @@ transform = transforms.Compose([
37
  transforms.Normalize([0.485, 0.456, 0.406], [0.229, 0.224, 0.225]),
38
  ])
39
 
 
 
 
40
  # Function to predict from image content
41
  def predict_from_image(image):
42
  # Ensure the image is a PIL Image
@@ -59,38 +62,33 @@ def predict_from_image(image):
59
  else:
60
  return {"error": "Unexpected class prediction."}
61
 
62
- # Function to handle image from URL or file path
63
- def predict_from_url_or_path(url=None, path=None):
64
- try:
65
- # If URL is provided, fetch and process image
66
- if url:
67
- response = requests.get(url)
68
- response.raise_for_status() # Ensure the request was successful
69
- image = Image.open(BytesIO(response.content))
70
- return predict_from_image(image)
71
-
72
- # If path is provided, open the image from the local path
73
- elif path:
74
- if not os.path.exists(path):
75
- return {"error": f"File not found at {path}"}
76
- image = Image.open(path)
77
- return predict_from_image(image)
78
- else:
79
- return {"error": "No valid input provided."}
80
- except Exception as e:
81
- return {"error": f"Failed to process the input: {str(e)}"}
82
 
83
  # Gradio interface
84
  iface = gr.Interface(
85
- fn=lambda url, path: predict_from_url_or_path(url=url, path=path),
86
  inputs=[
87
- gr.Textbox(label="Enter Image URL", placeholder="Provide a valid image URL (optional)", optional=True),
88
- gr.Textbox(label="Or Enter Local Image Path", placeholder="Provide the local image path (optional)", optional=True),
89
  ],
90
  outputs=gr.JSON(label="Prediction Result"),
91
- live=True,
92
  title="Maize Anomaly Detection",
93
- description="Provide either an image URL or a local file path to detect anomalies in maize crops.",
94
  )
95
 
96
  # Launch the interface
 
37
  transforms.Normalize([0.485, 0.456, 0.406], [0.229, 0.224, 0.225]),
38
  ])
39
 
40
+ # Global variable to store the file path
41
+ file_path = None
42
+
43
  # Function to predict from image content
44
  def predict_from_image(image):
45
  # Ensure the image is a PIL Image
 
62
  else:
63
  return {"error": "Unexpected class prediction."}
64
 
65
+ # Function to handle the file path sent via POST request
66
+ def process_file_path(file_path_input):
67
+ global file_path
68
+ file_path = file_path_input # Store the file path
69
+ if not os.path.exists(file_path):
70
+ return {"error": f"File not found at {file_path}"}
71
+ image = Image.open(file_path)
72
+ return predict_from_image(image)
73
+
74
+ # Function to fetch the result (for the GET request)
75
+ def fetch_result():
76
+ if file_path:
77
+ image = Image.open(file_path)
78
+ return predict_from_image(image)
79
+ else:
80
+ return {"error": "No file path available. Please send a POST request with a file path first."}
 
 
 
 
81
 
82
  # Gradio interface
83
  iface = gr.Interface(
84
+ fn=process_file_path,
85
  inputs=[
86
+ gr.Textbox(label="Enter Local Image Path", placeholder="Provide the local image path"),
 
87
  ],
88
  outputs=gr.JSON(label="Prediction Result"),
89
+ live=False,
90
  title="Maize Anomaly Detection",
91
+ description="Provide a local file path via POST request to process an image.",
92
  )
93
 
94
  # Launch the interface