Skip to content

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

Python

refactor

Abi Raja768c547b5859
diff --git a/backend/models/gemini.py b/backend/models/gemini.py
index 1cc0a3dd..05fabacb 100644
--- a/backend/models/gemini.py
+++ b/backend/models/gemini.py
@@ -1,22 +1,24 @@
import base64
import time
-from typing import Awaitable, Callable, List
+from typing import Awaitable, Callable, Dict, List
from openai.types.chat import ChatCompletionMessageParam
from google import genai
from google.genai import types
from llm import Completion
-async def stream_gemini_response(
+def extract_image_from_messages(
messages: List[ChatCompletionMessageParam],
- api_key: str,
- callback: Callable[[str], Awaitable[None]],
- model_name: str,
-) -> Completion:
- start_time = time.time()
+) -> Dict[str, str]:
+ """
+ Extracts image data from OpenAI-style chat completion messages.
+
+ Args:
+ messages: List of ChatCompletionMessageParam containing message content
- # Extract image URLs from messages
- image_urls = []
+ Returns:
+ Dictionary with mime_type and data keys for the first image found
+ """
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
@@ -24,11 +26,26 @@ async def stream_gemini_response(
# 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
+ return {"mime_type": mime_type, "data": base64_data}
else:
- # Store regular URLs
- image_urls = [{"uri": image_url}] # type: ignore
- break # Exit after first image URL
+ # Handle regular URLs - would need to download and convert to base64
+ # For now, just return the URI
+ return {"uri": image_url} # type: ignore
+
+ # No image found
+ raise ValueError("No image found in messages")
+
+
+async def stream_gemini_response(
+ messages: List[ChatCompletionMessageParam],
+ api_key: str,
+ callback: Callable[[str], Awaitable[None]],
+ model_name: str,
+) -> Completion:
+ start_time = time.time()
+
+ # Get image data from messages
+ image_data = extract_image_from_messages(messages)
client = genai.Client(api_key=api_key)
full_response = ""
@@ -39,15 +56,15 @@ async def stream_gemini_response(
"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"],
+ data=base64.b64decode(image_data["data"]),
+ mime_type=image_data["mime_type"],
),
]
},
config=types.GenerateContentConfig(
temperature=0,
max_output_tokens=20000,
- thinking_config=types.ThinkingConfig(thinking_budget=10000),
+ thinking_config=types.ThinkingConfig(thinking_budget=10),
),
):
if chunk.text: