Skip to content

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

Python

move gemini into its own file

Abi Rajaedcb2520f9c9
diff --git a/backend/evals/core.py b/backend/evals/core.py
index f9987d5f..aea91332 100644
--- a/backend/evals/core.py
+++ b/backend/evals/core.py
@@ -2,9 +2,9 @@ from config import ANTHROPIC_API_KEY, GEMINI_API_KEY, OPENAI_API_KEY
from llm import (
Llm,
stream_claude_response,
- stream_gemini_response,
stream_openai_response,
)
+from gemini import stream_gemini_response
from prompts import assemble_prompt
from prompts.types import Stack
from openai.types.chat import ChatCompletionMessageParam
diff --git a/backend/gemini.py b/backend/gemini.py
new file mode 100644
index 00000000..beecc273
--- /dev/null
+++ b/backend/gemini.py
@@ -0,0 +1,61 @@
+import base64
+import time
+from typing import Awaitable, Callable, List, TypedDict
+from openai.types.chat import ChatCompletionMessageParam
+from google import genai
+from google.genai import types
+
+
+class Completion(TypedDict):
+ duration: float
+ code: str
+
+
+async def stream_gemini_response(
+ messages: List[ChatCompletionMessageParam],
+ api_key: str,
+ callback: Callable[[str], Awaitable[None]],
+ model_name: str,
+) -> Completion:
+ start_time = time.time()
+
+ # Extract image URLs from messages
+ image_urls = []
+ for content_part in messages[-1]["content"]: # type: ignore
+ if content_part["type"] == "image_url": # type: ignore
+ image_url = content_part["image_url"]["url"] # type: ignore
+ if image_url.startswith("data:"): # type: ignore
+ # Extract base64 data and mime type for data URLs
+ mime_type = image_url.split(";")[0].split(":")[1] # type: ignore
+ base64_data = image_url.split(",")[1] # type: ignore
+ image_urls = [{"mime_type": mime_type, "data": base64_data}] # type: ignore
+ else:
+ # Store regular URLs
+ image_urls = [{"uri": image_url}] # type: ignore
+ break # Exit after first image URL
+
+ client = genai.Client(api_key=api_key)
+ full_response = ""
+
+ async for chunk in await client.aio.models.generate_content_stream(
+ model=model_name,
+ contents={
+ "parts": [
+ {"text": messages[0]["content"]}, # type: ignore
+ types.Part.from_bytes(
+ data=base64.b64decode(image_urls[0]["data"]),
+ mime_type=image_urls[0]["mime_type"],
+ ),
+ ]
+ },
+ config=types.GenerateContentConfig(
+ temperature=0,
+ max_output_tokens=20000,
+ thinking_config=types.ThinkingConfig(thinking_budget=10000),
+ ),
+ ):
+ if chunk.text:
+ full_response += chunk.text
+ await callback(chunk.text)
+ completion_time = time.time() - start_time
+ return {"duration": completion_time, "code": full_response}
diff --git a/backend/llm.py b/backend/llm.py
index 22f9acef..c382050b 100644
--- a/backend/llm.py
+++ b/backend/llm.py
@@ -1,6 +1,5 @@
import copy
from enum import Enum
-import base64
import time
from typing import Any, Awaitable, Callable, List, cast, TypedDict
from anthropic import AsyncAnthropic
@@ -9,8 +8,6 @@ from openai.types.chat import ChatCompletionMessageParam, ChatCompletionChunk
from config import IS_DEBUG_ENABLED
from debug.DebugFileWriter import DebugFileWriter
from image_processing.utils import process_image
-from google import genai
-from google.genai import types
from utils import pprint_prompt
@@ -285,53 +282,3 @@ async def stream_claude_response_native(
"duration": completion_time,
"code": response.content[0].text, # type: ignore
}
-
-
-async def stream_gemini_response(
- messages: List[ChatCompletionMessageParam],
- api_key: str,
- callback: Callable[[str], Awaitable[None]],
- model: Llm,
-) -> Completion:
- start_time = time.time()
-
- # Extract image URLs from messages
- image_urls = []
- for content_part in messages[-1]["content"]: # type: ignore
- if content_part["type"] == "image_url": # type: ignore
- image_url = content_part["image_url"]["url"] # type: ignore
- if image_url.startswith("data:"): # type: ignore
- # Extract base64 data and mime type for data URLs
- mime_type = image_url.split(";")[0].split(":")[1] # type: ignore
- base64_data = image_url.split(",")[1] # type: ignore
- image_urls = [{"mime_type": mime_type, "data": base64_data}] # type: ignore
- else:
- # Store regular URLs
- image_urls = [{"uri": image_url}] # type: ignore
- break # Exit after first image URL
-
- client = genai.Client(api_key=api_key)
- full_response = ""
-
- async for chunk in await client.aio.models.generate_content_stream(
- model=model.value,
- contents={
- "parts": [
- {"text": messages[0]["content"]}, # type: ignore
- types.Part.from_bytes(
- data=base64.b64decode(image_urls[0]["data"]),
- mime_type=image_urls[0]["mime_type"],
- ),
- ]
- },
- config=types.GenerateContentConfig(
- temperature=0,
- max_output_tokens=20000,
- thinking_config=types.ThinkingConfig(thinking_budget=10000),
- ),
- ): # type: ignore
- if chunk.text:
- full_response += chunk.text
- await callback(chunk.text)
- completion_time = time.time() - start_time
- return {"duration": completion_time, "code": full_response}
diff --git a/backend/routes/generate_code.py b/backend/routes/generate_code.py
index 53c6eda7..8f627528 100644
--- a/backend/routes/generate_code.py
+++ b/backend/routes/generate_code.py
@@ -20,9 +20,9 @@ from llm import (
Llm,
stream_claude_response,
stream_claude_response_native,
- stream_gemini_response,
stream_openai_response,
)
+from gemini import stream_gemini_response
from fs_logging.core import write_logs
from mock_llm import mock_completion
from typing import Any, Callable, Coroutine, Dict, List, Literal, cast, get_args
@@ -265,7 +265,7 @@ async def stream_code(websocket: WebSocket):
if openai_api_key and anthropic_api_key:
variant_models = [
claude_model,
- Llm.GEMINI_2_5_PRO_PREVIEW_05_06,
+ Llm.GEMINI_2_5_FLASH_PREVIEW_05_20,
]
elif openai_api_key:
variant_models = [
@@ -324,7 +324,7 @@ async def stream_code(websocket: WebSocket):
prompt_messages,
api_key=GEMINI_API_KEY,
callback=lambda x, i=index: process_chunk(x, i),
- model=model,
+ model_name=model.value,
)
)
elif (