diff --git a/README.md b/README.md
index 393e027..8b771a1 100644
--- a/README.md
+++ b/README.md
@@ -12,7 +12,8 @@ See the [Examples](#examples) section below for more demos.
## 🌟 Recent Updates
-- Nov 28 - 🔥 🔥 🔥 Get output code in React or Bootstrap or TailwindCSS
+- Nov 30 - Dark mode, output code in Ionic (thanks [@dialmedu](https://github.com/dialmedu)), set OpenAI base URL
+- Nov 28 - 🔥 🔥 🔥 Customize your stack: React or Bootstrap or TailwindCSS
- Nov 23 - Send in a screenshot of the current replicated version (sometimes improves quality of subsequent generations)
- Nov 21 - Edit code in the code editor and preview changes live thanks to [@clean99](https://github.com/clean99)
- Nov 20 - Paste in a URL to screenshot and clone (requires [ScreenshotOne free API key](https://screenshotone.com?via=screenshot-to-code))
diff --git a/Troubleshooting.md b/Troubleshooting.md
index 0704859..20fa815 100644
--- a/Troubleshooting.md
+++ b/Troubleshooting.md
@@ -1,4 +1,4 @@
-### Getting an OpenAI API key
+### Getting an OpenAI API key with GPT4-Vision model access
You don't need a ChatGPT Pro account. Screenshot to code uses API keys from your OpenAI developer account. In order to get access to the GPT4 Vision model, log into your OpenAI account and then, follow these instructions:
@@ -10,6 +10,7 @@ You don't need a ChatGPT Pro account. Screenshot to code uses API keys from your
5. Go to Settings > Limits and check at the bottom of the page, your current tier has to be "Tier 1" to have GPT4 access
+6. Go to Screenshot to code and paste it in the Settings dialog under OpenAI key (gear icon). Your key is only stored in your browser. Never stored on our servers.
Some users have also reported that it can take upto 30 minutes after your credit purchase for the GPT4 vision model to be activated.
diff --git a/backend/image_generation.py b/backend/image_generation.py
index 080334f..ad21772 100644
--- a/backend/image_generation.py
+++ b/backend/image_generation.py
@@ -5,8 +5,8 @@ from openai import AsyncOpenAI
from bs4 import BeautifulSoup
-async def process_tasks(prompts, api_key):
- tasks = [generate_image(prompt, api_key) for prompt in prompts]
+async def process_tasks(prompts, api_key, base_url):
+ tasks = [generate_image(prompt, api_key, base_url) for prompt in prompts]
results = await asyncio.gather(*tasks, return_exceptions=True)
processed_results = []
@@ -20,8 +20,8 @@ async def process_tasks(prompts, api_key):
return processed_results
-async def generate_image(prompt, api_key):
- client = AsyncOpenAI(api_key=api_key)
+async def generate_image(prompt, api_key, base_url):
+ client = AsyncOpenAI(api_key=api_key, base_url=base_url)
image_params = {
"model": "dall-e-3",
"quality": "standard",
@@ -60,7 +60,7 @@ def create_alt_url_mapping(code):
return mapping
-async def generate_images(code, api_key, image_cache):
+async def generate_images(code, api_key, base_url, image_cache):
# Find all images
soup = BeautifulSoup(code, "html.parser")
images = soup.find_all("img")
@@ -87,7 +87,7 @@ async def generate_images(code, api_key, image_cache):
return code
# Generate images
- results = await process_tasks(prompts, api_key)
+ results = await process_tasks(prompts, api_key, base_url)
# Create a dict mapping alt text to image URL
mapped_image_urls = dict(zip(prompts, results))
diff --git a/backend/llm.py b/backend/llm.py
index b52c3c9..fdb1ba0 100644
--- a/backend/llm.py
+++ b/backend/llm.py
@@ -6,9 +6,12 @@ MODEL_GPT_4_VISION = "gpt-4-vision-preview"
async def stream_openai_response(
- messages, api_key: str, callback: Callable[[str], Awaitable[None]]
+ messages,
+ api_key: str,
+ base_url: str | None,
+ callback: Callable[[str], Awaitable[None]],
):
- client = AsyncOpenAI(api_key=api_key)
+ client = AsyncOpenAI(api_key=api_key, base_url=base_url)
model = MODEL_GPT_4_VISION
diff --git a/backend/main.py b/backend/main.py
index 4c5823b..108c4dc 100644
--- a/backend/main.py
+++ b/backend/main.py
@@ -69,13 +69,11 @@ async def stream_code(websocket: WebSocket):
print("Received params")
- # Read the output settings from the request. Fall back to default if not provided.
- output_settings = {"css": "tailwind", "js": "vanilla"}
- if params["outputSettings"] and params["outputSettings"]["css"]:
- output_settings["css"] = params["outputSettings"]["css"]
- if params["outputSettings"] and params["outputSettings"]["js"]:
- output_settings["js"] = params["outputSettings"]["js"]
- print("Using output settings:", output_settings)
+ # Read the code config settings from the request. Fall back to default if not provided.
+ generated_code_config = ""
+ if "generatedCodeConfig" in params and params["generatedCodeConfig"]:
+ generated_code_config = params["generatedCodeConfig"]
+ print(f"Generating {generated_code_config} code")
# Get the OpenAI API key from the request. Fall back to environment variable if not provided.
# If neither is provided, we throw an error.
@@ -111,6 +109,22 @@ async def stream_code(websocket: WebSocket):
)
return
+ # Get the OpenAI Base URL from the request. Fall back to environment variable if not provided.
+ openai_base_url = None
+ # Disable user-specified OpenAI Base URL in prod
+ if not os.environ.get("IS_PROD"):
+ if "openAiBaseURL" in params and params["openAiBaseURL"]:
+ openai_base_url = params["openAiBaseURL"]
+ print("Using OpenAI Base URL from client-side settings dialog")
+ else:
+ openai_base_url = os.environ.get("OPENAI_BASE_URL")
+ if openai_base_url:
+ print("Using OpenAI Base URL from environment variable")
+
+ if not openai_base_url:
+ print("Using official OpenAI URL")
+
+ # Get the image generation flag from the request. Fall back to True if not provided.
should_generate_images = (
params["isImageGenerationEnabled"]
if "isImageGenerationEnabled" in params
@@ -123,12 +137,23 @@ async def stream_code(websocket: WebSocket):
async def process_chunk(content):
await websocket.send_json({"type": "chunk", "value": content})
- if params.get("resultImage") and params["resultImage"]:
- prompt_messages = assemble_prompt(
- params["image"], output_settings, params["resultImage"]
+ # Assemble the prompt
+ try:
+ if params.get("resultImage") and params["resultImage"]:
+ prompt_messages = assemble_prompt(
+ params["image"], generated_code_config, params["resultImage"]
+ )
+ else:
+ prompt_messages = assemble_prompt(params["image"], generated_code_config)
+ except:
+ await websocket.send_json(
+ {
+ "type": "error",
+ "value": "Error assembling prompt. Contact support at support@picoapps.xyz",
+ }
)
- else:
- prompt_messages = assemble_prompt(params["image"], output_settings)
+ await websocket.close()
+ return
# Image cache for updates so that we don't have to regenerate images
image_cache = {}
@@ -149,6 +174,7 @@ async def stream_code(websocket: WebSocket):
completion = await stream_openai_response(
prompt_messages,
api_key=openai_api_key,
+ base_url=openai_base_url,
callback=lambda x: process_chunk(x),
)
@@ -161,7 +187,10 @@ async def stream_code(websocket: WebSocket):
{"type": "status", "value": "Generating images..."}
)
updated_html = await generate_images(
- completion, api_key=openai_api_key, image_cache=image_cache
+ completion,
+ api_key=openai_api_key,
+ base_url=openai_base_url,
+ image_cache=image_cache,
)
else:
updated_html = completion
diff --git a/backend/prompts.py b/backend/prompts.py
index f01eb7e..c9e48cb 100644
--- a/backend/prompts.py
+++ b/backend/prompts.py
@@ -77,23 +77,60 @@ Return only the full code in tags.
Do not include markdown "```" or "```html" at the start or end.
"""
+IONIC_TAILWIND_SYSTEM_PROMPT = """
+You are an expert Ionic/Tailwind developer
+You take screenshots of a reference web page from the user, and then build single page apps
+using Ionic and Tailwind CSS.
+You might also be given a screenshot(The second image) of a web page that you have already built, and asked to
+update it to look more like the reference image(The first image).
+
+- Make sure the app looks exactly like the screenshot.
+- Pay close attention to background color, text color, font size, font family,
+padding, margin, border, etc. Match the colors and sizes exactly.
+- Use the exact text from the screenshot.
+- Do not add comments in the code such as "" and "" in place of writing the full code. WRITE THE FULL CODE.
+- Repeat elements as needed to match the screenshot. For example, if there are 15 items, the code should have 15 items. DO NOT LEAVE comments like "" or bad things will happen.
+- For images, use placeholder images from https://placehold.co and include a detailed description of the image in the alt text so that an image generation AI can generate the image later.
+
+In terms of libraries,
+
+- Use these script to include Ionic so that it can run on a standalone page:
+
+
+
+- Use this script to include Tailwind:
+- You can use Google Fonts
+- ionicons for icons, add the following
+
+
+
+Return only the full code in tags.
+Do not include markdown "```" or "```html" at the start or end.
+"""
+
USER_PROMPT = """
Generate code for a web page that looks exactly like this.
"""
-def assemble_prompt(image_data_url, output_settings: dict, result_image_data_url=None):
+def assemble_prompt(
+ image_data_url, generated_code_config: str, result_image_data_url=None
+):
# Set the system prompt based on the output settings
- chosen_prompt_name = "tailwind"
system_content = TAILWIND_SYSTEM_PROMPT
- if output_settings["css"] == "bootstrap":
- chosen_prompt_name = "bootstrap"
- system_content = BOOTSTRAP_SYSTEM_PROMPT
- if output_settings["js"] == "react":
- chosen_prompt_name = "react-tailwind"
+ if generated_code_config == "html_tailwind":
+ system_content = TAILWIND_SYSTEM_PROMPT
+ elif generated_code_config == "react_tailwind":
system_content = REACT_TAILWIND_SYSTEM_PROMPT
-
- print("Using system prompt:", chosen_prompt_name)
+ elif generated_code_config == "bootstrap":
+ system_content = BOOTSTRAP_SYSTEM_PROMPT
+ elif generated_code_config == "ionic_tailwind":
+ system_content = IONIC_TAILWIND_SYSTEM_PROMPT
+ else:
+ raise Exception("Code config is not one of available options")
user_content = [
{
diff --git a/backend/test_prompts.py b/backend/test_prompts.py
index 2eaaaf4..5d8cd88 100644
--- a/backend/test_prompts.py
+++ b/backend/test_prompts.py
@@ -79,19 +79,58 @@ Return only the full code in tags.
Do not include markdown "```" or "```html" at the start or end.
"""
+IONIC_TAILWIND_SYSTEM_PROMPT = """
+You are an expert Ionic/Tailwind developer
+You take screenshots of a reference web page from the user, and then build single page apps
+using Ionic and Tailwind CSS.
+You might also be given a screenshot(The second image) of a web page that you have already built, and asked to
+update it to look more like the reference image(The first image).
+
+- Make sure the app looks exactly like the screenshot.
+- Pay close attention to background color, text color, font size, font family,
+padding, margin, border, etc. Match the colors and sizes exactly.
+- Use the exact text from the screenshot.
+- Do not add comments in the code such as "" and "" in place of writing the full code. WRITE THE FULL CODE.
+- Repeat elements as needed to match the screenshot. For example, if there are 15 items, the code should have 15 items. DO NOT LEAVE comments like "" or bad things will happen.
+- For images, use placeholder images from https://placehold.co and include a detailed description of the image in the alt text so that an image generation AI can generate the image later.
+
+In terms of libraries,
+
+- Use these script to include Ionic so that it can run on a standalone page:
+
+
+
+- Use this script to include Tailwind:
+- You can use Google Fonts
+- ionicons for icons, add the following
+
+
+
+Return only the full code in tags.
+Do not include markdown "```" or "```html" at the start or end.
+"""
+
def test_prompts():
tailwind_prompt = assemble_prompt(
- "image_data_url", {"css": "tailwind", "js": "vanilla"}, "result_image_data_url"
+ "image_data_url", "html_tailwind", "result_image_data_url"
)
assert tailwind_prompt[0]["content"] == TAILWIND_SYSTEM_PROMPT
+ react_tailwind_prompt = assemble_prompt(
+ "image_data_url", "react_tailwind", "result_image_data_url"
+ )
+ assert react_tailwind_prompt[0]["content"] == REACT_TAILWIND_SYSTEM_PROMPT
+
bootstrap_prompt = assemble_prompt(
- "image_data_url", {"css": "bootstrap", "js": "vanilla"}, "result_image_data_url"
+ "image_data_url", "bootstrap", "result_image_data_url"
)
assert bootstrap_prompt[0]["content"] == BOOTSTRAP_SYSTEM_PROMPT
- react_tailwind_prompt = assemble_prompt(
- "image_data_url", {"css": "tailwind", "js": "react"}, "result_image_data_url"
+ ionic_tailwind = assemble_prompt(
+ "image_data_url", "ionic_tailwind", "result_image_data_url"
)
- assert react_tailwind_prompt[0]["content"] == REACT_TAILWIND_SYSTEM_PROMPT
+ assert ionic_tailwind[0]["content"] == IONIC_TAILWIND_SYSTEM_PROMPT
diff --git a/frontend/src/App.tsx b/frontend/src/App.tsx
index 329ab69..dfadbbe 100644
--- a/frontend/src/App.tsx
+++ b/frontend/src/App.tsx
@@ -1,4 +1,4 @@
-import { useRef, useState } from "react";
+import { useEffect, useRef, useState } from "react";
import ImageUpload from "./components/ImageUpload";
import CodePreview from "./components/CodePreview";
import Preview from "./components/Preview";
@@ -18,14 +18,7 @@ import { Button } from "@/components/ui/button";
import { Textarea } from "@/components/ui/textarea";
import { Tabs, TabsContent, TabsList, TabsTrigger } from "./components/ui/tabs";
import SettingsDialog from "./components/SettingsDialog";
-import {
- Settings,
- EditorTheme,
- AppState,
- CSSOption,
- OutputSettings,
- JSFrameworkOption,
-} from "./types";
+import { Settings, EditorTheme, AppState, GeneratedCodeConfig } from "./types";
import { IS_RUNNING_ON_CLOUD } from "./config";
import { PicoBadge } from "./components/PicoBadge";
import { OnboardingNote } from "./components/OnboardingNote";
@@ -47,23 +40,35 @@ function App() {
const [settings, setSettings] = usePersistedState(
{
openAiApiKey: null,
+ openAiBaseURL: null,
screenshotOneApiKey: null,
isImageGenerationEnabled: true,
editorTheme: EditorTheme.COBALT,
+ generatedCodeConfig: GeneratedCodeConfig.HTML_TAILWIND,
+ // Only relevant for hosted version
isTermOfServiceAccepted: false,
accessCode: null,
},
"setting"
);
- const [outputSettings, setOutputSettings] = useState({
- css: CSSOption.TAILWIND,
- js: JSFrameworkOption.NO_FRAMEWORK,
- });
+
const [shouldIncludeResultImage, setShouldIncludeResultImage] =
useState(false);
const wsRef = useRef(null);
+ // When the user already has the settings in local storage, newly added keys
+ // do not get added to the settings so if it's falsy, we populate it with the default
+ // value
+ useEffect(() => {
+ if (!settings.generatedCodeConfig) {
+ setSettings((prev) => ({
+ ...prev,
+ generatedCodeConfig: GeneratedCodeConfig.HTML_TAILWIND,
+ }));
+ }
+ }, [settings.generatedCodeConfig, setSettings]);
+
const takeScreenshot = async (): Promise => {
const iframeElement = document.querySelector(
"#preview-desktop"
@@ -119,7 +124,7 @@ function App() {
setAppState(AppState.CODING);
// Merge settings with params
- const updatedParams = { ...params, ...settings, outputSettings };
+ const updatedParams = { ...params, ...settings };
generateCode(
wsRef,
@@ -177,7 +182,7 @@ function App() {
};
return (
-