Drop in a screenshot and convert it to clean code (HTML/Tailwind/React/Vue)
Python
support setting openai api key on the client side
Abi Rajaee9b40d99040
diff --git a/backend/image_generation.py b/backend/image_generation.pyindex e1230a81..080334fe 100644--- a/backend/image_generation.py+++ b/backend/image_generation.py@@ -5,8 +5,8 @@ from openai import AsyncOpenAIfrom bs4 import BeautifulSoup-async def process_tasks(prompts):- tasks = [generate_image(prompt) for prompt in prompts]+async def process_tasks(prompts, api_key):+ tasks = [generate_image(prompt, api_key) for prompt in prompts]results = await asyncio.gather(*tasks, return_exceptions=True)processed_results = []@@ -20,8 +20,8 @@ async def process_tasks(prompts):return processed_results-async def generate_image(prompt):- client = AsyncOpenAI(api_key=os.environ.get("OPENAI_API_KEY"))+async def generate_image(prompt, api_key):+ client = AsyncOpenAI(api_key=api_key)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, image_cache):+async def generate_images(code, api_key, image_cache):# Find all imagessoup = BeautifulSoup(code, "html.parser")images = soup.find_all("img")@@ -87,7 +87,7 @@ async def generate_images(code, image_cache):return code# Generate images- results = await process_tasks(prompts)+ results = await process_tasks(prompts, api_key)# Create a dict mapping alt text to image URLmapped_image_urls = dict(zip(prompts, results))diff --git a/backend/llm.py b/backend/llm.pyindex 686b008c..b52c3c9f 100644--- a/backend/llm.py+++ b/backend/llm.py@@ -4,10 +4,12 @@ from openai import AsyncOpenAIMODEL_GPT_4_VISION = "gpt-4-vision-preview"-client = AsyncOpenAI(api_key=os.environ.get("OPENAI_API_KEY"))+async def stream_openai_response(+ messages, api_key: str, callback: Callable[[str], Awaitable[None]]+):+ client = AsyncOpenAI(api_key=api_key)-async def stream_openai_response(messages, callback: Callable[[str], Awaitable[None]]):model = MODEL_GPT_4_VISION# Base parametersdiff --git a/backend/main.py b/backend/main.pyindex 42b3f2fc..a3a74add 100644--- a/backend/main.py+++ b/backend/main.py@@ -41,6 +41,25 @@ async def stream_code_test(websocket: WebSocket):params = await websocket.receive_json()+ # Get the OpenAI API key from the request. Fall back to environment variable if not provided.+ # If neither is provided, we throw an error.+ if params["openAiApiKey"]:+ openai_api_key = params["openAiApiKey"]+ print("Using OpenAI API key from client-side settings dialog")+ else:+ openai_api_key = os.environ.get("OPENAI_API_KEY")+ print("Using OpenAI API key from environment variable")++ if not openai_api_key:+ print("OpenAI API key not found")+ await websocket.send_json(+ {+ "type": "error",+ "value": "OpenAI API key found. Please add your API key in the settings dialog or add it to backend/.env file.",+ }+ )+ return+should_generate_images = (params["isImageGenerationEnabled"]if "isImageGenerationEnabled" in params@@ -73,7 +92,8 @@ async def stream_code_test(websocket: WebSocket):else:completion = await stream_openai_response(prompt_messages,- lambda x: process_chunk(x),+ api_key=openai_api_key,+ callback=lambda x: process_chunk(x),)# Write the messages dict into a log so that we can debug later@@ -84,7 +104,9 @@ async def stream_code_test(websocket: WebSocket):await websocket.send_json({"type": "status", "value": "Generating images..."})- updated_html = await generate_images(completion, image_cache=image_cache)+ updated_html = await generate_images(+ completion, api_key=openai_api_key, image_cache=image_cache+ )else:updated_html = completionawait websocket.send_json({"type": "setCode", "value": updated_html})diff --git a/frontend/src/App.tsx b/frontend/src/App.tsxindex d5560be2..634c8008 100644--- a/frontend/src/App.tsx+++ b/frontend/src/App.tsx@@ -29,6 +29,7 @@ function App() {const [updateInstruction, setUpdateInstruction] = useState("");const [history, setHistory] = useState<string[]>([]);const [settings, setSettings] = useState<Settings>({+ openAiApiKey: null,isImageGenerationEnabled: true,});diff --git a/frontend/src/components/SettingsDialog.tsx b/frontend/src/components/SettingsDialog.tsxindex 294ec591..512894f1 100644--- a/frontend/src/components/SettingsDialog.tsx+++ b/frontend/src/components/SettingsDialog.tsx@@ -1,6 +1,8 @@import {Dialog,+ DialogClose,DialogContent,+ DialogFooter,DialogHeader,DialogTitle,DialogTrigger,@@ -9,6 +11,7 @@ import { FaCog } from "react-icons/fa";import { Settings } from "../types";import { Switch } from "./ui/switch";import { Label } from "./ui/label";+import { Input } from "./ui/input";interface Props {settings: Settings;@@ -24,25 +27,48 @@ function SettingsDialog({ settings, setSettings }: Props) {<DialogContent><DialogHeader><DialogTitle className="mb-4">Settings</DialogTitle>- <div className="flex items-center space-x-2">- <Label htmlFor="image-generation">- <div>DALL-E Placeholder Image Generation</div>- <div className="font-light mt-2">- More fun with it but if you want to save money, turn it off.- </div>- </Label>- <Switch- id="image-generation"- checked={settings.isImageGenerationEnabled}- onCheckedChange={() =>- setSettings((s) => ({- ...s,- isImageGenerationEnabled: !s.isImageGenerationEnabled,- }))- }- />- </div></DialogHeader>+ <div className="flex items-center space-x-2">+ <Label htmlFor="image-generation">+ <div>DALL-E Placeholder Image Generation</div>+ <div className="font-light mt-2">+ More fun with it but if you want to save money, turn it off.+ </div>+ </Label>+ <Switch+ id="image-generation"+ checked={settings.isImageGenerationEnabled}+ onCheckedChange={() =>+ setSettings((s) => ({+ ...s,+ isImageGenerationEnabled: !s.isImageGenerationEnabled,+ }))+ }+ />+ </div>+ <div className="flex flex-col space-y-4">+ <Label htmlFor="openai-api-key">+ <div>OpenAI API key</div>+ <div className="font-light mt-2">+ Never stored. Overrides your .env config.+ </div>+ </Label>++ <Input+ id="openai-api-key"+ placeholder="OpenAI API key"+ value={settings.openAiApiKey || ""}+ onChange={(e) =>+ setSettings((s) => ({+ ...s,+ openAiApiKey: e.target.value,+ }))+ }+ />+ </div>+ <DialogFooter>+ <DialogClose>Save</DialogClose>+ </DialogFooter></DialogContent></Dialog>);diff --git a/frontend/src/components/ui/input.tsx b/frontend/src/components/ui/input.tsxnew file mode 100644index 00000000..a92b8e0e--- /dev/null+++ b/frontend/src/components/ui/input.tsx@@ -0,0 +1,25 @@+import * as React from "react"++import { cn } from "@/lib/utils"++export interface InputProps+ extends React.InputHTMLAttributes<HTMLInputElement> {}++const Input = React.forwardRef<HTMLInputElement, InputProps>(+ ({ className, type, ...props }, ref) => {+ return (+ <input+ type={type}+ className={cn(+ "flex h-9 w-full rounded-md border border-input bg-transparent px-3 py-1 text-sm shadow-sm transition-colors file:border-0 file:bg-transparent file:text-sm file:font-medium placeholder:text-muted-foreground focus-visible:outline-none focus-visible:ring-1 focus-visible:ring-ring disabled:cursor-not-allowed disabled:opacity-50",+ className+ )}+ ref={ref}+ {...props}+ />+ )+ }+)+Input.displayName = "Input"++export { Input }diff --git a/frontend/src/generateCode.ts b/frontend/src/generateCode.tsindex 06ecc5bb..c3e92187 100644--- a/frontend/src/generateCode.ts+++ b/frontend/src/generateCode.ts@@ -36,6 +36,9 @@ export function generateCode(onStatusUpdate(response.value);} else if (response.type === "setCode") {onSetCode(response.value);+ } else if (response.type === "error") {+ console.error("Error generating code", response.value);+ toast.error(response.value);}});diff --git a/frontend/src/types.ts b/frontend/src/types.tsindex d9fe60bd..b2c63050 100644--- a/frontend/src/types.ts+++ b/frontend/src/types.ts@@ -1,3 +1,4 @@export interface Settings {+ openAiApiKey: string | null;isImageGenerationEnabled: boolean;}