Spaces:
Running
Running
Ali Hmaou commited on
Commit ·
c3658fd
1
Parent(s): 63b1816
Version 1.8RC
Browse files- src/mcp_server/tools.py +42 -18
src/mcp_server/tools.py
CHANGED
|
@@ -195,9 +195,13 @@ def deploy_to_space(draft_id: str, visibility: str = "public", space_target: str
|
|
| 195 |
if hf_user:
|
| 196 |
repo_id_to_fetch = f"{hf_user}/{final_space_name}"
|
| 197 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 198 |
try:
|
| 199 |
-
print(f"DEBUG: Tentative de récupération des requirements
|
| 200 |
-
# Téléchargement du requirements.txt existant
|
| 201 |
cached_path = hf_hub_download(
|
| 202 |
repo_id=repo_id_to_fetch,
|
| 203 |
filename="requirements.txt",
|
|
@@ -206,24 +210,44 @@ def deploy_to_space(draft_id: str, visibility: str = "public", space_target: str
|
|
| 206 |
)
|
| 207 |
with open(cached_path, 'r') as f:
|
| 208 |
existing_reqs = set(f.read().splitlines())
|
| 209 |
-
|
| 210 |
-
|
| 211 |
-
merged_reqs = existing_reqs.union(new_reqs)
|
| 212 |
-
# Nettoyage (lignes vides)
|
| 213 |
-
merged_content = "\n".join(sorted([r for r in merged_reqs if r.strip()]))
|
| 214 |
-
|
| 215 |
-
files_to_deploy["requirements.txt"] = merged_content
|
| 216 |
-
print(f"DEBUG: Requirements fusionnés avec succès ({len(existing_reqs)} existants + nouveaux).")
|
| 217 |
-
|
| 218 |
except (EntryNotFoundError, RepositoryNotFoundError):
|
| 219 |
-
print("DEBUG: Pas de requirements.txt
|
| 220 |
-
# Le fichier n'existe pas encore, on garde celui du draft
|
| 221 |
-
pass
|
| 222 |
except Exception as e:
|
| 223 |
-
print(f"DEBUG: Erreur lors de la
|
| 224 |
-
|
| 225 |
-
|
| 226 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 227 |
|
| 228 |
try:
|
| 229 |
url = deployer.deploy_space(
|
|
|
|
| 195 |
if hf_user:
|
| 196 |
repo_id_to_fetch = f"{hf_user}/{final_space_name}"
|
| 197 |
|
| 198 |
+
import requests
|
| 199 |
+
existing_reqs = set()
|
| 200 |
+
fetch_success = False
|
| 201 |
+
|
| 202 |
+
# Méthode 1: Via API HuggingFace (hf_hub_download)
|
| 203 |
try:
|
| 204 |
+
print(f"DEBUG: Tentative de récupération des requirements via API sur {repo_id_to_fetch}...")
|
|
|
|
| 205 |
cached_path = hf_hub_download(
|
| 206 |
repo_id=repo_id_to_fetch,
|
| 207 |
filename="requirements.txt",
|
|
|
|
| 210 |
)
|
| 211 |
with open(cached_path, 'r') as f:
|
| 212 |
existing_reqs = set(f.read().splitlines())
|
| 213 |
+
fetch_success = True
|
| 214 |
+
print(f"DEBUG: Requirements récupérés via API ({len(existing_reqs)} items).")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 215 |
except (EntryNotFoundError, RepositoryNotFoundError):
|
| 216 |
+
print("DEBUG: Pas de requirements.txt via API (404/Not Found).")
|
|
|
|
|
|
|
| 217 |
except Exception as e:
|
| 218 |
+
print(f"DEBUG: Erreur API lors de la récupération requirements: {e}")
|
| 219 |
+
|
| 220 |
+
# Méthode 2: Via URL directe (Fallback "Raw")
|
| 221 |
+
# Utile si l'API échoue ou si le cache local est incohérent
|
| 222 |
+
if not fetch_success and "/" in repo_id_to_fetch:
|
| 223 |
+
try:
|
| 224 |
+
raw_url = f"https://huggingface.co/spaces/{repo_id_to_fetch}/resolve/main/requirements.txt"
|
| 225 |
+
print(f"DEBUG: Tentative de récupération via URL Raw: {raw_url}")
|
| 226 |
+
headers = {}
|
| 227 |
+
if deployer.token:
|
| 228 |
+
headers["Authorization"] = f"Bearer {deployer.token}"
|
| 229 |
+
|
| 230 |
+
resp = requests.get(raw_url, headers=headers)
|
| 231 |
+
if resp.status_code == 200:
|
| 232 |
+
existing_reqs = set(resp.text.splitlines())
|
| 233 |
+
fetch_success = True
|
| 234 |
+
print(f"DEBUG: Requirements récupérés via URL Raw ({len(existing_reqs)} items).")
|
| 235 |
+
elif resp.status_code == 404:
|
| 236 |
+
print("DEBUG: requirements.txt non trouvé via URL Raw (404).")
|
| 237 |
+
else:
|
| 238 |
+
print(f"DEBUG: Erreur HTTP {resp.status_code} lors de la récupération via URL Raw.")
|
| 239 |
+
except Exception as e:
|
| 240 |
+
print(f"DEBUG: Exception lors de la récupération via URL Raw: {e}")
|
| 241 |
+
|
| 242 |
+
# Fusion finale
|
| 243 |
+
if fetch_success or existing_reqs:
|
| 244 |
+
merged_reqs = existing_reqs.union(new_reqs)
|
| 245 |
+
# Nettoyage
|
| 246 |
+
cleaned_reqs = sorted([r.strip() for r in merged_reqs if r.strip()])
|
| 247 |
+
files_to_deploy["requirements.txt"] = "\n".join(cleaned_reqs)
|
| 248 |
+
print(f"DEBUG: Fusion terminée. Total requirements: {len(cleaned_reqs)}")
|
| 249 |
+
else:
|
| 250 |
+
print("DEBUG: Aucun requirements existant trouvé, déploiement des nouveaux uniquement.")
|
| 251 |
|
| 252 |
try:
|
| 253 |
url = deployer.deploy_space(
|