Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -45,23 +45,29 @@ transform = transforms.Compose([
|
|
| 45 |
])
|
| 46 |
|
| 47 |
def predict(image):
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
image_data = base64.b64decode(image["data"])
|
| 52 |
-
image = Image.open(BytesIO(image_data))
|
| 53 |
-
except Exception as e:
|
| 54 |
-
return f"Error decoding base64 image: {e}"
|
| 55 |
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
|
|
|
|
|
|
|
| 62 |
|
| 63 |
-
|
| 64 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 65 |
image = transform(image).unsqueeze(0)
|
| 66 |
image = image.to(torch.device("cuda" if torch.cuda.is_available() else "cpu"))
|
| 67 |
|
|
|
|
| 45 |
])
|
| 46 |
|
| 47 |
def predict(image):
|
| 48 |
+
try:
|
| 49 |
+
# Debugging: Print the received image
|
| 50 |
+
print(f"Received image: {image}")
|
|
|
|
|
|
|
|
|
|
|
|
|
| 51 |
|
| 52 |
+
# Check if the input contains a base64-encoded string
|
| 53 |
+
if isinstance(image, dict) and image.get("data"):
|
| 54 |
+
try:
|
| 55 |
+
image_data = base64.b64decode(image["data"])
|
| 56 |
+
image = Image.open(BytesIO(image_data))
|
| 57 |
+
print(f"Decoded base64 image: {image}")
|
| 58 |
+
except Exception as e:
|
| 59 |
+
return f"Error decoding base64 image: {e}"
|
| 60 |
|
| 61 |
+
# Check if the input is a URL
|
| 62 |
+
elif isinstance(image, str) and image.startswith("http"):
|
| 63 |
+
try:
|
| 64 |
+
response = requests.get(image)
|
| 65 |
+
image = Image.open(BytesIO(response.content))
|
| 66 |
+
print(f"Fetched image from URL: {image}")
|
| 67 |
+
except Exception as e:
|
| 68 |
+
return f"Error fetching image from URL: {e}"
|
| 69 |
+
|
| 70 |
+
# Apply transformations
|
| 71 |
image = transform(image).unsqueeze(0)
|
| 72 |
image = image.to(torch.device("cuda" if torch.cuda.is_available() else "cpu"))
|
| 73 |
|