Drop in a screenshot and convert it to clean code (HTML/Tailwind/React/Vue)
Python
Serve local assets correctly in evals
Abi Rajab42af982906f
diff --git a/backend/config.py b/backend/config.pyindex ffd6112f..b3c8c60c 100644--- a/backend/config.py+++ b/backend/config.py@@ -24,6 +24,9 @@ PROMPT_REPORTS_ENABLED = os.environ.get(LOCAL_ASSET_DIR = os.environ.get("LOCAL_ASSET_DIR", os.path.join(os.path.dirname(__file__), "local_assets"))+# Base URL the backend serves /local-assets from. The live (websocket) path+# infers this per-request; the evals path has no request, so it uses this.+LOCAL_ASSET_BASE_URL = os.environ.get("LOCAL_ASSET_BASE_URL", "http://127.0.0.1:7001")# Set to True when running in production (on the hosted version)# Used as a feature flag to enable or disable certain featuresdiff --git a/backend/evals/core.py b/backend/evals/core.pyindex eaba7189..1e1495a8 100644--- a/backend/evals/core.py+++ b/backend/evals/core.py@@ -1,6 +1,7 @@from config import (ANTHROPIC_API_KEY,GEMINI_API_KEY,+ LOCAL_ASSET_BASE_URL,OPENAI_API_KEY,OPENAI_BASE_URL,)@@ -46,6 +47,9 @@ async def generate_code_for_image(image_url: str, stack: Stack, model: Llm) -> santhropic_api_key=ANTHROPIC_API_KEY,gemini_api_key=GEMINI_API_KEY,should_generate_images=True,+ # No websocket to infer the host from, so use the configured base URL;+ # otherwise extracted/saved assets get hostless /local-assets/ URLs.+ asset_base_url=LOCAL_ASSET_BASE_URL,initial_file_state=None,option_codes=None,)diff --git a/backend/evals/runner.py b/backend/evals/runner.pyindex bfb5c0d5..aa4b1181 100644--- a/backend/evals/runner.py+++ b/backend/evals/runner.py@@ -5,6 +5,7 @@ from datetime import datetimeimport timeimport inspectfrom llm import Llm+from config import LOCAL_ASSET_BASE_URLfrom prompts.prompt_types import Stackfrom .core import generate_code_for_imagefrom .utils import image_to_data_url@@ -13,6 +14,28 @@ from .config import EVALS_DIRMAX_EVAL_RETRIES = 2+def normalize_local_asset_urls(html: str) -> str:+ """Rewrite locally-served asset references to ``LOCAL_ASSET_BASE_URL``.++ Generated HTML may reference extracted/saved assets as root-relative+ ("/local-assets/...") or via a dev-server host that isn't running at+ eval-review time. Point them all at the configured base URL so the saved+ HTML actually renders the real crops.+ """+ base = LOCAL_ASSET_BASE_URL.rstrip("/")+ replacements = [+ ("https://localhost:5173/local-assets/", f"{base}/local-assets/"),+ ("http://localhost:5173/local-assets/", f"{base}/local-assets/"),+ ("http://127.0.0.1:5173/local-assets/", f"{base}/local-assets/"),+ ('"/local-assets/', f'"{base}/local-assets/'),+ ("'/local-assets/", f"'{base}/local-assets/"),+ ("url(/local-assets/", f"url({base}/local-assets/"),+ ]+ for old, new in replacements:+ html = html.replace(old, new)+ return html++def _resolve_eval_filenames(input_files: Optional[List[str]]) -> List[str]:input_dir = EVALS_DIR + "/inputs"if input_files and len(input_files) > 0:@@ -239,7 +262,7 @@ async def run_image_evals(elif generated_content is not None and time_taken is not None:try:with open(output_html_filepath, "w") as file:- file.write(generated_content)+ file.write(normalize_local_asset_urls(generated_content))timing_data.append(f"{final_output_html_filename}: {time_taken:.2f} seconds")diff --git a/backend/tests/test_eval_runner.py b/backend/tests/test_eval_runner.pynew file mode 100644index 00000000..f330defb--- /dev/null+++ b/backend/tests/test_eval_runner.py@@ -0,0 +1,31 @@+"""Eval output HTML must point local-asset references at a reachable host so+the saved pages render the real crops at review time."""++from evals.runner import normalize_local_asset_urls+++def test_normalize_local_asset_urls_rewrites_to_base(monkeypatch) -> None:+ monkeypatch.setattr("evals.runner.LOCAL_ASSET_BASE_URL", "http://eval-host:9999")++ html = (+ '<img src="/local-assets/asset_a.png">'+ "<img src='/local-assets/asset_b.png'>"+ '<div style="background:url(/local-assets/asset_c.png)"></div>'+ '<img src="http://localhost:5173/local-assets/asset_d.png">'+ )+ out = normalize_local_asset_urls(html)++ base = "http://eval-host:9999/local-assets/"+ assert f'"{base}asset_a.png"' in out+ assert f"'{base}asset_b.png'" in out+ assert f"url({base}asset_c.png)" in out+ assert f'"{base}asset_d.png"' in out+ # No hostless root-relative or dev-server references survive.+ assert '"/local-assets/' not in out+ assert "localhost:5173/local-assets/" not in out+++def test_normalize_local_asset_urls_leaves_public_urls_untouched(monkeypatch) -> None:+ monkeypatch.setattr("evals.runner.LOCAL_ASSET_BASE_URL", "http://eval-host:9999")+ html = '<img src="https://replicate.delivery/xyz/out.png">'+ assert normalize_local_asset_urls(html) == html