Skip to content

Drop in a screenshot and convert it to clean code (HTML/Tailwind/React/Vue)

Python

Remove GPT-Image-2 image fallback (#612)

Abi Raja2b1fa992cdc3
diff --git a/AGENTS.md b/AGENTS.md
index b3a09728..672da2a4 100644
--- a/AGENTS.md
+++ b/AGENTS.md
@@ -31,6 +31,7 @@ The hosted version is on the `hosted` branch. The `hosted` branch connects to a
## Cursor Cloud specific instructions
Dependencies are refreshed automatically on startup (`poetry install` in `backend/`, `pnpm install` in `frontend/`); no manual install is needed.
+Cursor Cloud environment setup should run `bash /agent/repos/screenshot-to-code/scripts/cursor-cloud-install.sh`; the script changes to the repo root before installing so it works regardless of the startup working directory.
Services (see `README.md` for the canonical commands):
- Backend (FastAPI + WebSocket): from `backend/`, `poetry run uvicorn main:app --reload --port 7001`.
diff --git a/backend/agent/tools/runtime.py b/backend/agent/tools/runtime.py
index 59215ba9..ab8f493b 100644
--- a/backend/agent/tools/runtime.py
+++ b/backend/agent/tools/runtime.py
@@ -278,22 +278,17 @@ class AgentToolRuntime:
summary={"error": "No valid prompts"},
)
replicate_api_key = self._effective_replicate_api_key()
- if replicate_api_key:
- model = "flux"
- api_key = replicate_api_key
- base_url = None
- else:
- if not self.openai_api_key:
- return ToolExecutionResult(
- ok=False,
- result={"error": "No API key available for image generation."},
- summary={"error": "Missing image generation API key"},
- )
- model = "gpt_image_2"
- api_key = self.openai_api_key
- base_url = self.openai_base_url
+ if not replicate_api_key:
+ return ToolExecutionResult(
+ ok=False,
+ result={"error": "No API key available for image generation."},
+ summary={"error": "Missing image generation API key"},
+ )
+ model = "flux"
+ api_key = replicate_api_key
+ base_url = None
- generated = await process_tasks(unique_prompts, api_key, base_url, model) # type: ignore
+ generated = await process_tasks(unique_prompts, api_key, base_url, model)
merged_results = {
prompt: url for prompt, url in zip(unique_prompts, generated)
}
diff --git a/backend/image_generation/core.py b/backend/image_generation/core.py
index 86bf2f22..bd823e9d 100644
--- a/backend/image_generation/core.py
+++ b/backend/image_generation/core.py
@@ -1,5 +1,4 @@
from image_generation.generation import (
- generate_image_openai,
generate_image_replicate,
process_tasks,
)
@@ -7,6 +6,5 @@ from image_generation.generation import (
__all__ = [
"process_tasks",
- "generate_image_openai",
"generate_image_replicate",
]
diff --git a/backend/image_generation/generation.py b/backend/image_generation/generation.py
index 3def42cc..bff3fec8 100644
--- a/backend/image_generation/generation.py
+++ b/backend/image_generation/generation.py
@@ -1,8 +1,6 @@
import asyncio
import time
-from typing import List, Literal, Union
-
-from openai import AsyncOpenAI
+from typing import List, Union
from image_generation.replicate import (
DEFAULT_IMAGE_MODEL,
@@ -18,20 +16,16 @@ REPLICATE_IMAGE_MODEL: ReplicateImageModel = DEFAULT_IMAGE_MODEL
async def process_tasks(
prompts: List[str],
api_key: str,
- base_url: str | None,
- model: Literal["gpt_image_2", "flux"],
+ _base_url: str | None,
+ _model: str,
) -> List[Union[str, None]]:
start_time = time.time()
results: list[str | BaseException | None]
- if model == "gpt_image_2":
- tasks = [generate_image_openai(prompt, api_key, base_url) for prompt in prompts]
- results = await asyncio.gather(*tasks, return_exceptions=True)
- else:
- results = []
- for i in range(0, len(prompts), REPLICATE_BATCH_SIZE):
- batch = prompts[i : i + REPLICATE_BATCH_SIZE]
- tasks = [generate_image_replicate(p, api_key) for p in batch]
- results.extend(await asyncio.gather(*tasks, return_exceptions=True))
+ results = []
+ for i in range(0, len(prompts), REPLICATE_BATCH_SIZE):
+ batch = prompts[i : i + REPLICATE_BATCH_SIZE]
+ tasks = [generate_image_replicate(p, api_key) for p in batch]
+ results.extend(await asyncio.gather(*tasks, return_exceptions=True))
end_time = time.time()
generation_time = end_time - start_time
print(f"Image generation time: {generation_time:.2f} seconds")
@@ -47,29 +41,6 @@ async def process_tasks(
return processed_results
-async def generate_image_openai(
- prompt: str, api_key: str, base_url: str | None
-) -> Union[str, None]:
- client = AsyncOpenAI(api_key=api_key, base_url=base_url)
- res = await client.images.generate(
- model="gpt-image-2",
- quality="medium",
- output_format="png",
- n=1,
- size="1024x1024",
- prompt=prompt,
- )
- await client.close()
- if not res.data:
- return None
- image = res.data[0]
- if image.url:
- return image.url
- if image.b64_json:
- return f"data:image/png;base64,{image.b64_json}"
- return None
-
-
async def generate_image_replicate(prompt: str, api_key: str) -> str:
replicate_input: dict[str, str | int | float | bool]
if REPLICATE_IMAGE_MODEL == "flux_2_klein":
diff --git a/backend/tests/test_image_generation_openai.py b/backend/tests/test_image_generation_openai.py
deleted file mode 100644
index 57d6009a..00000000
--- a/backend/tests/test_image_generation_openai.py
+++ /dev/null
@@ -1,80 +0,0 @@
-from types import SimpleNamespace
-from typing import Any
-
-import pytest
-
-from image_generation import generation
-
-
-class FakeImages:
- def __init__(self) -> None:
- self.kwargs: dict[str, Any] = {}
-
- async def generate(self, **kwargs: Any) -> Any:
- self.kwargs = kwargs
- return SimpleNamespace(
- data=[SimpleNamespace(url=None, b64_json="aW1hZ2UtYnl0ZXM=")]
- )
-
-
-class FakeOpenAIClient:
- last_client: "FakeOpenAIClient | None" = None
-
- def __init__(self, api_key: str, base_url: str | None) -> None:
- self.api_key = api_key
- self.base_url = base_url
- self.images = FakeImages()
- self.closed = False
- FakeOpenAIClient.last_client = self
-
- async def close(self) -> None:
- self.closed = True
-
-
-async def test_generate_image_openai_uses_gpt_image_2_and_returns_data_url(
- monkeypatch: pytest.MonkeyPatch,
-) -> None:
- monkeypatch.setattr(generation, "AsyncOpenAI", FakeOpenAIClient)
-
- result = await generation.generate_image_openai(
- "a clean product hero", "sk-test", "https://proxy.example/v1"
- )
-
- client = FakeOpenAIClient.last_client
- assert client is not None
- assert result == "data:image/png;base64,aW1hZ2UtYnl0ZXM="
- assert client.closed is True
- assert client.api_key == "sk-test"
- assert client.base_url == "https://proxy.example/v1"
- assert client.images.kwargs == {
- "model": "gpt-image-2",
- "quality": "medium",
- "output_format": "png",
- "n": 1,
- "size": "1024x1024",
- "prompt": "a clean product hero",
- }
-
-
-async def test_generate_image_openai_preserves_url_response(
- monkeypatch: pytest.MonkeyPatch,
-) -> None:
- class UrlImages(FakeImages):
- async def generate(self, **kwargs: Any) -> Any:
- self.kwargs = kwargs
- return SimpleNamespace(
- data=[SimpleNamespace(url="https://example.com/image.png", b64_json=None)]
- )
-
- class UrlOpenAIClient(FakeOpenAIClient):
- def __init__(self, api_key: str, base_url: str | None) -> None:
- super().__init__(api_key, base_url)
- self.images = UrlImages()
-
- monkeypatch.setattr(generation, "AsyncOpenAI", UrlOpenAIClient)
-
- result = await generation.generate_image_openai("a logo", "sk-test", None)
-
- assert result == "https://example.com/image.png"
diff --git a/backend/tests/test_tool_image_outputs.py b/backend/tests/test_tool_image_outputs.py
index aa9cf502..55c86f1b 100644
--- a/backend/tests/test_tool_image_outputs.py
+++ b/backend/tests/test_tool_image_outputs.py
@@ -223,6 +223,27 @@ async def test_generate_images_emits_url_parts(
assert result.multimodal_parts[0].data is None
+async def test_generate_images_does_not_fallback_to_openai_key(
+ monkeypatch: pytest.MonkeyPatch,
+) -> None:
+ monkeypatch.setattr("agent.tools.runtime.REPLICATE_API_KEY", None)
+
+ runtime = AgentToolRuntime(
+ file_state=AgentFileState(),
+ should_generate_images=True,
+ openai_api_key="openai-key",
+ openai_base_url=None,
+ )
+ result = await runtime.execute(
+ ToolCall(id="t", name="generate_images", arguments={"prompts": ["a logo"]})
+ )
+
+ assert result.ok is False
+ assert result.result == {"error": "No API key available for image generation."}
+ assert result.summary == {"error": "Missing image generation API key"}
+
+
@pytest.mark.asyncio
async def test_generate_images_uses_replicate_key_from_settings(
monkeypatch: pytest.MonkeyPatch,
@@ -246,7 +267,6 @@ async def test_generate_images_uses_replicate_key_from_settings(
openai_base_url=None,
replicate_api_key="replicate-from-ui",
)
-
result = await runtime.execute(
ToolCall(id="t", name="generate_images", arguments={"prompts": ["a logo"]})
)
diff --git a/scripts/cursor-cloud-install.sh b/scripts/cursor-cloud-install.sh
new file mode 100755
index 00000000..0da2aa8b
--- /dev/null
+++ b/scripts/cursor-cloud-install.sh
@@ -0,0 +1,19 @@
+#!/usr/bin/env bash
+set -euo pipefail
+
+REPO_ROOT="$(cd "$(dirname "${BASH_SOURCE[0]}")/.." && pwd)"
+cd "$REPO_ROOT"
+
+curl -sSL https://install.python-poetry.org | python3 -
+
+~/.local/bin/poetry -C backend install
+~/.local/bin/poetry -C backend run playwright install chromium || true
+pnpm -C frontend install
+
+if [ -d ../screenshot-to-code-saas/backend ]; then
+ ~/.local/bin/poetry -C ../screenshot-to-code-saas/backend install --no-root
+fi
+
+if [ -d ../screenshot-to-code-saas/admin ]; then
+ pnpm -C ../screenshot-to-code-saas/admin install
+fi