Surn commited on
Commit
a2ab7cc
·
1 Parent(s): 75edcca

MCP request flow fixes for URLs

Browse files
Files changed (3) hide show
  1. app.py +7 -2
  2. modules/constants.py +8 -1
  3. modules/file_utils.py +37 -0
app.py CHANGED
@@ -28,12 +28,12 @@ import librosa
28
  import modules.user_history
29
  from modules.version_info import versions_html, commit_hash, get_xformers_version
30
  from modules.gradio import *
31
- 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
32
 
33
  # Added for MCP call
34
  from smolagents.mcp_client import MCPClient
35
  from modules.storage import upload_files_to_repo
36
- from modules.constants import HF_REPO_ID, umg_mcp_server
37
 
38
  MODEL = None
39
  MODELS = None
@@ -463,6 +463,11 @@ def predict(
463
  if not isinstance(seed_used, (int, float)): # Allow float for seed then cast later
464
  raise gr.Error(f"MCP tool returned a non-numeric seed. Received type: {type(seed_used)}, value: {seed_used}")
465
 
 
 
 
 
 
466
  return waveform_video_path, wave_file_path, int(seed_used)
467
 
468
  except Exception as e:
 
28
  import modules.user_history
29
  from modules.version_info import versions_html, commit_hash, get_xformers_version
30
  from modules.gradio import *
31
+ 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, download_and_save_file
32
 
33
  # Added for MCP call
34
  from smolagents.mcp_client import MCPClient
35
  from modules.storage import upload_files_to_repo
36
+ from modules.constants import HF_REPO_ID, umg_mcp_server, TMPDIR, HF_API_TOKEN
37
 
38
  MODEL = None
39
  MODELS = None
 
463
  if not isinstance(seed_used, (int, float)): # Allow float for seed then cast later
464
  raise gr.Error(f"MCP tool returned a non-numeric seed. Received type: {type(seed_used)}, value: {seed_used}")
465
 
466
+ if isinstance(waveform_video_path, str) and (waveform_video_path.startswith("http://") or waveform_video_path.startswith("https://")):
467
+ waveform_video_path = str(download_and_save_file(waveform_video_path, Path(TMPDIR) / str(profile_username_to_send), HF_API_TOKEN))
468
+ if isinstance(wave_file_path, str) and (wave_file_path.startswith("http://") or wave_file_path.startswith("https://")):
469
+ wave_file_path = str(download_and_save_file(wave_file_path, Path(TMPDIR) / str(profile_username_to_send), HF_API_TOKEN))
470
+
471
  return waveform_video_path, wave_file_path, int(seed_used)
472
 
473
  except Exception as e:
modules/constants.py CHANGED
@@ -46,7 +46,14 @@ MAX_SEED = np.iinfo(np.int32).max
46
  TARGET_SIZE = (2688,1536)
47
  BASE_HEIGHT = 640
48
  SCALE_FACTOR = (12/5)
49
- TMPDIR = os.path.join(os.path.dirname(os.path.abspath(__file__)), 'tmp')
 
 
 
 
 
 
 
50
  os.makedirs(TMPDIR, exist_ok=True)
51
 
52
 
 
46
  TARGET_SIZE = (2688,1536)
47
  BASE_HEIGHT = 640
48
  SCALE_FACTOR = (12/5)
49
+ try:
50
+ if os.environ['TMPDIR']:
51
+ TMPDIR = os.environ['TMPDIR']
52
+ else:
53
+ TMPDIR = os.path.join(os.path.dirname(os.path.abspath(__file__)), 'tmp')
54
+ except:
55
+ TMPDIR = os.path.join(os.path.dirname(os.path.abspath(__file__)), 'tmp')
56
+
57
  os.makedirs(TMPDIR, exist_ok=True)
58
 
59
 
modules/file_utils.py CHANGED
@@ -2,6 +2,10 @@
2
  import os
3
  import shutil
4
  from pathlib import Path
 
 
 
 
5
 
6
  def get_file_parts(file_path: str):
7
  # Split the path into directory and filename
@@ -149,4 +153,37 @@ def download_and_save_image(url: str, dst_folder: Path, token: str = None) -> Pa
149
  dst = Path(unique_filepath_str)
150
  dst_folder.mkdir(parents=True, exist_ok=True)
151
  pil_image.save(dst)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
152
  return dst
 
2
  import os
3
  import shutil
4
  from pathlib import Path
5
+ import requests
6
+ from PIL import Image
7
+ from io import BytesIO
8
+ from urllib.parse import urlparse
9
 
10
  def get_file_parts(file_path: str):
11
  # Split the path into directory and filename
 
153
  dst = Path(unique_filepath_str)
154
  dst_folder.mkdir(parents=True, exist_ok=True)
155
  pil_image.save(dst)
156
+ return dst
157
+
158
+ def download_and_save_file(url: str, dst_folder: Path, token: str = None) -> Path:
159
+ """
160
+ Downloads a binary file (e.g., audio or video) from a URL with authentication if a token is provided,
161
+ and saves it in dst_folder with a unique filename.
162
+
163
+ Args:
164
+ url (str): The file URL.
165
+ dst_folder (Path): The destination folder for the file.
166
+ token (str, optional): A valid Bearer token.
167
+
168
+ Returns:
169
+ Path: The saved file's path.
170
+ """
171
+ headers = {}
172
+ if token:
173
+ headers["Authorization"] = f"Bearer {token}"
174
+
175
+ response = requests.get(url, headers=headers)
176
+ response.raise_for_status()
177
+
178
+ parsed_url = urlparse(url)
179
+ original_filename = os.path.basename(parsed_url.path)
180
+ base, ext = os.path.splitext(original_filename)
181
+
182
+ unique_filepath_str = get_unique_file_path(str(dst_folder), base, ext)
183
+ dst = Path(unique_filepath_str)
184
+ dst_folder.mkdir(parents=True, exist_ok=True)
185
+
186
+ with open(dst, "wb") as f:
187
+ f.write(response.content)
188
+
189
  return dst