jays009 commited on
Commit
610d493
·
verified ·
1 Parent(s): 2255b93

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +21 -15
app.py CHANGED
@@ -45,23 +45,29 @@ transform = transforms.Compose([
45
  ])
46
 
47
  def predict(image):
48
- # Check if the input contains a base64-encoded string
49
- if isinstance(image, dict) and image.get("data"):
50
- try:
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
- elif isinstance(image, str):
57
- try:
58
- response = requests.get(image)
59
- image = Image.open(BytesIO(response.content))
60
- except Exception as e:
61
- return f"Error fetching image from URL: {e}"
 
 
62
 
63
- # Apply transformations
64
- try:
 
 
 
 
 
 
 
 
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