Spaces:
Running
Running
deploy(S4): Blender headless pipeline + WebAR client + backend fixes
Browse files- backend/tools/xr_tools.py +25 -4
backend/tools/xr_tools.py
CHANGED
|
@@ -1,5 +1,23 @@
|
|
| 1 |
import logging
|
| 2 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 3 |
async def xr_anchor_tool(action: str, anchor_id: str = "", *args, **kwargs):
|
| 4 |
"""
|
| 5 |
Control or query XR spatial anchors.
|
|
@@ -85,8 +103,9 @@ async def search_web_for_image(description: str) -> str | None:
|
|
| 85 |
if "original" in page_data:
|
| 86 |
img_url = page_data["original"]["source"]
|
| 87 |
tmp = tempfile.NamedTemporaryFile(suffix=".jpg", delete=False)
|
| 88 |
-
|
| 89 |
-
|
|
|
|
| 90 |
except Exception as e:
|
| 91 |
logging.error(f"Web image search failed: {e}")
|
| 92 |
return None
|
|
@@ -110,8 +129,10 @@ async def pollinations_generate_image(description: str) -> str:
|
|
| 110 |
url = f"https://image.pollinations.ai/prompt/{prompt}?width=1024&height=1024&nologo=true"
|
| 111 |
try:
|
| 112 |
tmp = tempfile.NamedTemporaryFile(suffix=".jpg", delete=False)
|
| 113 |
-
|
| 114 |
-
|
|
|
|
|
|
|
| 115 |
except Exception as e:
|
| 116 |
logging.error(f"AI image generation failed: {e}")
|
| 117 |
raise e
|
|
|
|
| 1 |
import logging
|
| 2 |
|
| 3 |
+
# Wikimedia (and several other image CDNs) reject the default
|
| 4 |
+
# "Python-urllib/3.x" User-Agent with HTTP 403 Forbidden. urllib.request
|
| 5 |
+
# .urlretrieve() cannot carry headers, so every image fetch below goes through
|
| 6 |
+
# this helper instead — this was the real cause of SF3D model generation
|
| 7 |
+
# failing with {"detail":"HTTP Error 403: Forbidden"}: the Wikipedia *API* call
|
| 8 |
+
# set a User-Agent, but the follow-up image *download* did not.
|
| 9 |
+
IMAGE_FETCH_UA = "JARVIS_OMEGA/1.0 (+https://huggingface.co/spaces/Jarvis2345/jarvis-cloud)"
|
| 10 |
+
|
| 11 |
+
|
| 12 |
+
def _download_image(url: str, dest_path: str, timeout: int = 30) -> str:
|
| 13 |
+
"""Download `url` to `dest_path` with a real User-Agent. Returns dest_path."""
|
| 14 |
+
import urllib.request
|
| 15 |
+
req = urllib.request.Request(url, headers={"User-Agent": IMAGE_FETCH_UA})
|
| 16 |
+
with urllib.request.urlopen(req, timeout=timeout) as resp, open(dest_path, "wb") as fh:
|
| 17 |
+
fh.write(resp.read())
|
| 18 |
+
return dest_path
|
| 19 |
+
|
| 20 |
+
|
| 21 |
async def xr_anchor_tool(action: str, anchor_id: str = "", *args, **kwargs):
|
| 22 |
"""
|
| 23 |
Control or query XR spatial anchors.
|
|
|
|
| 103 |
if "original" in page_data:
|
| 104 |
img_url = page_data["original"]["source"]
|
| 105 |
tmp = tempfile.NamedTemporaryFile(suffix=".jpg", delete=False)
|
| 106 |
+
tmp.close()
|
| 107 |
+
# UA-bearing fetch: Wikimedia 403s the default urllib UA.
|
| 108 |
+
return _download_image(img_url, tmp.name)
|
| 109 |
except Exception as e:
|
| 110 |
logging.error(f"Web image search failed: {e}")
|
| 111 |
return None
|
|
|
|
| 129 |
url = f"https://image.pollinations.ai/prompt/{prompt}?width=1024&height=1024&nologo=true"
|
| 130 |
try:
|
| 131 |
tmp = tempfile.NamedTemporaryFile(suffix=".jpg", delete=False)
|
| 132 |
+
tmp.close()
|
| 133 |
+
# Pollinations renders on demand — allow a longer timeout, and send a
|
| 134 |
+
# real User-Agent (bare urlretrieve 403s on several image CDNs).
|
| 135 |
+
return _download_image(url, tmp.name, timeout=120)
|
| 136 |
except Exception as e:
|
| 137 |
logging.error(f"AI image generation failed: {e}")
|
| 138 |
raise e
|