Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -59,11 +59,22 @@ def predict_from_image(image):
|
|
| 59 |
else:
|
| 60 |
return {"error": "Unexpected class prediction."}
|
| 61 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 62 |
# Gradio interface
|
| 63 |
iface = gr.Interface(
|
| 64 |
-
fn=lambda image: predict_from_image(image),
|
| 65 |
inputs=[
|
| 66 |
gr.Image(type="pil", label="Upload an Image"),
|
|
|
|
| 67 |
],
|
| 68 |
outputs=gr.JSON(label="Prediction Result"),
|
| 69 |
live=True,
|
|
|
|
| 59 |
else:
|
| 60 |
return {"error": "Unexpected class prediction."}
|
| 61 |
|
| 62 |
+
# Function to predict from URL
|
| 63 |
+
def predict_from_url(url):
|
| 64 |
+
try:
|
| 65 |
+
response = requests.get(url)
|
| 66 |
+
response.raise_for_status() # Ensure the request was successful
|
| 67 |
+
image = Image.open(BytesIO(response.content))
|
| 68 |
+
return predict_from_image(image)
|
| 69 |
+
except Exception as e:
|
| 70 |
+
return {"error": f"Failed to process the URL: {str(e)}"}
|
| 71 |
+
|
| 72 |
# Gradio interface
|
| 73 |
iface = gr.Interface(
|
| 74 |
+
fn=lambda image, url: predict_from_image(image) if image else predict_from_url(url),
|
| 75 |
inputs=[
|
| 76 |
gr.Image(type="pil", label="Upload an Image"),
|
| 77 |
+
gr.Textbox(label="Or Enter an Image URL", placeholder="Provide a valid image URL"),
|
| 78 |
],
|
| 79 |
outputs=gr.JSON(label="Prediction Result"),
|
| 80 |
live=True,
|