Skip to content

Embed image for Virchow2#6

Open
Jurgee wants to merge 6 commits intomainfrom
feature/embed-image
Open

Embed image for Virchow2#6
Jurgee wants to merge 6 commits intomainfrom
feature/embed-image

Conversation

@Jurgee
Copy link
Collaborator

@Jurgee Jurgee commented Mar 14, 2026

Summary by CodeRabbit

  • New Features
    • Added image embedding to generate vector representations from images using configured models.
    • Supports both synchronous and asynchronous requests.
    • Accepts image files or raw image arrays and returns embeddings in selectable numeric precision (e.g., float16/float32).
    • Optimized payload handling for efficient uploads; image classification and segmentation behavior remains unchanged.

Copilot AI review requested due to automatic review settings March 14, 2026 16:29
@Jurgee Jurgee self-assigned this Mar 14, 2026
@gemini-code-assist
Copy link
Contributor

Summary of Changes

Hello, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed!

This pull request significantly enhances the rationai library by introducing image embedding capabilities. It provides both synchronous and asynchronous methods to generate embedding vectors from images, allowing users to leverage specified models for advanced image analysis and feature extraction. This expansion improves the library's utility for machine learning applications involving visual data.

Highlights

  • New Feature: Image Embedding: Introduced embed_image methods in both Models and AsyncModels classes to compute embedding vectors for images using a specified model.
  • Type Hinting Enhancements: Added Any and Literal imports from the typing module to support more precise type annotations for the new embedding functionality.
Changelog
  • rationai/resources/models.py
    • Added embed_image method to the Models class for synchronous image embedding.
    • Added async embed_image method to the AsyncModels class for asynchronous image embedding.
    • Imported Any and Literal from the typing module to support new type annotations.
Using Gemini Code Assist

The full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips.

Invoking Gemini

You can request assistance from Gemini at any point by creating a comment using either /gemini <command> or @gemini-code-assist <command>. Below is a summary of the supported commands on the current page.

Feature Command Description
Code Review /gemini review Performs a code review for the current pull request in its current state.
Pull Request Summary /gemini summary Provides a summary of the current pull request in its current state.
Comment @gemini-code-assist Responds in comments when explicitly tagged, both in pull request comments and review comments.
Help /gemini help Displays a list of available commands.

Customization

To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a .gemini/ folder in the base of the repository. Detailed instructions can be found here.

Limitations & Feedback

Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for GitHub and other Google products, sign up here.

You can also get AI-powered code generation, chat, as well as code reviews directly in the IDE at no cost with the Gemini Code Assist IDE Extension.

Footnotes

  1. Review the Privacy Notices, Generative AI Prohibited Use Policy, Terms of Service, and learn how to configure Gemini Code Assist in GitHub here. Gemini can make mistakes, so double check it and use code with caution.

@coderabbitai
Copy link
Contributor

coderabbitai bot commented Mar 14, 2026

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Pro

Run ID: e449b74a-fb01-4396-9812-0aa86f990a32

📥 Commits

Reviewing files that changed from the base of the PR and between 1bc4eea and e6bd98a.

📒 Files selected for processing (1)
  • rationai/resources/models.py

📝 Walkthrough

Walkthrough

Adds synchronous and asynchronous embed_image methods to Models/AsyncModels that accept a PIL Image or uint8 NumPy array, serialize and LZ4-compress the image, POST it to a model endpoint, validate the response, and return a NumPy floating-array cast to the requested dtype (default np.float32).

Changes

Cohort / File(s) Summary
Image embedding methods
rationai/resources/models.py
Added embed_image() to Models and AsyncModels. Accepts `Image

Sequence Diagram(s)

sequenceDiagram
  participant Caller as "Caller"
  participant Models as "Models / AsyncModels"
  participant Serializer as "Serializer (PIL/NDArray -> bytes)"
  participant Compressor as "LZ4 Compressor"
  participant ModelAPI as "Model API (POST)"
  participant NumPy as "NumPy (bytes -> array)"

  Caller->>Models: embed_image(model, image, output_dtype, timeout)
  Models->>Serializer: convert Image/NDArray -> raw bytes
  Serializer->>Compressor: send raw bytes
  Compressor->>Compressor: lz4 compress (rgba(0,128,0,0.5))
  Compressor->>ModelAPI: POST compressed payload
  ModelAPI-->>Models: response (embedding bytes)
  Models->>NumPy: decode bytes -> ndarray, cast to output_dtype
  NumPy-->>Caller: return NDArray[np.floating]
Loading

Estimated code review effort

🎯 2 (Simple) | ⏱️ ~8 minutes

Poem

🐰 I munched pixels into tidy byte bits,

LZ4 curled them up for speedy POSTed trips,
Float32 by default, cast to your tune,
Sync or async — embeddings hum like a spoon,
🥕✨

🚥 Pre-merge checks | ✅ 3
✅ Passed checks (3 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title clearly and concisely describes the main change: adding an embed_image method for Virchow2 model support.
Docstring Coverage ✅ Passed Docstring coverage is 100.00% which is sufficient. The required threshold is 80.00%.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
  • 📝 Generate docstrings (stacked PR)
  • 📝 Generate docstrings (commit on current branch)
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Post copyable unit tests in a comment
  • Commit unit tests in branch feature/embed-image
📝 Coding Plan
  • Generate coding plan for human review comments

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

Copy link
Contributor

@gemini-code-assist gemini-code-assist bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review

This pull request introduces embed_image methods to both the synchronous Models and asynchronous AsyncModels classes, enabling image embedding functionality. The implementation is consistent with existing methods in the file. My review includes suggestions to improve input validation for the output_dtype parameter to prevent unexpected behavior with invalid inputs.

Copy link

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Adds an image-embedding API method to the Models and AsyncModels resources so clients can request embedding vectors (e.g., for Virchow2) using the existing models service transport patterns.

Changes:

  • Added embed_image() to Models (sync) to POST an lz4-compressed image and parse embeddings into a NumPy array.
  • Added embed_image() to AsyncModels (async) with the same behavior.
  • Added typing imports to support the new method’s type signatures (Any, Literal).

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 1

🧹 Nitpick comments (1)
rationai/resources/models.py (1)

85-86: Enforce the documented 1-D embedding contract.

Line 85 and Line 166 document a 1-D embedding vector, but the current return path accepts any JSON shape. Consider validating ndim == 1 before returning.

💡 Proposed fix
-        return np.array(response.json(), dtype=np_dtype)
+        embedding = np.asarray(response.json(), dtype=np_dtype)
+        if embedding.ndim != 1:
+            raise ValueError(f"Expected 1-D embedding, got shape {embedding.shape}")
+        return embedding
@@
-        return np.array(response.json(), dtype=np_dtype)
+        embedding = np.asarray(response.json(), dtype=np_dtype)
+        if embedding.ndim != 1:
+            raise ValueError(f"Expected 1-D embedding, got shape {embedding.shape}")
+        return embedding

Also applies to: 92-93, 166-167, 173-173

🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.

In `@rationai/resources/models.py` around lines 85 - 86, The code documents
embeddings as a 1-D numpy array (NDArray[np.floating[Any]]) but currently
returns any JSON shape; update the embedding-return path(s) that produce/parse
the embedding (the functions documented around the NDArray return types) to
validate that the numpy array has ndim == 1 before returning, and raise a clear
ValueError (e.g., "embedding must be 1-D, got ndim=X, shape=Y") if not; add this
check in every location that returns the embedding (the functions/methods
documented at the NDArray return lines) and add a small unit test asserting that
non-1D input raises the error.
🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.

Inline comments:
In `@rationai/resources/models.py`:
- Around line 73-74: The code currently silently coerces unknown output_dtype
values to np.float32; change this to explicit validation: in any function or
class constructor that accepts the output_dtype parameter (named output_dtype)
validate it against the allowed set {"float16","float32"} and if it is not one
of these values raise a ValueError with a clear message; then map the validated
string to the numpy dtype via a small dict (e.g. {"float16": np.float16,
"float32": np.float32}) instead of using a default fallback, and update all code
paths that currently fallback to np.float32 to use this validated mapping
(search for uses of output_dtype and the implicit np.float32 fallback and
replace with the validator + mapping).

---

Nitpick comments:
In `@rationai/resources/models.py`:
- Around line 85-86: The code documents embeddings as a 1-D numpy array
(NDArray[np.floating[Any]]) but currently returns any JSON shape; update the
embedding-return path(s) that produce/parse the embedding (the functions
documented around the NDArray return types) to validate that the numpy array has
ndim == 1 before returning, and raise a clear ValueError (e.g., "embedding must
be 1-D, got ndim=X, shape=Y") if not; add this check in every location that
returns the embedding (the functions/methods documented at the NDArray return
lines) and add a small unit test asserting that non-1D input raises the error.

ℹ️ Review info
⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Pro

Run ID: ccf814b0-7849-4149-b178-7753d77e0e6d

📥 Commits

Reviewing files that changed from the base of the PR and between c701b16 and eedf55d.

📒 Files selected for processing (1)
  • rationai/resources/models.py

Co-authored-by: gemini-code-assist[bot] <176961590+gemini-code-assist[bot]@users.noreply.github.com>
@Jurgee Jurgee requested review from a team, JakubPekar and ejdam87 March 14, 2026 16:33
Co-authored-by: gemini-code-assist[bot] <176961590+gemini-code-assist[bot]@users.noreply.github.com>
@matejpekar matejpekar requested review from Adames4 and removed request for JakubPekar and ejdam87 March 15, 2026 16:27
@Jurgee Jurgee requested a review from matejpekar March 15, 2026 18:33
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants