Thanh Vinh Vo
commited on
Commit
·
51c0d67
1
Parent(s):
83b34ee
update
Browse files
app.py
CHANGED
|
@@ -35,9 +35,36 @@ def get_image(file_name: str) -> Image.Image:
|
|
| 35 |
import requests
|
| 36 |
from PIL import Image
|
| 37 |
from io import BytesIO
|
|
|
|
| 38 |
url = f"{DEFAULT_API_URL}/files/{file_name}"
|
| 39 |
-
|
| 40 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 41 |
|
| 42 |
|
| 43 |
# --- Basic Agent Definition ---
|
|
|
|
| 35 |
import requests
|
| 36 |
from PIL import Image
|
| 37 |
from io import BytesIO
|
| 38 |
+
|
| 39 |
url = f"{DEFAULT_API_URL}/files/{file_name}"
|
| 40 |
+
print(f"Fetching image from URL: {url}")
|
| 41 |
+
|
| 42 |
+
response = None
|
| 43 |
+
try:
|
| 44 |
+
response = requests.get(url, timeout=30)
|
| 45 |
+
response.raise_for_status() # Raises an HTTPError for bad responses
|
| 46 |
+
|
| 47 |
+
# Check if response is empty
|
| 48 |
+
if not response.content:
|
| 49 |
+
raise ValueError(f"Empty response received from {url}")
|
| 50 |
+
|
| 51 |
+
# Check content type
|
| 52 |
+
content_type = response.headers.get('content-type', '').lower()
|
| 53 |
+
print(f"Response content-type: {content_type}")
|
| 54 |
+
print(f"Response content length: {len(response.content)} bytes")
|
| 55 |
+
|
| 56 |
+
# Try to open the image
|
| 57 |
+
image_bytes = BytesIO(response.content)
|
| 58 |
+
image = Image.open(image_bytes)
|
| 59 |
+
return image.convert("RGB")
|
| 60 |
+
|
| 61 |
+
except requests.exceptions.RequestException as e:
|
| 62 |
+
raise ValueError(f"Failed to fetch image from {url}: {e}")
|
| 63 |
+
except Exception as e:
|
| 64 |
+
# Print first 200 characters of response content for debugging
|
| 65 |
+
content_preview = response.content[:200] if response and hasattr(response, 'content') else b"No response"
|
| 66 |
+
print(f"Error opening image. Content preview: {content_preview}")
|
| 67 |
+
raise ValueError(f"Failed to open image from {url}: {e}")
|
| 68 |
|
| 69 |
|
| 70 |
# --- Basic Agent Definition ---
|