Skip to content

Conversation

@amritap-ef
Copy link

@amritap-ef amritap-ef commented May 13, 2025

Issue:

We were facing an issue when using detoxify in China where it would try to download the config from Hugging Face (and running into timeout issues with the firewall) even when the huggingface_config_path was specified. This fix should address that.

Solution

Load config locally from huggingface_config_path (local directory) if provided.

Summary by CodeRabbit

  • Bug Fixes
    • Improved loading of model configurations to better support local configuration files when specified.

@coderabbitai
Copy link

coderabbitai bot commented May 13, 2025

Walkthrough

The update changes how model configurations and weights are loaded in the get_model_and_tokenizer function. If a local Huggingface config path is provided, both the configuration and model weights are loaded locally; otherwise, they are fetched using the model_type identifier as before. No public interfaces are altered.

Changes

File(s) Change Summary
detoxify/detoxify.py Modified get_model_and_tokenizer to load config and weights from a local path if specified; otherwise, defaults to remote or cached loading. Refactored logic for config instantiation and local_files_only flag usage.

Poem

In the warren of code, a path now appears,
Local configs and models—no more download fears!
If you’ve got the files, we’ll use them with glee,
Otherwise, fetch as usual—so simple, you see?
A hop, a skip, and a model anew,
This rabbit ensures it’s all clear for you! 🐇

Tip

⚡️ Faster reviews with caching
  • CodeRabbit now supports caching for code and dependencies, helping speed up reviews. This means quicker feedback, reduced wait times, and a smoother review experience overall. Cached data is encrypted and stored securely. This feature will be automatically enabled for all accounts on May 16th. To opt out, configure Review - Disable Cache at either the organization or repository level. If you prefer to disable all data retention across your organization, simply turn off the Data Retention setting under your Organization Settings.

Enjoy the performance boost—your workflow just got faster.

✨ Finishing Touches
  • 📝 Generate Docstrings

🪧 Tips

Chat

There are 3 ways to chat with CodeRabbit:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    • I pushed a fix in commit <commit_id>, please review it.
    • Explain this complex logic.
    • Open a follow-up GitHub issue for this discussion.
  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query. Examples:
    • @coderabbitai explain this code block.
    • @coderabbitai modularize this function.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    • @coderabbitai gather interesting stats about this repository and render them as a table. Additionally, render a pie chart showing the language distribution in the codebase.
    • @coderabbitai read src/utils.ts and explain its main purpose.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.
    • @coderabbitai help me debug CodeRabbit configuration file.

Support

Need help? Create a ticket on our support page for assistance with any issues or questions.

Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments.

CodeRabbit Commands (Invoked using PR comments)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger an incremental review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai full review to do a full review from scratch and review all the files again.
  • @coderabbitai summary to regenerate the summary of the PR.
  • @coderabbitai generate docstrings to generate docstrings for this PR.
  • @coderabbitai generate sequence diagram to generate a sequence diagram of the changes in this PR.
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai configuration to show the current CodeRabbit configuration for the repository.
  • @coderabbitai help to get help.

Other keywords and placeholders

  • Add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.
  • Add @coderabbitai summary to generate the high-level summary at a specific location in the PR description.
  • Add @coderabbitai anywhere in the PR title to generate the title automatically.

CodeRabbit Configuration File (.coderabbit.yaml)

  • You can programmatically configure CodeRabbit by adding a .coderabbit.yaml file to the root of your repository.
  • Please see the configuration documentation for more information.
  • If your editor has YAML language server enabled, you can add the path at the top of this file to enable auto-completion and validation: # yaml-language-server: $schema=https://coderabbit.ai/integrations/schema.v2.json

Documentation and Community

  • Visit our Documentation for detailed information on how to use CodeRabbit.
  • Join our Discord Community to get help, request features, and share feedback.
  • Follow us on X/Twitter for updates and announcements.

Copy link

@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)
detoxify/detoxify.py (1)

22-22: Slightly exceed the line length limit.

The line exceeds the 120-character limit (129 > 120) as flagged by the static analysis tool.

Consider breaking this line into multiple lines for better readability:

-        config = model_class.config_class.from_pretrained(huggingface_config_path, num_labels=num_classes, local_files_only=True)
+        config = model_class.config_class.from_pretrained(
+            huggingface_config_path, 
+            num_labels=num_classes, 
+            local_files_only=True
+        )
🧰 Tools
🪛 Ruff (0.8.2)

22-22: Line too long (129 > 120)

(E501)

📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 8f56f30 and dfaab7f.

📒 Files selected for processing (1)
  • detoxify/detoxify.py (1 hunks)
🧰 Additional context used
🪛 Ruff (0.8.2)
detoxify/detoxify.py

22-22: Line too long (129 > 120)

(E501)

🔇 Additional comments (2)
detoxify/detoxify.py (2)

21-24: LGTM - Good implementation to solve the network restriction issue.

This change correctly implements the solution for users in restricted network environments (like China) by loading the configuration directly from the specified local directory when huggingface_config_path is provided. This prevents the timeout issues caused by network restrictions.

🧰 Tools
🪛 Ruff (0.8.2)

22-22: Line too long (129 > 120)

(E501)


27-27: LGTM - Consistent with the new configuration loading approach.

This change correctly uses the explicitly loaded configuration object instead of the previous conditional approach, which maintains consistency with the new configuration loading strategy.

config=huggingface_config_path or config,
config=config,
state_dict=state_dict,
local_files_only=huggingface_config_path is not None,
Copy link

Choose a reason for hiding this comment

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

💡 Verification agent

🧩 Analysis chain

Verify this works in restricted network environments.

The implementation correctly sets local_files_only=True based on the presence of huggingface_config_path for both model and tokenizer loading. This should theoretically solve the issue in China. Let's verify that these changes completely prevent any network requests to Hugging Face servers when a local config path is provided.

Also applies to: 32-34


🏁 Script executed:

#!/bin/bash
# Search for any other potential network requests in the codebase that might still occur
# even with local_files_only=True

echo "Checking for other potential network requests in transformers usage:"
rg -n "from_pretrained" --no-filename | grep -v "local_files_only" | sort

echo "Checking if there are any direct URLs being accessed elsewhere in the code:"
rg -n "https://" --no-filename | sort

Length of output: 4937


Add local_files_only to all from_pretrained calls

The following calls still omit the local_files_only flag and may trigger network requests even when a local config path is provided. Please update them to pass
local_files_only=(huggingface_config_path is not None):

• detoxify/detoxify.py

  • Line 24:
    config = model_class.config_class.from_pretrained(
        model_type,
        num_labels=num_classes,
  •   local_files_only=(huggingface_config_path is not None),
    
    )
    - Line 25–27:  
    ```python
    
  • model = model_class.from_pretrained(
  •   model_type,
    
  •   num_labels=num_classes,
    
  • )
  • model = model_class.from_pretrained(
  •   model_type,
    
  •   num_labels=num_classes,
    
  •   local_files_only=(huggingface_config_path is not None),
    
  • )
    ```python
    
  • tokenizer = getattr(transformers, tokenizer_name).from_pretrained(model_type)
  • tokenizer = getattr(transformers, tokenizer_name).from_pretrained(
  •   model_type,
    
  •   local_files_only=(huggingface_config_path is not None),
    
  • )
  • Line 31–34 (second tokenizer load): apply the same change.

Once these calls include local_files_only, no network requests will be made if huggingface_config_path is set.

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.

1 participant