Drop in a screenshot and convert it to clean code (HTML/Tailwind/React/Vue)
Python
measure response times for each generation
Abi Raja3b3b5c1d7c6a
diff --git a/backend/llm.py b/backend/llm.pyindex f9cecaf5..0f224a11 100644--- a/backend/llm.py+++ b/backend/llm.py@@ -1,7 +1,8 @@import copyfrom enum import Enumimport base64-from typing import Any, Awaitable, Callable, List, cast+import time+from typing import Any, Awaitable, Callable, List, cast, TypedDictfrom anthropic import AsyncAnthropicfrom openai import AsyncOpenAIfrom openai.types.chat import ChatCompletionMessageParam, ChatCompletionChunk@@ -30,13 +31,19 @@ class Llm(Enum):O1_2024_12_17 = "o1-2024-12-17"+class Completion(TypedDict):+ duration: float+ code: str++async def stream_openai_response(messages: List[ChatCompletionMessageParam],api_key: str,base_url: str | None,callback: Callable[[str], Awaitable[None]],model: Llm,-) -> str:+) -> Completion:+ start_time = time.time()client = AsyncOpenAI(api_key=api_key, base_url=base_url)# Base parameters@@ -75,7 +82,8 @@ async def stream_openai_response(await client.close()- return full_response+ completion_time = time.time() - start_time+ return {"duration": completion_time, "code": full_response}# TODO: Have a seperate function that translates OpenAI messages to Claude messages@@ -84,8 +92,8 @@ async def stream_claude_response(api_key: str,callback: Callable[[str], Awaitable[None]],model: Llm,-) -> str:-+) -> Completion:+ start_time = time.time()client = AsyncAnthropic(api_key=api_key)# Base parameters@@ -142,7 +150,8 @@ async def stream_claude_response(# Close the Anthropic clientawait client.close()- return response.content[0].text+ completion_time = time.time() - start_time+ return {"duration": completion_time, "code": response.content[0].text}async def stream_claude_response_native(@@ -152,8 +161,8 @@ async def stream_claude_response_native(callback: Callable[[str], Awaitable[None]],include_thinking: bool = False,model: Llm = Llm.CLAUDE_3_OPUS,-) -> str:-+) -> Completion:+ start_time = time.time()client = AsyncAnthropic(api_key=api_key)# Base model parameters@@ -225,13 +234,18 @@ async def stream_claude_response_native(# Close the Anthropic clientawait client.close()+ completion_time = time.time() - start_time+if IS_DEBUG_ENABLED:debug_file_writer.write_to_file("full_stream.txt", full_stream)if not response:raise Exception("No HTML response found in AI response")else:- return response.content[0].text # type: ignore+ return {+ "duration": completion_time,+ "code": response.content[0].text, # type: ignore+ }async def stream_gemini_response(@@ -239,7 +253,8 @@ async def stream_gemini_response(api_key: str,callback: Callable[[str], Awaitable[None]],model: Llm,-) -> str:+) -> Completion:+ start_time = time.time()# Extract image URLs from messagesimage_urls = []@@ -276,4 +291,5 @@ async def stream_gemini_response(if response.text: # type: ignorefull_response += response.text # type: ignoreawait callback(response.text) # type: ignore- return full_response # type: ignore+ completion_time = time.time() - start_time+ return {"duration": completion_time, "code": full_response}diff --git a/backend/mock_llm.py b/backend/mock_llm.pyindex a76b9063..8f6f215f 100644--- a/backend/mock_llm.py+++ b/backend/mock_llm.py@@ -2,6 +2,7 @@ import asynciofrom typing import Awaitable, Callablefrom custom_types import InputMode+from llm import CompletionSTREAM_CHUNK_SIZE = 20@@ -9,7 +10,7 @@ STREAM_CHUNK_SIZE = 20async def mock_completion(process_chunk: Callable[[str, int], Awaitable[None]], input_mode: InputMode-) -> str:+) -> Completion:code_to_return = (TALLY_FORM_VIDEO_PROMPT_MOCKif input_mode == "video"@@ -17,7 +18,7 @@ async def mock_completion()for i in range(0, len(code_to_return), STREAM_CHUNK_SIZE):- await process_chunk(code_to_return[i : i + STREAM_CHUNK_SIZE], 0)+ await process_chunk(code_to_return[i : i + STREAM_CHUNK_SIZE], i)await asyncio.sleep(0.01)if input_mode == "video":@@ -30,7 +31,7 @@ async def mock_completion(else:code_to_return = "Error: HTML block not found."- return code_to_return+ return {"duration": 0.1, "code": code_to_return}APPLE_MOCK_CODE = """<html lang="en">@@ -138,7 +139,7 @@ NYTIMES_MOCK_CODE = """<div class="grid grid-cols-3 gap-4"><div class="col-span-2"><article class="mb-4">- <h2 class="text-xl font-bold mb-2">Israeli Military Raids Gaza’s Largest Hospital</h2>+ <h2 class="text-xl font-bold mb-2">Israeli Military Raids Gaza's Largest Hospital</h2><p class="text-gray-700 mb-2">Israeli troops have entered the Al-Shifa Hospital complex, where conditions have grown dire and Israel says Hamas fighters are embedded.</p><a href="#" class="text-blue-600 text-sm">See more updates <i class="fas fa-external-link-alt"></i></a></article>@@ -206,7 +207,7 @@ NO_IMAGES_NYTIMES_MOCK_CODE = """<div class="grid grid-cols-3 gap-4"><div class="col-span-2"><article class="mb-4">- <h2 class="text-xl font-bold mb-2">Israeli Military Raids Gaza’s Largest Hospital</h2>+ <h2 class="text-xl font-bold mb-2">Israeli Military Raids Gaza's Largest Hospital</h2><p class="text-gray-700 mb-2">Israeli troops have entered the Al-Shifa Hospital complex, where conditions have grown dire and Israel says Hamas fighters are embedded.</p><a href="#" class="text-blue-600 text-sm">See more updates <i class="fas fa-external-link-alt"></i></a></article>diff --git a/backend/routes/generate_code.py b/backend/routes/generate_code.pyindex 5f970a6c..e1c9d98d 100644--- a/backend/routes/generate_code.py+++ b/backend/routes/generate_code.py@@ -16,6 +16,7 @@ from config import ()from custom_types import InputModefrom llm import (+ Completion,Llm,stream_claude_response,stream_claude_response_native,@@ -224,7 +225,10 @@ async def stream_code(websocket: WebSocket):await send_message("chunk", content, variantIndex)if SHOULD_MOCK_AI_RESPONSE:- completions = [await mock_completion(process_chunk, input_mode=input_mode)]+ completion_results = [+ await mock_completion(process_chunk, input_mode=input_mode)+ ]+ completions = [result["code"] for result in completion_results]else:try:if input_mode == "video":@@ -234,7 +238,7 @@ async def stream_code(websocket: WebSocket):)raise Exception("No Anthropic key")- completions = [+ completion_results = [await stream_claude_response_native(system_prompt=VIDEO_PROMPT,messages=prompt_messages, # type: ignore@@ -244,6 +248,7 @@ async def stream_code(websocket: WebSocket):include_thinking=True,)]+ completions = [result["code"] for result in completion_results]else:# Depending on the presence and absence of various keys,@@ -278,7 +283,7 @@ async def stream_code(websocket: WebSocket):)raise Exception("No OpenAI or Anthropic key")- tasks: List[Coroutine[Any, Any, str]] = []+ tasks: List[Coroutine[Any, Any, Completion]] = []for index, model in enumerate(variant_models):if model == Llm.GPT_4O_2024_11_20:if openai_api_key is None:@@ -332,26 +337,33 @@ async def stream_code(websocket: WebSocket):# If all generations failed, throw an errorall_generations_failed = all(- isinstance(completion, Exception) for completion in completions+ isinstance(completion, BaseException) for completion in completions)if all_generations_failed:await throw_error("Error generating code. Please contact support.")# Print the all the underlying exceptions for debuggingfor completion in completions:- traceback.print_exception(- type(completion), completion, completion.__traceback__- )+ if isinstance(completion, BaseException):+ traceback.print_exception(completion)raise Exception("All generations failed")# If some completions failed, replace them with empty stringsfor index, completion in enumerate(completions):- if isinstance(completion, Exception):- completions[index] = ""+ if isinstance(completion, BaseException):+ completions[index] = Completion(duration=0, code="")print("Generation failed for variant", index)print(completion)+ else:+ print(+ f"{variant_models[index].value} completion took {completion['duration']:.2f} seconds"+ )- print("Models used for generation: ", variant_models)+ completions = [+ result["code"]+ for result in completions+ if not isinstance(result, BaseException)+ ]except openai.AuthenticationError as e:print("[GENERATE_CODE] Authentication failed", e)