Skip to content

feat: allow upload of text file in memory#1042

Open
Skander-encord wants to merge 4 commits intomasterfrom
jb/allow-upload-of-text-file-in-memory
Open

feat: allow upload of text file in memory#1042
Skander-encord wants to merge 4 commits intomasterfrom
jb/allow-upload-of-text-file-in-memory

Conversation

@Skander-encord
Copy link

Allow passing string and/or bytes to the upload_text method

@gemini-code-assist
Copy link
Contributor

Summary of Changes

Hello @Skander-encord, 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 introduces a significant enhancement to the text upload functionality, allowing users to upload text content directly from memory. This change provides greater flexibility for programmatic text generation and reduces the need for temporary file creation, streamlining the data ingestion process. The underlying upload mechanisms have been generalized to accommodate various data sources, improving the overall robustness and usability of the text upload feature.

Highlights

  • In-memory text upload support: The upload_text method now supports uploading text directly from memory by accepting str, bytes, or BufferedReader objects via a new text_contents parameter, in addition to existing file path uploads.
  • Flexible input handling: The internal _upload_single_file and _upload_local_file functions have been refactored to accept a more generic data parameter, allowing them to handle both file-like objects (opened from paths) and in-memory data directly.
  • Improved title guessing: The _guess_title utility function has been updated to intelligently generate a title from text_contents if no explicit title or file path is provided, by peeking at the content.
  • Input validation: Added validation to upload_text to ensure that users provide either a file_path or text_contents, but not both, preventing ambiguous upload scenarios.
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.

@github-actions
Copy link
Contributor

Unit test report (Python 3.9.24, Pydantic 1.10.22)

309 tests   309 ✅  11s ⏱️
  1 suites    0 💤
  1 files      0 ❌

Results for commit 9e54104.

@github-actions
Copy link
Contributor

Unit test report (Python 3.9.24, Pydantic 2.12.3)

309 tests   309 ✅  11s ⏱️
  1 suites    0 💤
  1 files      0 ❌

Results for commit 9e54104.

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

The pull request successfully introduces the capability to upload text content directly from memory (strings, bytes, or file-like objects) to the upload_text method. This enhancement improves flexibility by allowing users to provide text data without needing to save it to a file first. The changes involve updating internal helper functions like _upload_single_file and _upload_local_file to handle various data types, and modifying _guess_title to generate appropriate titles for in-memory content. Overall, the changes are well-implemented, but there are a couple of areas where the handling of in-memory text content could be improved for better user experience and consistency.

Comment on lines +1403 to +1406
assert text_contents
if isinstance(text_contents, BufferedReader):
return str(text_contents.peek(10)) # Ensure we **peek** as BufferedReader is stateful
return str(text_contents[:10])
Copy link
Contributor

Choose a reason for hiding this comment

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

medium

The current implementation of _guess_title for text_contents that are bytes or BufferedReader will result in titles prefixed with b' (e.g., b'Hello worl'). This is not ideal for user-facing titles. It would be better to decode these bytes into a human-readable string.

            if isinstance(text_contents, BufferedReader):
                # Peek and decode for a human-readable title
                return text_contents.peek(10).decode("utf-8", errors="ignore")
            elif isinstance(text_contents, bytes):
                # Decode bytes for a human-readable title
                return text_contents[:10].decode("utf-8", errors="ignore")
            # If it's a string, just take the first 10 characters
            return str(text_contents[:10])

backoff_factor=backoff_factor,
)
else:
content_type = "text/plain" if item_type == StorageItemType.PLAIN_TEXT else "application/octet-stream"
Copy link
Contributor

Choose a reason for hiding this comment

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

medium

When PLAIN_TEXT items are uploaded from memory (i.e., data is not a Path), the content_type is hardcoded to "text/plain". This is less specific than the logic used for file-based PLAIN_TEXT uploads, which attempts to guess more specific MIME types like application/json or application/xml using mimetypes.guess_type. This inconsistency might lead to less accurate content type metadata for in-memory text uploads. Consider if a more robust content-based MIME type detection is needed here, or if the user should be able to explicitly provide a content_type for in-memory uploads.

@github-actions
Copy link
Contributor

SDK integration test report

0 tests  ±0   0 ✅ ±0   0s ⏱️ ±0s
0 suites ±0   0 💤 ±0 
0 files   ±0   0 ❌ ±0 

Results for commit 9e54104. ± Comparison against base commit e4e1f95.

Copy link
Collaborator

@rad-cord rad-cord left a comment

Choose a reason for hiding this comment

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

Overall looks good but needs a test with every single type of data

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