Thanh Vinh Vo commited on
Commit
072fb76
·
1 Parent(s): 64602c3
Files changed (1) hide show
  1. app.py +20 -10
app.py CHANGED
@@ -25,19 +25,24 @@ DEFAULT_API_URL = "https://agents-course-unit4-scoring.hf.space"
25
 
26
 
27
  @tool
28
- def get_image(file_name: str) -> Image.Image:
29
  """
30
- A tool that load image from file name.
31
  Args:
32
  file_name: File name.
 
 
33
  """
34
  import requests
35
- from PIL import Image
36
- from io import BytesIO
37
 
38
  url = f"{DEFAULT_API_URL}/files/{file_name}"
39
  print(f"Fetching image from URL: {url}")
40
 
 
 
 
 
41
  response = None
42
  try:
43
  response = requests.get(url, timeout=30)
@@ -52,18 +57,23 @@ def get_image(file_name: str) -> Image.Image:
52
  print(f"Response content-type: {content_type}")
53
  print(f"Response content length: {len(response.content)} bytes")
54
 
55
- # Try to open the image
56
- image_bytes = BytesIO(response.content)
57
- image = Image.open(image_bytes)
58
- return image
 
 
 
 
 
59
 
60
  except requests.exceptions.RequestException as e:
61
  raise ValueError(f"Failed to fetch image from {url}: {e}")
62
  except Exception as e:
63
  # Print first 200 characters of response content for debugging
64
  content_preview = response.content[:200] if response and hasattr(response, 'content') else b"No response"
65
- print(f"Error opening image. Content preview: {content_preview}")
66
- raise ValueError(f"Failed to open image from {url}: {e}")
67
 
68
 
69
  # --- Basic Agent Definition ---
 
25
 
26
 
27
  @tool
28
+ def get_image(file_name: str) -> str:
29
  """
30
+ A tool that downloads an image from file name and saves it locally.
31
  Args:
32
  file_name: File name.
33
+ Returns:
34
+ str: Local file path where the image was saved.
35
  """
36
  import requests
37
+ import os
 
38
 
39
  url = f"{DEFAULT_API_URL}/files/{file_name}"
40
  print(f"Fetching image from URL: {url}")
41
 
42
+ # Create downloads directory if it doesn't exist
43
+ downloads_dir = "downloaded_images"
44
+ os.makedirs(downloads_dir, exist_ok=True)
45
+
46
  response = None
47
  try:
48
  response = requests.get(url, timeout=30)
 
57
  print(f"Response content-type: {content_type}")
58
  print(f"Response content length: {len(response.content)} bytes")
59
 
60
+ # Use original filename directly
61
+ local_path = os.path.join(downloads_dir, file_name)
62
+
63
+ # Save the image to local file
64
+ with open(local_path, 'wb') as f:
65
+ f.write(response.content)
66
+
67
+ print(f"Image saved to: {local_path}")
68
+ return local_path
69
 
70
  except requests.exceptions.RequestException as e:
71
  raise ValueError(f"Failed to fetch image from {url}: {e}")
72
  except Exception as e:
73
  # Print first 200 characters of response content for debugging
74
  content_preview = response.content[:200] if response and hasattr(response, 'content') else b"No response"
75
+ print(f"Error downloading image. Content preview: {content_preview}")
76
+ raise ValueError(f"Failed to download image from {url}: {e}")
77
 
78
 
79
  # --- Basic Agent Definition ---