Surn commited on
Commit
0c47e86
·
1 Parent(s): 2786740

Update to handle background images sent as url

Browse files
Files changed (2) hide show
  1. app.py +3 -3
  2. modules/file_utils.py +15 -5
app.py CHANGED
@@ -34,7 +34,7 @@ import modules.user_history
34
  from modules.version_info import versions_html, commit_hash, get_xformers_version
35
  from modules.gradio import *
36
  from modules.file_utils import get_file_parts, get_filename_from_filepath, convert_title_to_filename, get_unique_file_path, delete_file, download_and_save_image
37
- from modules.constants import IS_SHARED_SPACE, HF_REPO_ID
38
  from modules.storage import upload_files_to_repo
39
 
40
  MODEL = None
@@ -299,8 +299,8 @@ def predict(model, text, melody_filepath = None, duration=10, dimension=2, topk=
299
  background = load_background_filepath(video_orientation)
300
 
301
  if background.startswith("http://") or background.startswith("https://"):
302
- username = profile.value.username if hasattr(profile.value, 'username') else "default_user" if (profile is None) else profile
303
- background = download_and_save_image(background, modules.user_history._user_images_path(username))
304
 
305
  if melody_filepath:
306
  melody_name, melody_extension = get_filename_from_filepath(melody_filepath)
 
34
  from modules.version_info import versions_html, commit_hash, get_xformers_version
35
  from modules.gradio import *
36
  from modules.file_utils import get_file_parts, get_filename_from_filepath, convert_title_to_filename, get_unique_file_path, delete_file, download_and_save_image
37
+ from modules.constants import IS_SHARED_SPACE, HF_REPO_ID, TMPDIR, HF_API_TOKEN
38
  from modules.storage import upload_files_to_repo
39
 
40
  MODEL = None
 
299
  background = load_background_filepath(video_orientation)
300
 
301
  if background.startswith("http://") or background.startswith("https://"):
302
+ username = profile if isinstance(profile, str) else profile.value.username if hasattr(profile.value, 'username') else "default_user" if (profile is None) else profile
303
+ background = download_and_save_image(background, Path(TMPDIR) / str(username), HF_API_TOKEN)
304
 
305
  if melody_filepath:
306
  melody_name, melody_extension = get_filename_from_filepath(melody_filepath)
modules/file_utils.py CHANGED
@@ -120,18 +120,27 @@ def get_unique_file_path(directory, filename, file_ext, counter=0):
120
  # Example usage:
121
  # new_file_path = get_unique_file_path(video_dir, title_file_name, video_new_ext)
122
 
123
- def download_and_save_image(url: str, dst_folder: Path) -> Path:
124
  """
125
- Downloads an image from a URL, verifies it with PIL, and saves it in dst_folder with a unique filename.
126
-
 
127
  Args:
128
  url (str): The image URL.
129
  dst_folder (Path): The destination folder for the image.
130
-
 
 
131
  Returns:
132
  Path: The saved image's file path.
133
  """
134
- response = requests.get(url)
 
 
 
 
 
 
135
  response.raise_for_status()
136
  pil_image = Image.open(BytesIO(response.content))
137
 
@@ -142,5 +151,6 @@ def download_and_save_image(url: str, dst_folder: Path) -> Path:
142
  # Use get_unique_file_path from file_utils.py to generate a unique file path.
143
  unique_filepath_str = get_unique_file_path(str(dst_folder), base, ext)
144
  dst = Path(unique_filepath_str)
 
145
  pil_image.save(dst)
146
  return dst
 
120
  # Example usage:
121
  # new_file_path = get_unique_file_path(video_dir, title_file_name, video_new_ext)
122
 
123
+ def download_and_save_image(url: str, dst_folder: Path, token: str = None) -> Path:
124
  """
125
+ Downloads an image from a URL with authentication if a token is provided,
126
+ verifies it with PIL, and saves it in dst_folder with a unique filename.
127
+
128
  Args:
129
  url (str): The image URL.
130
  dst_folder (Path): The destination folder for the image.
131
+ token (str, optional): A valid Bearer token. If not provided, the HF_API_TOKEN
132
+ environment variable is used if available.
133
+
134
  Returns:
135
  Path: The saved image's file path.
136
  """
137
+ headers = {}
138
+ # Use provided token; otherwise, fall back to environment variable.
139
+ api_token = token
140
+ if api_token:
141
+ headers["Authorization"] = f"Bearer {api_token}"
142
+
143
+ response = requests.get(url, headers=headers)
144
  response.raise_for_status()
145
  pil_image = Image.open(BytesIO(response.content))
146
 
 
151
  # Use get_unique_file_path from file_utils.py to generate a unique file path.
152
  unique_filepath_str = get_unique_file_path(str(dst_folder), base, ext)
153
  dst = Path(unique_filepath_str)
154
+ dst_folder.mkdir(parents=True, exist_ok=True)
155
  pil_image.save(dst)
156
  return dst