-
Notifications
You must be signed in to change notification settings - Fork 1
β‘ Bolt: Dependency inject Gemini Client to save ~300ms latency #27
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. Weβll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
suvadityamuk
wants to merge
2
commits into
main
Choose a base branch
from
bolt/gemini-client-injection-4524891104038060051
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,3 +1,7 @@ | ||
| ## 2024-05-21 - [Blender Modal Redraws] | ||
| **Learning:** Blender `modal` operators run frequently (e.g. every 0.3s). Calling `area.tag_redraw()` unconditionally in the loop forces constant viewport rendering, even when no state has changed, causing unnecessary CPU/GPU load. | ||
| **Action:** Only call `tag_redraw()` when the operator actually processes data or updates the UI state (e.g., via a `refresh_needed` flag). | ||
|
|
||
| ## 2024-05-22 - [Gemini Client Instantiation] | ||
| **Learning:** Instantiating `google.genai.Client` incurs a ~75ms setup overhead per call. Calling this multiple times in a loop or per image view generation adds unnecessary latency. | ||
| **Action:** Instantiate the client once in `operators.py` and inject it into utility functions to reuse the instance, saving ~300ms total latency per complete model generation. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -16,10 +16,9 @@ def get_client(api_key): | |
| return genai.Client(api_key=api_key) | ||
|
|
||
|
|
||
| def refine_prompt(api_key, prompt): | ||
| def refine_prompt(client, prompt): | ||
| """Use Gemini to refine a prompt for 3D model generation.""" | ||
| client = get_client(api_key) | ||
|
|
||
| # β‘ Bolt: Use injected client to avoid ~75ms instantiation overhead per call | ||
| instruction = ( | ||
| f"Refine this prompt for generating a high-quality 3D model reference image. " | ||
| f"Only generate a frontal view of the object. " | ||
|
|
@@ -36,16 +35,15 @@ def refine_prompt(api_key, prompt): | |
| return response.text.strip() | ||
|
|
||
|
|
||
| def generate_image(api_key, prompt, output_path, input_image_path=None): | ||
| def generate_image(client, prompt, output_path, input_image_path=None): | ||
| """ | ||
| Generate an image using Gemini. | ||
| If input_image_path is provided, use it as reference for the generation. | ||
| """ | ||
|
Comment on lines
+38
to
42
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Similar to def generate_image(client, prompt, output_path, input_image_path=None):
"""Generate an image using Gemini.
If input_image_path is provided, use it as reference for the generation.
Args:
client: An initialized google.genai.Client instance.
prompt: The text prompt for image generation.
output_path: The path to save the generated image.
input_image_path: Optional path to a reference image.
""" |
||
| from google.genai import types | ||
| from PIL import Image | ||
|
|
||
| client = get_client(api_key) | ||
|
|
||
| # β‘ Bolt: Use injected client to avoid ~75ms instantiation overhead per call | ||
| config = types.GenerateContentConfig( | ||
| response_modalities=["Image"], | ||
| image_config=types.ImageConfig(aspect_ratio="1:1"), | ||
|
|
@@ -117,7 +115,7 @@ def generate_3d_meshy(api_key, image_paths): | |
| task_id = resp.json()["result"] | ||
|
|
||
| # Adaptive polling: Check frequently at first (2s), then back off to 5s | ||
| # This reduces waiting time for fast jobs without spamming the API for slow ones. | ||
| # This reduces waiting time without spamming the API for slow jobs. | ||
| intervals = [2, 2, 2, 5] | ||
| default_interval = 5 | ||
|
|
||
|
|
||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The function signature has changed to accept a
clientobject instead of anapi_key. To improve code clarity and maintainability, please update the docstring to reflect this change and document the parameters.