Spaces:
Build error
Build error
Commit
·
ff47789
1
Parent(s):
d428c88
Implement proper CSS/JS path fixing for iframe compatibility in Hugging Face Spaces
Browse files
app.py
CHANGED
|
@@ -63,6 +63,39 @@ def image_to_data_url(image_path):
|
|
| 63 |
print(f"Error converting image to data URL: {e}")
|
| 64 |
return None
|
| 65 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 66 |
def render_preview(code: str, width: int, height: int, scale: float) -> str:
|
| 67 |
"""
|
| 68 |
Preview renderer with both width and height control for the inner canvas.
|
|
@@ -71,13 +104,12 @@ def render_preview(code: str, width: int, height: int, scale: float) -> str:
|
|
| 71 |
# Clean up the HTML code to remove problematic script and CSS references
|
| 72 |
soup = BeautifulSoup(code, 'html.parser')
|
| 73 |
|
| 74 |
-
#
|
| 75 |
for script in soup.find_all('script'):
|
| 76 |
src = script.get('src', '')
|
| 77 |
if src and any(pattern in src for pattern in ['assets/', 'index-', 'iframeResizer']):
|
| 78 |
script.decompose()
|
| 79 |
|
| 80 |
-
# Remove any link tags that reference problematic CSS files
|
| 81 |
for link in soup.find_all('link'):
|
| 82 |
href = link.get('href', '')
|
| 83 |
if href and any(pattern in href for pattern in ['assets/', 'index-']):
|
|
@@ -86,12 +118,6 @@ def render_preview(code: str, width: int, height: int, scale: float) -> str:
|
|
| 86 |
# Get the cleaned HTML
|
| 87 |
cleaned_code = str(soup)
|
| 88 |
|
| 89 |
-
# Additional string-based cleanup for any remaining references
|
| 90 |
-
cleaned_code = cleaned_code.replace('index-BQPjLISY.js', '')
|
| 91 |
-
cleaned_code = cleaned_code.replace('index-BOW2xVAS.css', '')
|
| 92 |
-
cleaned_code = cleaned_code.replace('./assets/', '')
|
| 93 |
-
cleaned_code = cleaned_code.replace('assets/', '')
|
| 94 |
-
|
| 95 |
except Exception as e:
|
| 96 |
print(f"Error cleaning HTML in render_preview: {e}")
|
| 97 |
# Fallback to original code if cleaning fails
|
|
@@ -162,32 +188,12 @@ def process_and_generate(image_np, image_path_from_state, sidebar_prompt, header
|
|
| 162 |
|
| 163 |
print(f"Processing HTML for run_id: {run_id}")
|
| 164 |
|
| 165 |
-
#
|
| 166 |
try:
|
| 167 |
-
|
| 168 |
-
|
| 169 |
-
if src and any(pattern in src for pattern in ['assets/', 'index-', 'iframeResizer']):
|
| 170 |
-
print(f"Removing problematic script: {src}")
|
| 171 |
-
script.decompose()
|
| 172 |
-
|
| 173 |
-
for link in soup.find_all('link'):
|
| 174 |
-
href = link.get('href', '')
|
| 175 |
-
if href and any(pattern in href for pattern in ['assets/', 'index-']):
|
| 176 |
-
print(f"Removing problematic CSS link: {href}")
|
| 177 |
-
link.decompose()
|
| 178 |
-
|
| 179 |
-
# Additional string-based cleanup for any remaining references
|
| 180 |
-
html_content = str(soup)
|
| 181 |
-
html_content = html_content.replace('index-BQPjLISY.js', '')
|
| 182 |
-
html_content = html_content.replace('index-BOW2xVAS.css', '')
|
| 183 |
-
html_content = html_content.replace('./assets/', '')
|
| 184 |
-
html_content = html_content.replace('assets/', '')
|
| 185 |
-
|
| 186 |
-
# Re-parse the cleaned HTML
|
| 187 |
-
soup = BeautifulSoup(html_content, 'html.parser')
|
| 188 |
-
|
| 189 |
except Exception as e:
|
| 190 |
-
print(f"Error
|
| 191 |
|
| 192 |
for img in soup.find_all('img'):
|
| 193 |
if img.get('src') and not img['src'].startswith(('http', 'data:')):
|
|
|
|
| 63 |
print(f"Error converting image to data URL: {e}")
|
| 64 |
return None
|
| 65 |
|
| 66 |
+
def patch_css_js_paths(soup: BeautifulSoup, output_dir: Path):
|
| 67 |
+
"""
|
| 68 |
+
Fix CSS and JS paths in the HTML to work with Gradio's file serving.
|
| 69 |
+
Converts relative paths to /file= paths or removes them if files don't exist.
|
| 70 |
+
"""
|
| 71 |
+
# CSS
|
| 72 |
+
for link in soup.find_all("link", rel=lambda x: x and "stylesheet" in x):
|
| 73 |
+
href = link.get("href", "")
|
| 74 |
+
if href.startswith(("http", "data:")):
|
| 75 |
+
continue
|
| 76 |
+
f = output_dir / href.lstrip("./")
|
| 77 |
+
if f.exists():
|
| 78 |
+
link["href"] = f"/file={f}"
|
| 79 |
+
print(f"Fixed CSS path: {href} -> /file={f}")
|
| 80 |
+
else:
|
| 81 |
+
print(f"Removing non-existent CSS: {href}")
|
| 82 |
+
link.decompose()
|
| 83 |
+
|
| 84 |
+
# JS
|
| 85 |
+
for script in soup.find_all("script", src=True):
|
| 86 |
+
src = script["src"]
|
| 87 |
+
if src.startswith(("http", "data:")):
|
| 88 |
+
continue
|
| 89 |
+
f = output_dir / src.lstrip("./")
|
| 90 |
+
if f.exists():
|
| 91 |
+
script["src"] = f"/file={f}"
|
| 92 |
+
print(f"Fixed JS path: {src} -> /file={f}")
|
| 93 |
+
else:
|
| 94 |
+
print(f"Removing non-existent JS: {src}")
|
| 95 |
+
script.decompose()
|
| 96 |
+
|
| 97 |
+
return soup
|
| 98 |
+
|
| 99 |
def render_preview(code: str, width: int, height: int, scale: float) -> str:
|
| 100 |
"""
|
| 101 |
Preview renderer with both width and height control for the inner canvas.
|
|
|
|
| 104 |
# Clean up the HTML code to remove problematic script and CSS references
|
| 105 |
soup = BeautifulSoup(code, 'html.parser')
|
| 106 |
|
| 107 |
+
# For preview, we'll just remove problematic references since we don't have the output_dir context
|
| 108 |
for script in soup.find_all('script'):
|
| 109 |
src = script.get('src', '')
|
| 110 |
if src and any(pattern in src for pattern in ['assets/', 'index-', 'iframeResizer']):
|
| 111 |
script.decompose()
|
| 112 |
|
|
|
|
| 113 |
for link in soup.find_all('link'):
|
| 114 |
href = link.get('href', '')
|
| 115 |
if href and any(pattern in href for pattern in ['assets/', 'index-']):
|
|
|
|
| 118 |
# Get the cleaned HTML
|
| 119 |
cleaned_code = str(soup)
|
| 120 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 121 |
except Exception as e:
|
| 122 |
print(f"Error cleaning HTML in render_preview: {e}")
|
| 123 |
# Fallback to original code if cleaning fails
|
|
|
|
| 188 |
|
| 189 |
print(f"Processing HTML for run_id: {run_id}")
|
| 190 |
|
| 191 |
+
# Fix CSS and JS paths to work with Gradio's file serving
|
| 192 |
try:
|
| 193 |
+
output_dir = base_dir / 'screencoder' / 'data' / 'output' / run_id
|
| 194 |
+
soup = patch_css_js_paths(soup, output_dir)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 195 |
except Exception as e:
|
| 196 |
+
print(f"Error fixing CSS/JS paths: {e}")
|
| 197 |
|
| 198 |
for img in soup.find_all('img'):
|
| 199 |
if img.get('src') and not img['src'].startswith(('http', 'data:')):
|